Raytracing camera now losely follows the debug camera

Differences in Viewport size change how the scene is rendered but the position and rotation of the camera is close enough.
main
Brett 2022-11-14 20:44:49 -05:00
parent bd34d6e6e7
commit a28d49b132
24 changed files with 308 additions and 189 deletions

View File

@ -2,104 +2,25 @@
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 1840 1668361409895046083 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4 2 1773 1668466039063176140 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 1716 1668360835926339635 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 7565 1668362431984323343 CMakeFiles/Step_3.dir/src/engine/image/image.cpp.o 1c68ba16e6b4d09b 1 7895 1668377853507218742 CMakeFiles/Step_3.dir/src/engine/image/image.cpp.o 1c68ba16e6b4d09b
2 1499 1668360835706333174 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 1621 1668360835826336698 CMakeFiles/Step_3.dir/src/engine/globals.cpp.o 4ef77d2224f86511 1 1129 1668448492108683969 CMakeFiles/Step_3.dir/src/engine/globals.cpp.o 4ef77d2224f86511
1 1405 1668362425824144748 CMakeFiles/Step_3.dir/src/engine/math/colliders.cpp.o 39e9f435096d066b 1 996 1668448491972679960 CMakeFiles/Step_3.dir/src/engine/math/colliders.cpp.o 39e9f435096d066b
2 1712 1668362494026122707 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072 1 1294 1668465885880220128 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
2268 2354 1668361410407060406 Step_3 f055ce2b85635598 1930 1998 1668466411374360550 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
2 2015 1668362494330131524 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca 1 1719 1668466319832594099 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1499 2524 1668360836734363373 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
521 6249 1668323541180911801 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_widgets.cpp.o 84017388bc7af144
2 1830 1668362494146126186 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1624 1668362426044151124 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
2 1617 1668362426036150892 CMakeFiles/Step_3.dir/src/graphics/gl/shader.cpp.o 2553d57dba16057b
1 2419 1668362494734143245 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2 2242 1668360836450355031 CMakeFiles/Step_3.dir/src/engine/util/parser.cpp.o 1007c86c207ac940
2 1579 1668362569316307713 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1579 1704 1668362569440311315 Step_3 f055ce2b85635598
1 1319 1668362704700239985 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 1568 1668362704948247191 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1667 1668362705048250097 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 1710 1668362705092251375 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2005 1668362705384259861 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2005 2097 1668362705476262536 Step_3 f055ce2b85635598
2 1272 1668363213055029566 CMakeFiles/Step_3.dir/src/engine/globals.cpp.o 4ef77d2224f86511
2 1432 1668363213215034227 CMakeFiles/Step_3.dir/src/engine/util/debug.cpp.o d86a254d2bce8f74
2 1683 1668363213467041565 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
3 1733 1668363213515042961 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
3 1931 1668363213715048785 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
2 1954 1668363213739049483 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
2 2289 1668363214071059151 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2289 2410 1668363214191062645 Step_3 f055ce2b85635598
1 1208 1668364152310674596 CMakeFiles/Step_3.dir/src/engine/math/colliders.cpp.o 39e9f435096d066b
1 1510 1668364152614683513 CMakeFiles/Step_3.dir/src/graphics/gl/gl.cpp.o 330ad35a6abf06c3
2 1564 1668364152666685039 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 1385 1668364289054683772 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 1530 1668364289202688109 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1813 1668364289482696315 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 1987 1668364289658701473 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2206 1668364289878707920 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2206 2315 1668364289982710970 Step_3 f055ce2b85635598
2 1944 1668365722348270196 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
1944 2035 1668365722436272698 Step_3 f055ce2b85635598
1 1300 1668366223258619732 CMakeFiles/Step_3.dir/src/engine/math/colliders.cpp.o 39e9f435096d066b
2 1301 1668366223258619732 CMakeFiles/Step_3.dir/src/graphics/debug_gui.cpp.o 9493619f74acd06a
1 1382 1668366223338622038 CMakeFiles/Step_3.dir/src/engine/globals.cpp.o 4ef77d2224f86511
2 1641 1668366223598629532 CMakeFiles/Step_3.dir/src/engine/util/debug.cpp.o d86a254d2bce8f74
2 1660 1668366223618630108 CMakeFiles/Step_3.dir/src/graphics/gl/gl.cpp.o 330ad35a6abf06c3
2 1744 1668366223702632529 CMakeFiles/Step_3.dir/src/graphics/gl/shader.cpp.o 2553d57dba16057b
2 1909 1668366223866637256 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 2095 1668366224050642556 CMakeFiles/Step_3.dir/src/engine/util/parser.cpp.o 1007c86c207ac940
1301 2392 1668366224350651205 CMakeFiles/Step_3.dir/src/graphics/input.cpp.o 510001b0955ab019
2 2453 1668366224410652933 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
2 2479 1668366224434653626 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 2602 1668366224558657200 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
1300 2954 1668366224910667345 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 7659 1668366229618803041 CMakeFiles/Step_3.dir/src/engine/image/image.cpp.o 1c68ba16e6b4d09b
7659 7729 1668366229686805000 Step_3 f055ce2b85635598
3 1994 1668366292612619600 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1994 1668366292624619946 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 1994 1668366292612619600 CMakeFiles/Step_3.dir/src/graphics/gl/gl.cpp.o 330ad35a6abf06c3
2 2406 1668366293180635989 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
2 2527 1668366293300639450 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2658 1668366293432643257 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2658 2744 1668366293516645683 Step_3 f055ce2b85635598
1 1246 1668366327169616852 CMakeFiles/Step_3.dir/src/graphics/gl/gl.cpp.o 330ad35a6abf06c3
1 1400 1668366327325621356 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 1457 1668366327381622973 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 2170 1668366328093643522 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
1 2192 1668366328117644217 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
1 2211 1668366328133644679 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
2211 2280 1668366328201646641 Step_3 f055ce2b85635598
2 1393 1668366431552631890 CMakeFiles/Step_3.dir/src/graphics/gl/gl.cpp.o 330ad35a6abf06c3
2 1607 1668366431768638134 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
2 1645 1668366431808639289 CMakeFiles/Step_3.dir/src/engine/util/models.cpp.o 164394d360c43072
2 1992 1668366432152649232 CMakeFiles/Step_3.dir/src/engine/world.cpp.o 6470df278966c4
2 2096 1668366432256652239 CMakeFiles/Step_3.dir/src/engine/raytracing.cpp.o 11f0e227e9fda9ca
1 2208 1668366432368655476 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2208 2278 1668366432440657557 Step_3 f055ce2b85635598
1 1579 1668366543867879961 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1580 1655 1668366543943882158 Step_3 f055ce2b85635598
2 1394 1668366561612393436 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1394 1480 1668366561696395867 Step_3 f055ce2b85635598
1 811 1668366640958690420 CMakeFiles/Step_3.dir/src/graphics/input.cpp.o 510001b0955ab019
1 1412 1668366641558707792 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 1963 1668366642110723778 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
1963 2035 1668366642178725749 Step_3 f055ce2b85635598
1 820 1668366663439341462 CMakeFiles/Step_3.dir/src/graphics/input.cpp.o 510001b0955ab019
1 1408 1668366664027358491 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1 2072 1668366664691377723 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2072 2145 1668366664763379807 Step_3 f055ce2b85635598
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
1 1424 1668366743293654856 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d 5 4715 1668323514180134213 CMakeFiles/Step_3.dir/src/graphics/imgui/imgui_draw.cpp.o f89fa3d7b779872a
1 1979 1668366743849670968 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e 2 1366 1668466038659168342 CMakeFiles/Step_3.dir/src/graphics/graphics.cpp.o ce988de97a5cb51d
1979 2054 1668366743921673055 Step_3 f055ce2b85635598 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
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
1 1930 1668466411306359238 CMakeFiles/Step_3.dir/src/engine/main.cpp.o 641dce3f86933e2e
2 2095 1668366224050642556 CMakeFiles/Step_3.dir/src/engine/util/parser.cpp.o 1007c86c207ac940

