mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
+ | TopLevel builded
This commit is contained in:
@ -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);
|
||||
|
@ -58,6 +58,9 @@ bool Scene::parseScene(char *name)
|
||||
}
|
||||
file.close();
|
||||
|
||||
TopBVH *top_bvh = new TopBVH(_gpu_bvh_data, _gpu_bvh, 0, _gpu_bvh_data.size());
|
||||
(void) top_bvh;
|
||||
|
||||
|
||||
std::cout << "Parsing done" << std::endl;
|
||||
|
||||
@ -187,7 +190,6 @@ void Scene::addBvh(std::vector<Triangle> &triangles, glm::vec3 offset, float sc
|
||||
std::cout << "\tMin triangles per leaf: " << stats.min_triangles << std::endl;
|
||||
std::cout << "\tMax triangles per leaf: " << stats.max_triangles << std::endl;
|
||||
std::cout << "\tAverage triangles per leaf: " << stats.average_triangles << std::endl << std::endl;
|
||||
|
||||
}
|
||||
|
||||
void Scene::addMaterial(Material *material)
|
||||
|
144
srcs/class/TopBVH.cpp
Normal file
144
srcs/class/TopBVH.cpp
Normal file
@ -0,0 +1,144 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* TopBvh.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/21 21:46:45 by TheRed #+# #+# */
|
||||
/* Updated: 2025/01/21 21:46:45 by TheRed ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "TopBVH.hpp"
|
||||
|
||||
TopBVH::TopBVH(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs, int first_bvh, int bvh_count) : _aabb(AABB(glm::vec3(1e30f), glm::vec3(-1e30f)))
|
||||
{
|
||||
_left = nullptr;
|
||||
_right = nullptr;
|
||||
|
||||
_is_leaf = true;
|
||||
|
||||
_first_bvh = first_bvh;
|
||||
_bvh_count = bvh_count;
|
||||
|
||||
updateBounds(bvhs_data, bvhs);
|
||||
subdivide(bvhs_data, bvhs);
|
||||
}
|
||||
|
||||
void TopBVH::updateBounds(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs)
|
||||
{
|
||||
for (int i = 0; i < _bvh_count; i++)
|
||||
{
|
||||
GPUBvhData bvh_data = bvhs_data[_first_bvh + i];
|
||||
GPUBvh root_bvh = bvhs[bvh_data.bvh_start_index];
|
||||
|
||||
glm::vec3 min = root_bvh.min - bvh_data.offset;
|
||||
glm::vec3 max = root_bvh.max - bvh_data.offset;
|
||||
|
||||
_aabb.min = glm::min(_aabb.min, min);
|
||||
_aabb.max = glm::max(_aabb.max, max);
|
||||
}
|
||||
}
|
||||
|
||||
float TopBVH::evaluateSah(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs, int axis, float pos)
|
||||
{
|
||||
AABB left_box(glm::vec3(1e30f), glm::vec3(-1e30f));
|
||||
AABB right_box(glm::vec3(1e30f), glm::vec3(-1e30f));
|
||||
|
||||
int left_count = 0;
|
||||
int right_count = 0;
|
||||
|
||||
for (int i = 0; i < _bvh_count; i++)
|
||||
{
|
||||
GPUBvhData bvh_data = bvhs_data[_first_bvh + i];
|
||||
GPUBvh bvh_root = bvhs[bvh_data.bvh_start_index];
|
||||
|
||||
glm::vec3 min = bvh_root.min - bvh_data.offset;
|
||||
glm::vec3 max = bvh_root.max - bvh_data.offset;
|
||||
|
||||
if (min[axis] < pos)
|
||||
{
|
||||
left_count++;
|
||||
left_box.grow( min );
|
||||
left_box.grow( max );
|
||||
}
|
||||
else
|
||||
{
|
||||
right_count++;
|
||||
right_box.grow( min );
|
||||
right_box.grow( max );
|
||||
}
|
||||
}
|
||||
|
||||
return (left_box.area() * left_count + right_box.area() * right_count);
|
||||
}
|
||||
|
||||
void TopBVH::subdivide(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &bvhs)
|
||||
{
|
||||
if (_bvh_count <= 1)
|
||||
return ;
|
||||
|
||||
|
||||
const int num_test_per_axis = 5;
|
||||
|
||||
int best_axis = 0;
|
||||
float best_pos = 0.0f;
|
||||
float best_cost = 1e30f;
|
||||
|
||||
for (int axis = 0; axis < 3; axis++)
|
||||
{
|
||||
float start_pos = _aabb.min[axis];
|
||||
float end_pos = _aabb.max[axis];
|
||||
|
||||
for (int i = 0; i < num_test_per_axis; i++)
|
||||
{
|
||||
float split_t = (i + 1) / (num_test_per_axis + 1.0f);
|
||||
float candidate_pos = start_pos + (end_pos - start_pos) * split_t;
|
||||
|
||||
float cost = evaluateSah(bvhs_data, bvhs, axis, candidate_pos);
|
||||
|
||||
if (cost < best_cost)
|
||||
{
|
||||
best_pos = candidate_pos;
|
||||
best_axis = axis;
|
||||
best_cost = cost;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int axis = best_axis;
|
||||
float split_pos = best_pos;
|
||||
|
||||
int i = _first_bvh;
|
||||
int j = _first_bvh + _bvh_count - 1;
|
||||
|
||||
while (i <= j)
|
||||
{
|
||||
GPUBvhData bvh_data = bvhs_data[i];
|
||||
GPUBvh root = bvhs[bvh_data.bvh_start_index];
|
||||
glm::vec3 min = root.min - bvh_data.offset;
|
||||
glm::vec3 max = root.max - bvh_data.offset;
|
||||
glm::vec3 centroid = (min + max) * 0.5f;
|
||||
|
||||
if (centroid[axis] < split_pos)
|
||||
i++;
|
||||
else
|
||||
{
|
||||
std::swap(bvhs_data[i], bvhs_data[j]);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
int left_count = i - _first_bvh;
|
||||
|
||||
if (left_count == 0 || left_count == _bvh_count)
|
||||
return ;
|
||||
|
||||
_is_leaf = false;
|
||||
|
||||
_left = new TopBVH(bvhs_data, bvhs, _first_bvh, left_count);
|
||||
_right = new TopBVH(bvhs_data, bvhs, i, _bvh_count - left_count);
|
||||
|
||||
_bvh_count = 0;
|
||||
}
|
Reference in New Issue
Block a user