diff --git a/include/3d/AABB.cuh b/include/3d/AABB.cuh index 6cc3431..9019876 100644 --- a/include/3d/AABB.cuh +++ b/include/3d/AABB.cuh @@ -12,6 +12,7 @@ public: __device__ AABB(); __device__ AABB(const Interval &_x, const Interval &_y, const Interval &_z); __device__ AABB(const Point3 &a, const Point3 &b); + __device__ AABB(const AABB &box1, const AABB &box2); __device__ const Interval &axis(int i) const; __device__ bool hit(const Ray &r, Interval ray_t) const; diff --git a/include/3d/Scene.cuh b/include/3d/Scene.cuh index f7727c1..546809b 100644 --- a/include/3d/Scene.cuh +++ b/include/3d/Scene.cuh @@ -8,10 +8,10 @@ class Scene : public Object { public: - Object **_objects; - int _num_objects = 0; + Object **objects; + int count = 0; - __device__ Scene(Object **objects, int num_objects); + __device__ Scene(Object **objects, int numobjects); // __device__ void add(Object *object); diff --git a/include/math/Interval.cuh b/include/math/Interval.cuh index 4b7860a..e838013 100644 --- a/include/math/Interval.cuh +++ b/include/math/Interval.cuh @@ -8,19 +8,16 @@ public: float min; float max; - __host__ __device__ Interval() : min(+infinity), max(-infinity) {} + __device__ Interval() : min(+infinity), max(-infinity) {} + __device__ Interval(float min, float max) : min(min), max(max) {} + __device__ Interval(const Interval &a, const Interval &b) + : min(fmin(a.min, b.min)), max(fmax(a.max, b.max)) {} - __host__ __device__ Interval(float min, float max) : min(min), max(max) {} + __device__ bool contains(float x) const { return min <= x && x <= max; } - __host__ __device__ bool contains(float x) const { - return min <= x && x <= max; - } - - __host__ __device__ bool surrounds(float x) const { - return min < x && x < max; - } + __device__ bool surrounds(float x) const { return min < x && x < max; } - __host__ __device__ float clamp(float x) const { + __device__ float clamp(float x) const { if (x < min) return min; diff --git a/src/3d/AABB.cu b/src/3d/AABB.cu index fa6f80b..6e8495b 100644 --- a/src/3d/AABB.cu +++ b/src/3d/AABB.cu @@ -11,6 +11,11 @@ __device__ AABB::AABB(const Point3 &a, const Point3 &b) { y = Interval(fmin(a.y, b.y), fmax(a.y, b.y)); z = Interval(fmin(a.z, b.z), fmax(a.z, b.z)); } +__device__ AABB::AABB(const AABB &a, const AABB &b) { + x = Interval(a.x, b.x); + y = Interval(a.y, b.y); + z = Interval(a.z, b.z); +} __device__ const Interval &AABB::axis(int i) const { if (i == 0) diff --git a/src/3d/Scene.cu b/src/3d/Scene.cu index d6d3f50..3a22207 100644 --- a/src/3d/Scene.cu +++ b/src/3d/Scene.cu @@ -1,11 +1,11 @@ #include "Scene.cuh" #include "cuda_helper.cuh" -__device__ Scene::Scene(Object **objects, int num_objects) - : _objects(objects), _num_objects(num_objects) {} +__device__ Scene::Scene(Object **objects, int numobjects) + : objects(objects), count(numobjects) {} // __device__ void Scene::add(Object *object) { -// *(d_objects + _num_objects++) = object; +// *(dobjects + count++) = object; // } __device__ bool Scene::hit(const Ray &ray, float t_min, float t_max, @@ -14,8 +14,8 @@ __device__ bool Scene::hit(const Ray &ray, float t_min, float t_max, bool hit_anything = false; float closest_so_far = t_max; - for (int i = 0; i < _num_objects; i++) { - bool got_hit = _objects[i]->hit(ray, t_min, t_max, temp_record); + for (int i = 0; i < count; i++) { + bool got_hit = objects[i]->hit(ray, t_min, t_max, temp_record); if (got_hit && temp_record.t < closest_so_far) { hit_anything = true; @@ -27,4 +27,11 @@ __device__ bool Scene::hit(const Ray &ray, float t_min, float t_max, return hit_anything; } -__device__ void Scene::computeBoundingBox() { return; } +__device__ void Scene::computeBoundingBox() { + boundingBox = AABB(); + + for (int i = 0; i < count; i++) { + objects[i]->computeBoundingBox(); + boundingBox = AABB(boundingBox, objects[i]->boundingBox); + } +}