~ | BVH sent to gpu

This commit is contained in:
TheRedShip
2025-01-17 16:27:39 +01:00
parent 6f80a66550
commit 6fdea11adb
14 changed files with 359 additions and 46 deletions

View File

@ -15,6 +15,9 @@
# include "RT.hpp"
struct GPUObject;
struct GPUBvh;
struct AABB
{
glm::vec3 min;
@ -23,7 +26,6 @@ struct AABB
AABB(glm::vec3 min, glm::vec3 max) : min(min), max(max) {}
};
struct GPUObject;
class BVH
{
@ -36,7 +38,13 @@ class BVH
void updateBounds(std::vector<GPUObject> primitives);
void subdivide(std::vector<GPUObject> primitives);
const AABB &getAABB() const;
int size();
void flatten(std::vector<GPUBvh> &bvhs, int &currentIndex);
GPUBvh toGPUBvh();
const AABB &getAABB() const;
std::vector<GPUBvh> getGPUBvhs();
private:

View File

@ -54,6 +54,20 @@ struct GPUVolume
int enabled;
};
struct GPUBvh
{
alignas(16) glm::vec3 min;
alignas(16) glm::vec3 max;
int left_index;
int right_index;
int is_leaf;
int first_primitive;
int primitive_count;
};
class Sphere;
class Camera;
@ -74,11 +88,14 @@ class Scene
const std::vector<GPUObject> &getObjectData() const;
std::vector<GPUMaterial> &getMaterialData();
GPUVolume &getVolume();
std::vector<GPUBvh> &getBVH();
Camera *getCamera(void) const;
GPUMaterial getMaterial(int material_index);
private:
std::vector<GPUBvh> _gpu_bvh;
std::vector<GPUObject> _gpu_objects;
std::vector<GPUMaterial> _gpu_materials;

View File

@ -25,8 +25,6 @@ class Shader
void setupVertexBuffer(const Vertex* vertices, size_t size);
void drawTriangles(size_t size);
// void setBool(const std::string &name, bool value) const;
void set_int(const std::string &name, int value) const;
void set_float(const std::string &name, float value) const;

View File

@ -42,8 +42,8 @@ class Triangle : public Object
_vertex2 = glm::vec3(x2, y2, z2);
_vertex3 = glm::vec3(x3, y3, z3);
_vertex2 -= _position; //optimization
_vertex3 -= _position; //optimization
// _vertex2 -= _position; //optimization
// _vertex3 -= _position; //optimization
_normal = glm::normalize(glm::cross(_vertex2, _vertex3)); //optimization
@ -51,8 +51,8 @@ class Triangle : public Object
}
Triangle(const glm::vec3& position, const glm::vec3& vertex2, const glm::vec3& vertex3, const int mat_index)
: Object(position, mat_index), _vertex2(vertex2), _vertex3(vertex3) {
_vertex2 -= _position; //optimization
_vertex3 -= _position; //optimization
// _vertex2 -= _position; //optimization
// _vertex3 -= _position; //optimization
_normal = glm::normalize(glm::cross(_vertex2, _vertex3)); //optimization
}