Binary file not shown.

View File

@ -1,3 +1,3 @@
Start testing: Nov 13 14:12 EST Start testing: Nov 14 20:44 EST
---------------------------------------------------------- ----------------------------------------------------------
End testing: Nov 13 14:12 EST End testing: Nov 14 20:44 EST

View File

@ -4,12 +4,86 @@ Size=400,400
Collapsed=0 Collapsed=0
[Window][Dear ImGui Demo] [Window][Dear ImGui Demo]
Pos=650,20 Pos=649,26
Size=550,680 Size=550,680
Collapsed=1 Collapsed=1
[Window][Debug Menu] [Window][Debug Menu]
Pos=26,34 Pos=13,12
Size=339,226 Size=339,226
Collapsed=0 Collapsed=0
[Window][BVH Data]
Pos=953,64
Size=775,355
Collapsed=0
[Window][Example: Property editor]
Pos=60,60
Size=430,450
Collapsed=0
[Window][Example: Simple layout]
Pos=76,222
Size=500,440
Collapsed=0
[Window][BVH Data 0]
Pos=780,106
Size=556,373
Collapsed=0
[Window][BVH Data 10]
Pos=1079,43
Size=491,217
Collapsed=0
[Window][BVH Data 1]
Pos=60,60
Size=1127,267
Collapsed=0
[Window][BVH Data 4]
Pos=1111,78
Size=498,168
Collapsed=0
[Window][BVH Data 3]
Pos=60,60
Size=358,266
Collapsed=0
[Window][BVH Data 9]
Pos=60,60
Size=293,240
Collapsed=0
[Window][BVH Data 6]
Pos=60,60
Size=372,270
Collapsed=0
[Window][BVH Data 8]
Pos=60,60
Size=327,303
Collapsed=0
[Window][BVH Data 2]
Pos=60,60
Size=324,211
Collapsed=0
[Window][BVH Data ]
Pos=930,180
Size=397,473
Collapsed=0
[Window][Example: Long text display]
Pos=60,60
Size=520,600
Collapsed=0
[Table][0xD0F0C6E3,2]
Column 0 Weight=1.0000
Column 1 Weight=1.0000

