+ | Starting bvh

This commit is contained in:
TheRedShip
2025-01-16 23:50:09 +01:00
parent 67eca73d47
commit 6f80a66550
10 changed files with 204 additions and 16 deletions

127
srcs/class/BVH.cpp Normal file
View File

@ -0,0 +1,127 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* BVH.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 21:48:48 by TheRed #+# #+# */
/* Updated: 2025/01/16 21:48:48 by TheRed ### ########.fr */
/* */
/* ************************************************************************** */
#include "BVH.hpp"
BVH::BVH(std::vector<GPUObject> primitives, int first_primitive, int primitive_count) : _aabb(AABB(glm::vec3(1e30f), glm::vec3(-1e30f)))
{
_left = nullptr;
_right = nullptr;
_is_leaf = true;
_first_primitive = first_primitive;
_primitive_count = primitive_count;
updateBounds(primitives);
subdivide(primitives);
}
void BVH::updateBounds(std::vector <GPUObject> primitives)
{
for (int i = 0; i < _primitive_count; i++)
{
GPUObject leaf_triangle = primitives[_first_primitive + i];
if (leaf_triangle.type != (int)Object::Type::TRIANGLE)
continue ;
if (leaf_triangle.type == (int)Object::Type::TRIANGLE)
{
leaf_triangle.vertex1 += leaf_triangle.position;
leaf_triangle.vertex2 += leaf_triangle.position;
}
_aabb.min = glm::min(_aabb.min, leaf_triangle.position);
_aabb.min = glm::min(_aabb.min, leaf_triangle.vertex1);
_aabb.min = glm::min(_aabb.min, leaf_triangle.vertex2);
_aabb.max = glm::max(_aabb.max, leaf_triangle.position);
_aabb.max = glm::max(_aabb.max, leaf_triangle.vertex1);
_aabb.max = glm::max(_aabb.max, leaf_triangle.vertex2);
}
}
void BVH::subdivide(std::vector<GPUObject> primitives)
{
if (_primitive_count <= 2)
return ;
std::cout << "subdivide" << std::endl;
glm::vec3 extent = _aabb.max - _aabb.min;
int axis = 0;
if (extent.y > extent.x) axis = 1;
if (extent.z > extent[axis]) axis = 2;
glm::vec3 split_pos = _aabb.min + extent[axis] * 0.5f;
int i = _first_primitive;
int j = _first_primitive + _primitive_count - 1;
while (i <= j)
{
glm::vec3 centroid = primitives[i].position + primitives[i].vertex1 + primitives[i].vertex2;
centroid /= 3.0f;
if (centroid[axis] < split_pos[axis])
i++;
else
{
std::swap(primitives[i], primitives[j]);
j--;
}
}
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 ;
_is_leaf = false;
_left = new BVH(primitives, _first_primitive, left_count);
_right = new BVH(primitives, i, _primitive_count - left_count);
_primitive_count = 0;
}
void BVH::showAABB(Scene *scene)
{
if (_is_leaf)
{
scene->addObject(new Sphere(_aabb.min, 0.5f, 6));
scene->addObject(new Sphere(_aabb.max, 0.5f, 6));
scene->addObject(new Sphere(glm::vec3(_aabb.min.x, _aabb.min.y, _aabb.max.z), 0.5f, 6));
scene->addObject(new Sphere(glm::vec3(_aabb.min.x, _aabb.max.y, _aabb.min.z), 0.5f, 6));
scene->addObject(new Sphere(glm::vec3(_aabb.max.x, _aabb.min.y, _aabb.min.z), 0.5f, 6));
scene->addObject(new Sphere(glm::vec3(_aabb.min.x, _aabb.max.y, _aabb.max.z), 0.5f, 6));
scene->addObject(new Sphere(glm::vec3(_aabb.max.x, _aabb.min.y, _aabb.max.z), 0.5f, 6));
scene->addObject(new Sphere(glm::vec3(_aabb.max.x, _aabb.max.y, _aabb.min.z), 0.5f, 6));
}
else
{
_left->showAABB(scene);
_right->showAABB(scene);
}
}
const AABB &BVH::getAABB() const
{
return (_aabb);
}

View File

@ -61,20 +61,20 @@ void ObjParser::addTriangleFromPolygon(std::vector<glm::vec3> &vertices, Scene &
v4 = vertices[(i + 2) % vertices.size()];
normal = glm::normalize(glm::cross(v2 - v1, v3 - v1));
std::cout << glm::to_string(normal) << std::endl;
// std::cout << glm::to_string(normal) << std::endl;
float dot = glm::dot(normal, v4 - v1);
std::cout << dot << std::endl;
// float dot = glm::dot(normal, v4 - v1);
// std::cout << dot << std::endl;
//if(isEar(v1, v2 ,v3, vertices))
//{
// vertices.erase(vertices.begin() + i);
// scene.addObject(new Triangle(v1, v2 ,v3));
//}
}
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
// std::cout << std::endl;
// std::cout << std::endl;
// std::cout << std::endl;
// std::cout << std::endl;
}
void ObjParser::addFace(std::stringstream &line, std::vector<glm::vec3> &vertices, int mat, Scene &scene)
@ -89,9 +89,9 @@ void ObjParser::addFace(std::stringstream &line, std::vector<glm::vec3> &vertice
if(face_vertices.size() < 3)
throw std::runtime_error("OBJ : face does not have enough vertices");
for(size_t i = 0; i < face_vertices.size(); i++)
std::cout << face_vertices[i].x << " " << face_vertices[i].y << " " << face_vertices[i].z << " | ";
std::cout << std::endl;
// for(size_t i = 0; i < face_vertices.size(); i++)
// std::cout << face_vertices[i].x << " " << face_vertices[i].y << " " << face_vertices[i].z << " | ";
// std::cout << std::endl;
// while(face_vertices.size() > 3)
// addTriangleFromPolygon(face_vertices, scene);

View File

@ -52,6 +52,13 @@ bool Scene::parseScene(char *name)
}
}
file.close();
//bvh
BVH *bvh = new BVH(_gpu_objects, 0, _gpu_objects.size());
bvh->showAABB(this);
// addObject(new Cube((bvh->getAABB().max + bvh->getAABB().min) / 2.0f, bvh->getAABB().max - bvh->getAABB().min, 7));
//
return (true);
}

View File

@ -67,7 +67,7 @@ Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string com
const char *fragmentCode = loadFileWithIncludes(fragmentPath);
const char *computeCode = loadFileWithIncludes(computePath);
printWithLineNumbers(computeCode);
// printWithLineNumbers(computeCode);
_vertex = glCreateShader(GL_VERTEX_SHADER);