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 <GeometryUtil.h>
#include <SharedUtil.h>
#include <MetavoxelMessages.h>
@ -1095,30 +1096,6 @@ VoxelBuffer::VoxelBuffer(const QVector<VoxelPoint>& vertices, const QVector<int>
_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;

View file

@ -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;

View file

@ -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;
}

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,
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);
}