diff --git a/include/cglm/cam.h b/include/cglm/cam.h index d69de83..00e1acb 100644 --- a/include/cglm/cam.h +++ b/include/cglm/cam.h @@ -323,12 +323,28 @@ glm_persp_decomp(mat4 proj, float * __restrict bottom, float * __restrict left, float * __restrict right) { - *nearVal = proj[3][2] / (proj[2][2] - 1); - *farVal = proj[3][2] / (proj[2][2] + 1); - *bottom = *nearVal * (proj[2][1] - 1) / proj[1][1]; - *top = *nearVal * (proj[2][1] + 1) / proj[1][1]; - *left = *nearVal * (proj[2][0] - 1) / proj[0][0]; - *right = *nearVal * (proj[2][0] + 1) / proj[0][0]; + float m00, m11, m20, m21, m22, m32, n, f; + float n_m11, n_m00; + + m00 = proj[0][0]; + m11 = proj[1][1]; + m20 = proj[2][0]; + m21 = proj[2][1]; + m22 = proj[2][2]; + m32 = proj[3][2]; + + f = m32 / (m22 - 1); + n = m32 / (m22 + 1); + + n_m11 = n / m11; + n_m00 = n / m00; + + *nearVal = f; + *farVal = n; + *bottom = n_m11 * (m21 - 1); + *top = n_m11 * (m21 + 1); + *left = n_m00 * (m20 - 1); + *right = n_m00 * (m20 + 1); } /*! @@ -358,11 +374,14 @@ void glm_persp_decomp_x(mat4 proj, float * __restrict left, float * __restrict right) { - float nearVal; + float nearVal, m20, m00; + + m00 = proj[0][0]; + m20 = proj[2][0]; nearVal = proj[3][2] / (proj[3][3] - 1); - *left = nearVal * (proj[2][0] - 1) / proj[0][0]; - *right = nearVal * (proj[2][0] + 1) / proj[0][0]; + *left = nearVal * (m20 - 1) / m00; + *right = nearVal * (m20 + 1) / m00; } /*! @@ -378,11 +397,14 @@ void glm_persp_decomp_y(mat4 proj, float * __restrict top, float * __restrict bottom) { - float nearVal; + float nearVal, m21, m11; + + m21 = proj[2][1]; + m11 = proj[1][1]; nearVal = proj[3][2] / (proj[3][3] - 1); - *bottom = nearVal * (proj[2][1] - 1) / proj[1][1]; - *top = nearVal * (proj[2][1] + 1) / proj[1][1]; + *bottom = nearVal * (m21 - 1) / m11; + *top = nearVal * (m21 + 1) / m11; } /*! @@ -398,8 +420,13 @@ void glm_persp_decomp_z(mat4 proj, float * __restrict nearVal, float * __restrict farVal) { - *nearVal = proj[3][2] / (proj[2][2] - 1); - *farVal = proj[3][2] / (proj[2][2] + 1); + float m32, m22; + + m32 = proj[3][2]; + m22 = proj[2][2]; + + *nearVal = m32 / (m22 - 1); + *farVal = m32 / (m22 + 1); } /*! @@ -437,7 +464,7 @@ glm_persp_decomp_near(mat4 proj, float * __restrict nearVal) { CGLM_INLINE float glm_persp_fovy(mat4 proj) { - return 2.0 * atan(1.0 / proj[1][1]); + return 2.0f * atanf(1.0f / proj[1][1]); } /*! @@ -510,7 +537,7 @@ CGLM_INLINE void glm_frustum_corners(mat4 invMat, vec4 dest[8]) { vec4 c[8]; - vec4 ndcCorners[8] = { + vec4 csCoords[8] = { {-1.0f, -1.0f, -1.0f, 1.0f}, {-1.0f, 1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, -1.0f, 1.0f}, @@ -521,14 +548,14 @@ glm_frustum_corners(mat4 invMat, vec4 dest[8]) { { 1.0f, 1.0f, 1.0f, 1.0f} }; - glm_mat4_mulv(invMat, ndcCorners[0], c[0]); - glm_mat4_mulv(invMat, ndcCorners[1], c[1]); - glm_mat4_mulv(invMat, ndcCorners[2], c[2]); - glm_mat4_mulv(invMat, ndcCorners[3], c[3]); - glm_mat4_mulv(invMat, ndcCorners[4], c[4]); - glm_mat4_mulv(invMat, ndcCorners[5], c[5]); - glm_mat4_mulv(invMat, ndcCorners[6], c[6]); - glm_mat4_mulv(invMat, ndcCorners[7], c[7]); + glm_mat4_mulv(invMat, csCoords[0], c[0]); + glm_mat4_mulv(invMat, csCoords[1], c[1]); + glm_mat4_mulv(invMat, csCoords[2], c[2]); + glm_mat4_mulv(invMat, csCoords[3], c[3]); + glm_mat4_mulv(invMat, csCoords[4], c[4]); + glm_mat4_mulv(invMat, csCoords[5], c[5]); + glm_mat4_mulv(invMat, csCoords[6], c[6]); + glm_mat4_mulv(invMat, csCoords[7], c[7]); glm_vec4_scale(c[1], 1.0f / c[1][3], dest[1]); glm_vec4_scale(c[2], 1.0f / c[2][3], dest[2]);