Skip to content

Commit

Permalink
[bvh]: Add Scene's AABB compute
Browse files Browse the repository at this point in the history
  • Loading branch information
cszach committed Feb 5, 2024
1 parent 9417544 commit e80b4a7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
1 change: 1 addition & 0 deletions include/3d/AABB.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions include/3d/Scene.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
17 changes: 7 additions & 10 deletions include/math/Interval.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions src/3d/AABB.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 13 additions & 6 deletions src/3d/Scene.cu
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand All @@ -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);
}
}

0 comments on commit e80b4a7

Please sign in to comment.