pick matrix helper (aka gluPickMatrix)

This commit is contained in:
Recep Aslantas
2021-08-16 16:53:46 +03:00
parent a2bd00df32
commit 8427d02a9b
5 changed files with 63 additions and 0 deletions

View File

@@ -78,3 +78,7 @@ https://stackoverflow.com/a/57793352/2676533
16. ARM NEON Div
http://github.com/microsoft/DirectXMath
17. Pick Matrix
glu project -> project.c

View File

@@ -25,6 +25,10 @@ CGLM_EXPORT
void
glmc_project(vec3 pos, mat4 m, vec4 vp, vec3 dest);
CGLM_EXPORT
void
glmc_pickmatrix(vec2 center, vec2 size, vec4 vp, mat4 dest);
#ifdef __cplusplus
}
#endif

View File

@@ -114,4 +114,37 @@ glm_project(vec3 pos, mat4 m, vec4 vp, vec3 dest) {
#endif
}
/*!
* @brief define a picking region
*
* @param[in] center center [x, y] of a picking region in window coordinates
* @param[in] size size [width, height] of the picking region in window coordinates
* @param[in] vp viewport as [x, y, width, height]
* @param[out] dest projected coordinates
*/
CGLM_INLINE
void
glm_pickmatrix(vec3 center, vec2 size, vec4 vp, mat4 dest) {
mat4 res;
vec3 v;
if (size[0] <= 0.0f || size[1] <= 0.0f)
return;
/* Translate and scale the picked region to the entire window */
v[0] = (vp[2] - 2.0f * (center[0] - vp[0])) / size[0];
v[1] = (vp[3] - 2.0f * (center[1] - vp[1])) / size[1];
v[2] = 0.0f;
glm_translate_make(res, v);
v[0] = vp[2] / size[0];
v[1] = vp[3] / size[1];
v[2] = 1.0f;
glm_scale(res, v);
glm_mat4_copy(res, dest);
}
#endif /* cglm_project_h */

View File

@@ -101,4 +101,20 @@ glms_project(vec3s pos, mat4s m, vec4s vp) {
return r;
}
/*!
* @brief define a picking region
*
* @param[in] center center [x, y] of a picking region in window coordinates
* @param[in] size size [width, height] of the picking region in window coordinates
* @param[in] vp viewport as [x, y, width, height]
* @returns projected coordinates
*/
CGLM_INLINE
mat4s
glms_pickmatrix(vec3s center, vec2s size, vec4s vp) {
mat4s res;
glm_pickmatrix(center.raw, size.raw, vp.raw, res.raw);
return res;
}
#endif /* cglms_projects_h */

View File

@@ -25,3 +25,9 @@ void
glmc_project(vec3 pos, mat4 m, vec4 vp, vec3 dest) {
glm_project(pos, m, vp, dest);
}
CGLM_EXPORT
void
glmc_pickmatrix(vec2 center, vec2 size, vec4 vp, mat4 dest) {
glm_pickmatrix(center, size, vp, dest);
}