View File

@ -11,6 +11,7 @@
#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>
#endif #endif
#include <utility> #include <utility>
@ -21,10 +22,9 @@
namespace Raytracing { namespace Raytracing {
#ifdef COMPILE_GUI #ifdef COMPILE_GUI
struct BVHDebugVAO { extern std::shared_ptr<VAO> aabbVAO;
VAO* vaoPtr; extern int count;
Vec4 position; extern int selected;
};
#endif #endif
struct BVHObject { struct BVHObject {
@ -38,13 +38,64 @@ namespace Raytracing {
}; };
struct BVHNode { struct BVHNode {
private:
static Raytracing::Mat4x4 getTransform(const AABB& _aabb){
Raytracing::Mat4x4 transform {};
auto center = _aabb.getCenter();
transform.translate(center);
auto xRadius = _aabb.getXRadius(center) * 2;
auto yRadius = _aabb.getYRadius(center) * 2;
auto zRadius = _aabb.getZRadius(center) * 2;
transform.scale(float(xRadius), float(yRadius), float(zRadius));
return transform;
}
public: public:
std::vector<BVHObject> objs; std::vector<BVHObject> objs;
AABB aabb; AABB aabb;
BVHNode* left; BVHNode* left;
BVHNode* right; BVHNode* right;
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++;
}
#ifdef COMPILE_GUI
void draw(Shader& worldShader){
worldShader.setVec3("color", {1.0, 1.0, 1.0});
if (selected == index){
worldShader.setVec3("color", {0.0, 0.0, 1.0});
if (ImGui::BeginListBox("")){
for (const auto& item : objs){
auto pos = item.ptr->getPosition();
ImGui::Text("%s", (std::to_string(pos.x()) + " " + std::to_string(pos.y()) + " " + std::to_string(pos.z())).c_str());
}
ImGui::EndListBox();
}
aabbVAO->bind();
for (const auto& obj : objs) {
auto transform = getTransform(obj.aabb);
worldShader.setMatrix("transform", transform);
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))
selected = index;
}
#endif
~BVHNode() { ~BVHNode() {
delete (left); delete (left);
delete (right); delete (right);
@ -53,9 +104,6 @@ namespace Raytracing {
class BVHTree { class BVHTree {
private: private:
#ifdef COMPILE_GUI
std::vector<BVHDebugVAO> aabbVAOs;
#endif
const int MAX_TREE_DEPTH = 50; const int MAX_TREE_DEPTH = 50;
BVHNode* root = nullptr; BVHNode* root = nullptr;
@ -66,21 +114,23 @@ namespace Raytracing {
// 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 (obj.aabb.intersects(aabbs.first)) { if (aabbs.first.intersects(obj.aabb)) {
space.left.push_back(obj); space.left.push_back(obj);
tlog << "left\n";
} }
if (obj.aabb.intersects(aabbs.second)) { 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, unsigned long prevSize) { BVHNode* addObjectsRecur(const std::vector<BVHObject>& objects, long prevSize) {
//ilog << "size: " << objects.size() << "\n"; ilog << "size: " << objects.size() << " " << prevSize << "\n";
// prevSize was required to solve some really weird bugs // prevSize was required to solve some really weird bugs
// which are a TODO: // which are a TODO:
if ((objects.size() <= 2 && !objects.empty()) || prevSize == objects.size()) { if ((objects.size() <= 1 && !objects.empty()) || prevSize == objects.size()) {
AABB local; AABB local;
for (const auto& obj: objects) for (const auto& obj: objects)
local = local.expand(obj.aabb); local = local.expand(obj.aabb);
@ -93,21 +143,21 @@ namespace Raytracing {
// 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) {
//tlog << obj->getAABB();
world = world.expand(obj.aabb); world = world.expand(obj.aabb);
} }
//tlog << "\n";
// 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);
elog << partitionedObjs.left.size() << " :: " << partitionedObjs.right.size() << "\n";
BVHNode* left = nullptr; BVHNode* left = nullptr;
BVHNode* right = nullptr; BVHNode* right = nullptr;
// don't try to explore nodes which don't have anything in them. // don't try to explore nodes which don't have anything in them.
if (!partitionedObjs.left.empty()) if (!partitionedObjs.left.empty())
left = addObjectsRecur(partitionedObjs.left, objects.size()); left = addObjectsRecur(partitionedObjs.left, (long) objects.size());
if (!partitionedObjs.right.empty()) if (!partitionedObjs.right.empty())
right = addObjectsRecur(partitionedObjs.right, objects.size()); right = addObjectsRecur(partitionedObjs.right, (long) objects.size());
return new BVHNode(objects, world, left, right); return new BVHNode(objects, world, left, right);
} }
@ -128,10 +178,31 @@ namespace Raytracing {
// 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
void drawNodesRecur(Shader& worldShader, BVHNode* node){
node->draw(worldShader);
if (node->left != nullptr)
drawNodesRecur(worldShader, node->left);
if (node->right != nullptr)
drawNodesRecur(worldShader, node->right);
}
void guiNodesRecur(BVHNode* node){
node->gui();
if (node->left != nullptr)
guiNodesRecur(node->left);
if (node->right != nullptr)
guiNodesRecur(node->right);
}
#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
auto aabbVertexData = Shapes::cubeVertexBuilder{};
if (aabbVAO == nullptr)
aabbVAO = std::make_shared<VAO>(aabbVertexData.cubeVerticesRaw, aabbVertexData.cubeUVs);
#endif
} }
void addObjects(const std::vector<Object*>& objects) { void addObjects(const std::vector<Object*>& objects) {
@ -147,19 +218,6 @@ namespace Raytracing {
continue; continue;
} }
#ifdef COMPILE_GUI
// create a VAO for debugging the AABB bounds.
BVHDebugVAO vaoStorage {};
auto aabbCenter = obj->getAABB().getCenter();
auto aabbXRadius = obj->getAABB().getXRadius(aabbCenter);
auto aabbYRadius = obj->getAABB().getYRadius(aabbCenter);
auto aabbZRadius = obj->getAABB().getZRadius(aabbCenter);
auto aabbVertexData = Shapes::cubeVertexBuilder::getCubeExtends(float(aabbXRadius), float(aabbYRadius), float(aabbZRadius));
vaoStorage.vaoPtr = new VAO(aabbVertexData.cubeVerticesRaw, aabbVertexData.cubeUVs);
vaoStorage.position = obj->getPosition();
aabbVAOs.push_back(vaoStorage);
#endif
BVHObject bvhObject; BVHObject bvhObject;
// returns a copy of the AABB object and assigns it in to the tree storage object // returns a copy of the AABB object and assigns it in to the tree storage object
bvhObject.aabb = obj->getAABB().translate(obj->getPosition()); bvhObject.aabb = obj->getAABB().translate(obj->getPosition());
@ -167,7 +225,7 @@ namespace Raytracing {
bvhObject.ptr = obj; bvhObject.ptr = obj;
objs.push_back(bvhObject); objs.push_back(bvhObject);
} }
root = addObjectsRecur(objs, -1); root = addObjectsRecur(objs, 1);
} }
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) {
@ -177,22 +235,30 @@ namespace Raytracing {
#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);
worldShader.use(); worldShader.use();
worldShader.setInt("useWhite", 1); worldShader.setInt("useWhite", 1);
for (const auto& debugVAO : aabbVAOs){ worldShader.setVec3("color", {1.0, 1.0, 1.0});
debugVAO.vaoPtr->bind(); {
debugVAO.vaoPtr->draw(worldShader, {debugVAO.position}); ImGui::BeginChild("left pane", ImVec2(150, 0), true);
guiNodesRecur(root);
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); worldShader.setInt("useWhite", 0);
ImGui::End();
} }
#endif #endif
~BVHTree() { ~BVHTree() {
delete (root); delete (root);
#ifdef COMPILE_GUI
for (const auto& debugVAORef : aabbVAOs)
delete(debugVAORef.vaoPtr);
#endif
} }
}; };

View File

@ -104,13 +104,13 @@ namespace Raytracing {
[[nodiscard]] Vec4 getMin() const { return min; } [[nodiscard]] Vec4 getMin() const { return min; }
[[nodiscard]] Vec4 getMax() const { return max; } [[nodiscard]] Vec4 getMax() const { return max; }
inline PRECISION_TYPE getXRadius(const Vec4& center){ [[nodiscard]] inline PRECISION_TYPE getXRadius(const Vec4& center) const {
return max.x() - center.x(); return max.x() - center.x();
} }
inline PRECISION_TYPE getYRadius(const Vec4& center){ [[nodiscard]] inline PRECISION_TYPE getYRadius(const Vec4& center) const {
return max.y() - center.y(); return max.y() - center.y();
} }
inline PRECISION_TYPE getZRadius(const Vec4& center){ [[nodiscard]] inline PRECISION_TYPE getZRadius(const Vec4& center) const {
return max.z() - center.z(); return max.z() - center.z();
} }
@ -118,8 +118,13 @@ namespace Raytracing {
}; };
inline std::ostream& operator<<(std::ostream& out, const AABB& v) { inline std::ostream& operator<<(std::ostream& out, const AABB& v) {
return out << "AABB{min{" << v.getMin().x() << ", " << v.getMin().y() << ", " << v.getMin().z() << "}, max{" << v.getMax().x() << ", " << v.getMax().y() auto max = v.getMax();
<< ", " << v.getMax().z() << "}} "; auto min = v.getMin();
auto center = v.getCenter();
return out << "AABB {\n\t min{" << min.x() << ", " << min.y() << ", " << min.z() << "},\n\t max{" << max.x() << ", " << max.y()
<< ", " << max.z() << "},\n\t diff{" << max.x() - min.x() << ", " << max.y() - min.y() << ", " << max.z() - min.z() << "},\n\t "
<< "center{" << center.x() << ", " << center.y() << ", " << center.z() << "},\n\t "
<< "radi{" << v.getXRadius(center) << ", " << v.getYRadius(center) << ", " << v.getZRadius(center) << "}\n};\n";
} }
} }

View File

@ -617,6 +617,20 @@ namespace Raytracing {
inline float m23(float d) { return data.dim[3][2] = d; } inline float m23(float d) { return data.dim[3][2] = d; }
inline float m33(float d) { return data.dim[3][3] = d; } inline float m33(float d) { return data.dim[3][3] = d; }
inline float m(int i, int j, float d) { return data.dim[i][j] = d; }; inline float m(int i, int j, float d) { return data.dim[i][j] = d; };
// inline float* operator [](int _i) {
// return data.dim[_i];
// }
[[nodiscard]] PRECISION_TYPE determinant() const {
return m00() * (m11() * m22() * m33() + m12() * m23() * m31() + m13() * m21() * m32()
- m31() * m22() * m13() - m32() * m23() * m11() - m33() * m21() * m12())
- m10() * (m01() * m22() * m33() + m02() * m23() * m31() + m03() * m21() * m32()
- m31() * m32() * m03() - m32() * m23() * m01() - m33() * m21() * m02())
+ m20() * (m01() * m12() * m33() + m02() * m13() * m31() + m03() * m11() * m32()
- m31() * m12() * m03() - m32() * m13() * m01() - m33() * m11() * m02())
- m30() * (m01() * m12() * m23() + m02() * m13() * m21() + m03() * m11() * m22()
- m21() * m12() * m03() - m22() * m13() * m01() - m23() * m11() * m02());
}
}; };
// adds the two Mat4x4 left and right // adds the two Mat4x4 left and right

View File

@ -72,7 +72,7 @@ namespace Raytracing {
void setPosition(const Vec4& pos) { this->position = pos; } void setPosition(const Vec4& pos) { this->position = pos; }
void setRotation(PRECISION_TYPE yaw, PRECISION_TYPE pitch, PRECISION_TYPE roll); void setRotation(PRECISION_TYPE yaw, PRECISION_TYPE pitch);
// the follow utility functions are actually taking forever to get right // the follow utility functions are actually taking forever to get right
// I can't tell if my projection calculation is off or the view calc? // I can't tell if my projection calculation is off or the view calc?
@ -95,7 +95,7 @@ namespace Raytracing {
//glm::mat4 projectG = glm::perspective(glm::radians(90.0f), (float)aspectRatio, 0.1f, (float)1000); //glm::mat4 projectG = glm::perspective(glm::radians(90.0f), (float)aspectRatio, 0.1f, (float)1000);
//return Mat4x4{projectG}; //return Mat4x4{projectG};
} }
Mat4x4 view(const Vec4& lookAtPos){ [[nodiscard]] Mat4x4 view(const Vec4& lookAtPos) const {
Mat4x4 view; Mat4x4 view;
auto w = (position - lookAtPos).normalize(); // forward auto w = (position - lookAtPos).normalize(); // forward
@ -125,44 +125,8 @@ namespace Raytracing {
return view; return view;
} }
Mat4x4 view(PRECISION_TYPE yaw, PRECISION_TYPE pitch) { Mat4x4 view(PRECISION_TYPE yaw, PRECISION_TYPE pitch);
Mat4x4 view;
pitch = degreeeToRadian(pitch);
yaw = degreeeToRadian(yaw);
PRECISION_TYPE cosPitch = std::cos(pitch);
PRECISION_TYPE cosYaw = std::cos(yaw);
PRECISION_TYPE sinPitch = std::sin(pitch);
PRECISION_TYPE sinYaw = std::sin(yaw);
auto x = Vec4{cosYaw, 0, -sinYaw}; // forward
auto y = Vec4{sinYaw * sinPitch, cosPitch, cosYaw * sinPitch}; // right
auto z = Vec4{sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw}; // up
view.m00(float(x.x()));
view.m01(float(x.y()));
view.m02(float(x.z()));
view.m03(float(x.w()));
view.m10(float(y.x()));
view.m11(float(y.y()));
view.m12(float(y.z()));
view.m13(float(y.w()));
view.m20(float(z.x()));
view.m21(float(z.y()));
view.m22(float(z.z()));
view.m23(float(z.w()));
// view matrix are inverted, dot product to simulate translate matrix multiplication
view.m03(-float(Vec4::dot(x, position)));
view.m13(-float(Vec4::dot(y, position)));
view.m23(-float(Vec4::dot(z, position)));
view.m33(1);
return view;
}
[[nodiscard]] inline Vec4 getPosition() const {return position;}; [[nodiscard]] inline Vec4 getPosition() const {return position;};
// the camera's position must be set with setPosition(Vec4); // the camera's position must be set with setPosition(Vec4);

View File

@ -237,6 +237,7 @@ class VAO {
void draw() const; void draw() const;
// draw as if it's a box that we need to bulk draw. // draw as if it's a box that we need to bulk draw.
void draw(Raytracing::Shader& shader, const std::vector<Raytracing::Vec4>& positions); void draw(Raytracing::Shader& shader, const std::vector<Raytracing::Vec4>& positions);
void draw(Raytracing::Shader& shader);
~VAO(); ~VAO();
}; };

View File

@ -6,6 +6,7 @@ in vec3 outNormal;
uniform sampler2D tex; uniform sampler2D tex;
uniform int useWhite; uniform int useWhite;
uniform vec3 color;
const vec3 lightDir = vec3(1.0, 1.0, 1.0); const vec3 lightDir = vec3(1.0, 1.0, 1.0);
@ -15,5 +16,5 @@ void main() {
if (useWhite == 0) if (useWhite == 0)
FragColor = vec4(vec3(1.0, 0.0f, 0.0f) * dot(lightDir, outNormal), 1.0f); FragColor = vec4(vec3(1.0, 0.0f, 0.0f) * dot(lightDir, outNormal), 1.0f);
else else
FragColor = vec4(1.0); FragColor = vec4(color, 1.0f);
} }

