mirror of
https://github.com/recp/cglm.git
synced 2025-10-04 09:08:53 +00:00
add unproject function
This commit is contained in:
@@ -24,6 +24,7 @@ extern "C" {
|
|||||||
#include "call/frustum.h"
|
#include "call/frustum.h"
|
||||||
#include "call/box.h"
|
#include "call/box.h"
|
||||||
#include "call/io.h"
|
#include "call/io.h"
|
||||||
|
#include "call/project.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
29
include/cglm/call/project.h
Normal file
29
include/cglm/call/project.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c), Recep Aslantas.
|
||||||
|
*
|
||||||
|
* MIT License (MIT), http://opensource.org/licenses/MIT
|
||||||
|
* Full license can be found in the LICENSE file
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef cglmc_project_h
|
||||||
|
#define cglmc_project_h
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "../cglm.h"
|
||||||
|
|
||||||
|
CGLM_EXPORT
|
||||||
|
void
|
||||||
|
glmc_unprojecti(mat4 invMat, vec4 vp, vec3 coord, vec3 dest);
|
||||||
|
|
||||||
|
CGLM_EXPORT
|
||||||
|
void
|
||||||
|
glmc_unproject(mat4 m, vec2 vp, vec3 coord, vec3 dest);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif /* cglmc_project_h */
|
||||||
|
|
||||||
|
|
@@ -23,5 +23,6 @@
|
|||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
#include "project.h"
|
||||||
|
|
||||||
#endif /* cglm_h */
|
#endif /* cglm_h */
|
||||||
|
84
include/cglm/project.h
Normal file
84
include/cglm/project.h
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c), Recep Aslantas.
|
||||||
|
*
|
||||||
|
* MIT License (MIT), http://opensource.org/licenses/MIT
|
||||||
|
* Full license can be found in the LICENSE file
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef cglm_project_h
|
||||||
|
#define cglm_project_h
|
||||||
|
|
||||||
|
#include "mat4.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] invMat matrix (see brief)
|
||||||
|
* @param[in] vp viewport as [x, y, width, height]
|
||||||
|
* @param[in] coord viewport coordinates
|
||||||
|
* @param[out] dest unprojected coordinates
|
||||||
|
*/
|
||||||
|
CGLM_INLINE
|
||||||
|
void
|
||||||
|
glm_unprojecti(mat4 invMat, vec4 vp, vec3 coord, vec4 dest) {
|
||||||
|
vec4 v;
|
||||||
|
|
||||||
|
v[0] = 2.0f * (coord[0] - vp[0]) / vp[2] - 1.0f;
|
||||||
|
v[1] = 2.0f * (coord[1] - vp[1]) / vp[3] - 1.0f;
|
||||||
|
v[2] = 2.0f * coord[2] - 1.0f;
|
||||||
|
v[3] = 1.0f;
|
||||||
|
|
||||||
|
glm_mat4_mulv(invMat, v, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @brief maps the specified viewport coordinates into specified space [1]
|
||||||
|
* the matrix should contain projection matrix.
|
||||||
|
*
|
||||||
|
* this is same as glm_unprojecti except this function get inverse matrix for
|
||||||
|
* you.
|
||||||
|
*
|
||||||
|
* [1] space:
|
||||||
|
* 1- if m = proj: View Space
|
||||||
|
* 2- if m = viewProj: World Space
|
||||||
|
* 3- if m = MVP: Object Space
|
||||||
|
*
|
||||||
|
* You probably want to map the coordinates into object space
|
||||||
|
* so use MVP as m
|
||||||
|
*
|
||||||
|
* Computing viewProj:
|
||||||
|
* glm_mat4_mul(proj, view, viewProj);
|
||||||
|
* glm_mat4_mul(viewProj, model, MVP);
|
||||||
|
*
|
||||||
|
* @param[in] m matrix (see brief)
|
||||||
|
* @param[in] vp viewport as [x, y, width, height]
|
||||||
|
* @param[in] coord viewport coordinates
|
||||||
|
* @param[out] dest unprojected coordinates
|
||||||
|
*/
|
||||||
|
CGLM_INLINE
|
||||||
|
void
|
||||||
|
glm_unproject(mat4 m, vec2 vp, vec3 coord, vec3 dest) {
|
||||||
|
mat4 inv;
|
||||||
|
glm_mat4_inv(m, inv);
|
||||||
|
glm_unprojecti(inv, vp, coord, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* cglm_project_h */
|
21
src/project.c
Normal file
21
src/project.c
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c), Recep Aslantas.
|
||||||
|
*
|
||||||
|
* MIT License (MIT), http://opensource.org/licenses/MIT
|
||||||
|
* Full license can be found in the LICENSE file
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "../include/cglm/cglm.h"
|
||||||
|
#include "../include/cglm/call.h"
|
||||||
|
|
||||||
|
CGLM_EXPORT
|
||||||
|
void
|
||||||
|
glmc_unprojecti(mat4 invMat, vec4 vp, vec3 coord, vec3 dest) {
|
||||||
|
glm_unprojecti(invMat, vp, coord, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
CGLM_EXPORT
|
||||||
|
void
|
||||||
|
glmc_unproject(mat4 m, vec2 vp, vec3 coord, vec3 dest) {
|
||||||
|
glm_unproject(m, vp, coord, dest);
|
||||||
|
}
|
@@ -29,6 +29,7 @@
|
|||||||
<ClCompile Include="..\src\mat3.c" />
|
<ClCompile Include="..\src\mat3.c" />
|
||||||
<ClCompile Include="..\src\mat4.c" />
|
<ClCompile Include="..\src\mat4.c" />
|
||||||
<ClCompile Include="..\src\plane.c" />
|
<ClCompile Include="..\src\plane.c" />
|
||||||
|
<ClCompile Include="..\src\project.c" />
|
||||||
<ClCompile Include="..\src\quat.c" />
|
<ClCompile Include="..\src\quat.c" />
|
||||||
<ClCompile Include="..\src\vec3.c" />
|
<ClCompile Include="..\src\vec3.c" />
|
||||||
<ClCompile Include="..\src\vec4.c" />
|
<ClCompile Include="..\src\vec4.c" />
|
||||||
@@ -47,6 +48,7 @@
|
|||||||
<ClInclude Include="..\include\cglm\call\mat3.h" />
|
<ClInclude Include="..\include\cglm\call\mat3.h" />
|
||||||
<ClInclude Include="..\include\cglm\call\mat4.h" />
|
<ClInclude Include="..\include\cglm\call\mat4.h" />
|
||||||
<ClInclude Include="..\include\cglm\call\plane.h" />
|
<ClInclude Include="..\include\cglm\call\plane.h" />
|
||||||
|
<ClInclude Include="..\include\cglm\call\project.h" />
|
||||||
<ClInclude Include="..\include\cglm\call\quat.h" />
|
<ClInclude Include="..\include\cglm\call\quat.h" />
|
||||||
<ClInclude Include="..\include\cglm\call\vec3.h" />
|
<ClInclude Include="..\include\cglm\call\vec3.h" />
|
||||||
<ClInclude Include="..\include\cglm\call\vec4.h" />
|
<ClInclude Include="..\include\cglm\call\vec4.h" />
|
||||||
@@ -60,6 +62,7 @@
|
|||||||
<ClInclude Include="..\include\cglm\mat3.h" />
|
<ClInclude Include="..\include\cglm\mat3.h" />
|
||||||
<ClInclude Include="..\include\cglm\mat4.h" />
|
<ClInclude Include="..\include\cglm\mat4.h" />
|
||||||
<ClInclude Include="..\include\cglm\plane.h" />
|
<ClInclude Include="..\include\cglm\plane.h" />
|
||||||
|
<ClInclude Include="..\include\cglm\project.h" />
|
||||||
<ClInclude Include="..\include\cglm\quat.h" />
|
<ClInclude Include="..\include\cglm\quat.h" />
|
||||||
<ClInclude Include="..\include\cglm\simd\avx\affine.h" />
|
<ClInclude Include="..\include\cglm\simd\avx\affine.h" />
|
||||||
<ClInclude Include="..\include\cglm\simd\avx\mat4.h" />
|
<ClInclude Include="..\include\cglm\simd\avx\mat4.h" />
|
||||||
|
@@ -75,6 +75,9 @@
|
|||||||
<ClCompile Include="..\src\box.c">
|
<ClCompile Include="..\src\box.c">
|
||||||
<Filter>src</Filter>
|
<Filter>src</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\src\project.c">
|
||||||
|
<Filter>src</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\src\config.h">
|
<ClInclude Include="..\src\config.h">
|
||||||
@@ -206,5 +209,11 @@
|
|||||||
<ClInclude Include="..\include\cglm\color.h">
|
<ClInclude Include="..\include\cglm\color.h">
|
||||||
<Filter>include\cglm</Filter>
|
<Filter>include\cglm</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\include\cglm\project.h">
|
||||||
|
<Filter>include\cglm</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\include\cglm\call\project.h">
|
||||||
|
<Filter>include\cglm\call</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Reference in New Issue
Block a user