optimize project
@@ -67,7 +67,7 @@ glm_unprojecti(mat4 invMat, vec4 vp, vec3 coord, vec4 dest) {
* You probably want to map the coordinates into object space
* so use MVP as m
*
- * Computing viewProj:
+ * Computing viewProj and MVP:
* glm_mat4_mul(proj, view, viewProj);
* glm_mat4_mul(viewProj, model, MVP);
*
@@ -87,6 +87,10 @@ glm_unproject(mat4 m, vec4 vp, vec3 coord, vec4 dest) {
/*!
* @brief map object coordinates to window coordinates
*
+ * Computing MVP:
+ * glm_mat4_mul(proj, view, viewProj);
+ * glm_mat4_mul(viewProj, model, MVP);
+ *
* @param[in] pos object coordinates
* @param[in] m MVP matrix
* @param[in] vp viewport as [x, y, width, height]
@@ -95,16 +99,18 @@ glm_unproject(mat4 m, vec4 vp, vec3 coord, vec4 dest) {
CGLM_INLINE
void
glm_project(vec3 pos, mat4 m, vec4 vp, vec3 dest) {
- vec4 pos4;
+ vec4 pos4, vone = GLM_VEC4_ONE_INIT;
glm_vec4(pos, 1.0f, pos4);
glm_mat4_mulv(m, pos4, pos4);
glm_vec4_scale(pos4, 1.0f / pos4[3], pos4); /* pos = pos / pos.w */
+ glm_vec4_add(pos4, vone, pos4);
+ glm_vec4_scale(pos4, 0.5f, pos4);
- dest[0] = (pos4[0] + 1.0f) * 0.5f * vp[2] + vp[0];
- dest[1] = (pos4[1] + 1.0f) * 0.5f * vp[3] + vp[1];
- dest[2] = (pos4[2] + 1.0f) * 0.5f;
+ dest[0] = pos4[0] * vp[2] + vp[0];
+ dest[1] = pos4[1] * vp[3] + vp[1];
+ dest[2] = pos4[2];
}
#endif /* cglm_project_h */
