change GPUBvh struct to have only one index

This commit is contained in:
2025-01-25 15:19:53 +01:00
parent 6378ed0737
commit e1e53b99fe
5 changed files with 21 additions and 32 deletions

View File

@ -3,17 +3,16 @@ Pos=60,60
Size=400,400 Size=400,400
[Window][Camera] [Window][Camera]
Pos=1643,7 Pos=1277,16
Size=259,200 Size=259,200
Collapsed=1
[Window][Material] [Window][Material]
Pos=1642,29 Pos=927,29
Size=266,299 Size=266,299
Collapsed=1 Collapsed=1
[Window][Fog settings] [Window][Fog settings]
Pos=1641,52 Pos=927,52
Size=247,130 Size=247,130
Collapsed=1 Collapsed=1
@ -22,7 +21,7 @@ Pos=1642,668
Size=260,143 Size=260,143
[Window][Debug BVH] [Window][Debug BVH]
Pos=1641,72 Pos=927,72
Size=274,205 Size=274,205
Collapsed=1 Collapsed=1

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 18:30:18 by ycontre #+# #+# */ /* Created: 2024/12/23 18:30:18 by ycontre #+# #+# */
/* Updated: 2025/01/24 19:35:52 by ycontre ### ########.fr */ /* Updated: 2025/01/25 14:10:19 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -77,10 +77,7 @@ struct GPUBvh
alignas(16) glm::vec3 min; alignas(16) glm::vec3 min;
alignas(16) glm::vec3 max; alignas(16) glm::vec3 max;
int left_index; int index;
int right_index;
int first_primitive;
int primitive_count; int primitive_count;
}; };
@ -146,4 +143,4 @@ class Scene
Camera *_camera; Camera *_camera;
}; };
#endif #endif

View File

@ -78,10 +78,7 @@ struct GPUBvh
vec3 min; vec3 min;
vec3 max; vec3 max;
int left_index; int index;
int right_index;
int first_primitive;
int primitive_count; int primitive_count;
}; };

View File

@ -75,14 +75,14 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
{ {
for (int i = 0; i < node.primitive_count; i++) for (int i = 0; i < node.primitive_count; i++)
{ {
GPUTriangle obj = triangles[bvh_data.triangle_start_index + node.first_primitive + i]; GPUTriangle obj = triangles[bvh_data.triangle_start_index + node.index + i];
hitInfo temp_hit; hitInfo temp_hit;
if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t < hit.t) if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t < hit.t)
{ {
hit.t = temp_hit.t; hit.t = temp_hit.t;
hit.last_t = temp_hit.last_t; hit.last_t = temp_hit.last_t;
hit.obj_index = bvh_data.triangle_start_index + node.first_primitive + i; hit.obj_index = bvh_data.triangle_start_index + node.index + i;
hit.mat_index = obj.mat_index; hit.mat_index = obj.mat_index;
hit.position = temp_hit.position; hit.position = temp_hit.position;
hit.normal = temp_hit.normal; hit.normal = temp_hit.normal;
@ -91,8 +91,8 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
} }
else else
{ {
GPUBvh left_node = Bvh[bvh_data.bvh_start_index + node.left_index]; GPUBvh left_node = Bvh[bvh_data.bvh_start_index + current_index + 1];
GPUBvh right_node = Bvh[bvh_data.bvh_start_index + node.right_index]; GPUBvh right_node = Bvh[bvh_data.bvh_start_index + node.index];
hitInfo left_hit; hitInfo left_hit;
hitInfo right_hit; hitInfo right_hit;
@ -105,13 +105,13 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
if (left_hit.t > right_hit.t) if (left_hit.t > right_hit.t)
{ {
if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = node.left_index; if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = current_index + 1;
if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.right_index; if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.index;
} }
else else
{ {
if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.right_index; if (right_hit.t < hit.t && right_bool) stack[++stack_ptr] = node.index;
if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = node.left_index; if (left_hit.t < hit.t && left_bool) stack[++stack_ptr] = current_index + 1;
} }
} }
} }
@ -179,4 +179,4 @@ hitInfo traceRay(Ray ray)
} }
return (hit); return (hit);
} }

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 21:48:48 by TheRed #+# #+# */ /* Created: 2025/01/16 21:48:48 by TheRed #+# #+# */
/* Updated: 2025/01/24 19:36:07 by ycontre ### ########.fr */ /* Updated: 2025/01/25 14:25:57 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -146,7 +146,7 @@ GPUBvh BVH::toGPUBvh()
{ {
GPUBvh bvh; GPUBvh bvh;
bvh.first_primitive = _first_primitive; bvh.index = _first_primitive;
bvh.primitive_count = _primitive_count; bvh.primitive_count = _primitive_count;
bvh.max = _aabb.max; bvh.max = _aabb.max;
bvh.min = _aabb.min; bvh.min = _aabb.min;
@ -159,15 +159,11 @@ void BVH::flatten(std::vector<GPUBvh> &bvhs, int &currentIndex)
GPUBvh self_bvh = toGPUBvh(); GPUBvh self_bvh = toGPUBvh();
int self_index = currentIndex++; int self_index = currentIndex++;
self_bvh.left_index = -1;
self_bvh.right_index = -1;
if (!_is_leaf) if (!_is_leaf)
{ {
self_bvh.left_index = currentIndex;
_left->flatten(bvhs, currentIndex); _left->flatten(bvhs, currentIndex);
self_bvh.right_index = currentIndex; self_bvh.index = currentIndex;
_right->flatten(bvhs, currentIndex); _right->flatten(bvhs, currentIndex);
} }
@ -244,4 +240,4 @@ BVHStats BVH::analyzeBVHLeaves(BVH *root)
right.average_triangles * right_leaf_count) / total_leaf_count; right.average_triangles * right_leaf_count) / total_leaf_count;
return {min_count, max_count, avg_count}; return {min_count, max_count, avg_count};
} }