Files
RayTracer/src/scene/BVH.cpp
T

113 lines
3.2 KiB
C++
Raw Normal View History

2025-01-23 17:19:53 +01:00
#include "BVH.h"
2025-01-24 18:21:34 +01:00
#include <algorithm>
#include <ranges>
2025-01-23 17:19:53 +01:00
void BVH::addModel(PModel model, glm::mat4 transform)
{
model->boundingBox.transform(transform);
2025-01-24 23:20:26 +01:00
for (auto& point : model->positions)
{
point = glm::vec3(transform * glm::vec4(point, 1));
}
models.push_back(std::move(model));
}
2025-01-23 17:19:53 +01:00
void BVH::addModels(std::vector<PModel> _models, glm::mat4 transform)
2025-01-23 17:19:53 +01:00
{
2025-01-24 23:32:16 +01:00
for (auto & _model : _models)
2025-01-23 17:19:53 +01:00
{
2025-01-24 23:32:16 +01:00
_model->boundingBox.transform(transform);
for (auto& point : _model->positions)
2025-01-24 23:20:26 +01:00
{
point = glm::vec3(transform * glm::vec4(point, 1));
}
2025-01-24 23:32:16 +01:00
models.push_back(std::move(_model));
2025-01-23 17:19:53 +01:00
}
}
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]);
}
2025-01-24 18:21:34 +01:00
std::optional<IntersectionInfo> BVH::traceRay(Ray ray)
{
2025-01-24 19:14:20 +01:00
auto results = generateIntersections(hierarchy, ray);
float closestT = std::numeric_limits<float>::max();
IntersectionInfo info;
for (uint32_t i = 0; i < results.size(); ++i)
{
if (results[i].t < closestT)
{
closestT = results[i].t;
info = results[i];
}
}
if (closestT < std::numeric_limits<float>::max())
{
return info;
}
return {};
2025-01-24 18:21:34 +01:00
}
std::vector<IntersectionInfo> BVH::generateIntersections(PNode& currentNode, Ray ray)
{
2025-01-25 15:22:49 +01:00
if (!currentNode->aabb.intersects(ray, 0, std::numeric_limits<float>::max()))
2025-01-24 18:21:34 +01:00
{
return {};
}
2025-01-24 19:14:20 +01:00
if (currentNode->model != nullptr)
2025-01-24 18:21:34 +01:00
{
auto result = currentNode->model->intersect(ray);
2025-01-24 19:14:20 +01:00
if (result.has_value())
2025-01-24 18:21:34 +01:00
{
return {*result};
}
2025-01-24 19:14:20 +01:00
else
2025-01-24 18:21:34 +01:00
{
return {};
}
}
auto leftResults = generateIntersections(currentNode->left, ray);
auto rightResults = generateIntersections(currentNode->right, ray);
2025-01-24 19:14:20 +01:00
for (auto& it : rightResults)
2025-01-24 18:21:34 +01:00
{
2025-01-24 19:14:20 +01:00
leftResults.push_back(std::move(it));
2025-01-24 18:21:34 +01:00
}
return leftResults;
}