View File

@ -4,6 +4,10 @@
*/ */
// Yes, globals are bad. // Yes, globals are bad.
#include "engine/util/debug.h" #include "engine/util/debug.h"
#include <config.h>
#ifdef COMPILE_GUI
#include <graphics/gl/gl.h>
#endif
bool* haltExecution; bool* haltExecution;
bool* pauseRaytracing; bool* pauseRaytracing;
@ -11,4 +15,9 @@ bool* haltRaytracing;
namespace Raytracing { namespace Raytracing {
std::unordered_map<std::string, std::shared_ptr<profiler>> profiles; std::unordered_map<std::string, std::shared_ptr<profiler>> profiles;
#ifdef COMPILE_GUI
std::shared_ptr<VAO> aabbVAO = nullptr;
int count = 0;
int selected = 0;
#endif
} }

View File

@ -126,11 +126,17 @@ int main(int argc, char** args) {
//world.add(new Raytracing::SphereObject({1,0,-1,0}, 0.5, world.getMaterial("redMetal"))); //world.add(new Raytracing::SphereObject({1,0,-1,0}, 0.5, world.getMaterial("redMetal")));
world.add(new Raytracing::SphereObject({0, -100.5, -1, 0}, 100, world.getMaterial("greenDiffuse"))); world.add(new Raytracing::SphereObject({0, -100.5, -1, 0}, 100, world.getMaterial("greenDiffuse")));
//world.add(new Raytracing::TriangleObject({0,0.1,-0.5f,0}, {{-0.5, -0.5, 0.0}, {0.5, -0.5, 0.0}, {0.0, 0.5, 0}}, world.getMaterial("greenDiffuse"))); //world.add(new Raytracing::TriangleObject({0,0.1,-0.5f,0}, {{-0.5, -0.5, 0.0}, {0.5, -0.5, 0.0}, {0.0, 0.5, 0}}, world.getMaterial("greenDiffuse")));
world.add(new Raytracing::ModelObject({0, 1, 0}, spider, world.getMaterial("redDiffuse"))); /*world.add(new Raytracing::ModelObject({0, 1, 0}, spider, world.getMaterial("redDiffuse")));
world.add(new Raytracing::ModelObject({-5, 0.5, 0}, plane, world.getMaterial("greenMetal"))); world.add(new Raytracing::ModelObject({-5, 0.5, 0}, plane, world.getMaterial("greenMetal")));
world.add(new Raytracing::ModelObject({5, 1, 0}, house, world.getMaterial("redDiffuse"))); world.add(new Raytracing::ModelObject({5, 1, 0}, house, world.getMaterial("redDiffuse")));
world.add(new Raytracing::ModelObject({0, 0, -5}, house, world.getMaterial("blueDiffuse"))); world.add(new Raytracing::ModelObject({0, 0, -5}, house, world.getMaterial("blueDiffuse")));
world.add(new Raytracing::ModelObject({0, 0, 5}, house, world.getMaterial("blueDiffuse"))); world.add(new Raytracing::ModelObject({0, 0, 5}, house, world.getMaterial("blueDiffuse")));*/
world.add(new Raytracing::ModelObject({0, 1, 0}, spider, world.getMaterial("redDiffuse")));
world.add(new Raytracing::ModelObject({-15, 0.5, 0}, plane, world.getMaterial("greenMetal")));
world.add(new Raytracing::ModelObject({15, 1, 0}, house, world.getMaterial("redDiffuse")));
world.add(new Raytracing::ModelObject({0, 0, -15}, house, world.getMaterial("blueDiffuse")));
world.add(new Raytracing::ModelObject({0, 0, 15}, house, world.getMaterial("blueDiffuse")));
if (parser.hasOption("--gui") || parser.hasOption("-g")) { if (parser.hasOption("--gui") || parser.hasOption("-g")) {
#ifdef COMPILE_GUI #ifdef COMPILE_GUI

View File

@ -33,9 +33,53 @@ namespace Raytracing {
imageOrigin = position - horizontalAxis / 2 - verticalAxis / 2 - w; imageOrigin = position - horizontalAxis / 2 - verticalAxis / 2 - w;
} }
void Camera::setRotation(const PRECISION_TYPE yaw, const PRECISION_TYPE pitch, const PRECISION_TYPE roll) { void Camera::setRotation(const PRECISION_TYPE yaw, const PRECISION_TYPE pitch) {
// TODO: // TODO:
} }
Mat4x4 Camera::view(PRECISION_TYPE yaw, PRECISION_TYPE pitch) {
Mat4x4 view;
pitch = degreeeToRadian(pitch);
yaw = degreeeToRadian(yaw);
PRECISION_TYPE cosPitch = std::cos(pitch);
PRECISION_TYPE cosYaw = std::cos(yaw);
PRECISION_TYPE sinPitch = std::sin(pitch);
PRECISION_TYPE sinYaw = std::sin(yaw);
auto x = Vec4{cosYaw, 0, -sinYaw}; // forward
auto y = Vec4{sinYaw * sinPitch, cosPitch, cosYaw * sinPitch}; // right
auto z = Vec4{sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw}; // up
viewportHeight = 1 * tanFovHalf;
viewportWidth = aspectRatio * viewportHeight;
horizontalAxis = viewportWidth * x;
verticalAxis = viewportHeight * y;
imageOrigin = position - horizontalAxis / 2 - verticalAxis / 2 - z;
view.m00(float(x.x()));
view.m01(float(x.y()));
view.m02(float(x.z()));
view.m03(float(x.w()));
view.m10(float(y.x()));
view.m11(float(y.y()));
view.m12(float(y.z()));
view.m13(float(y.w()));
view.m20(float(z.x()));
view.m21(float(z.y()));
view.m22(float(z.z()));
view.m23(float(z.w()));
// view matrix are inverted, dot product to simulate translate matrix multiplication
view.m03(-float(Vec4::dot(x, position)));
view.m13(-float(Vec4::dot(y, position)));
view.m23(-float(Vec4::dot(z, position)));
view.m33(1);
return view;
}
struct RayData { struct RayData {
Ray ray; Ray ray;

View File

@ -209,6 +209,9 @@ void VAO::draw(Raytracing::Shader& shader, const std::vector<Raytracing::Vec4>&
glDrawArrays(GL_TRIANGLES, 0, drawCount); glDrawArrays(GL_TRIANGLES, 0, drawCount);
} }
} }
void VAO::draw(Raytracing::Shader& shader) {
glDrawArrays(GL_TRIANGLES, 0, drawCount);
}
void VAO::draw() const { void VAO::draw() const {
if (drawCount < 0) if (drawCount < 0)
return; return;

View File

@ -516,6 +516,17 @@ namespace Raytracing {
yaw += (float) Input::getMouseDelta().x * (1000.0f / ImGui::GetIO().Framerate / 1000.0f) * 3; yaw += (float) Input::getMouseDelta().x * (1000.0f / ImGui::GetIO().Framerate / 1000.0f) * 3;
pitch += (float) Input::getMouseDelta().y * (1000.0f / ImGui::GetIO().Framerate / 1000.0f) * 1.5f; pitch += (float) Input::getMouseDelta().y * (1000.0f / ImGui::GetIO().Framerate / 1000.0f) * 1.5f;
const PRECISION_TYPE turnSpeed = 50;
if (Input::isKeyDown(GLFW_KEY_LEFT))
yaw += float(turnSpeed * m_window.getFrameTimeSeconds());
if (Input::isKeyDown(GLFW_KEY_RIGHT))
yaw -= float(turnSpeed * m_window.getFrameTimeSeconds());
if (Input::isKeyDown(GLFW_KEY_UP))
pitch += float(turnSpeed * m_window.getFrameTimeSeconds());
if (Input::isKeyDown(GLFW_KEY_DOWN))
pitch -= float(turnSpeed * m_window.getFrameTimeSeconds());
PRECISION_TYPE moveAtX = 0, moveAtY = 0, moveAtZ = 0; PRECISION_TYPE moveAtX = 0, moveAtY = 0, moveAtZ = 0;
PRECISION_TYPE speed = 40.0f; PRECISION_TYPE speed = 40.0f;
@ -554,9 +565,9 @@ namespace Raytracing {
m_spiderVAO->bind(); m_spiderVAO->bind();
m_spiderVAO->draw(m_worldShader, {{0, 1, 0}}); m_spiderVAO->draw(m_worldShader, {{0, 1, 0}});
m_houseVAO->bind(); m_houseVAO->bind();
m_houseVAO->draw(m_worldShader, {{5, 1, 0}, {0, 0, -5}, {0, 0, 5}}); m_houseVAO->draw(m_worldShader, {{15, 1, 0}, {0, 0, -15}, {0, 0, 15}});
m_planeVAO->bind(); m_planeVAO->bind();
m_planeVAO->draw(m_worldShader, {{-5, 0.5, 0}}); m_planeVAO->draw(m_worldShader, {{-15, 0.5, 0}});
} else { } else {
m_imageShader.use(); m_imageShader.use();
m_mainImage.updateImage(); m_mainImage.updateImage();