ray fix
This commit is contained in:
parent
a367a780ba
commit
4a254a3a66
1 changed files with 9 additions and 13 deletions
|
|
@ -24,36 +24,32 @@ import std;
|
|||
namespace Crafter {
|
||||
export template<typename T>
|
||||
constexpr T IntersectionTestRayTriangle(Vector<T, 3, 0> vert0, Vector<T, 3, 0> vert1, Vector<T, 3, 0> vert2, Vector<T, 3, 0> rayOrigin, Vector<T, 3, 0> rayDir) {
|
||||
constexpr float EPSILON = 0.0000001;
|
||||
constexpr T EPSILON = 0.0000001;
|
||||
Vector<T, 3, 0> edge1 = vert1 - vert0;
|
||||
Vector<T, 3, 0> edge2 = vert2 - vert0;
|
||||
|
||||
Vector<T, 3, 0> h = Vector<T, 3, 0>::Cross(rayDir, edge2);
|
||||
float determinant = Vector<T, 3, 0>::Dot(edge1, h);
|
||||
T determinant = Vector<T, 3, 0>::Dot(edge1, h);
|
||||
|
||||
if(determinant < 0) {
|
||||
return std::numeric_limits<float>::max();
|
||||
if (determinant <= EPSILON) {
|
||||
return std::numeric_limits<T>::max();
|
||||
}
|
||||
|
||||
if (determinant > -EPSILON && determinant < EPSILON) {
|
||||
return std::numeric_limits<float>::max();
|
||||
}
|
||||
|
||||
float inverse_determinant = 1.0 / determinant;
|
||||
T inverse_determinant = T(1) / determinant;
|
||||
|
||||
Vector<T, 3, 0> origins_diff_vector = rayOrigin - vert0;
|
||||
float u = Vector<T, 3, 0>::Dot(origins_diff_vector, h) * inverse_determinant;
|
||||
T u = Vector<T, 3, 0>::Dot(origins_diff_vector, h) * inverse_determinant;
|
||||
|
||||
if (u < 0.0 || u > 1.0)
|
||||
{
|
||||
return false;
|
||||
return std::numeric_limits<T>::max();
|
||||
}
|
||||
|
||||
Vector<T, 3, 0> q = Vector<T, 3, 0>::Cross(origins_diff_vector, edge1);
|
||||
float v = inverse_determinant * Vector<T, 3, 0>::Dot(rayDir, q);
|
||||
T v = inverse_determinant * Vector<T, 3, 0>::Dot(rayDir, q);
|
||||
|
||||
if (v < 0.0 || u + v > 1.0) {
|
||||
return std::numeric_limits<float>::max();
|
||||
return std::numeric_limits<T>::max();
|
||||
}
|
||||
|
||||
return inverse_determinant * Vector<T, 3, 0>::Dot(edge2, q);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue