mirror of
https://github.com/recp/cglm.git
synced 2025-12-31 12:47:05 +00:00
tests: test for ray, reflect, refract and faceforward
This commit is contained in:
@@ -752,3 +752,70 @@ TEST_IMPL(GLM_PREFIX, vec2_make) {
|
||||
|
||||
TEST_SUCCESS
|
||||
}
|
||||
|
||||
TEST_IMPL(GLM_PREFIX, vec2_reflect) {
|
||||
vec2 dest;
|
||||
|
||||
/* Reflecting off a "horizontal" surface in 2D */
|
||||
vec2 I1 = {1.0f, -1.0f}; /* Incoming vector */
|
||||
vec2 N1 = {0.0f, 1.0f}; /* Normal vector */
|
||||
GLM(vec2_reflect)(I1, N1, dest);
|
||||
ASSERT(fabsf(dest[0] - 1.0f) < 0.00001f &&
|
||||
fabsf(dest[1] - 1.0f) < 0.00001f); /* Expect reflection upwards */
|
||||
|
||||
/* Reflecting at an angle in 2D */
|
||||
vec2 I2 = {sqrtf(2)/2, -sqrtf(2)/2}; /* Incoming vector at 45 degrees */
|
||||
vec2 N2 = {0.0f, 1.0f}; /* Upwards normal vector */
|
||||
GLM(vec2_reflect)(I2, N2, dest);
|
||||
ASSERT(fabsf(dest[0] - sqrtf(2)/2) < 0.00001f &&
|
||||
fabsf(dest[1] - sqrtf(2)/2) < 0.00001f); /* Expect reflection upwards */
|
||||
|
||||
/* Reflecting off a line in 2D representing a "vertical" surface analogy */
|
||||
vec2 I3 = {1.0f, 0.0f}; /* Incoming vector */
|
||||
vec2 N3 = {-1.0f, 0.0f}; /* Normal vector representing a "vertical" line */
|
||||
GLM(vec2_reflect)(I3, N3, dest);
|
||||
ASSERT(fabsf(dest[0] + 1.0f) < 0.00001f &&
|
||||
fabsf(dest[1]) < 0.00001f); /* Expect reflection to the left */
|
||||
|
||||
TEST_SUCCESS
|
||||
}
|
||||
|
||||
TEST_IMPL(GLM_PREFIX, vec2_refract) {
|
||||
vec2 I = {sqrtf(0.5f), -sqrtf(0.5f)}; /* Incoming vector at 45 degrees to normal */
|
||||
vec2 N = {0.0f, 1.0f}; /* Surface normal */
|
||||
vec2 dest;
|
||||
float eta;
|
||||
|
||||
/* Water to Air (eta = 1.33/1.0) */
|
||||
eta = 1.33f / 1.0f;
|
||||
GLM(vec2_refract)(I, 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
|
||||
} else {
|
||||
ASSERT(dest[0] == 0.0f && dest[1] == 0.0f); // Total internal reflection
|
||||
}
|
||||
|
||||
/* Air to Glass (eta = 1.0 / 1.5) */
|
||||
eta = 1.0f / 1.5f;
|
||||
GLM(vec2_refract)(I, N, eta, dest);
|
||||
ASSERT(dest[1] < -sqrtf(0.5f)); // Expect bending towards the normal
|
||||
|
||||
/* Glass to Water (eta = 1.5 / 1.33) */
|
||||
eta = 1.5f / 1.33f;
|
||||
GLM(vec2_refract)(I, N, eta, dest);
|
||||
ASSERT(dest[1] < -sqrtf(0.5f)); // Expect bending towards the normal, less bending than air to glass
|
||||
|
||||
/* Diamond to Air (eta = 2.42 / 1.0) */
|
||||
eta = 2.42f / 1.0f;
|
||||
GLM(vec2_refract)(I, N, eta, dest);
|
||||
if (!(dest[0] == 0.0f && dest[1] == 0.0f)) {
|
||||
/* High potential for total internal reflection, but if it occurs, expect significant bending */
|
||||
ASSERT(dest[1] < -sqrtf(0.5f));
|
||||
} else {
|
||||
ASSERT(dest[0] == 0.0f && dest[1] == 0.0f); // Total internal reflection
|
||||
}
|
||||
|
||||
TEST_SUCCESS
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user