Fixed AABB Sizing issue.

Fixed is a strong word. TODO: I have solved the problem by using isEmpty to tell when it's a fresh AABB
and just returing the passed AABB as the new expanded AABB. This feels lazy and I expect it'll cause issues in the future.
Plus it isn't truely correct and the context which uses it could just use the first obj AABB since there is only 1 object
main
Brett 2022-11-15 00:53:21 -05:00
parent a28d49b132
commit 17c7942405
15 changed files with 211 additions and 114 deletions

View File

@ -2,25 +2,82 @@
374 3235 1668323538168825062 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_tables.cpp.o f6dab204e07e8dcf 374 3235 1668323538168825062 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_tables.cpp.o f6dab204e07e8dcf
2 2022 1668323499583713805 CMakeFiles/Step_3.dir/src/graphics/gl/gl.c.o 30ab06816e8637c1 2 2022 1668323499583713805 CMakeFiles/Step_3.dir/src/graphics/gl/gl.c.o 30ab06816e8637c1
4 3390 1668323512856096078 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_demo.cpp.o 7798aba97da63e31 4 3390 1668323512856096078 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_demo.cpp.o 7798aba97da63e31
2 1773 1668466039063176140 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4 13 1984 1668489872732758544 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
2 1641 1668366223598629532 CMakeFiles/Step_3.dir/src/engine/util/debug.cpp.o d86a254d2bce8f74 2 1641 1668366223598629532 CMakeFiles/Step_3.dir/src/engine/util/debug.cpp.o d86a254d2bce8f74
1 7895 1668377853507218742 CMakeFiles/Step_3.dir/src/engine/image/image.cpp.o 1c68ba16e6b4d09b 1 6863 1668489146695541389 CMakeFiles/Step_3.dir/src/engine/image/image.cpp.o 1c68ba16e6b4d09b
2 1301 1668366223258619732 CMakeFiles/Step_3.dir/src/graphics/debug_gui.cpp.o 9493619f74acd06a 2 1301 1668366223258619732 CMakeFiles/Step_3.dir/src/graphics/debug_gui.cpp.o 9493619f74acd06a
4 605 1668323535536749264 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_impl_x11.cpp.o d1e9a2fcb31e2ec8 4 605 1668323535536749264 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_impl_x11.cpp.o d1e9a2fcb31e2ec8
0 26 1668358519550031316 build.ninja ccb5de0c063412ab 0 26 1668358519550031316 build.ninja ccb5de0c063412ab
1 1129 1668448492108683969 CMakeFiles/Step_3.dir/src/engine/globals.cpp.o 4ef77d2224f86511 1 1034 1668489140867427595 CMakeFiles/Step_3.dir/src/engine/globals.cpp.o 4ef77d2224f86511
1 996 1668448491972679960 CMakeFiles/Step_3.dir/src/engine/math/colliders.cpp.o 39e9f435096d066b 1 1244 1668489141075431646 CMakeFiles/Step_3.dir/src/engine/math/colliders.cpp.o 39e9f435096d066b
1 1294 1668465885880220128 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072 2 1373 1668489872120742440 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
3 6633 1668323516100189511 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui.cpp.o 55e7538fd27b47be 3 6633 1668323516100189511 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui.cpp.o 55e7538fd27b47be
1930 1998 1668466411374360550 Step_3 f055ce2b85635598 2275 2350 1668489873096768119 Step_3 f055ce2b85635598
4 521 1668323535452746847 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_impl_glfw.cpp.o 6aef9db88eb3d76d 4 521 1668323535452746847 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_impl_glfw.cpp.o 6aef9db88eb3d76d
1 1719 1668466319832594099 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca 2 1850 1668489872596754964 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 827 1668366742693637470 CMakeFiles/Step_3.dir/src/graphics/input.cpp.o 510001b0955ab019 1 827 1668366742693637470 CMakeFiles/Step_3.dir/src/graphics/input.cpp.o 510001b0955ab019
5 4715 1668323514180134213 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_draw.cpp.o f89fa3d7b779872a 5 4715 1668323514180134213 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_draw.cpp.o f89fa3d7b779872a
2 1366 1668466038659168342 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
521 6249 1668323541180911801 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_widgets.cpp.o 84017388bc7af144 521 6249 1668323541180911801 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_widgets.cpp.o 84017388bc7af144
2 1340 1668448492316690097 CMakeFiles/Step_3.dir/src/graphics/gl/gl.cpp.o 330ad35a6abf06c3 13 1419 1668489872168743705 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1717 1668489141551440921 CMakeFiles/Step_3.dir/src/graphics/gl/gl.cpp.o 330ad35a6abf06c3
4 374 1668323535308742700 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_impl_opengl3.cpp.o 1b5f0fbe3cf8ce4e 4 374 1668323535308742700 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_impl_opengl3.cpp.o 1b5f0fbe3cf8ce4e
2 1790 1668377847399042915 CMakeFiles/Step_3.dir/src/graphics/gl/shader.cpp.o 2553d57dba16057b 2 1655 1668489141487439674 CMakeFiles/Step_3.dir/src/graphics/gl/shader.cpp.o 2553d57dba16057b
1 1930 1668466411306359238 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e 1 2275 1668489873024766225 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2 2095 1668366224050642556 CMakeFiles/Step_3.dir/src/engine/util/parser.cpp.o 1007c86c207ac940 2 2095 1668366224050642556 CMakeFiles/Step_3.dir/src/engine/util/parser.cpp.o 1007c86c207ac940
2 1303 1668489957751009761 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 1330 1668489957775010400 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1652 1668489958099019032 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
2 1717 1668489958163020738 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2233 1668489958679034483 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2233 2297 1668489958743036189 Step_3 f055ce2b85635598
1 1328 1668490081042319128 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
1 1374 1668490081086320317 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 1640 1668490081354327561 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 1726 1668490081438329831 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2239 1668490081950343672 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2239 2302 1668490082014345402 Step_3 f055ce2b85635598
2 1256 1668491007091920404 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 1303 1668491007139921206 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1588 1668491007423925947 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
2 1684 1668491007519927550 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2245 1668491008079936900 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2245 2308 1668491008143937971 Step_3 f055ce2b85635598
1 1258 1668491031264329637 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
1 1307 1668491031312330460 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 1588 1668491031592335269 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 1666 1668491031672336643 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2226 1668491032232346264 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2226 2286 1668491032292347296 Step_3 f055ce2b85635598
1 1248 1668491266864840576 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
1 1301 1668491266920841745 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 1581 1668491267200847580 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 1660 1668491267276849164 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2223 1668491267840860925 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2223 2284 1668491267900862176 Step_3 f055ce2b85635598
2 1275 1668491276045032437 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 1285 1668491276057032688 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1604 1668491276373039313 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 1655 1668491276425040402 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2261 1668491277033053146 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2261 2322 1668491277093054405 Step_3 f055ce2b85635598
2 1357 1668491283805195413 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 1532 1668491283981199118 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 1651 1668491284097201557 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 1723 1668491284169203075 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2530 1668491284977220089 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2531 2593 1668491285041221437 Step_3 f055ce2b85635598
1 1314 1668491300465547709 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
1 1439 1668491300589550343 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 1634 1668491300785554506 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 1684 1668491300837555611 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2282 1668491301433568277 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2282 2342 1668491301493569552 Step_3 f055ce2b85635598
1 945 1668491473161369910 CMakeFiles/Step_3.dir/src/engine/globals.cpp.o 4ef77d2224f86511
2 1141 1668491473361374498 CMakeFiles/Step_3.dir/src/graphics/gl/gl.cpp.o 330ad35a6abf06c3
1 1161 1668491473377374863 CMakeFiles/Step_3.dir/src/engine/math/colliders.cpp.o 39e9f435096d066b
2 1346 1668491473565379176 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1656 1668491473873386239 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
2 1688 1668491473905386973 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
1 1730 1668491473949387979 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2359 1668491474577402387 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2359 2426 1668491474641403855 Step_3 f055ce2b85635598

