mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
+ | BVH Working
This commit is contained in:
@ -3,14 +3,14 @@ Pos=60,60
|
|||||||
Size=400,400
|
Size=400,400
|
||||||
|
|
||||||
[Window][Camera]
|
[Window][Camera]
|
||||||
Pos=651,101
|
Pos=1682,11
|
||||||
Size=229,184
|
Size=229,184
|
||||||
|
|
||||||
[Window][Material]
|
[Window][Material]
|
||||||
Pos=1136,164
|
Pos=1650,202
|
||||||
Size=266,285
|
Size=266,285
|
||||||
|
|
||||||
[Window][Fog settings]
|
[Window][Fog settings]
|
||||||
Pos=884,462
|
Pos=1679,495
|
||||||
Size=247,157
|
Size=247,157
|
||||||
|
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* BVH.hpp :+: :+: :+: */
|
/* BVH.hpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/01/16 21:36:19 by TheRed #+# #+# */
|
/* Created: 2025/01/16 21:36:19 by TheRed #+# #+# */
|
||||||
/* Updated: 2025/01/16 21:36:19 by TheRed ### ########.fr */
|
/* Updated: 2025/01/17 18:59:28 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -30,13 +30,13 @@ struct AABB
|
|||||||
class BVH
|
class BVH
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BVH(std::vector<GPUObject> primitives, int first_primitive, int primitive_count);
|
BVH(std::vector<GPUObject> &primitives, int first_primitive, int primitive_count);
|
||||||
|
|
||||||
|
|
||||||
void showAABB(Scene *scene);
|
void showAABB(Scene *scene);
|
||||||
|
|
||||||
void updateBounds(std::vector<GPUObject> primitives);
|
void updateBounds(std::vector <GPUObject> &primitives);
|
||||||
void subdivide(std::vector<GPUObject> primitives);
|
void subdivide(std::vector<GPUObject> &primitives);
|
||||||
|
|
||||||
int size();
|
int size();
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */
|
/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */
|
||||||
/* Updated: 2025/01/13 18:43:18 by ycontre ### ########.fr */
|
/* Updated: 2025/01/17 18:17:30 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ class Triangle : public Object
|
|||||||
// _vertex2 -= _position; //optimization
|
// _vertex2 -= _position; //optimization
|
||||||
// _vertex3 -= _position; //optimization
|
// _vertex3 -= _position; //optimization
|
||||||
|
|
||||||
_normal = glm::normalize(glm::cross(_vertex2, _vertex3)); //optimization
|
_normal = glm::normalize(glm::cross(_vertex2 - _position, _vertex3 - _position)); //optimization
|
||||||
}
|
}
|
||||||
|
|
||||||
const glm::vec3 &getVertex2() const { return (_vertex2); }
|
const glm::vec3 &getVertex2() const { return (_vertex2); }
|
||||||
|
@ -19138,7 +19138,7 @@ vt 0.0798 0.4469 0.0000
|
|||||||
# 6409 texture coords
|
# 6409 texture coords
|
||||||
|
|
||||||
g 10517_Motorcycle_Helmet_v01
|
g 10517_Motorcycle_Helmet_v01
|
||||||
usemtl 10517_Motorcycle_Helmet_v01
|
###usemtl 10517_Motorcycle_Helmet_v01
|
||||||
s 3
|
s 3
|
||||||
f 1 2 3 4
|
f 1 2 3 4
|
||||||
f 2 5 6 3
|
f 2 5 6 3
|
||||||
@ -23841,7 +23841,7 @@ f 4267 4008 4591 4614
|
|||||||
f 4614 4591 4587 4608
|
f 4614 4591 4587 4608
|
||||||
f 4269 4267 4614 4613
|
f 4269 4267 4614 4613
|
||||||
f 4613 4614 4608 4609
|
f 4613 4614 4608 4609
|
||||||
usemtl 10517_Motorcycle_Helmet_v01_glass
|
##usemtl 10517_Motorcycle_Helmet_v01_glass
|
||||||
s 2
|
s 2
|
||||||
f 4615 4616 4617 4618
|
f 4615 4616 4617 4618
|
||||||
f 4616 4619 4620 4617
|
f 4616 4619 4620 4617
|
||||||
@ -25254,7 +25254,7 @@ f 6008 5106 5105 6024
|
|||||||
f 6024 5105 5100 6018
|
f 6024 5105 5100 6018
|
||||||
f 6010 6008 6024 6023
|
f 6010 6008 6024 6023
|
||||||
f 6023 6024 6018 6019
|
f 6023 6024 6018 6019
|
||||||
usemtl 10517_Motorcycle_Helmet_v01
|
#usemtl 10517_Motorcycle_Helmet_v01
|
||||||
s 1
|
s 1
|
||||||
f 1315 1643 6025 1316
|
f 1315 1643 6025 1316
|
||||||
f 1643 1647 6026 6025
|
f 1643 1647 6026 6025
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
CAM 10.2849 6.27579 9.45482 -0.600081 -128.999 0 4 90 5
|
CAM 0.582504 9.47921 50.0705 -20.6001 -87.3991 0 4 90 5
|
||||||
|
|
||||||
MAT 200 200 200 0.0 0.0 0.0 //white
|
MAT 200 200 200 0.0 0.0 0.0 //white
|
||||||
MAT 255 50 50 0.0 1.0 0.05 //red
|
MAT 255 50 50 0.0 1.0 0.05 //red
|
||||||
@ -13,16 +13,16 @@ MAT 255 255 255 0.0 0.0 0.0 TRN // glass
|
|||||||
|
|
||||||
# sp 0 -25 0 50 1
|
# sp 0 -25 0 50 1
|
||||||
|
|
||||||
tr -4 5 0 -3 5 0 -4 5 1 0
|
# tr -4 5 0 -3 5 0 -4 5 1 0
|
||||||
tr 0 7 2 1 4 2 0 5 3 0
|
# tr 0 7 2 1 4 2 0 5 3 0
|
||||||
tr 3 7 2 4 5 1 2 6 3 0
|
# tr 3 7 2 4 5 1 2 6 3 0
|
||||||
|
|
||||||
tr 7 5 0 8 5 0 7 5 1 0
|
# tr 7 5 0 8 5 0 7 5 1 0
|
||||||
tr 7 7 2 8 4 2 7 5 3 0
|
# tr 7 7 2 8 4 2 7 5 3 0
|
||||||
tr 10 7 2 11 5 1 8 6 3 0
|
# tr 10 7 2 11 5 1 8 6 3 0
|
||||||
tr 15 -2 4 11 5 1 8 6 3 0
|
# tr 15 -2 4 11 5 1 8 6 3 0
|
||||||
tr 15 -2 4 11 5 1 7 5 9 0
|
# tr 15 -2 4 11 5 1 7 5 9 0
|
||||||
tr 12 4 4 4 3 1 7 5 9 0
|
# tr 12 4 4 4 3 1 7 5 9 0
|
||||||
|
|
||||||
|
|
||||||
# OBJ obj/Lowpoly_tree_sample.obj
|
OBJ obj/10517_Motorcycle_Helmet_v01_L3.obj
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
bool intersectRayBVH(Ray ray, GPUBvh node)
|
|
||||||
{
|
|
||||||
vec3 invDir = 1.0 / ray.direction;
|
|
||||||
|
|
||||||
vec3 t1 = (node.min - ray.origin) * invDir;
|
|
||||||
vec3 t2 = (node.max - ray.origin) * invDir;
|
|
||||||
|
|
||||||
vec3 tMin = min(t1, t2);
|
|
||||||
vec3 tMax = max(t1, t2);
|
|
||||||
|
|
||||||
float tEnter = max(max(tMin.x, tMin.y), tMin.z);
|
|
||||||
float tExit = min(min(tMax.x, tMax.y), tMax.z);
|
|
||||||
|
|
||||||
return tEnter <= tExit && tExit >= 0.0;
|
|
||||||
}
|
|
@ -66,6 +66,24 @@ layout(std430, binding = 3) buffer LightsBuffer
|
|||||||
int lightsIndex[];
|
int lightsIndex[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct GPUBvh
|
||||||
|
{
|
||||||
|
vec3 min;
|
||||||
|
vec3 max;
|
||||||
|
|
||||||
|
int left_index;
|
||||||
|
int right_index;
|
||||||
|
|
||||||
|
int is_leaf;
|
||||||
|
|
||||||
|
int first_primitive;
|
||||||
|
int primitive_count;
|
||||||
|
};
|
||||||
|
layout(std430, binding = 4) buffer BvhBuffer
|
||||||
|
{
|
||||||
|
GPUBvh bvh[];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
layout(std140, binding = 0) uniform CameraData
|
layout(std140, binding = 0) uniform CameraData
|
||||||
{
|
{
|
||||||
@ -105,64 +123,8 @@ struct hitInfo
|
|||||||
#include "shaders/scatter.glsl"
|
#include "shaders/scatter.glsl"
|
||||||
#include "shaders/light.glsl"
|
#include "shaders/light.glsl"
|
||||||
#include "shaders/volumetric.glsl"
|
#include "shaders/volumetric.glsl"
|
||||||
|
#include "shaders/trace.glsl"
|
||||||
|
|
||||||
Ray portalRay(Ray ray, hitInfo hit)
|
|
||||||
{
|
|
||||||
GPUObject portal_1;
|
|
||||||
GPUObject portal_2;
|
|
||||||
vec3 relative;
|
|
||||||
|
|
||||||
portal_1 = objects[hit.obj_index];
|
|
||||||
portal_2 = objects[int(portal_1.radius)]; // saving memory radius = portal_index
|
|
||||||
|
|
||||||
relative = hit.position - portal_1.position;
|
|
||||||
|
|
||||||
mat3 rotation = mat3(portal_2.rotation) * transpose(mat3(portal_1.rotation));
|
|
||||||
|
|
||||||
if (dot(portal_1.normal, portal_2.normal) > 0.0)
|
|
||||||
{
|
|
||||||
mat3 reflection = mat3(1.0) - 2.0 * outerProduct(portal_2.normal, portal_2.normal);
|
|
||||||
rotation *= reflection;
|
|
||||||
}
|
|
||||||
|
|
||||||
ray.origin = portal_2.position + rotation * relative;
|
|
||||||
ray.direction = normalize(rotation * ray.direction);
|
|
||||||
|
|
||||||
ray.origin += ray.direction * 0.01f;
|
|
||||||
|
|
||||||
return (ray);
|
|
||||||
}
|
|
||||||
|
|
||||||
hitInfo traceRay(Ray ray)
|
|
||||||
{
|
|
||||||
hitInfo hit;
|
|
||||||
|
|
||||||
for (int p = 0; p < 25; p++) //portals
|
|
||||||
{
|
|
||||||
hit.t = 1e30;
|
|
||||||
hit.obj_index = -1;
|
|
||||||
|
|
||||||
for (int i = 0; i < u_objectsNum; i++)
|
|
||||||
{
|
|
||||||
GPUObject obj = objects[i];
|
|
||||||
|
|
||||||
hitInfo temp_hit;
|
|
||||||
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
|
|
||||||
{
|
|
||||||
hit.t = temp_hit.t;
|
|
||||||
hit.last_t = temp_hit.last_t;
|
|
||||||
hit.obj_index = i;
|
|
||||||
hit.position = temp_hit.position;
|
|
||||||
hit.normal = temp_hit.normal;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (hit.obj_index == -1 || objects[hit.obj_index].type != 5)
|
|
||||||
break ;
|
|
||||||
ray = portalRay(ray, hit);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (hit);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 pathtrace(Ray ray, inout uint rng_state)
|
vec3 pathtrace(Ray ray, inout uint rng_state)
|
||||||
{
|
{
|
||||||
|
@ -100,33 +100,6 @@ int traceRay(Ray ray)
|
|||||||
return (num_hit);
|
return (num_hit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int traceBVHBad(Ray ray)
|
|
||||||
{
|
|
||||||
int num_hit;
|
|
||||||
|
|
||||||
num_hit = 0;
|
|
||||||
for (int i = 0; i < u_bvhNum; i++)
|
|
||||||
{
|
|
||||||
GPUBvh node = bvh[i];
|
|
||||||
|
|
||||||
if (intersectRayBVH(ray, node))
|
|
||||||
{
|
|
||||||
// num_hit++;
|
|
||||||
for (int i = 0; i < node.primitive_count; i++)
|
|
||||||
{
|
|
||||||
GPUObject obj = objects[node.first_primitive + i];
|
|
||||||
|
|
||||||
hitInfo tmp;
|
|
||||||
if (intersect(ray, obj, tmp))
|
|
||||||
num_hit++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (num_hit);
|
|
||||||
}
|
|
||||||
|
|
||||||
int traceBVH(Ray ray)
|
int traceBVH(Ray ray)
|
||||||
{
|
{
|
||||||
int num_hit = 0;
|
int num_hit = 0;
|
||||||
@ -157,7 +130,7 @@ int traceBVH(Ray ray)
|
|||||||
|
|
||||||
hitInfo tmp;
|
hitInfo tmp;
|
||||||
if (intersect(ray, obj, tmp))
|
if (intersect(ray, obj, tmp))
|
||||||
num_hit++;
|
num_hit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,6 +172,6 @@ void main()
|
|||||||
Ray ray = initRay(uv);
|
Ray ray = initRay(uv);
|
||||||
int hits = traceBVH(ray);
|
int hits = traceBVH(ray);
|
||||||
|
|
||||||
vec3 color = vec3(float(hits) / float(10));
|
vec3 color = vec3(float(hits) / float(u_bvhNum / 4));
|
||||||
imageStore(output_image, pixel_coords, vec4(color, 1.));
|
imageStore(output_image, pixel_coords, vec4(color, 1.));
|
||||||
}
|
}
|
@ -226,3 +226,22 @@ bool intersect(Ray ray, GPUObject obj, out hitInfo hit)
|
|||||||
return (intersectCylinder(ray, obj, hit));
|
return (intersectCylinder(ray, obj, hit));
|
||||||
return (false);
|
return (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool intersectRayBVH(Ray ray, GPUBvh node)
|
||||||
|
{
|
||||||
|
vec3 invDir = 1.0 / ray.direction;
|
||||||
|
|
||||||
|
vec3 t1 = (node.min - ray.origin) * invDir;
|
||||||
|
vec3 t2 = (node.max - ray.origin) * invDir;
|
||||||
|
|
||||||
|
vec3 tMin = min(t1, t2);
|
||||||
|
vec3 tMax = max(t1, t2);
|
||||||
|
|
||||||
|
float tEnter = max(max(tMin.x, tMin.y), tMin.z);
|
||||||
|
float tExit = min(min(tMax.x, tMax.y), tMax.z);
|
||||||
|
|
||||||
|
return tEnter <= tExit && tExit >= 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
118
shaders/trace.glsl
Normal file
118
shaders/trace.glsl
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
|
||||||
|
|
||||||
|
Ray portalRay(Ray ray, hitInfo hit)
|
||||||
|
{
|
||||||
|
GPUObject portal_1;
|
||||||
|
GPUObject portal_2;
|
||||||
|
vec3 relative;
|
||||||
|
|
||||||
|
portal_1 = objects[hit.obj_index];
|
||||||
|
portal_2 = objects[int(portal_1.radius)]; // saving memory radius = portal_index
|
||||||
|
|
||||||
|
relative = hit.position - portal_1.position;
|
||||||
|
|
||||||
|
mat3 rotation = mat3(portal_2.rotation) * transpose(mat3(portal_1.rotation));
|
||||||
|
|
||||||
|
if (dot(portal_1.normal, portal_2.normal) > 0.0)
|
||||||
|
{
|
||||||
|
mat3 reflection = mat3(1.0) - 2.0 * outerProduct(portal_2.normal, portal_2.normal);
|
||||||
|
rotation *= reflection;
|
||||||
|
}
|
||||||
|
|
||||||
|
ray.origin = portal_2.position + rotation * relative;
|
||||||
|
ray.direction = normalize(rotation * ray.direction);
|
||||||
|
|
||||||
|
ray.origin += ray.direction * 0.01f;
|
||||||
|
|
||||||
|
return (ray);
|
||||||
|
}
|
||||||
|
|
||||||
|
hitInfo traceScene(Ray ray)
|
||||||
|
{
|
||||||
|
hitInfo hit;
|
||||||
|
|
||||||
|
for (int p = 0; p < 25; p++) //portals
|
||||||
|
{
|
||||||
|
hit.t = 1e30;
|
||||||
|
hit.obj_index = -1;
|
||||||
|
|
||||||
|
for (int i = 0; i < u_objectsNum; i++)
|
||||||
|
{
|
||||||
|
GPUObject obj = objects[i];
|
||||||
|
|
||||||
|
hitInfo temp_hit;
|
||||||
|
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
|
||||||
|
{
|
||||||
|
hit.t = temp_hit.t;
|
||||||
|
hit.last_t = temp_hit.last_t;
|
||||||
|
hit.obj_index = i;
|
||||||
|
hit.position = temp_hit.position;
|
||||||
|
hit.normal = temp_hit.normal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hit.obj_index == -1 || objects[hit.obj_index].type != 5)
|
||||||
|
break ;
|
||||||
|
ray = portalRay(ray, hit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (hit);
|
||||||
|
}
|
||||||
|
|
||||||
|
hitInfo traceBVH(Ray ray)
|
||||||
|
{
|
||||||
|
hitInfo hit;
|
||||||
|
|
||||||
|
hit.t = 1e30;
|
||||||
|
hit.obj_index = -1;
|
||||||
|
|
||||||
|
const int MAX_STACK_SIZE = 64;
|
||||||
|
int stack[MAX_STACK_SIZE];
|
||||||
|
int stack_ptr = 0;
|
||||||
|
|
||||||
|
stack[0] = 0;
|
||||||
|
|
||||||
|
while (stack_ptr >= 0)
|
||||||
|
{
|
||||||
|
int current_index = stack[stack_ptr--];
|
||||||
|
|
||||||
|
GPUBvh node = bvh[current_index];
|
||||||
|
|
||||||
|
if (intersectRayBVH(ray, node))
|
||||||
|
{
|
||||||
|
if (node.is_leaf != 0)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < node.primitive_count; i++)
|
||||||
|
{
|
||||||
|
GPUObject obj = objects[node.first_primitive + i];
|
||||||
|
|
||||||
|
hitInfo temp_hit;
|
||||||
|
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
|
||||||
|
{
|
||||||
|
hit.t = temp_hit.t;
|
||||||
|
hit.last_t = temp_hit.last_t;
|
||||||
|
hit.obj_index = node.first_primitive + i;
|
||||||
|
hit.position = temp_hit.position;
|
||||||
|
hit.normal = temp_hit.normal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.is_leaf == 0 && stack_ptr < MAX_STACK_SIZE - 2)
|
||||||
|
{
|
||||||
|
stack_ptr++;
|
||||||
|
stack[stack_ptr] = node.left_index;
|
||||||
|
stack_ptr++;
|
||||||
|
stack[stack_ptr] = node.right_index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (hit);
|
||||||
|
}
|
||||||
|
|
||||||
|
hitInfo traceRay(Ray ray)
|
||||||
|
{
|
||||||
|
if (true)
|
||||||
|
return (traceBVH(ray));
|
||||||
|
return (traceScene(ray));
|
||||||
|
}
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
|
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
|
||||||
/* Updated: 2025/01/13 17:40:45 by tomoron ### ########.fr */
|
/* Updated: 2025/01/17 19:25:14 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,8 +20,8 @@ int main(int argc, char **argv)
|
|||||||
return (1);
|
return (1);
|
||||||
|
|
||||||
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0);
|
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0);
|
||||||
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
|
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
|
||||||
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
|
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
|
||||||
|
|
||||||
GLint max_gpu_size;
|
GLint max_gpu_size;
|
||||||
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);
|
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);
|
||||||
|
@ -3,16 +3,16 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* BVH.cpp :+: :+: :+: */
|
/* BVH.cpp :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: TheRed <TheRed@students.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/16 21:48:48 by TheRed ### ########.fr */
|
/* Updated: 2025/01/17 19:25:08 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "BVH.hpp"
|
#include "BVH.hpp"
|
||||||
|
|
||||||
BVH::BVH(std::vector<GPUObject> primitives, int first_primitive, int primitive_count) : _aabb(AABB(glm::vec3(1e30f), glm::vec3(-1e30f)))
|
BVH::BVH(std::vector<GPUObject> &primitives, int first_primitive, int primitive_count) : _aabb(AABB(glm::vec3(1e30f), glm::vec3(-1e30f)))
|
||||||
{
|
{
|
||||||
_left = nullptr;
|
_left = nullptr;
|
||||||
_right = nullptr;
|
_right = nullptr;
|
||||||
@ -26,7 +26,7 @@ BVH::BVH(std::vector<GPUObject> primitives, int first_primitive, int primitive_c
|
|||||||
subdivide(primitives);
|
subdivide(primitives);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BVH::updateBounds(std::vector <GPUObject> primitives)
|
void BVH::updateBounds(std::vector<GPUObject> &primitives)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < _primitive_count; i++)
|
for (int i = 0; i < _primitive_count; i++)
|
||||||
{
|
{
|
||||||
@ -44,13 +44,11 @@ void BVH::updateBounds(std::vector <GPUObject> primitives)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BVH::subdivide(std::vector<GPUObject> primitives)
|
void BVH::subdivide(std::vector<GPUObject> &primitives)
|
||||||
{
|
{
|
||||||
if (_primitive_count <= 2)
|
if (_primitive_count <= 100)
|
||||||
return ;
|
return ;
|
||||||
|
|
||||||
std::cout << "subdivide" << std::endl;
|
|
||||||
|
|
||||||
glm::vec3 extent = _aabb.max - _aabb.min;
|
glm::vec3 extent = _aabb.max - _aabb.min;
|
||||||
|
|
||||||
int axis = 0;
|
int axis = 0;
|
||||||
@ -78,9 +76,6 @@ void BVH::subdivide(std::vector<GPUObject> primitives)
|
|||||||
|
|
||||||
int left_count = i - _first_primitive;
|
int left_count = i - _first_primitive;
|
||||||
|
|
||||||
std::cout << "left bvh starts from " << _first_primitive << " and has " << left_count << " primitives" << std::endl;
|
|
||||||
std::cout << "right bvh starts from " << i << " and has " << _primitive_count - left_count << " primitives" << std::endl;
|
|
||||||
|
|
||||||
if (left_count == 0 || left_count == _primitive_count)
|
if (left_count == 0 || left_count == _primitive_count)
|
||||||
return ;
|
return ;
|
||||||
|
|
||||||
@ -95,7 +90,7 @@ void BVH::subdivide(std::vector<GPUObject> primitives)
|
|||||||
|
|
||||||
void BVH::showAABB(Scene *scene)
|
void BVH::showAABB(Scene *scene)
|
||||||
{
|
{
|
||||||
if (_is_leaf)
|
if (!_is_leaf)
|
||||||
{
|
{
|
||||||
scene->addObject(new Sphere(_aabb.min, 0.5f, 6));
|
scene->addObject(new Sphere(_aabb.min, 0.5f, 6));
|
||||||
scene->addObject(new Sphere(_aabb.max, 0.5f, 6));
|
scene->addObject(new Sphere(_aabb.max, 0.5f, 6));
|
||||||
@ -108,8 +103,8 @@ void BVH::showAABB(Scene *scene)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_left->showAABB(scene);
|
// _left->showAABB(scene);
|
||||||
_right->showAABB(scene);
|
// _right->showAABB(scene);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,5 +166,7 @@ std::vector<GPUBvh> BVH::getGPUBvhs()
|
|||||||
int currentIndex = 0;
|
int currentIndex = 0;
|
||||||
flatten(bvhs, currentIndex);
|
flatten(bvhs, currentIndex);
|
||||||
|
|
||||||
|
std::cout << "BVH Done: " << bvhs.size() << std::endl;
|
||||||
|
|
||||||
return (bvhs);
|
return (bvhs);
|
||||||
}
|
}
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */
|
/* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */
|
||||||
/* Updated: 2025/01/15 19:34:49 by ycontre ### ########.fr */
|
/* Updated: 2025/01/17 19:05:53 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -56,9 +56,6 @@ bool Scene::parseScene(char *name)
|
|||||||
BVH *bvh = new BVH(_gpu_objects, 0, _gpu_objects.size());
|
BVH *bvh = new BVH(_gpu_objects, 0, _gpu_objects.size());
|
||||||
_gpu_bvh = bvh->getGPUBvhs();
|
_gpu_bvh = bvh->getGPUBvhs();
|
||||||
|
|
||||||
// bvh->showAABB(this);
|
|
||||||
|
|
||||||
|
|
||||||
return (true);
|
return (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */
|
/* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */
|
||||||
/* Updated: 2025/01/10 18:53:39 by ycontre ### ########.fr */
|
/* Updated: 2025/01/17 19:05:41 by ycontre ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user