mirror of
https://github.com/recp/cglm.git
synced 2026-02-17 03:39:05 +00:00
Merge pull request #219 from recp/project_zo
extend glm_project() to support ZERO_TO_ONE
This commit is contained in:
4
CREDITS
4
CREDITS
@@ -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
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,12 +11,16 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
glmc_unprojecti_no(vec3 pos, mat4 invMat, vec4 vp, vec3 dest);
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
glmc_project_no(vec3 pos, mat4 m, vec4 vp, vec3 dest);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -11,12 +11,16 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
glmc_unprojecti_zo(vec3 pos, mat4 invMat, vec4 vp, vec3 dest);
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
glmc_project_zo(vec3 pos, mat4 m, vec4 vp, vec3 dest);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "../cglm.h"
|
||||
#include "../../cglm.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
#include "../common.h"
|
||||
#include "../plane.h"
|
||||
#include "../mat4.h"
|
||||
|
||||
/*!
|
||||
* @brief set up orthographic projection matrix
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
#include "../common.h"
|
||||
#include "../plane.h"
|
||||
#include "../mat4.h"
|
||||
|
||||
/*!
|
||||
* @brief set up orthographic projection matrix with a left-hand coordinate
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
#include "../common.h"
|
||||
#include "../plane.h"
|
||||
#include "../mat4.h"
|
||||
|
||||
/*!
|
||||
* @brief set up orthographic projection matrix
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
#include "../common.h"
|
||||
#include "../plane.h"
|
||||
#include "../mat4.h"
|
||||
|
||||
/*!
|
||||
* @brief set up orthographic projection matrix with a right-hand coordinate
|
||||
|
||||
@@ -47,7 +47,6 @@
|
||||
#define cglm_persp_lh_no_h
|
||||
|
||||
#include "../common.h"
|
||||
#include "../plane.h"
|
||||
#include "persp.h"
|
||||
|
||||
/*!
|
||||
|
||||
@@ -47,7 +47,6 @@
|
||||
#define cglm_persp_lh_zo_h
|
||||
|
||||
#include "../common.h"
|
||||
#include "../plane.h"
|
||||
#include "persp.h"
|
||||
|
||||
/*!
|
||||
|
||||
@@ -47,7 +47,6 @@
|
||||
#define cglm_persp_rh_no_h
|
||||
|
||||
#include "../common.h"
|
||||
#include "../plane.h"
|
||||
#include "persp.h"
|
||||
|
||||
/*!
|
||||
|
||||
@@ -47,7 +47,6 @@
|
||||
#define cglm_persp_rh_zo_h
|
||||
|
||||
#include "../common.h"
|
||||
#include "../plane.h"
|
||||
#include "persp.h"
|
||||
|
||||
/*!
|
||||
|
||||
@@ -54,4 +54,33 @@ glm_unprojecti_no(vec3 pos, mat4 invMat, vec4 vp, vec3 dest) {
|
||||
glm_vec3(v, 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]
|
||||
* @param[out] dest projected coordinates
|
||||
*/
|
||||
CGLM_INLINE
|
||||
void
|
||||
glm_project_no(vec3 pos, mat4 m, vec4 vp, vec3 dest) {
|
||||
CGLM_ALIGN(16) vec4 pos4;
|
||||
|
||||
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_scale(pos4, 0.5f, pos4);
|
||||
glm_vec4_adds(pos4, 0.5f, pos4);
|
||||
|
||||
dest[0] = pos4[0] * vp[2] + vp[0];
|
||||
dest[1] = pos4[1] * vp[3] + vp[1];
|
||||
dest[2] = pos4[2];
|
||||
}
|
||||
|
||||
#endif /* cglm_project_no_h */
|
||||
|
||||
@@ -54,4 +54,35 @@ glm_unprojecti_zo(vec3 pos, mat4 invMat, vec4 vp, vec3 dest) {
|
||||
glm_vec3(v, 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]
|
||||
* @param[out] dest projected coordinates
|
||||
*/
|
||||
CGLM_INLINE
|
||||
void
|
||||
glm_project_zo(vec3 pos, mat4 m, vec4 vp, vec3 dest) {
|
||||
CGLM_ALIGN(16) vec4 pos4;
|
||||
|
||||
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 */
|
||||
|
||||
dest[2] = pos4[2];
|
||||
|
||||
glm_vec4_scale(pos4, 0.5f, pos4);
|
||||
glm_vec4_adds(pos4, 0.5f, pos4);
|
||||
|
||||
dest[0] = pos4[0] * vp[2] + vp[0];
|
||||
dest[1] = pos4[1] * vp[3] + vp[1];
|
||||
}
|
||||
|
||||
#endif /* cglm_project_zo_h */
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#ifndef cglm_view_lh_no_h
|
||||
#define cglm_view_lh_no_h
|
||||
|
||||
#include "../common.h"
|
||||
#include "view_lh.h"
|
||||
|
||||
/*!
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#ifndef cglm_view_lh_zo_h
|
||||
#define cglm_view_lh_zo_h
|
||||
|
||||
#include "../common.h"
|
||||
#include "view_lh.h"
|
||||
|
||||
/*!
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#ifndef cglm_view_rh_no_h
|
||||
#define cglm_view_rh_no_h
|
||||
|
||||
#include "../common.h"
|
||||
#include "view_rh.h"
|
||||
|
||||
/*!
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#ifndef cglm_view_rh_zo_h
|
||||
#define cglm_view_rh_zo_h
|
||||
|
||||
#include "../common.h"
|
||||
#include "view_rh.h"
|
||||
|
||||
/*!
|
||||
|
||||
@@ -107,18 +107,44 @@ glm_unproject(vec3 pos, mat4 m, vec4 vp, vec3 dest) {
|
||||
CGLM_INLINE
|
||||
void
|
||||
glm_project(vec3 pos, mat4 m, vec4 vp, vec3 dest) {
|
||||
CGLM_ALIGN(16) vec4 pos4, vone = GLM_VEC4_ONE_INIT;
|
||||
#if CGLM_CONFIG_CLIP_CONTROL & CGLM_CLIP_CONTROL_ZO_BIT
|
||||
glm_project_zo(pos, m, vp, dest);
|
||||
#elif CGLM_CONFIG_CLIP_CONTROL & CGLM_CLIP_CONTROL_NO_BIT
|
||||
glm_project_no(pos, m, vp, dest);
|
||||
#endif
|
||||
}
|
||||
|
||||
glm_vec4(pos, 1.0f, pos4);
|
||||
/*!
|
||||
* @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;
|
||||
|
||||
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);
|
||||
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;
|
||||
|
||||
dest[0] = pos4[0] * vp[2] + vp[0];
|
||||
dest[1] = pos4[1] * vp[3] + vp[1];
|
||||
dest[2] = pos4[2];
|
||||
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 */
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -426,7 +426,7 @@ glms_quat_lerpc(versors from, versors to, float t) {
|
||||
* @param[in] from from
|
||||
* @param[in] to to
|
||||
* @param[in] t interpolant (amount)
|
||||
* @param[out] dest result quaternion
|
||||
* @returns result quaternion
|
||||
*/
|
||||
CGLM_INLINE
|
||||
versors
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/cglm.h"
|
||||
#include "../../include/cglm/clipspace/ortho_lh_no.h"
|
||||
#include "../../include/cglm/call/clipspace/ortho_lh_no.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/cglm.h"
|
||||
#include "../../include/cglm/clipspace/ortho_lh_zo.h"
|
||||
#include "../../include/cglm/call/clipspace/ortho_lh_zo.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/cglm.h"
|
||||
#include "../../include/cglm/clipspace/ortho_rh_no.h"
|
||||
#include "../../include/cglm/call/clipspace/ortho_rh_no.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/cglm.h"
|
||||
#include "../../include/cglm/clipspace/ortho_rh_zo.h"
|
||||
#include "../../include/cglm/call/clipspace/ortho_rh_zo.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/clipspace/persp_lh_no.h"
|
||||
#include "../../include/cglm/call/clipspace/persp_lh_no.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/clipspace/persp_lh_zo.h"
|
||||
#include "../../include/cglm/call/clipspace/persp_lh_zo.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/clipspace/persp_rh_no.h"
|
||||
#include "../../include/cglm/call/clipspace/persp_rh_no.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/clipspace/persp_rh_zo.h"
|
||||
#include "../../include/cglm/call/clipspace/persp_rh_zo.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -6,9 +6,16 @@
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/clipspace/project_no.h"
|
||||
#include "../../include/cglm/call/clipspace/project_no.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
glmc_unprojecti_no(vec3 pos, mat4 invMat, vec4 vp, vec3 dest) {
|
||||
glm_unprojecti_no(pos, invMat, vp, dest);
|
||||
}
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
glmc_project_no(vec3 pos, mat4 m, vec4 vp, vec3 dest) {
|
||||
glm_project_no(pos, m, vp, dest);
|
||||
}
|
||||
|
||||
@@ -6,9 +6,16 @@
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/clipspace/project_zo.h"
|
||||
#include "../../include/cglm/call/clipspace/project_zo.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
glmc_unprojecti_zo(vec3 pos, mat4 invMat, vec4 vp, vec3 dest) {
|
||||
glm_unprojecti_zo(pos, invMat, vp, dest);
|
||||
}
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
glmc_project_zo(vec3 pos, mat4 m, vec4 vp, vec3 dest) {
|
||||
glm_project_zo(pos, m, vp, dest);
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/cglm.h"
|
||||
#include "../../include/cglm/clipspace/view_lh_no.h"
|
||||
#include "../../include/cglm/call/clipspace/view_lh_no.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/cglm.h"
|
||||
#include "../../include/cglm/clipspace/view_lh_zo.h"
|
||||
#include "../../include/cglm/call/clipspace/view_lh_zo.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/cglm.h"
|
||||
#include "../../include/cglm/clipspace/view_rh_no.h"
|
||||
#include "../../include/cglm/call/clipspace/view_rh_no.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
* Full license can be found in the LICENSE file
|
||||
*/
|
||||
|
||||
#include "../../include/cglm/cglm.h"
|
||||
#include "../../include/cglm/clipspace/view_rh_zo.h"
|
||||
#include "../../include/cglm/call/clipspace/view_rh_zo.h"
|
||||
|
||||
CGLM_EXPORT
|
||||
void
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user