From ce69ecaa5ad06b8d3a0167d24c872720bd318f1e Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Tue, 23 Aug 2022 15:31:55 +0300 Subject: [PATCH] struct: project_no and project_zo apis for struct api --- Makefile.am | 4 +- include/cglm/struct/clipspace/project_no.h | 76 ++++++++++++++++++++++ include/cglm/struct/clipspace/project_zo.h | 76 ++++++++++++++++++++++ 3 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 include/cglm/struct/clipspace/project_no.h create mode 100644 include/cglm/struct/clipspace/project_zo.h diff --git a/Makefile.am b/Makefile.am index b7c9470..0a36c72 100644 --- a/Makefile.am +++ b/Makefile.am @@ -191,7 +191,9 @@ cglm_struct_clipspace_HEADERS = include/cglm/struct/clipspace/persp_lh_no.h \ include/cglm/struct/clipspace/view_lh_no.h \ include/cglm/struct/clipspace/view_lh_zo.h \ include/cglm/struct/clipspace/view_rh_no.h \ - include/cglm/struct/clipspace/view_rh_zo.h + include/cglm/struct/clipspace/view_rh_zo.h \ + include/cglm/struct/clipspace/project_no.h \ + include/cglm/struct/clipspace/project_zo.h libcglm_la_SOURCES=\ src/euler.c \ diff --git a/include/cglm/struct/clipspace/project_no.h b/include/cglm/struct/clipspace/project_no.h new file mode 100644 index 0000000..4855a5e --- /dev/null +++ b/include/cglm/struct/clipspace/project_no.h @@ -0,0 +1,76 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Functions: + CGLM_INLINE vec3s glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) + CGLM_INLINE vec3s glms_project_no(vec3s pos, mat4s m, vec4s vp, vec3s dest) + */ + +#ifndef cglms_project_no_h +#define cglms_project_no_h + +#include "../../common.h" +#include "../../types-struct.h" +#include "../../plane.h" +#include "../../cam.h" + +/*! + * @brief maps the specified viewport coordinates into specified space [1] + * the matrix should contain projection matrix. + * + * if you don't have ( and don't want to have ) an inverse matrix then use + * glm_unproject version. You may use existing inverse of matrix in somewhere + * else, this is why glm_unprojecti exists to save save inversion cost + * + * [1] space: + * 1- if m = invProj: View Space + * 2- if m = invViewProj: World Space + * 3- if m = invMVP: Object Space + * + * You probably want to map the coordinates into object space + * so use invMVP as m + * + * Computing viewProj: + * glm_mat4_mul(proj, view, viewProj); + * glm_mat4_mul(viewProj, model, MVP); + * glm_mat4_inv(viewProj, invMVP); + * + * @param[in] pos point/position in viewport coordinates + * @param[in] invMat matrix (see brief) + * @param[in] vp viewport as [x, y, width, height] + * @returns unprojected coordinates + */ +CGLM_INLINE +vec3s +glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) { + vec3s dest; + glm_unprojecti_no(pos.raw, invMat.raw, vp.raw, dest.raw); + return 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] + * @returns projected coordinates + */ +CGLM_INLINE +vec3s +glms_project_no(vec3s pos, mat4s m, vec4s vp, vec3s dest) { + vec3s dest; + glm_project_no(pos.raw, m.raw, vp.raw, dest.raw); + return dest; +} + +#endif /* cglms_project_rh_no_h */ diff --git a/include/cglm/struct/clipspace/project_zo.h b/include/cglm/struct/clipspace/project_zo.h new file mode 100644 index 0000000..93117e9 --- /dev/null +++ b/include/cglm/struct/clipspace/project_zo.h @@ -0,0 +1,76 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Functions: + CGLM_INLINE vec3s glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) + CGLM_INLINE vec3s glms_project_no(vec3s pos, mat4s m, vec4s vp, vec3s dest) + */ + +#ifndef cglms_project_zo_h +#define cglms_project_zo_h + +#include "../../common.h" +#include "../../types-struct.h" +#include "../../plane.h" +#include "../../cam.h" + +/*! + * @brief maps the specified viewport coordinates into specified space [1] + * the matrix should contain projection matrix. + * + * if you don't have ( and don't want to have ) an inverse matrix then use + * glm_unproject version. You may use existing inverse of matrix in somewhere + * else, this is why glm_unprojecti exists to save save inversion cost + * + * [1] space: + * 1- if m = invProj: View Space + * 2- if m = invViewProj: World Space + * 3- if m = invMVP: Object Space + * + * You probably want to map the coordinates into object space + * so use invMVP as m + * + * Computing viewProj: + * glm_mat4_mul(proj, view, viewProj); + * glm_mat4_mul(viewProj, model, MVP); + * glm_mat4_inv(viewProj, invMVP); + * + * @param[in] pos point/position in viewport coordinates + * @param[in] invMat matrix (see brief) + * @param[in] vp viewport as [x, y, width, height] + * @returns unprojected coordinates + */ +CGLM_INLINE +vec3s +glms_unprojecti_zo(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) { + vec3s dest; + glm_unprojecti_zo(pos.raw, invMat.raw, vp.raw, dest.raw); + return 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] + * @returns projected coordinates + */ +CGLM_INLINE +vec3s +glms_project_zo(vec3s pos, mat4s m, vec4s vp, vec3 dest) { + vec3s dest; + glm_project_zo(pos.raw, m.raw, vp.raw, dest.raw); + return dest; +} + +#endif /* cglm_project_zo_h */