Skip to content

Commit

Permalink
cam: extend frustum's far distance helper (#71)
Browse files Browse the repository at this point in the history
* this will help to implement zoom easily
  • Loading branch information
recp committed Jan 16, 2019
1 parent e3d3cd8 commit 07e60bd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/source/cam.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Functions:
#. :c:func:`glm_ortho_default`
#. :c:func:`glm_ortho_default_s`
#. :c:func:`glm_perspective`
#. :c:func:`glm_persp_move_far`
#. :c:func:`glm_perspective_default`
#. :c:func:`glm_perspective_resize`
#. :c:func:`glm_lookat`
Expand Down Expand Up @@ -145,6 +146,16 @@ Functions documentation
| *[in]* **farVal** far clipping planes
| *[out]* **dest** result matrix
.. c:function:: void glm_persp_move_far(mat4 proj, float deltaFar)
| extend perspective projection matrix's far distance
| this function does not guarantee far >= near, be aware of that!
Parameters:
| *[in, out]* **proj** projection matrix to extend
| *[in]* **deltaFar** distance from existing far (negative to shink)
.. c:function:: void glm_perspective_default(float aspect, mat4 dest)
| set up perspective projection matrix with default near/far
Expand Down
4 changes: 4 additions & 0 deletions include/cglm/call/cam.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ glmc_perspective(float fovy,
float farVal,
mat4 dest);

CGLM_EXPORT
void
glmc_persp_move_far(mat4 proj, float deltaFar);

CGLM_EXPORT
void
glmc_perspective_default(float aspect, mat4 dest);
Expand Down
24 changes: 24 additions & 0 deletions include/cglm/cam.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,30 @@ glm_perspective(float fovy,
dest[3][2] = 2.0f * nearVal * farVal * fn;
}

/*!
* @brief extend perspective projection matrix's far distance
*
* this function does not guarantee far >= near, be aware of that!
*
* @param[in, out] proj projection matrix to extend
* @param[in] deltaFar distance from existing far (negative to shink)
*/
CGLM_INLINE
void
glm_persp_move_far(mat4 proj, float deltaFar) {
float fn, farVal, nearVal, p22, p32;

p22 = proj[2][2];
p32 = proj[3][2];

nearVal = p32 / (p22 - 1.0f);
farVal = p32 / (p22 + 1.0f) + deltaFar;
fn = 1.0f / (nearVal - farVal);

proj[2][2] = (nearVal + farVal) * fn;
proj[3][2] = 2.0f * nearVal * farVal * fn;
}

/*!
* @brief set up perspective projection matrix with default near/far
* and angle values
Expand Down
6 changes: 6 additions & 0 deletions src/cam.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ glmc_perspective(float fovy,
dest);
}

CGLM_EXPORT
void
glmc_persp_move_far(mat4 proj, float deltaFar) {
glm_persp_move_far(proj, deltaFar);
}

CGLM_EXPORT
void
glmc_perspective_default(float aspect, mat4 dest) {
Expand Down

0 comments on commit 07e60bd

Please sign in to comment.