Binary file not shown.

View File

@ -1,3 +1,3 @@
Start testing: Nov 14 20:44 EST Start testing: Nov 15 00:51 EST
---------------------------------------------------------- ----------------------------------------------------------
End testing: Nov 14 20:44 EST End testing: Nov 15 00:51 EST

View File

@ -9,7 +9,7 @@ Size=550,680
Collapsed=1 Collapsed=1
[Window][Debug Menu] [Window][Debug Menu]
Pos=13,12 Pos=-6,23
Size=339,226 Size=339,226
Collapsed=0 Collapsed=0
@ -74,8 +74,8 @@ Size=324,211
Collapsed=0 Collapsed=0
[Window][BVH Data ] [Window][BVH Data ]
Pos=930,180 Pos=913,184
Size=397,473 Size=522,476
Collapsed=0 Collapsed=0
[Window][Example: Long text display] [Window][Example: Long text display]
@ -83,7 +83,17 @@ Pos=60,60
Size=520,600 Size=520,600
Collapsed=0 Collapsed=0
[Window][Example: Log]
Pos=60,60
Size=500,400
Collapsed=0
[Table][0xD0F0C6E3,2] [Table][0xD0F0C6E3,2]
Column 0 Weight=1.0000 Column 0 Weight=1.0000
Column 1 Weight=1.0000 Column 1 Weight=1.0000
[Table][0xC9935533,3]
Column 0 Weight=1.0000
Column 1 Weight=1.0000
Column 2 Weight=1.0000

