From 5bdbcbd1e353c36d71fc9f1c49c312fa95a95e48 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Wed, 1 Nov 2023 16:04:53 +0200 Subject: [PATCH 1/3] updated sphere model --- samples/models/sphere.xml | 454 +++++++++++++++++++------------------- 1 file changed, 227 insertions(+), 227 deletions(-) diff --git a/samples/models/sphere.xml b/samples/models/sphere.xml index 7bb6797..f866172 100644 --- a/samples/models/sphere.xml +++ b/samples/models/sphere.xml @@ -1,288 +1,288 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - + + + - - - - + + + + - - - - - + + + + + - - - - - + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - + + + + + - - - - + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - - - - - - - - + + + + + + + + - + - - - - + + + + - - - - - - + + + + + + - - - - - - - + + + + + + + - - - - - + + + + + - - + + - - - - - - - + + + + + + + - - - - + + + + - + - - + + - - - + + + - + - - + + - - - - + + + + - - - - - + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - + - - - - - - - - + + + + + + + + - - + + - - - - + + + + - - - - - - - - - + + + + + + + + + \ No newline at end of file From 397c3dc23693b589bb0f8f37f1f972bf09b26161 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Wed, 1 Nov 2023 16:28:00 +0200 Subject: [PATCH 2/3] refactored ray casting --- ray.cpp | 43 ++++++++++++++++++++++++++++++------------- ray.h | 9 ++++++--- raySample.cpp | 4 +--- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/ray.cpp b/ray.cpp index 5030b5d..6e98d4e 100644 --- a/ray.cpp +++ b/ray.cpp @@ -7,16 +7,32 @@ using namespace std; namespace vb01{ - void Ray::retrieveCollisions(Vector3 rayPos, Vector3 rayDir, Node *node, std::vector &results, const float rayLength){ - castRay(rayPos, rayDir, node, results, rayLength); - - for(Node *c : node->getChildren()) - retrieveCollisions(rayPos, rayDir, c, results, rayLength); + vector RayCaster::cast(Vector3 rayPos, Vector3 rayDir, Node *node, const float rayLength){ + return cast(rayPos, rayDir, vector{node}, rayLength); } - void Ray::castRay(Vector3 rayPos, Vector3 rayDir, Node *node, vector &results, float rayLength){ + vector RayCaster::cast(Vector3 rayPos, Vector3 rayDir, vector nodes, const float rayLength){ + vector results; + + for(Node *node : nodes){ + vector descendants = vector{node}; + node->getDescendants(descendants); + + for(Node *desc : descendants){ + vector res = retrieveCollisions(rayPos, rayDir, desc, rayLength); + results.insert(results.end(), res.begin(), res.end()); + } + } + + if(!results.empty()) sortResults(results); + + return results; + } + + vector RayCaster::retrieveCollisions(Vector3 rayPos, Vector3 rayDir, Node *node, float rayLength){ Vector3 pos = node->localToGlobalPosition(Vector3::VEC_ZERO); Quaternion rot = node->localToGlobalOrientation(Quaternion::QUAT_W); + vector results; for(Mesh *m : node->getMeshes()){ const int numVerts = m->getMeshBase().numTris * 3; @@ -64,14 +80,15 @@ namespace vb01{ } } } + + return results; } - void Ray::sortResults(std::vector &results){ - if(!results.empty()) - for(int i = 0; i < results.size(); i++){ - for(int i2 = i; i2 < results.size(); i2++) - if(results[i].distance > results[i2].distance) - swap(results[i], results[i2]); - } + void RayCaster::sortResults(std::vector &results){ + for(int i = 0; i < results.size(); i++){ + for(int j = i; j < results.size(); j++) + if(results[i].distance > results[j].distance) + swap(results[i], results[j]); + } } } diff --git a/ray.h b/ray.h index 5c7d40b..2e8deb8 100644 --- a/ray.h +++ b/ray.h @@ -7,15 +7,18 @@ namespace vb01{ class Node; class Mesh; - class Ray{ + class RayCaster{ public: struct CollisionResult{ Vector3 pos, norm; float distance; Mesh *mesh = nullptr; }; - static void retrieveCollisions(Vector3, Vector3, Node*, std::vector&, const float = .0); - static void castRay(Vector3, Vector3, Node*, std::vector&, const float); + + static std::vector cast(Vector3, Vector3, Node*, const float = .0); + static std::vector cast(Vector3, Vector3, std::vector, const float = .0); + private: + static std::vector retrieveCollisions(Vector3, Vector3, Node*, const float); static void sortResults(std::vector&); }; diff --git a/raySample.cpp b/raySample.cpp index 536258e..d8b5ae9 100644 --- a/raySample.cpp +++ b/raySample.cpp @@ -76,11 +76,9 @@ int main(){ * Casts an infinite ray that checks for collision against meshes under * the icoshpere model node and paints it red upon collision */ - vector results; - Ray::retrieveCollisions(rayStart, rayEnd - rayStart, sphModel, results); - Ray::sortResults(results); mat->setVec4Uniform("diffuseColor", Vector4::VEC_L); + vector results = RayCaster::cast(rayStart, rayEnd - rayStart, sphModel); if(!results.empty()) mat->setVec4Uniform("diffuseColor", Vector4(1, 0, 0, 1)); From 54bdd5ffdf152aad7165bc930d1c21d78b76278e Mon Sep 17 00:00:00 2001 From: devZoGok Date: Wed, 1 Nov 2023 16:30:46 +0200 Subject: [PATCH 3/3] renamed files --- CMakeLists.txt | 6 +++--- ray.cpp => rayCaster.cpp | 2 +- ray.h => rayCaster.h | 0 raySample.cpp => rayCasterSample.cpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) rename ray.cpp => rayCaster.cpp (99%) rename ray.h => rayCaster.h (100%) rename raySample.cpp => rayCasterSample.cpp (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5904ce5..86bde16 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ cmake_policy(SET CMP0015 NEW) set(GUI text.cpp) set(UTILS util.cpp) -set(MATH quaternion.cpp vector.cpp matrix.cpp ray.cpp) +set(MATH quaternion.cpp vector.cpp matrix.cpp rayCaster.cpp) set(RENDER lineRenderer.cpp particleEmitter.cpp camera.cpp light.cpp material.cpp mesh.cpp meshData.cpp model.cpp node.cpp quad.cpp box.cpp root.cpp shader.cpp texture.cpp) set(ANIM animationController.cpp animationChannel.cpp animation.cpp animatable.cpp keyframeChannel.cpp driver.cpp) set(ARMATURE skeleton.cpp bone.cpp ikSolver.cpp) @@ -121,8 +121,8 @@ if(BUILD_SAMPLES) add_executable(particleEmitterSample particleEmitterSample.cpp) target_link_libraries(particleEmitterSample ${LIB_NAME}) - add_executable(raySample raySample.cpp) - target_link_libraries(raySample ${LIB_NAME}) + add_executable(rayCasterSample rayCasterSample.cpp) + target_link_libraries(rayCasterSample ${LIB_NAME}) add_executable(pbrSample pbrSample.cpp) target_link_libraries(pbrSample ${LIB_NAME}) diff --git a/ray.cpp b/rayCaster.cpp similarity index 99% rename from ray.cpp rename to rayCaster.cpp index 6e98d4e..342dc1e 100644 --- a/ray.cpp +++ b/rayCaster.cpp @@ -1,6 +1,6 @@ #include -#include "ray.h" +#include "rayCaster.h" #include "mesh.h" #include "node.h" diff --git a/ray.h b/rayCaster.h similarity index 100% rename from ray.h rename to rayCaster.h diff --git a/raySample.cpp b/rayCasterSample.cpp similarity index 99% rename from raySample.cpp rename to rayCasterSample.cpp index d8b5ae9..5439610 100644 --- a/raySample.cpp +++ b/rayCasterSample.cpp @@ -3,7 +3,7 @@ #include "material.h" #include "assetManager.h" #include "lineRenderer.h" -#include "ray.h" +#include "rayCaster.h" using namespace std; using namespace vb01;