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
+1
View File
@@ -4,3 +4,4 @@ target_sources(RayTracer
add_subdirectory(scene/)
add_subdirectory(window/)
add_subdirectory(util/)
+5
View File
@@ -1,6 +1,11 @@
#include "window/Window.h"
#include "util/ModelLoader.h"
#include "scene/BVH.h"
int main() {
BVH bvh;
bvh.addModels(ModelLoader::loadModel("../../cube.fbx"));
bvh.generate();
Window w(1920, 1080);
while (true) {
w.update();
+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)
+1
View File
@@ -4,4 +4,5 @@ target_sources(RayTracer
Model.cpp
ModelLoader.h
ModelLoader.cpp
Ray.h
)
+2 -1
View File
@@ -1,11 +1,12 @@
#pragma once
#include "Minimal.h"
#include "scene/AABB.h"
#include <vector>
#include <glm/glm.hpp>
class Model
{
public:
AABB boundingBox;
std::vector<glm::vec3> positions;
std::vector<uint32_t> indices;
};
+12 -6
View File
@@ -8,22 +8,28 @@
std::vector<PModel> ModelLoader::loadModel(std::string_view filename)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(filename, 0);
const aiScene* scene = importer.ReadFile(std::string(filename), aiProcess_Triangulate);
std::vector<PModel> result;
for (int m = 0; m < scene->mNumMeshes; ++m)
{
PModel result = std::make_unique<Model>();
PModel model = std::make_unique<Model>();
const aiMesh* mesh = scene->mMeshes[m];
AABB aabb;
for (int v = 0; v < mesh->mNumVertices; ++v)
{
auto aiVert = mesh->mVertices[v];
result->positions.add(glm::vec3(aiVert.x, aiVert.y, aiVert.z));
model->positions.push_back(glm::vec3(aiVert.x, aiVert.y, aiVert.z));
aabb.adjust(model->positions.back());
}
for (int i = 0; i < mesh->mNumFaces; ++i)
{
auto face = mesh->mFaces[i];
result->indices.add(face.mIndices[0]);
result->indices.add(face.mIndices[1]);
result->indices.add(face.mIndices[2]);
model->indices.push_back(face.mIndices[0]);
model->indices.push_back(face.mIndices[1]);
model->indices.push_back(face.mIndices[2]);
}
model->boundingBox = aabb;
result.push_back(std::move(model));
}
return result;
}
+1
View File
@@ -1,5 +1,6 @@
#pragma once
#include "Model.h"
#include <string_view>
class ModelLoader
{
View File