From: Daniel Karbach Date: Mon, 14 Nov 2016 16:32:56 +0000 (+0100) Subject: make Intersection() repeatable for normal X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=c2bf905f4b9863fe0f5c876ed00fe298cb95ab6b;p=blank.git make Intersection() repeatable for normal --- diff --git a/src/geometry/geometry.cpp b/src/geometry/geometry.cpp index 5db29c8..6ff26ca 100644 --- a/src/geometry/geometry.cpp +++ b/src/geometry/geometry.cpp @@ -109,14 +109,14 @@ bool Intersection( glm::vec3 min_all(glm::min(t1, t2)); if (min_all.x > min_all.y) { if (min_all.x > min_all.z) { - normal->x = t2.x < t1.x ? 1 : -1; + *normal = glm::vec3(t2.x < t1.x ? 1 : -1, 0, 0); } else { - normal->z = t2.z < t1.z ? 1 : -1; + *normal = glm::vec3(0, 0, t2.z < t1.z ? 1 : -1); } } else if (min_all.y > min_all.z) { - normal->y = t2.y < t1.y ? 1 : -1; + *normal = glm::vec3(0, t2.y < t1.y ? 1 : -1, 0); } else { - normal->z = t2.z < t1.z ? 1 : -1; + *normal = glm::vec3(0, 0, t2.z < t1.z ? 1 : -1); } } return true; diff --git a/tst/geometry/IntersectionTest.cpp b/tst/geometry/IntersectionTest.cpp index 1b61838..04980c6 100644 --- a/tst/geometry/IntersectionTest.cpp +++ b/tst/geometry/IntersectionTest.cpp @@ -87,7 +87,7 @@ void IntersectionTest::testRayBoxIntersection() { // should be 4 units to the left now ray.orig.x = -5; CPPUNIT_ASSERT_MESSAGE( - "ray pointing at box doesn't intersect", + "ray pointing at box to the right doesn't intersect", Intersection(ray, box, M, &distance, &normal) ); CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( @@ -102,10 +102,101 @@ void IntersectionTest::testRayBoxIntersection() { // move ray to the other side, so it's pointing away now ray.orig.x = 5; CPPUNIT_ASSERT_MESSAGE( - "ray pointing away from box still intersects", + "ray pointing away from box to the left still intersects", !Intersection(ray, box, M) ); + // turn ray around + ray.dir.x = -1; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing at box to the left does not intersect", + Intersection(ray, box, M, &distance, &normal) + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "intersection distance way off", + 4.0f, distance, delta + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "wrong surface normal at intersection point", + glm::vec3(1, 0, 0), normal + ); + + // ray below + ray.orig = { 0, -5, 0 }; + ray.dir = { 0, 1, 0 }; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing at box above does not intersect", + Intersection(ray, box, M, &distance, &normal) + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "intersection distance way off", + 4.0f, distance, delta + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "wrong surface normal at intersection point", + glm::vec3(0, -1, 0), normal + ); + + // turn ray around + ray.dir.y = -1; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing away from box above still intersects", + !Intersection(ray, box, M) + ); + + // move ray above + ray.orig.y = 5; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing at box below does not intersect", + Intersection(ray, box, M, &distance, &normal) + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "intersection distance way off", + 4.0f, distance, delta + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "wrong surface normal at intersection point", + glm::vec3(0, 1, 0), normal + ); + + // ray behind + ray.orig = { 0, 0, -5 }; + ray.dir = { 0, 0, 1 }; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing at box in front does not intersect", + Intersection(ray, box, M, &distance, &normal) + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "intersection distance way off", + 4.0f, distance, delta + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "wrong surface normal at intersection point", + glm::vec3(0, 0, -1), normal + ); + + // turn ray around + ray.dir.z = -1; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing away from box in front still intersects", + !Intersection(ray, box, M) + ); + + // move ray in front + ray.orig.z = 5; + CPPUNIT_ASSERT_MESSAGE( + "ray pointing at box behind does not intersect", + Intersection(ray, box, M, &distance, &normal) + ); + CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( + "intersection distance way off", + 4.0f, distance, delta + ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( + "wrong surface normal at intersection point", + glm::vec3(0, 0, 1), normal + ); + // 45 deg down from 4 units away, so should be about 4 * sqrt(2) ray.orig = { -5.0f, 4.5f, 0.0f }; ray.dir = { 0.70710678118654752440f, -0.70710678118654752440f, 0.0f };