Adding model loading and bvh generation

This commit is contained in:
Dynamitos
2025-01-23 17:19:53 +01:00
parent 4566e19526
commit 91bdbe4a09
13 changed files with 135 additions and 28 deletions
+38
View File
@@ -0,0 +1,38 @@
#pragma once
#include <glm/vec3.hpp>
#include <utility>
struct AABB
{
glm::vec3 min = glm::vec3(std::numeric_limits<float>::max());
glm::vec3 max = glm::vec3(std::numeric_limits<float>::lowest());
float surfaceArea() const
{
glm::vec3 d = glm::vec3(max.x - min.x, max.y - min.y, max.z - min.z);
return 2.0f * (d.x * d.y + d.y * d.z + d.z * d.x);
}
void adjust(glm::vec3 pos)
{
min.x = std::min(min.x, pos.x);
min.y = std::min(min.y, pos.y);
min.z = std::min(min.z, pos.z);
max.x = std::max(max.x, pos.x);
max.y = std::max(max.y, pos.y);
max.z = std::max(max.z, pos.z);
}
static AABB combine(AABB lhs, AABB rhs)
{
AABB result;
result.min.x = std::min(lhs.min.x, rhs.min.x);
result.min.y = std::min(lhs.min.y, rhs.min.y);
result.min.z = std::min(lhs.min.z, rhs.min.z);
result.max.x = std::max(lhs.max.x, rhs.max.x);
result.max.y = std::max(lhs.max.y, rhs.max.y);
result.max.z = std::max(lhs.max.z, rhs.max.z);
return result;
}
};
+51 -1
View File
@@ -1 +1,51 @@
#include "BVH.h"
#include "BVH.h"
#include <list>
void BVH::addModel(PModel model) { models.push_back(std::move(model)); }
void BVH::addModels(std::vector<PModel> _models)
{
for (int i = 0; i < _models.size(); ++i)
{
models.push_back(std::move(_models[i]));
}
}
void BVH::generate()
{
std::vector<PNode> pendingNodes;
while (!models.empty())
{
pendingNodes.push_back(std::make_unique<Node>(std::move(models.back())));
models.pop_back();
}
while (pendingNodes.size() > 1)
{
int lhs = pendingNodes.size();
int rhs = pendingNodes.size();
float minSurface = std::numeric_limits<float>::max();
for (int i = 0; i < pendingNodes.size(); ++i)
{
for (int j = 0; j < pendingNodes.size(); ++j)
{
if (i == j)
continue;
AABB combined = AABB::combine(pendingNodes[i]->aabb, pendingNodes[j]->aabb);
float surface = combined.surfaceArea();
if (minSurface > surface)
{
lhs = i;
rhs = j;
minSurface = surface;
}
}
}
PNode newNode = std::make_unique<Node>(AABB::combine(pendingNodes[lhs]->aabb, pendingNodes[rhs]->aabb));
newNode->left = std::move(pendingNodes[lhs]);
newNode->right = std::move(pendingNodes[rhs]);
pendingNodes.erase(pendingNodes.begin() + lhs);
pendingNodes.erase(pendingNodes.begin() + rhs);
pendingNodes.push_back(std::move(newNode));
}
hierarchy = std::move(pendingNodes[0]);
}
+20 -9
View File
@@ -1,15 +1,26 @@
#pragma once
#include <glm/glm.hpp>
#include "AABB.h"
#include "util/Model.h"
#include <vector>
struct AABB {
glm::vec3 min;
glm::vec3 max;
};
class BVH
{
public:
private:
std::vector<AABB> boundingBoxes;
public:
void addModel(PModel model);
void addModels(std::vector<PModel> models);
void generate();
private:
DECLARE_REF(Node)
struct Node
{
PNode left;
PNode right;
AABB aabb;
PModel model;
Node(AABB aabb) : aabb(aabb) {}
Node(PModel model) : aabb(model->boundingBox), model(std::move(model)) {}
};
PNode hierarchy;
std::vector<PModel> models;
};
+3
View File
@@ -1,4 +1,7 @@
target_sources(RayTracer
PRIVATE
AABB.h
BVH.h
BVH.cpp
Scene.h
Scene.cpp)