diff --git a/CMakeLists.txt b/CMakeLists.txt index 88cab3d..87b1f2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ include_directories(${executable} file(GLOB SourceFiles ${SrcDIR}/* + ${SrcDIR}/acceleration/* ${SrcDIR}/core/* ${SrcDIR}/engine/* ${SrcDIR}/display/* diff --git a/src/acceleration/kd.cpp b/src/acceleration/kd.cpp new file mode 100644 index 0000000..23993d5 --- /dev/null +++ b/src/acceleration/kd.cpp @@ -0,0 +1,3 @@ +#include "kd.hpp" + + diff --git a/src/acceleration/kd.hpp b/src/acceleration/kd.hpp new file mode 100644 index 0000000..2aef6af --- /dev/null +++ b/src/acceleration/kd.hpp @@ -0,0 +1,38 @@ +#ifndef INFERNO_ACCELERATION_KD_H_ +#define INFERNO_ACCELERATION_KD_H_ + +#include "../maths.hpp" + +class Triangle; +class Ray; + +class Box { +public: + Box(); + Box(Triangle* object); + + void ExtendTriangle(Triangle* object); + void ExtendPoint(glm::vec3 p); + int LongestAxis(); + + bool Hit(Ray* ray); + + glm::vec3 min; + glm::vec3 max; +}; + +class KDTree { +public: + Box bounds; + + KDTree* child0 = nullptr; + KDTree* child1 = nullptr; + + std::vector children; +}; + +KDTree* BuildKDTree(const std::vector& triangles); + +bool KDIntersect(KDTree* tree, Ray* ray, Triangle*& triMin, float& uMin, float& vMin, float& tMin); + +#endif diff --git a/src/definitions/primatives/triangle.hpp b/src/definitions/primatives/triangle.hpp index c9d9cb6..0add235 100644 --- a/src/definitions/primatives/triangle.hpp +++ b/src/definitions/primatives/triangle.hpp @@ -12,6 +12,7 @@ public: glm::vec3 SurfaceNormal(glm::vec3 hitPoint) override; glm::vec2 TexCoords(glm::vec3 hitPoint) override; void Translate(glm::vec3 trans) override; + glm::vec3 Midpoint(Triangle* t); }; #endif diff --git a/src/definitions/ray.cpp b/src/definitions/ray.cpp index 3161dc9..d1f29ae 100644 --- a/src/definitions/ray.cpp +++ b/src/definitions/ray.cpp @@ -2,16 +2,13 @@ #include "scene.hpp" #include "primatives/primative.hpp" -bool TraceRay(Ray ray, Scene* scene, float& t, float& iOfHit) { - iOfHit = -1; - +bool TraceRay(Ray ray, Scene* scene, float& t, Primative*& hit) { int i = 0; float lastDistance = INFINITY; int index = -1; for (auto& object : scene->objects) { float distance = INFINITY; if (object->DoesIntersect(ray, distance)) { - iOfHit = i; if (distance < lastDistance) { index = i; lastDistance = distance; @@ -20,8 +17,11 @@ bool TraceRay(Ray ray, Scene* scene, float& t, float& iOfHit) { i++; } - iOfHit = index; t = lastDistance; - return (iOfHit != -1); + if (index == -1) return false; + + hit = scene->objects[index]; + + return true; } diff --git a/src/definitions/ray.hpp b/src/definitions/ray.hpp index dc6e6a4..ce8d4f2 100644 --- a/src/definitions/ray.hpp +++ b/src/definitions/ray.hpp @@ -4,6 +4,7 @@ #include "../maths.hpp" class Scene; +class Primative; class Ray { public: @@ -11,6 +12,6 @@ public: glm::vec3 direction = {}; }; -bool TraceRay(Ray ray, Scene* scene, float& t, float& iOfHit); +bool TraceRay(Ray ray, Scene* scene, float& t, Primative*& hit); #endif diff --git a/src/engine/progressiverenderer.cpp b/src/engine/progressiverenderer.cpp index 564a0cd..621e746 100644 --- a/src/engine/progressiverenderer.cpp +++ b/src/engine/progressiverenderer.cpp @@ -47,14 +47,14 @@ void ProgressiveRenderer::Render() { Ray ray = m_scene->camera->CastRay(x, y); - float t, i; - bool didhit = TraceRay(ray, m_scene, t, i); + float t; + Primative* hit = nullptr; + bool didhit = TraceRay(ray, m_scene, t, hit); if (!didhit) { m_interface->SetPixelSafe(x, y, 0x000000); continue; } - Primative* hit = m_scene->objects[i]; glm::vec3 hitPoint = ray.origin + ray.direction * t;