2022-10-20 11:30:15 -04:00
|
|
|
/*
|
|
|
|
* Created by Brett Terpstra 6920201 on 17/10/22.
|
|
|
|
* Copyright (c) 2022 Brett Terpstra. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef STEP_2_BVH_H
|
|
|
|
#define STEP_2_BVH_H
|
|
|
|
|
2022-10-23 23:46:12 -04:00
|
|
|
#include "engine/util/std.h"
|
|
|
|
#include "engine/types.h"
|
2022-11-13 14:13:14 -05:00
|
|
|
#include <config.h>
|
2022-11-15 00:53:21 -05:00
|
|
|
|
2022-11-13 14:13:14 -05:00
|
|
|
#ifdef COMPILE_GUI
|
2022-11-15 00:53:21 -05:00
|
|
|
|
2022-11-13 14:13:14 -05:00
|
|
|
#include <graphics/gl/gl.h>
|
2022-11-14 20:44:49 -05:00
|
|
|
#include <graphics/imgui/imgui.h>
|
2022-11-15 00:53:21 -05:00
|
|
|
|
2022-11-13 14:13:14 -05:00
|
|
|
#endif
|
2022-10-20 11:30:15 -04:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace Raytracing {
|
|
|
|
|
2022-11-13 14:13:14 -05:00
|
|
|
#ifdef COMPILE_GUI
|
2022-11-15 00:53:21 -05:00
|
|
|
extern std::shared_ptr<VAO> aabbVAO;
|
|
|
|
extern int count;
|
|
|
|
extern int selected;
|
2022-11-13 14:13:14 -05:00
|
|
|
#endif
|
|
|
|
|
2022-11-13 13:03:48 -05:00
|
|
|
struct BVHObject {
|
|
|
|
Object* ptr = nullptr;
|
|
|
|
AABB aabb;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BVHPartitionedSpace {
|
|
|
|
std::vector<BVHObject> left;
|
|
|
|
std::vector<BVHObject> right;
|
|
|
|
};
|
|
|
|
|
2022-10-20 11:30:15 -04:00
|
|
|
struct BVHNode {
|
|
|
|
public:
|
2022-11-15 16:23:37 -05:00
|
|
|
struct BVHHitData {
|
2022-11-16 14:56:50 -05:00
|
|
|
BVHNode* ptr{};
|
|
|
|
AABBHitData data{};
|
2022-11-15 16:23:37 -05:00
|
|
|
bool hit = false;
|
|
|
|
};
|
2022-11-13 13:03:48 -05:00
|
|
|
std::vector<BVHObject> objs;
|
2022-10-20 11:30:15 -04:00
|
|
|
AABB aabb;
|
|
|
|
BVHNode* left;
|
|
|
|
BVHNode* right;
|
2022-11-14 20:44:49 -05:00
|
|
|
int index;
|
2022-11-15 16:23:37 -05:00
|
|
|
int hit = 0;
|
2022-11-13 13:03:48 -05:00
|
|
|
BVHNode(std::vector<BVHObject> objs, AABB aabb, BVHNode* left, BVHNode* right): objs(std::move(objs)), aabb(std::move(aabb)),
|
2022-11-15 00:53:21 -05:00
|
|
|
left(left), right(right) {
|
2022-11-14 20:44:49 -05:00
|
|
|
index = count++;
|
|
|
|
}
|
2022-11-16 14:56:50 -05:00
|
|
|
BVHHitData firstHitRayIntersectTraversal(const Ray& r, PRECISION_TYPE min, PRECISION_TYPE max);
|
2022-10-20 11:30:15 -04:00
|
|
|
~BVHNode() {
|
|
|
|
delete (left);
|
|
|
|
delete (right);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BVHTree {
|
|
|
|
private:
|
|
|
|
BVHNode* root = nullptr;
|
|
|
|
|
|
|
|
// splits the objs in the vector based on the provided AABBs
|
2022-11-16 14:56:50 -05:00
|
|
|
static BVHPartitionedSpace partition(const std::pair<AABB, AABB>& aabbs, const std::vector<BVHObject>& objs);
|
2022-11-16 16:15:08 -05:00
|
|
|
static bool vectorEquals(const BVHPartitionedSpace& oldSpace, const BVHPartitionedSpace& newSpace);
|
|
|
|
BVHNode* addObjectsRecur(const std::vector<BVHObject>& objects, const BVHPartitionedSpace& prevSpace);
|
2022-10-20 11:30:15 -04:00
|
|
|
public:
|
|
|
|
std::vector<Object*> noAABBObjects;
|
|
|
|
explicit BVHTree(const std::vector<Object*>& objectsInWorld) {
|
|
|
|
addObjects(objectsInWorld);
|
2022-11-14 20:44:49 -05:00
|
|
|
#ifdef COMPILE_GUI
|
2022-11-15 00:53:21 -05:00
|
|
|
auto aabbVertexData = Shapes::cubeVertexBuilder{};
|
|
|
|
if (aabbVAO == nullptr)
|
|
|
|
aabbVAO = std::make_shared<VAO>(aabbVertexData.cubeVerticesRaw, aabbVertexData.cubeUVs);
|
2022-11-14 20:44:49 -05:00
|
|
|
#endif
|
2022-10-20 11:30:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-16 16:15:08 -05:00
|
|
|
void addObjects(const std::vector<Object*>& objects);
|
2022-10-20 11:30:15 -04:00
|
|
|
|
2022-11-16 14:56:50 -05:00
|
|
|
std::vector<BVHObject> rayFirstHitIntersect(const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max);
|
|
|
|
std::vector<BVHObject> rayAnyHitIntersect(const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max);
|
2022-10-20 11:30:15 -04:00
|
|
|
~BVHTree() {
|
2022-11-13 13:03:48 -05:00
|
|
|
delete (root);
|
2022-10-20 11:30:15 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //STEP_2_BVH_H
|