Fixed? Normal calculation on 3D AABB

This commit is contained in:
2023-02-24 23:52:58 -08:00
parent b32c7e8af6
commit f74ca2501f

View File

@ -89,6 +89,10 @@ bool_t Dawn::raytestAABB(
glm::vec3 *normal, glm::vec3 *normal,
float_t *distance float_t *distance
) { ) {
assertNotNull(point);
assertNotNull(normal);
assertNotNull(distance);
// Compute the inverse direction of the ray, for numerical stability // Compute the inverse direction of the ray, for numerical stability
glm::vec3 invDir(1.0f / ray.direction.x, 1.0f / ray.direction.y, 1.0f / ray.direction.z); glm::vec3 invDir(1.0f / ray.direction.x, 1.0f / ray.direction.y, 1.0f / ray.direction.z);
@ -111,25 +115,25 @@ bool_t Dawn::raytestAABB(
// Compute the hit point and normal // Compute the hit point and normal
glm::vec3 hitPoint = ray.origin + tNear * ray.direction; glm::vec3 hitPoint = ray.origin + tNear * ray.direction;
if(point != nullptr) *point = hitPoint; *point = hitPoint;
if(distance != nullptr) *distance = tNear; *distance = tNear;
if(normal != nullptr) {
if (hitPoint.x == box.min.x) { // Small value to account for floating point imprecision
const float epsilon = 0.001f;
if(std::abs(hitPoint.x - box.min.x) < epsilon) {
*normal = glm::vec3(-1, 0, 0); *normal = glm::vec3(-1, 0, 0);
} else if (hitPoint.x == box.max.x) { } else if(std::abs(hitPoint.x - box.max.x) < epsilon) {
*normal = glm::vec3(1, 0, 0); *normal = glm::vec3(1, 0, 0);
} else if (hitPoint.y == box.min.y) { } else if(std::abs(hitPoint.y - box.min.y) < epsilon) {
*normal = glm::vec3(0, -1, 0); *normal = glm::vec3(0, -1, 0);
} else if (hitPoint.y == box.max.y) { } else if(std::abs(hitPoint.y - box.max.y) < epsilon) {
*normal = glm::vec3(0, 1, 0); *normal = glm::vec3(0, 1, 0);
} else if (hitPoint.z == box.min.z) { } else if(std::abs(hitPoint.z - box.min.z) < epsilon) {
*normal = glm::vec3(0, 0, -1); *normal = glm::vec3(0, 0, -1);
} else if (hitPoint.z == box.max.z) { } else if(std::abs(hitPoint.z - box.max.z) < epsilon) {
*normal = glm::vec3(0, 0, 1); *normal = glm::vec3(0, 0, 1);
} }
}
// The ray intersects the box
return true; return true;
} }