From 48839a38a14a653c233693fa911348ce58d57167 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Mon, 15 Jul 2024 12:41:23 +0300 Subject: [PATCH] fix refract --- include/cglm/vec2.h | 2 +- include/cglm/vec3.h | 2 +- include/cglm/vec4.h | 2 +- test/src/test_vec2.h | 4 ++-- test/src/test_vec3.h | 4 ++-- test/src/test_vec4.h | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/cglm/vec2.h b/include/cglm/vec2.h index aaac759f..98cd4df1 100644 --- a/include/cglm/vec2.h +++ b/include/cglm/vec2.h @@ -749,7 +749,7 @@ glm_vec2_refract(vec2 v, vec2 n, float eta, vec2 dest) { ndi = glm_vec2_dot(n, v); eni = eta * ndi; - k = 1.0f + eta * eta - eni * eni; + k = 1.0f - eta * eta + eni * eni; if (k < 0.0f) { glm_vec2_zero(dest); diff --git a/include/cglm/vec3.h b/include/cglm/vec3.h index be4029b4..e6a98984 100644 --- a/include/cglm/vec3.h +++ b/include/cglm/vec3.h @@ -1263,7 +1263,7 @@ glm_vec3_refract(vec3 v, vec3 n, float eta, vec3 dest) { ndi = glm_vec3_dot(n, v); eni = eta * ndi; - k = 1.0f + eta * eta - eni * eni; + k = 1.0f - eta * eta + eni * eni; if (k < 0.0f) { glm_vec3_zero(dest); diff --git a/include/cglm/vec4.h b/include/cglm/vec4.h index e24675fc..b259235a 100644 --- a/include/cglm/vec4.h +++ b/include/cglm/vec4.h @@ -1350,7 +1350,7 @@ glm_vec4_refract(vec4 v, vec4 n, float eta, vec4 dest) { ndi = glm_vec4_dot(n, v); eni = eta * ndi; - k = 1.0f + eta * eta - eni * eni; + k = 1.0f - eta * eta + eni * eni; if (k < 0.0f) { glm_vec4_zero(dest); diff --git a/test/src/test_vec2.h b/test/src/test_vec2.h index e7bfe884..966847b7 100644 --- a/test/src/test_vec2.h +++ b/test/src/test_vec2.h @@ -792,7 +792,7 @@ TEST_IMPL(GLM_PREFIX, vec2_refract) { r = GLM(vec2_refract)(v, N, eta, dest); // In 2D, we expect a similar bending behavior as in 3D, so we check dest[1] if (!(dest[0] == 0.0f && dest[1] == 0.0f)) { - ASSERT(dest[1] < -sqrtf(0.5f)); // Refracted ray bends away from the normal + ASSERT(dest[1] < -0.3f); // Refracted ray bends away from the normal ASSERT(r == true); } else { ASSERT(dest[0] == 0.0f && dest[1] == 0.0f); // Total internal reflection @@ -809,7 +809,7 @@ TEST_IMPL(GLM_PREFIX, vec2_refract) { eta = 1.5f / 1.33f; r = GLM(vec2_refract)(v, N, eta, dest); ASSERT(r == true); - ASSERT(dest[1] < -sqrtf(0.5f)); // Expect bending towards the normal, less bending than air to glass + ASSERT(dest[1] < -0.6f); // Expect bending towards the normal, less bending than air to glass /* Diamond to Air (eta = 2.42 / 1.0) */ eta = 2.42f / 1.0f; diff --git a/test/src/test_vec3.h b/test/src/test_vec3.h index 7c057f30..ac504739 100644 --- a/test/src/test_vec3.h +++ b/test/src/test_vec3.h @@ -1901,7 +1901,7 @@ TEST_IMPL(GLM_PREFIX, vec3_refract) { eta = 1.33f / 1.0f; r = GLM(vec3_refract)(v, N, eta, dest); if (!(dest[0] == 0.0f && dest[1] == 0.0f && dest[2] == 0.0f)) { - ASSERT(dest[1] < -sqrtf(0.5f)); + ASSERT(dest[1] < -0.3f); ASSERT(r == true); } else { ASSERT(dest[0] == 0.0f && dest[1] == 0.0f && dest[2] == 0.0f); @@ -1922,7 +1922,7 @@ TEST_IMPL(GLM_PREFIX, vec3_refract) { /* Expect bending towards the normal, less bending than air to glass */ ASSERT(r == true); - ASSERT(dest[1] < -sqrtf(0.5f)); + ASSERT(dest[1] < -0.6f); /* Diamond to Air (eta = 2.42 / 1.0) */ eta = 2.42f / 1.0f; diff --git a/test/src/test_vec4.h b/test/src/test_vec4.h index 435c7005..90122f2d 100644 --- a/test/src/test_vec4.h +++ b/test/src/test_vec4.h @@ -1586,7 +1586,7 @@ TEST_IMPL(GLM_PREFIX, vec4_refract) { eta = 1.33f / 1.0f; r = GLM(vec4_refract)(v, N, eta, dest); if (!(dest[0] == 0.0f && dest[1] == 0.0f && dest[2] == 0.0f && dest[3] == 0.0f)) { - ASSERT(dest[1] < -sqrtf(0.5f)); + ASSERT(dest[1] < -0.3f); ASSERT(r == true); } else { ASSERT(dest[0] == 0.0f && dest[1] == 0.0f && dest[2] == 0.0f && dest[3] == 0.0f); @@ -1603,7 +1603,7 @@ TEST_IMPL(GLM_PREFIX, vec4_refract) { eta = 1.5f / 1.33f; r = GLM(vec4_refract)(v, N, eta, dest); ASSERT(r == true); - ASSERT(dest[1] < -sqrtf(0.5f)); // Expect bending towards the normal, less bending than air to glass + ASSERT(dest[1] < -0.6f); // Expect bending towards the normal, less bending than air to glass /* Diamond to Air (eta = 2.42 / 1.0) */ eta = 2.42f / 1.0f;