add unproject function

This commit is contained in:
Recep Aslantas
2018-03-08 13:02:33 +03:00
parent cfab79e546
commit 29996d0bdd
7 changed files with 148 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ extern "C" {
#include "call/frustum.h"
#include "call/box.h"
#include "call/io.h"
#include "call/project.h"
#ifdef __cplusplus
}

View 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 */

View File

@@ -23,5 +23,6 @@
#include "color.h"
#include "util.h"
#include "io.h"
#include "project.h"
#endif /* cglm_h */

84
include/cglm/project.h Normal file
View 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
View 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);
}

View File

@@ -29,6 +29,7 @@
<ClCompile Include="..\src\mat3.c" />
<ClCompile Include="..\src\mat4.c" />
<ClCompile Include="..\src\plane.c" />
<ClCompile Include="..\src\project.c" />
<ClCompile Include="..\src\quat.c" />
<ClCompile Include="..\src\vec3.c" />
<ClCompile Include="..\src\vec4.c" />
@@ -47,6 +48,7 @@
<ClInclude Include="..\include\cglm\call\mat3.h" />
<ClInclude Include="..\include\cglm\call\mat4.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\vec3.h" />
<ClInclude Include="..\include\cglm\call\vec4.h" />
@@ -60,6 +62,7 @@
<ClInclude Include="..\include\cglm\mat3.h" />
<ClInclude Include="..\include\cglm\mat4.h" />
<ClInclude Include="..\include\cglm\plane.h" />
<ClInclude Include="..\include\cglm\project.h" />
<ClInclude Include="..\include\cglm\quat.h" />
<ClInclude Include="..\include\cglm\simd\avx\affine.h" />
<ClInclude Include="..\include\cglm\simd\avx\mat4.h" />

View File

@@ -75,6 +75,9 @@
<ClCompile Include="..\src\box.c">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\project.c">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\config.h">
@@ -206,5 +209,11 @@
<ClInclude Include="..\include\cglm\color.h">
<Filter>include\cglm</Filter>
</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>
</Project>