From d698594899a669b9b69f4f4fb61d7380f80274cd Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sat, 6 Dec 2014 19:39:02 -0800 Subject: [PATCH] use common findRayTriangleIntersection --- interface/src/MetavoxelSystem.cpp | 25 +---------- interface/src/renderer/Model.cpp | 2 +- libraries/shared/src/GeometryUtil.cpp | 62 +++++++++------------------ libraries/shared/src/GeometryUtil.h | 6 +-- 4 files changed, 25 insertions(+), 70 deletions(-) diff --git a/interface/src/MetavoxelSystem.cpp b/interface/src/MetavoxelSystem.cpp index ebf2188b4d..87060d7dfa 100644 --- a/interface/src/MetavoxelSystem.cpp +++ b/interface/src/MetavoxelSystem.cpp @@ -21,6 +21,7 @@ #include +#include #include #include @@ -1095,30 +1096,6 @@ VoxelBuffer::VoxelBuffer(const QVector& vertices, const QVector _materials(materials) { } -static bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3& direction, - const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance) { - glm::vec3 firstSide = v0 - v1; - glm::vec3 secondSide = v2 - v1; - glm::vec3 normal = glm::cross(secondSide, firstSide); - float dividend = glm::dot(normal, v1) - glm::dot(origin, normal); - if (dividend > 0.0f) { - return false; // origin below plane - } - float divisor = glm::dot(normal, direction); - if (divisor > -EPSILON) { - return false; - } - float t = dividend / divisor; - glm::vec3 point = origin + direction * t; - if (glm::dot(normal, glm::cross(point - v1, firstSide)) > 0.0f && - glm::dot(normal, glm::cross(secondSide, point - v1)) > 0.0f && - glm::dot(normal, glm::cross(point - v0, v2 - v0)) > 0.0f) { - distance = t; - return true; - } - return false; -} - bool VoxelBuffer::findFirstRayIntersection(const glm::vec3& entry, const glm::vec3& origin, const glm::vec3& direction, float& distance) const { float highest = _size - 1.0f; diff --git a/interface/src/renderer/Model.cpp b/interface/src/renderer/Model.cpp index 91bb4735fd..71f5129a1e 100644 --- a/interface/src/renderer/Model.cpp +++ b/interface/src/renderer/Model.cpp @@ -626,7 +626,7 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g t++; float thisTriangleDistance; - if (findRayTrianlgeIntersection(origin, direction, triangle, thisTriangleDistance)) { + if (findRayTriangleIntersection(origin, direction, triangle, thisTriangleDistance)) { if (thisTriangleDistance < bestDistance) { bestTriangleDistance = thisTriangleDistance; someTriangleHit = true; diff --git a/libraries/shared/src/GeometryUtil.cpp b/libraries/shared/src/GeometryUtil.cpp index e2736f8502..5376883438 100644 --- a/libraries/shared/src/GeometryUtil.cpp +++ b/libraries/shared/src/GeometryUtil.cpp @@ -252,48 +252,26 @@ bool findRayCapsuleIntersection(const glm::vec3& origin, const glm::vec3& direct return true; } - -bool findRayTrianlgeIntersection(const glm::vec3& origin, const glm::vec3& direction, - const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance) { - - glm::vec3 e1, e2, h, s, q; - float a, f, u, v, t; - - e1 = v1 - v0; - e2 = v2 - v0; - - h = glm::cross(direction, e2); - a = glm::dot(e1, h); - - if (a > EPSILON && a < EPSILON) { - return false; - } - - f = 1/a; - s = origin - v0; - u = f * glm::dot(s,h); - - if (u < 0.0 || u > 1.0) { - return false; - } - - q = glm::cross(s, e1); - v = f * glm::dot(direction, q); - - if (v < 0.0 || u + v > 1.0) { - return false; - } - - // at this stage we can compute t to find out where the intersection point is on the line - t = f * glm::dot(e2,q); - - // ray intersection - if (t > EPSILON) { - distance = t; - return true; - } else { - // this means that there is a line intersection but not a ray intersection - return false; +bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3& direction, + const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance) { + glm::vec3 firstSide = v0 - v1; + glm::vec3 secondSide = v2 - v1; + glm::vec3 normal = glm::cross(secondSide, firstSide); + float dividend = glm::dot(normal, v1) - glm::dot(origin, normal); + if (dividend > 0.0f) { + return false; // origin below plane + } + float divisor = glm::dot(normal, direction); + if (divisor > -EPSILON) { + return false; + } + float t = dividend / divisor; + glm::vec3 point = origin + direction * t; + if (glm::dot(normal, glm::cross(point - v1, firstSide)) > 0.0f && + glm::dot(normal, glm::cross(secondSide, point - v1)) > 0.0f && + glm::dot(normal, glm::cross(point - v0, v2 - v0)) > 0.0f) { + distance = t; + return true; } return false; } diff --git a/libraries/shared/src/GeometryUtil.h b/libraries/shared/src/GeometryUtil.h index f439352ca8..a6889ef73e 100644 --- a/libraries/shared/src/GeometryUtil.h +++ b/libraries/shared/src/GeometryUtil.h @@ -76,7 +76,7 @@ bool findRaySphereIntersection(const glm::vec3& origin, const glm::vec3& directi bool findRayCapsuleIntersection(const glm::vec3& origin, const glm::vec3& direction, const glm::vec3& start, const glm::vec3& end, float radius, float& distance); -bool findRayTrianlgeIntersection(const glm::vec3& origin, const glm::vec3& direction, +bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3& direction, const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance); class Triangle { @@ -86,9 +86,9 @@ public: glm::vec3 v2; }; -inline bool findRayTrianlgeIntersection(const glm::vec3& origin, const glm::vec3& direction, +inline bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3& direction, const Triangle& triangle, float& distance) { - return findRayTrianlgeIntersection(origin, direction, triangle.v0, triangle.v1, triangle.v2, distance); + return findRayTriangleIntersection(origin, direction, triangle.v0, triangle.v1, triangle.v2, distance); }