From 3606ab9b938e15ddbcc604fe6d6c322fc931471d Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Thu, 4 Aug 2022 21:16:33 +0100 Subject: [PATCH] No need to cache direction --- meshers/transvoxel/distance_normalmaps.cpp | 3 ++- util/math/triangle.h | 8 +++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/meshers/transvoxel/distance_normalmaps.cpp b/meshers/transvoxel/distance_normalmaps.cpp index fb5fc15d..56834ffb 100644 --- a/meshers/transvoxel/distance_normalmaps.cpp +++ b/meshers/transvoxel/distance_normalmaps.cpp @@ -227,7 +227,8 @@ void compute_normalmap(Span cell_infos, const transv const float NO_HIT = 999999.f; float nearest_hit_distance = NO_HIT; for (unsigned int ti = 0; ti < triangle_count; ++ti) { - const math::TriangleIntersectionResult result = baked_triangles[ti].intersect(ray_origin_mesh); + const math::TriangleIntersectionResult result = + baked_triangles[ti].intersect(ray_origin_mesh, direction); if (result.case_id == math::TriangleIntersectionResult::INTERSECTION && result.distance < nearest_hit_distance) { nearest_hit_distance = result.distance; diff --git a/util/math/triangle.h b/util/math/triangle.h index 77358c58..920fd060 100644 --- a/util/math/triangle.h +++ b/util/math/triangle.h @@ -79,7 +79,6 @@ inline TriangleIntersectionResult ray_intersects_triangle(const Vector3f &p_from // If you need to do a lot of raycasts on a triangle using the same direction every time struct BakedIntersectionTriangleForFixedDirection { - Vector3f dir; Vector3f v0; Vector3f e1; Vector3f e2; @@ -87,12 +86,11 @@ struct BakedIntersectionTriangleForFixedDirection { float f; bool bake(Vector3f p_v0, Vector3f p_v1, Vector3f p_v2, Vector3f p_dir) { - dir = p_dir; v0 = p_v0; e1 = p_v1 - v0; e2 = p_v2 - v0; - h = math::cross(dir, e2); + h = math::cross(p_dir, e2); const float a = math::dot(e1, h); if (Math::abs(a) < 0.00001f) { // Parallel, will never hit @@ -102,7 +100,7 @@ struct BakedIntersectionTriangleForFixedDirection { return true; } - inline TriangleIntersectionResult intersect(const Vector3f &p_from) { + inline TriangleIntersectionResult intersect(const Vector3f &p_from, const Vector3f &p_dir) { const Vector3f s = p_from - v0; const float u = f * math::dot(s, h); @@ -112,7 +110,7 @@ struct BakedIntersectionTriangleForFixedDirection { const Vector3f q = math::cross(s, e1); - const float v = f * math::dot(dir, q); + const float v = f * math::dot(p_dir, q); if ((v < 0.0f) || (u + v > 1.0f)) { return { TriangleIntersectionResult::NO_INTERSECTION, -1 };