diff --git a/glslUtil/glslUtil.c b/glslUtil/glslUtil.c index f7b716d..f681c15 100644 --- a/glslUtil/glslUtil.c +++ b/glslUtil/glslUtil.c @@ -1,4 +1,5 @@ +#include #include #include "glslUtil.h" @@ -58,6 +59,41 @@ void guMatrixScale(guMatrix4x4 *m, GLfloat x, GLfloat y, GLfloat z) } } +void guMatrixRotate(guMatrix4x4 *m, GLfloat angle, + GLfloat x, GLfloat y, GLfloat z) +{ + GLfloat lx = x, ly = y, lz = z, p; + p = lx*lx + ly*ly + lz*lz; + if (p != 1.0) + { + GLfloat scale = sqrt(p); + lx /= scale; + ly /= scale; + lz /= scale; + } + GLfloat c = cos(angle); + GLfloat s = sin(angle); + guMatrix4x4 mult; + GLfloat oc = 1 - c; + mult[0][0] = lx * lx * oc + c; + mult[0][1] = lx * ly * oc - lz * s; + mult[0][2] = lx * lz * oc + ly * s; + mult[0][3] = 0.0; + mult[1][0] = ly * lx * oc + lz * s; + mult[1][1] = ly * ly * oc + c; + mult[1][2] = ly * lz * oc - lx * s; + mult[1][3] = 0.0; + mult[2][0] = lx * lz * oc - ly * s; + mult[2][1] = ly * lz * oc + lx * s; + mult[2][2] = lz * lz * oc + c; + mult[2][3] = 0.0; + mult[3][0] = 0.0; + mult[3][1] = 0.0; + mult[3][2] = 0.0; + mult[3][3] = 1.0; + guMatrixMult(m, m, &mult); +} + void guMatrixFrustum(guMatrix4x4 *m, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, diff --git a/glslUtil/glslUtil.h b/glslUtil/glslUtil.h index 1d961d2..ec03737 100644 --- a/glslUtil/glslUtil.h +++ b/glslUtil/glslUtil.h @@ -18,6 +18,8 @@ void guMatrixLoadIdentity(guMatrix4x4 *m); void guMatrixMult(guMatrix4x4 *m, guMatrix4x4 *a, guMatrix4x4 *b); void guMatrixTranslate(guMatrix4x4 *m, GLfloat x, GLfloat y, GLfloat z); void guMatrixScale(guMatrix4x4 *m, GLfloat x, GLfloat y, GLfloat z); +void guMatrixRotate(guMatrix4x4 *m, GLfloat angle, + GLfloat x, GLfloat y, GLfloat z); void guMatrixFrustum(guMatrix4x4 *m, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top,