From 42de0d77d9c9c9998b21b10e4033ac5fe77ce7f8 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 20 Oct 2024 15:31:06 +0300 Subject: [PATCH 1/5] dettaching lights before deleting the node --- node.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/node.cpp b/node.cpp index 7b3a5f7..721820e 100755 --- a/node.cpp +++ b/node.cpp @@ -32,8 +32,10 @@ namespace vb01{ for(Mesh *m : meshes) delete m; - for(Light *l : lights) + for(Light *l : lights){ + removeLight(l); delete l; + } for(ParticleEmitter *p : emitters) delete p; From 9e6613d065182c8e9f0affb6a50f35f5981cb1db Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 20 Oct 2024 15:31:26 +0300 Subject: [PATCH 2/5] simplified destructor --- model.cpp | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/model.cpp b/model.cpp index 8d5f46a..0684ba1 100755 --- a/model.cpp +++ b/model.cpp @@ -28,18 +28,7 @@ namespace vb01{ } Model::~Model(){ - vector descendants; - getDescendants(descendants); - - for(Node *node : descendants) - for(Mesh *mesh : node->getMeshes()) - mesh->setMaterial(nullptr); - - while(!children.empty()){ - Node *c = children[children.size() - 1]; - dettachChild(c); - delete c; - } + this->setMaterial(nullptr); } void Model::update(){ From cb22e6d4000586544e9a042f7ff34f948829976c Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 18 Jan 2025 19:19:20 +0200 Subject: [PATCH 3/5] Plane ray cast sample --- CMakeLists.txt | 5 +- planeRayCastSample.cpp | 82 ++++++ samples/models/sphere.xml | 564 +++++++++++++++++++------------------- 3 files changed, 368 insertions(+), 283 deletions(-) create mode 100644 planeRayCastSample.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5800646..08899e2 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(vb01) set(CMAKE_BUILD_TYPE Debug) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -pedantic") set(BUILD_TESTS OFF) -set(BUILD_SAMPLES OFF) +set(BUILD_SAMPLES ON) set(BUILD_WITH_ASSIMP OFF) set(BUILD_GUI_ELEMENTS ON) set(LIB_NAME vb01) @@ -123,6 +123,9 @@ if(BUILD_SAMPLES) add_executable(rayCasterSample rayCasterSample.cpp) target_link_libraries(rayCasterSample ${LIB_NAME}) + add_executable(planeRayCastSample planeRayCastSample.cpp) + target_link_libraries(planeRayCastSample ${LIB_NAME}) + add_executable(pbrSample pbrSample.cpp) target_link_libraries(pbrSample ${LIB_NAME}) endif() diff --git a/planeRayCastSample.cpp b/planeRayCastSample.cpp new file mode 100644 index 0000000..fa9f6e5 --- /dev/null +++ b/planeRayCastSample.cpp @@ -0,0 +1,82 @@ +#include "root.h" +#include "assetManager.h" +#include "quad.h" +#include "box.h" +#include "node.h" +#include "material.h" +#include "rayCaster.h" + +using namespace std; +using namespace vb01; + +int main(){ + const string PATH = "../", TEX_PATH = PATH + "samples/textures/", MODEL_PATH = PATH + "samples/models/"; + const int numTextures = 6; + string skyboxTextures[numTextures] = { + TEX_PATH + "top.jpg", + TEX_PATH + "bottom.jpg", + TEX_PATH + "left.jpg", + TEX_PATH + "right.jpg", + TEX_PATH + "front.jpg", + TEX_PATH + "back.jpg" + }; + + for(int i = 0; i < numTextures; i++) + AssetManager::getSingleton()->load(skyboxTextures[i]); + + Root *root = Root::getSingleton(); + root->start(800, 600, PATH, "Plane ray cast sample"); + root->createSkybox(skyboxTextures); + + Camera *cam = root->getCamera(); + cam->setFov(70); + cam->setFarPlane(500); + cam->setPosition(70 * Vector3::VEC_IJK); + cam->lookAt(-Vector3::VEC_IJK, Vector3(-1, 1, -1).norm()); + + Material *mat = new Material(PATH + "texture"); + mat->addBoolUniform("lightingEnabled", false); + mat->addBoolUniform("texturingEnabled", false); + mat->addVec4Uniform("diffuseColor", Vector4(0, 0, 1, 1)); + + Vector3 size = Vector3(100, 100, 0); + int numVertDivs = 20, numHorDivs = 20; + Quad *quad = new Quad(size, true, numVertDivs, numHorDivs); + quad->setMaterial(mat); + + Node *plane = new Node(); + plane->attachMesh(quad); + + Node *rootNode = root->getRootNode(); + rootNode->attachChild(plane); + + float stepX = (numHorDivs > 0 ? size.x / numHorDivs : size.x); + float stepZ = (numVertDivs > 0 ? size.y / numVertDivs : size.y); + Vector3 startPos = -.5 * (Vector3(size.x, 0, size.y) - Vector3(stepX, 0, stepZ)); + + for(int i = 0; i < numVertDivs; i++){ + for(int j = 0; j < numHorDivs; j++){ + Vector3 rayPos = startPos + Vector3(stepX * j, 0, stepZ * i); + vector results = RayCaster::cast(rayPos + Vector3(0, 100, 0), -Vector3::VEC_J, plane); + + Material *mat = new Material(PATH + "texture"); + mat->addBoolUniform("lightingEnabled", false); + mat->addBoolUniform("texturingEnabled", false); + mat->addVec4Uniform("diffuseColor", (results.empty() ? Vector4(1, 0, 0, 1) : Vector4(0, 1, 0, 1))); + + Box *box = new Box(Vector3::VEC_IJK); + box->setMaterial(mat); + + Node *node = new Node(rayPos); + node->attachMesh(box); + + rootNode->attachChild(node); + } + } + + while(true){ + root->update(); + } + + return 0; +} diff --git a/samples/models/sphere.xml b/samples/models/sphere.xml index f866172..05a564d 100644 --- a/samples/models/sphere.xml +++ b/samples/models/sphere.xml @@ -1,288 +1,288 @@ - - - - - - - - + + + + + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 49718103228bfc036a8887b784160f5db3353f5b Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 19 Jan 2025 20:35:45 +0200 Subject: [PATCH 4/5] improved polygon check --- planeRayCastSample.cpp | 16 +++++++++++----- rayCaster.cpp | 8 +++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/planeRayCastSample.cpp b/planeRayCastSample.cpp index fa9f6e5..00f7804 100644 --- a/planeRayCastSample.cpp +++ b/planeRayCastSample.cpp @@ -1,3 +1,5 @@ +#include + #include "root.h" #include "assetManager.h" #include "quad.h" @@ -39,8 +41,8 @@ int main(){ mat->addBoolUniform("texturingEnabled", false); mat->addVec4Uniform("diffuseColor", Vector4(0, 0, 1, 1)); - Vector3 size = Vector3(100, 100, 0); - int numVertDivs = 20, numHorDivs = 20; + Vector3 size = Vector3(1000, 1000, 0); + int numVertDivs = 50, numHorDivs = 50; Quad *quad = new Quad(size, true, numVertDivs, numHorDivs); quad->setMaterial(mat); @@ -57,14 +59,18 @@ int main(){ for(int i = 0; i < numVertDivs; i++){ for(int j = 0; j < numHorDivs; j++){ Vector3 rayPos = startPos + Vector3(stepX * j, 0, stepZ * i); - vector results = RayCaster::cast(rayPos + Vector3(0, 100, 0), -Vector3::VEC_J, plane); + vector results = RayCaster::cast(startPos + Vector3(0, 100, 0), -Vector3::VEC_J, plane, 0, 30); + bool hit = !results.empty(); + + if(!hit) + cout << "x: " << rayPos.x << ", z: " << rayPos.z << endl; Material *mat = new Material(PATH + "texture"); mat->addBoolUniform("lightingEnabled", false); mat->addBoolUniform("texturingEnabled", false); - mat->addVec4Uniform("diffuseColor", (results.empty() ? Vector4(1, 0, 0, 1) : Vector4(0, 1, 0, 1))); + mat->addVec4Uniform("diffuseColor", (hit ? Vector4(0, 1, 0, 1) : Vector4(1, 0, 0, 1))); - Box *box = new Box(Vector3::VEC_IJK); + Box *box = new Box(Vector3::VEC_IJK * .7); box->setMaterial(mat); Node *node = new Node(rayPos); diff --git a/rayCaster.cpp b/rayCaster.cpp index dc6f6d2..cc56c43 100644 --- a/rayCaster.cpp +++ b/rayCaster.cpp @@ -42,7 +42,7 @@ namespace vb01{ bool skip = false; for(int j = 0; j < 3; j++){ - Vector3 rayPosToVert = (*(vertices[i * 3 + j].pos) - rayPos); + Vector3 rayPosToVert = (*(vertices[indices[i * 3 + j]].pos) - rayPos); float angle = rayDir.getAngleBetween(rayPosToVert.norm()); if(angle > PI / 2) angle = PI - angle; @@ -58,7 +58,9 @@ namespace vb01{ if(skip) continue; } - Vector3 pointA = pos + rot * *(vertices[indices[i * 3]].pos), pointB = pos + rot * *(vertices[indices[i * 3 + 1]].pos), pointC = pos + rot * *(vertices[indices[i * 3 + 2]].pos); + Vector3 pointA = pos + rot * *(vertices[indices[i * 3]].pos); + Vector3 pointB = pos + rot * *(vertices[indices[i * 3 + 1]].pos); + Vector3 pointC = pos + rot * *(vertices[indices[i * 3 + 2]].pos); Vector3 hypVec = pointA - rayPos; Vector3 perpVec = (pointB - pointA).cross(pointC - pointA).norm(); float a1 = hypVec.norm().getAngleBetween(perpVec); @@ -87,7 +89,7 @@ namespace vb01{ bool withinBisecB = ((contactPoint - pointB).norm().getAngleBetween(bisecBVec.norm()) - angleB / 2) <= eps; bool withinBisecC = ((contactPoint - pointC).norm().getAngleBetween(bisecCVec.norm()) - angleC / 2) <= eps; - if(withinBisecA && withinBisecB && withinBisecC){ + if((withinBisecA && withinBisecB) || (withinBisecB && withinBisecC) || (withinBisecA && withinBisecC)){ CollisionResult result; result.pos = contactPoint; result.norm = -perpVec; From 26319dd6fce56b3753ca6602b5095ed27d64f24d Mon Sep 17 00:00:00 2001 From: devZoGok Date: Wed, 12 Feb 2025 13:52:47 +0200 Subject: [PATCH 5/5] changed number of ray casts --- planeRayCastSample.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/planeRayCastSample.cpp b/planeRayCastSample.cpp index 00f7804..4cf6691 100644 --- a/planeRayCastSample.cpp +++ b/planeRayCastSample.cpp @@ -41,8 +41,8 @@ int main(){ mat->addBoolUniform("texturingEnabled", false); mat->addVec4Uniform("diffuseColor", Vector4(0, 0, 1, 1)); - Vector3 size = Vector3(1000, 1000, 0); - int numVertDivs = 50, numHorDivs = 50; + Vector3 size = Vector3(100, 100, 0); + int numVertDivs = 11, numHorDivs = 11; Quad *quad = new Quad(size, true, numVertDivs, numHorDivs); quad->setMaterial(mat);