+ | BVH Working

This commit is contained in:
RedShip
2025-01-17 19:31:43 +01:00
parent f5482258ba
commit ce49ecb219
14 changed files with 201 additions and 152 deletions

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
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/debug.glsl");
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
GLint max_gpu_size;
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);

View File

@ -3,16 +3,16 @@
/* ::: :::::::: */
/* BVH.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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;
_right = nullptr;
@ -26,7 +26,7 @@ BVH::BVH(std::vector<GPUObject> primitives, int first_primitive, int primitive_c
subdivide(primitives);
}
void BVH::updateBounds(std::vector <GPUObject> primitives)
void BVH::updateBounds(std::vector<GPUObject> &primitives)
{
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 ;
std::cout << "subdivide" << std::endl;
glm::vec3 extent = _aabb.max - _aabb.min;
int axis = 0;
@ -78,9 +76,6 @@ void BVH::subdivide(std::vector<GPUObject> primitives)
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)
return ;
@ -88,14 +83,14 @@ void BVH::subdivide(std::vector<GPUObject> primitives)
_is_leaf = false;
_left = new BVH(primitives, _first_primitive, left_count);
_right = new BVH(primitives, i, _primitive_count - left_count);
_right = new BVH(primitives, i , _primitive_count - left_count);
_primitive_count = 0;
}
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.max, 0.5f, 6));
@ -108,8 +103,8 @@ void BVH::showAABB(Scene *scene)
}
else
{
_left->showAABB(scene);
_right->showAABB(scene);
// _left->showAABB(scene);
// _right->showAABB(scene);
}
}
@ -171,5 +166,7 @@ std::vector<GPUBvh> BVH::getGPUBvhs()
int currentIndex = 0;
flatten(bvhs, currentIndex);
std::cout << "BVH Done: " << bvhs.size() << std::endl;
return (bvhs);
}

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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());
_gpu_bvh = bvh->getGPUBvhs();
// bvh->showAABB(this);
return (true);
}

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */