g-truc / glm

OpenGL Mathematics (GLM)

Home Page:https://glm.g-truc.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

glm_vec4_refract

gottfriedleibniz opened this issue · comments

glm_vec4_refract incorrectly computes k. Per GLSL:

k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))

Stepping through the function:

glm_vec4 const dot0 = glm_vec4_dot(N, I);                  // dot(N, I)
glm_vec4 const mul0 = _mm_mul_ps(eta, eta);                // eta * eta
glm_vec4 const mul1 = _mm_mul_ps(dot0, dot0);              // dot(N, I) * dot(N, I)
glm_vec4 const sub0 = _mm_sub_ps(_mm_set1_ps(1.0f), mul0); // 1.0 - eta * eta
glm_vec4 const sub1 = _mm_sub_ps(_mm_set1_ps(1.0f), mul1); // 1.0 - dot(N, I) * dot(N, I)
glm_vec4 const mul2 = _mm_mul_ps(sub0, sub1);              // k = (1.0 - eta * eta) * (1.0 - dot(N, I) * dot(N, I))

Which is incorrect. Additionally, the _mm_movemask_ps logic is confusing (_mm_cmplt_ss only sets the lowest element while _mm_movemask_ps creates a mask from all four elements).