Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a quaternion cubic bezier for cglm #417

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/source/quat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Functions:
#. :c:func:`glm_quat_nlerp`
#. :c:func:`glm_quat_slerp`
#. :c:func:`glm_quat_slerp_longest`
#. :c:func:`glm_quat_bezier`
#. :c:func:`glm_quat_look`
#. :c:func:`glm_quat_for`
#. :c:func:`glm_quat_forp`
Expand Down Expand Up @@ -363,6 +364,18 @@ Functions documentation
| *[in]* **t** interpolant (amount) clamped between 0 and 1
| *[out]* **dest** result quaternion

.. c:function:: void glm_quat_bezier(versor from, versor ctrl1, versor ctrl2, versor to, float t, versor dest);

| interpolates between two quaternions using a cubic bezier

Parameters:
| *[in]* **from** from
| *[in]* **ctrl1** control point corresponding to from
| *[in]* **ctrl2** control point corresponding to to
| *[in]* **to** to
| *[in]* **t** interpolant (amount) clamped between 0 and 1
| *[out]* **dest** result quaternion

.. c:function:: void glm_quat_look(vec3 eye, versor ori, mat4 dest)

| creates view matrix using quaternion as camera orientation
Expand Down
4 changes: 4 additions & 0 deletions include/cglm/call/quat.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ CGLM_EXPORT
void
glmc_quat_slerp_longest(versor q, versor r, float t, versor dest);

CGLM_EXPORT
void
glmc_quat_bezier(versor from, versor ctrl1, versor ctrl2, versor to, float t, versor dest);

CGLM_EXPORT
void
glmc_quat_look(vec3 eye, versor ori, mat4 dest);
Expand Down
35 changes: 35 additions & 0 deletions include/cglm/quat.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
CGLM_INLINE void glm_quat_slerp(versor q, versor r, float t, versor dest);
CGLM_INLINE void glm_quat_slerp_longest(versor q, versor r, float t, versor dest);
CGLM_INLINE void glm_quat_nlerp(versor q, versor r, float t, versor dest);
CGLM_INLINE void glm_quat_bezier(versor from,
versor ctrl1,
versor ctrl2,
versor to,
float t,
versor dest);
CGLM_INLINE void glm_quat_look(vec3 eye, versor ori, mat4 dest);
CGLM_INLINE void glm_quat_for(vec3 dir, vec3 fwd, vec3 up, versor dest);
CGLM_INLINE void glm_quat_forp(vec3 from,
Expand Down Expand Up @@ -789,6 +795,35 @@ glm_quat_slerp_longest(versor from, versor to, float t, versor dest) {
glm_vec4_scale(q1, 1.0f / sinTheta, dest);
}

/*!
* @brief interpolates between two quaternions using a cubic bezier
*
* @param[in] from from
* @param[in] ctrl1 control point corresponding to from
* @param[in] ctrl2 control point corresponding to to
* @param[in] to to
* @param[in] t interpolant (amount)
* @param[out] dest result
*/
CGLM_INLINE
void
glm_quat_bezier(versor from, versor ctrl1, versor ctrl2, versor to, float t, versor dest) {

//do normal Decasteljau bezier but replace lerp with slerp

//tried to get it into bernstein polynomial but seems more efficient this way?
//because unlike normal bezier, this one depends on angle between quaternions to slerp
CGLM_ALIGN(16) versor a, b, c, d, e;

glm_quat_slerp(from, ctrl1, t, a);
glm_quat_slerp(ctrl1, ctrl2, t, b);
glm_quat_slerp(ctrl2, to, t, c);
glm_quat_slerp(a, b, t, d);
glm_quat_slerp(b, c, t, e);
glm_quat_slerp(d, e, t, dest);

}

/*!
* @brief creates view matrix using quaternion as camera orientation
*
Expand Down
23 changes: 23 additions & 0 deletions include/cglm/struct/quat.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
CGLM_INLINE versors glms_quat_nlerp(versors from, versors to, float t)
CGLM_INLINE versors glms_quat_slerp(versors from, versors to, float t)
CGLM_INLINE versors glms_quat_slerp_longest(versors from, versors to, float t)
CGLM_INLINE versors glms_quat_bezier(versor from,
versor ctrl1,
versor ctrl2,
versor to,
float t);
CGLM_INLINE mat4s. glms_quat_look(vec3s eye, versors ori)
CGLM_INLINE versors glms_quat_for(vec3s dir, vec3s fwd, vec3s up)
CGLM_INLINE versors glms_quat_forp(vec3s from, vec3s to, vec3s fwd, vec3s up)
Expand Down Expand Up @@ -475,6 +480,24 @@ glms_quat_(slerp_longest)(versors from, versors to, float t) {
return dest;
}

/*!
* @brief interpolates between two quaternions using a cubic bezier
*
* @param[in] from from
* @param[in] ctrl1 control point corresponding to from
* @param[in] ctrl2 control point corresponding to to
* @param[in] to to
* @param[in] t interpolant (amount)
* @returns result
*/
CGLM_INLINE
versors
glms_quat_(bezier)(versors from, versors ctrl1, versors ctrl2, versors to, float t) {
versors dest;
glm_quat_bezier(from.raw, ctrl1.raw, ctrl2.raw, to.raw, t, dest.raw);
return dest;
}

/*!
* @brief creates view matrix using quaternion as camera orientation
*
Expand Down
Loading