View File

@ -9,9 +9,12 @@
#include "engine/util/std.h" #include "engine/util/std.h"
#include "engine/types.h" #include "engine/types.h"
#include <config.h> #include <config.h>
#ifdef COMPILE_GUI #ifdef COMPILE_GUI
#include <graphics/gl/gl.h> #include <graphics/gl/gl.h>
#include <graphics/imgui/imgui.h> #include <graphics/imgui/imgui.h>
#endif #endif
#include <utility> #include <utility>
@ -22,9 +25,9 @@
namespace Raytracing { namespace Raytracing {
#ifdef COMPILE_GUI #ifdef COMPILE_GUI
extern std::shared_ptr<VAO> aabbVAO; extern std::shared_ptr<VAO> aabbVAO;
extern int count; extern int count;
extern int selected; extern int selected;
#endif #endif
struct BVHObject { struct BVHObject {
@ -39,8 +42,8 @@ namespace Raytracing {
struct BVHNode { struct BVHNode {
private: private:
static Raytracing::Mat4x4 getTransform(const AABB& _aabb){ static Raytracing::Mat4x4 getTransform(const AABB& _aabb) {
Raytracing::Mat4x4 transform {}; Raytracing::Mat4x4 transform{};
auto center = _aabb.getCenter(); auto center = _aabb.getCenter();
transform.translate(center); transform.translate(center);
auto xRadius = _aabb.getXRadius(center) * 2; auto xRadius = _aabb.getXRadius(center) * 2;
@ -56,45 +59,62 @@ namespace Raytracing {
BVHNode* right; BVHNode* right;
int index; int index;
BVHNode(std::vector<BVHObject> objs, AABB aabb, BVHNode* left, BVHNode* right): objs(std::move(objs)), aabb(std::move(aabb)), BVHNode(std::vector<BVHObject> objs, AABB aabb, BVHNode* left, BVHNode* right): objs(std::move(objs)), aabb(std::move(aabb)),
left(left), right(right) { left(left), right(right) {
index = count++; index = count++;
} }
#ifdef COMPILE_GUI #ifdef COMPILE_GUI
void draw(Shader& worldShader){ void draw(Shader& worldShader) {
worldShader.setVec3("color", {1.0, 1.0, 1.0}); worldShader.setVec3("color", {1.0, 1.0, 1.0});
if (selected == index){ if (selected == index) {
worldShader.setVec3("color", {0.0, 0.0, 1.0}); worldShader.setVec3("color", {0.0, 0.0, 1.0});
if (ImGui::BeginListBox("")){ if (ImGui::BeginListBox("", ImVec2(250, 350))) {
for (const auto& item : objs){ std::stringstream strs;
auto pos = item.ptr->getPosition(); strs << aabb;
ImGui::Text("%s", (std::to_string(pos.x()) + " " + std::to_string(pos.y()) + " " + std::to_string(pos.z())).c_str()); ImGui::Text("%s", strs.str().c_str());
} for (const auto& item: objs) {
ImGui::EndListBox(); auto pos = item.ptr->getPosition();
std::stringstream stm;
stm << item.aabb;
ImGui::Text("%s,\n\t%s", (std::to_string(pos.x()) + " " + std::to_string(pos.y()) + " " + std::to_string(pos.z())).c_str(),
stm.str().c_str());
} }
ImGui::EndListBox();
aabbVAO->bind(); }
for (const auto& obj : objs) {
auto transform = getTransform(obj.aabb); aabbVAO->bind();
worldShader.setMatrix("transform", transform); for (const auto& obj: objs) {
aabbVAO->draw(worldShader); auto transform = getTransform(obj.aabb);
}
auto transform = getTransform(aabb);
worldShader.setMatrix("transform", transform);
aabbVAO->draw(worldShader);
auto splitAABBs = aabb.splitByLongestAxis();
transform = getTransform(splitAABBs.second);
worldShader.setMatrix("transform", transform);
aabbVAO->draw(worldShader);
transform = getTransform(splitAABBs.first);
worldShader.setMatrix("transform", transform); worldShader.setMatrix("transform", transform);
aabbVAO->draw(worldShader); aabbVAO->draw(worldShader);
} }
auto transform = getTransform(aabb);
worldShader.setMatrix("transform", transform);
aabbVAO->draw(worldShader);
auto splitAABBs = aabb.splitByLongestAxis();
transform = getTransform(splitAABBs.second);
worldShader.setMatrix("transform", transform);
aabbVAO->draw(worldShader);
transform = getTransform(splitAABBs.first);
worldShader.setMatrix("transform", transform);
aabbVAO->draw(worldShader);
} }
void gui() const { }
if (ImGui::Selectable(("S: " + std::to_string(objs.size()) + " I: " + std::to_string(index)).c_str(), selected == index)) void gui() const {
selected = index; int c1 = -1;
} int c2 = -1;
if (left != nullptr)
c1 = left->index;
if (right != nullptr)
c2 = right->index;
std::string t;
if (c1 == -1 && c2 == -1)
t = " LEAF";
else
t = " L: " + std::to_string(c1) + " R: " + std::to_string(c2);
if (ImGui::Selectable(("S: " + std::to_string(objs.size()) + " I: " + std::to_string(index) + t).c_str(), selected == index))
selected = index;
}
#endif #endif
~BVHNode() { ~BVHNode() {
delete (left); delete (left);
@ -111,40 +131,32 @@ namespace Raytracing {
static BVHPartitionedSpace partition(const std::pair<AABB, AABB>& aabbs, const std::vector<BVHObject>& objs) { static BVHPartitionedSpace partition(const std::pair<AABB, AABB>& aabbs, const std::vector<BVHObject>& objs) {
BVHPartitionedSpace space; BVHPartitionedSpace space;
for (const auto& obj: objs) { for (const auto& obj: objs) {
// if this object doesn't have an AABB, we cannot use a BVH on it // if this object doesn't have an AABB, we cannot use a BVH on it. If this ever fails we have a problem with the implementation.
// If this ever fails we have a problem with the implementation.
RTAssert(!obj.aabb.isEmpty()); RTAssert(!obj.aabb.isEmpty());
if (aabbs.first.intersects(obj.aabb)) { if (aabbs.first.intersects(obj.aabb))
space.left.push_back(obj); space.left.push_back(obj);
tlog << "left\n"; else if (aabbs.second.intersects(obj.aabb))
}
if (aabbs.second.intersects(obj.aabb)) {
space.right.push_back(obj); space.right.push_back(obj);
tlog << "right\n";
}
} }
return space; return space;
} }
BVHNode* addObjectsRecur(const std::vector<BVHObject>& objects, long prevSize) { BVHNode* addObjectsRecur(const std::vector<BVHObject>& objects, long prevSize) {
ilog << "size: " << objects.size() << " " << prevSize << "\n"; ilog << "size: " << objects.size() << " " << prevSize << "\n";
// prevSize was required to solve some really weird bugs
// which are a TODO:
if ((objects.size() <= 1 && !objects.empty()) || prevSize == objects.size()) {
AABB local;
for (const auto& obj: objects)
local = local.expand(obj.aabb);
return new BVHNode(objects, local, nullptr, nullptr);
} else if (objects.empty()) // should never reach here!!
return nullptr;
// create a volume for the entire world. // create a volume for the entire world.
// yes, we could use the recursion provided AABB, // yes, we could use the recursion provided AABB,
// but that wouldn't be minimum, only half. // but that wouldn't be minimum, only half.
// this ensures that we have a minimum AABB. // this ensures that we have a minimum AABB.
AABB world; AABB world;
for (const auto& obj: objects) { for (const auto& obj: objects)
world = world.expand(obj.aabb); world = world.expand(obj.aabb);
} // prevSize was required to solve some really weird bugs
// which are a TODO:
if ((objects.size() <= 1 && !objects.empty()) || prevSize == objects.size()) {
return new BVHNode(objects, world, nullptr, nullptr);
} else if (objects.empty()) // should never reach here!!
return nullptr;
// then split and partition the world // then split and partition the world
auto splitAABBs = world.splitByLongestAxis(); auto splitAABBs = world.splitByLongestAxis();
auto partitionedObjs = partition(splitAABBs, objects); auto partitionedObjs = partition(splitAABBs, objects);
@ -172,36 +184,36 @@ namespace Raytracing {
// divide and conquer and so on // divide and conquer and so on
if (node->right != nullptr) if (node->right != nullptr)
if (node->right->aabb.intersects(ray, min, max)) if (node->right->aabb.intersects(ray, min, max))
return traverseFindRayIntersection(node->left, ray, min, max); return traverseFindRayIntersection(node->right, ray, min, max);
// return the objects of the lowest BVH node we can find // return the objects of the lowest BVH node we can find
// if this is implemented properly this should only contain one, maybe two objects // if this is implemented properly this should only contain one, maybe two objects
// which is much faster! (especially when dealing with triangles) // which is much faster! (especially when dealing with triangles)
return node->objs; return node->objs;
} }
#ifdef COMPILE_GUI #ifdef COMPILE_GUI
void drawNodesRecur(Shader& worldShader, BVHNode* node){ void drawNodesRecur(Shader& worldShader, BVHNode* node) {
node->draw(worldShader); node->draw(worldShader);
if (node->left != nullptr) if (node->left != nullptr)
drawNodesRecur(worldShader, node->left); drawNodesRecur(worldShader, node->left);
if (node->right != nullptr) if (node->right != nullptr)
drawNodesRecur(worldShader, node->right); drawNodesRecur(worldShader, node->right);
} }
void guiNodesRecur(BVHNode* node){ void guiNodesRecur(BVHNode* node) {
node->gui(); node->gui();
if (node->left != nullptr) if (node->left != nullptr)
guiNodesRecur(node->left); guiNodesRecur(node->left);
if (node->right != nullptr) if (node->right != nullptr)
guiNodesRecur(node->right); guiNodesRecur(node->right);
} }
#endif #endif
public: public:
std::vector<Object*> noAABBObjects; std::vector<Object*> noAABBObjects;
explicit BVHTree(const std::vector<Object*>& objectsInWorld) { explicit BVHTree(const std::vector<Object*>& objectsInWorld) {
addObjects(objectsInWorld); addObjects(objectsInWorld);
#ifdef COMPILE_GUI #ifdef COMPILE_GUI
auto aabbVertexData = Shapes::cubeVertexBuilder{}; auto aabbVertexData = Shapes::cubeVertexBuilder{};
if (aabbVAO == nullptr) if (aabbVAO == nullptr)
aabbVAO = std::make_shared<VAO>(aabbVertexData.cubeVerticesRaw, aabbVertexData.cubeUVs); aabbVAO = std::make_shared<VAO>(aabbVertexData.cubeVerticesRaw, aabbVertexData.cubeUVs);
#endif #endif
} }
@ -211,8 +223,7 @@ namespace Raytracing {
// move all the object's aabb's into world position // move all the object's aabb's into world position
std::vector<BVHObject> objs; std::vector<BVHObject> objs;
for (auto* obj: objects) { for (auto* obj: objects) {
// we don't want to store all the AABBs which don't exist // we don't want to store all the AABBs which don't exist: ie spheres
// ie spheres
if (obj->getAABB().isEmpty()) { if (obj->getAABB().isEmpty()) {
noAABBObjects.push_back(obj); noAABBObjects.push_back(obj);
continue; continue;
@ -231,30 +242,33 @@ namespace Raytracing {
std::vector<BVHObject> rayIntersect(const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max) { std::vector<BVHObject> rayIntersect(const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max) {
return traverseFindRayIntersection(root, ray, min, max); return traverseFindRayIntersection(root, ray, min, max);
} }
#ifdef COMPILE_GUI #ifdef COMPILE_GUI
// renders all the debug VAOs on screen. // renders all the debug VAOs on screen.
void render (Shader& worldShader){ void render(Shader& worldShader) {
ImGui::Begin(("BVH Data "), nullptr, ImGuiWindowFlags_NoCollapse); ImGui::Begin(("BVH Data "), nullptr, ImGuiWindowFlags_NoCollapse);
worldShader.use(); worldShader.use();
worldShader.setInt("useWhite", 1); worldShader.setInt("useWhite", 1);
worldShader.setVec3("color", {1.0, 1.0, 1.0}); worldShader.setVec3("color", {1.0, 1.0, 1.0});
{ {
ImGui::BeginChild("left pane", ImVec2(150, 0), true); ImGui::BeginChild("left pane", ImVec2(180, 0), true);
guiNodesRecur(root); guiNodesRecur(root);
ImGui::EndChild(); ImGui::EndChild();
}
ImGui::SameLine();
{
ImGui::BeginGroup();
ImGui::BeginChild("item view", ImVec2(0, -ImGui::GetFrameHeightWithSpacing()), true, ImGuiWindowFlags_AlwaysAutoResize); // Leave room for 1 line below us
drawNodesRecur(worldShader, root);
ImGui::EndChild();
ImGui::EndGroup();
}
worldShader.setInt("useWhite", 0);
ImGui::End();
} }
ImGui::SameLine();
{
ImGui::BeginGroup();
ImGui::BeginChild("item view",
ImVec2(0, -ImGui::GetFrameHeightWithSpacing()),
true,
ImGuiWindowFlags_AlwaysAutoResize); // Leave room for 1 line below us
drawNodesRecur(worldShader, root);
ImGui::EndChild();
ImGui::EndGroup();
}
worldShader.setInt("useWhite", 0);
ImGui::End();
}
#endif #endif
~BVHTree() { ~BVHTree() {

View File

@ -41,6 +41,11 @@ namespace Raytracing {
// returns an expanded version of this AABB is the other AABB is larger then this AABB // returns an expanded version of this AABB is the other AABB is larger then this AABB
[[nodiscard]] AABB expand(const AABB& other) const { [[nodiscard]] AABB expand(const AABB& other) const {
// terrible hack
// a 0 init AABB was having issues when being "expanded" to a place which is larger
// this should prevent that by side stepping the issue. Which is a TODO:
if (isEmpty())
return other;
PRECISION_TYPE minX = std::min(min.x(), other.min.x()); PRECISION_TYPE minX = std::min(min.x(), other.min.x());
PRECISION_TYPE minY = std::min(min.y(), other.min.y()); PRECISION_TYPE minY = std::min(min.y(), other.min.y());
PRECISION_TYPE minZ = std::min(min.z(), other.min.z()); PRECISION_TYPE minZ = std::min(min.z(), other.min.z());

View File

@ -43,6 +43,7 @@
// mul // mul
// 70.9977ms normal // 70.9977ms normal
// 286.656ms avx // 286.656ms avx
#ifdef COMPILER_DEBUG_ENABLED #ifdef COMPILER_DEBUG_ENABLED
#define USE_SIMD_CPU #define USE_SIMD_CPU
#endif #endif

View File

@ -86,20 +86,30 @@ namespace Raytracing {
tmin = std::min(tmin, std::min(tx1, tx2)); tmin = std::min(tmin, std::min(tx1, tx2));
tmax = std::max(tmax, std::max(tx1, tx2)); tmax = std::max(tmax, std::max(tx1, tx2));
// gives us an early exit if we know it's outside the bounds.
if (tmax <= tmin)
return false;
PRECISION_TYPE ty1 = (min.y() - ray.getStartingPoint().y())*ray.getInverseDirection().y(); PRECISION_TYPE ty1 = (min.y() - ray.getStartingPoint().y())*ray.getInverseDirection().y();
PRECISION_TYPE ty2 = (max.y() - ray.getStartingPoint().y())*ray.getInverseDirection().y(); PRECISION_TYPE ty2 = (max.y() - ray.getStartingPoint().y())*ray.getInverseDirection().y();
tmin = std::max(tmin, std::min(ty1, ty2)); tmin = std::max(tmin, std::min(ty1, ty2));
tmax = std::min(tmax, std::max(ty1, ty2)); tmax = std::min(tmax, std::max(ty1, ty2));
if (tmax <= tmin)
return false;
PRECISION_TYPE tz1 = (min.z() - ray.getStartingPoint().z())*ray.getInverseDirection().z(); PRECISION_TYPE tz1 = (min.z() - ray.getStartingPoint().z())*ray.getInverseDirection().z();
PRECISION_TYPE tz2 = (max.z() - ray.getStartingPoint().z())*ray.getInverseDirection().z(); PRECISION_TYPE tz2 = (max.z() - ray.getStartingPoint().z())*ray.getInverseDirection().z();
tmin = std::max(tmin, std::min(tz1, tz2)); tmin = std::max(tmin, std::min(tz1, tz2));
tmax = std::min(tmax, std::max(tz1, tz2)); tmax = std::min(tmax, std::max(tz1, tz2));
if (tmax <= tmin)
return false;
return tmax > std::max(tmin, 0.0); return true;
} }
bool AABB::intersects(const Ray& ray, PRECISION_TYPE tmin, PRECISION_TYPE tmax) { bool AABB::intersects(const Ray& ray, PRECISION_TYPE tmin, PRECISION_TYPE tmax) {