use common findRayTriangleIntersection

This commit is contained in:
ZappoMan 2014-12-06 19:39:02 -08:00
parent 8dd1a2815f
commit d698594899
4 changed files with 25 additions and 70 deletions

View file

@ -21,6 +21,7 @@
#include <glm/gtx/transform.hpp> #include <glm/gtx/transform.hpp>
#include <GeometryUtil.h>
#include <SharedUtil.h> #include <SharedUtil.h>
#include <MetavoxelMessages.h> #include <MetavoxelMessages.h>
@ -1095,30 +1096,6 @@ VoxelBuffer::VoxelBuffer(const QVector<VoxelPoint>& vertices, const QVector<int>
_materials(materials) { _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, bool VoxelBuffer::findFirstRayIntersection(const glm::vec3& entry, const glm::vec3& origin,
const glm::vec3& direction, float& distance) const { const glm::vec3& direction, float& distance) const {
float highest = _size - 1.0f; float highest = _size - 1.0f;

View file

@ -626,7 +626,7 @@ bool Model::findRayIntersectionAgainstSubMeshes(const glm::vec3& origin, const g
t++; t++;
float thisTriangleDistance; float thisTriangleDistance;
if (findRayTrianlgeIntersection(origin, direction, triangle, thisTriangleDistance)) { if (findRayTriangleIntersection(origin, direction, triangle, thisTriangleDistance)) {
if (thisTriangleDistance < bestDistance) { if (thisTriangleDistance < bestDistance) {
bestTriangleDistance = thisTriangleDistance; bestTriangleDistance = thisTriangleDistance;
someTriangleHit = true; someTriangleHit = true;

View file

@ -252,48 +252,26 @@ bool findRayCapsuleIntersection(const glm::vec3& origin, const glm::vec3& direct
return true; return true;
} }
bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3& direction,
bool findRayTrianlgeIntersection(const glm::vec3& origin, const glm::vec3& direction,
const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance) { const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance) {
glm::vec3 firstSide = v0 - v1;
glm::vec3 e1, e2, h, s, q; glm::vec3 secondSide = v2 - v1;
float a, f, u, v, t; glm::vec3 normal = glm::cross(secondSide, firstSide);
float dividend = glm::dot(normal, v1) - glm::dot(origin, normal);
e1 = v1 - v0; if (dividend > 0.0f) {
e2 = v2 - v0; return false; // origin below plane
}
h = glm::cross(direction, e2); float divisor = glm::dot(normal, direction);
a = glm::dot(e1, h); if (divisor > -EPSILON) {
if (a > EPSILON && a < EPSILON) {
return false; return false;
} }
float t = dividend / divisor;
f = 1/a; glm::vec3 point = origin + direction * t;
s = origin - v0; if (glm::dot(normal, glm::cross(point - v1, firstSide)) > 0.0f &&
u = f * glm::dot(s,h); glm::dot(normal, glm::cross(secondSide, point - v1)) > 0.0f &&
glm::dot(normal, glm::cross(point - v0, v2 - v0)) > 0.0f) {
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; distance = t;
return true; return true;
} else {
// this means that there is a line intersection but not a ray intersection
return false;
} }
return false; return false;
} }

View file

@ -76,7 +76,7 @@ bool findRaySphereIntersection(const glm::vec3& origin, const glm::vec3& directi
bool findRayCapsuleIntersection(const glm::vec3& origin, const glm::vec3& direction, bool findRayCapsuleIntersection(const glm::vec3& origin, const glm::vec3& direction,
const glm::vec3& start, const glm::vec3& end, float radius, float& distance); 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); const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance);
class Triangle { class Triangle {
@ -86,9 +86,9 @@ public:
glm::vec3 v2; 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) { 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);
} }