Added basic triangle raycasting.
This commit is contained in:
@ -23,6 +23,9 @@ bool_t Dawn::raytestSphere(
|
||||
glm::vec3 *hit,
|
||||
glm::vec3 *normal
|
||||
) {
|
||||
assertNotNull(hit);
|
||||
assertNotNull(normal);
|
||||
|
||||
glm::vec3 h, n;
|
||||
auto result = glm::intersectRaySphere(
|
||||
ray.origin, ray.direction,
|
||||
@ -30,8 +33,8 @@ bool_t Dawn::raytestSphere(
|
||||
h, n
|
||||
);
|
||||
|
||||
if(hit != nullptr) *hit = h;
|
||||
if(normal != nullptr) *normal = n;
|
||||
*hit = h;
|
||||
*normal = n;
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -39,19 +42,54 @@ bool_t Dawn::raytestSphere(
|
||||
bool_t Dawn::raytestTriangle(
|
||||
struct Ray3D ray,
|
||||
struct PhysicsTriangle triangle,
|
||||
glm::vec2 *hit,
|
||||
float_t *distance
|
||||
glm::vec3 *hitPoint,
|
||||
glm::vec3 *hitNormal,
|
||||
float_t *hitDistance
|
||||
) {
|
||||
glm::vec2 h;
|
||||
float_t d;
|
||||
auto result = glm::intersectRayTriangle(
|
||||
ray.origin, ray.direction,
|
||||
triangle.v0, triangle.v1, triangle.v2,
|
||||
h, d
|
||||
);
|
||||
if(hit != nullptr) *hit = h;
|
||||
if(distance != nullptr) *distance = d;
|
||||
return result;
|
||||
assertNotNull(hitPoint);
|
||||
assertNotNull(hitNormal);
|
||||
assertNotNull(hitDistance);
|
||||
|
||||
// Calculate the normal of the triangle
|
||||
glm::vec3 e0 = triangle.v1 - triangle.v0;
|
||||
glm::vec3 e1 = triangle.v2 - triangle.v0;
|
||||
glm::vec3 normal = glm::normalize(glm::cross(e0, e1));
|
||||
|
||||
// Calculate the denominator of the ray-triangle intersection formula
|
||||
float_t denominator = glm::dot(normal, ray.direction);
|
||||
|
||||
// If the denominator is zero, the ray and triangle are parallel and there is no intersection
|
||||
if(denominator == 0) return -1;
|
||||
|
||||
// Calculate the distance from the ray origin to the plane of the triangle
|
||||
float_t d = glm::dot(triangle.v0 - ray.origin, normal) / denominator;
|
||||
|
||||
// If the distance is negative, the intersection point is behind the ray origin and there is no intersection
|
||||
if(d < 0) return -1;
|
||||
|
||||
// Calculate the intersection point
|
||||
glm::vec3 intersectionPoint = ray.origin + d * ray.direction;
|
||||
|
||||
// Check if the intersection point is inside the triangle
|
||||
glm::vec3 edge0 = triangle.v1 - triangle.v0;
|
||||
glm::vec3 edge1 = triangle.v2 - triangle.v1;
|
||||
glm::vec3 edge2 = triangle.v0 - triangle.v2;
|
||||
glm::vec3 c0 = intersectionPoint - triangle.v0;
|
||||
glm::vec3 c1 = intersectionPoint - triangle.v1;
|
||||
glm::vec3 c2 = intersectionPoint - triangle.v2;
|
||||
glm::vec3 n0 = glm::cross(edge0, c0);
|
||||
glm::vec3 n1 = glm::cross(edge1, c1);
|
||||
glm::vec3 n2 = glm::cross(edge2, c2);
|
||||
if (glm::dot(n0, normal) >= 0 && glm::dot(n1, normal) >= 0 && glm::dot(n2, normal) >= 0) {
|
||||
// If the intersection point is inside the triangle, set the hit point, normal and distance
|
||||
*hitPoint = intersectionPoint;
|
||||
*hitNormal = normal;
|
||||
*hitDistance = d;
|
||||
return true;
|
||||
}
|
||||
|
||||
// If the intersection point is outside the triangle, there is no intersection
|
||||
return false;
|
||||
}
|
||||
|
||||
bool_t Dawn::raytestAABB(
|
||||
|
@ -26,8 +26,9 @@ namespace Dawn {
|
||||
bool_t raytestTriangle(
|
||||
struct Ray3D ray,
|
||||
struct PhysicsTriangle triangle,
|
||||
glm::vec2 *hit,
|
||||
float_t *distance
|
||||
glm::vec3 *hitPoint,
|
||||
glm::vec3 *hitNormal,
|
||||
float_t *hitDistance
|
||||
);
|
||||
|
||||
bool_t raytestAABB(
|
||||
|
Reference in New Issue
Block a user