diff options
author | Ian Romanick <ian.d.romanick@intel.com> | 2009-05-04 13:23:29 -0700 |
---|---|---|
committer | Ian Romanick <ian.d.romanick@intel.com> | 2009-05-04 13:23:29 -0700 |
commit | d4b2a87674a06bc05f04dcb215b76a088adebf7a (patch) | |
tree | d678e8b20f9e88f1829649b0a31fc21533872ff8 | |
parent | a50c6490b94e3e1c54dd98f4d8455350689047f3 (diff) |
Numerous small clean-ups and fixes.
Sorry for the rubbish commit message.
-rw-r--r-- | include/glu3.h | 68 | ||||
-rw-r--r-- | include/glu3_scalar.h | 89 | ||||
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/matrix.c | 102 |
4 files changed, 225 insertions, 37 deletions
diff --git a/include/glu3.h b/include/glu3.h index 8130b3e..76254aa 100644 --- a/include/glu3.h +++ b/include/glu3.h @@ -28,10 +28,16 @@ #define GLU_VERSION_3_0 +struct GLUmat4; + struct GLUvec4 { GLfloat values[4]; #ifdef __cplusplus + inline GLUvec4(void) + { + } + inline GLUvec4(GLfloat x , GLfloat y, GLfloat z, GLfloat w) { values[0] = x; @@ -40,6 +46,14 @@ struct GLUvec4 { values[3] = w; } + inline GLUvec4(const GLUvec4 &v) + { + values[0] = v.values[0]; + values[1] = v.values[1]; + values[2] = v.values[2]; + values[3] = v.values[3]; + } + GLUvec4 operator *(const GLUmat4 &) const; GLUvec4 operator *(const GLUvec4 &) const; GLUvec4 operator *(GLfloat) const; @@ -54,6 +68,10 @@ struct GLUmat4 { struct GLUvec4 col[4]; #ifdef __cplusplus + inline GLUmat4(void) + { + } + inline GLUmat4(const GLUvec4 & c0, const GLUvec4 & c1, const GLUvec4 & c2, const GLUvec4 & c3) { @@ -63,11 +81,20 @@ struct GLUmat4 { col[3] = c3; } + inline GLUmat4(const GLUmat4 &m) + { + col[0] = m.col[0]; + col[1] = m.col[1]; + col[2] = m.col[2]; + col[3] = m.col[3]; + } + + GLUvec4 operator *(const GLUvec4 &) const; GLUmat4 operator *(const GLUmat4 &) const; GLUmat4 operator *(GLfloat) const; - GLUmat4 operator *(const GLUmat4 &) const; + GLUmat4 operator +(const GLUmat4 &) const; GLUmat4 operator -(const GLUmat4 &) const; #endif /* __cplusplus */ }; @@ -93,28 +120,20 @@ typedef struct GLUmat4Stack GLUmat4Stack; #endif /* __cplusplus */ +#ifdef __cplusplus +extern "C" { +#endif + GLfloat gluDot4_4v(const GLUvec4 *, const GLUvec4 *); GLfloat gluDot3_4v(const GLUvec4 *, const GLUvec4 *); GLfloat gluDot2_4v(const GLUvec4 *, const GLUvec4 *); -#ifdef __cplusplus -GLfloat gluDot4(const GLUvec4 &, const GLUvec4 &); -GLfloat gluDot3(const GLUvec4 &, const GLUvec4 &); -GLfloat gluDot2(const GLUvec4 &, const GLUvec4 &); -#endif - GLUvec4 gluCross4v(const GLUvec4 *, const GLUvec4 *); GLUvec4 gluNormalize4v(const GLUvec4 *); GLfloat gluLength4v(const GLUvec4 *); GLfloat gluLengthSqr4v(const GLUvec4 *); GLUmat4 gluOuter4v(const GLUvec4 *, const GLUvec4 *); -#ifdef __cplusplus -GLUvec4 gluCross(const GLUvec4 &, const GLUvec4 &); -GLUvec4 gluNormalize(const GLUvec4 &); -GLfloat gluLength(const GLUvec4 &); -GLfloat gluLengthSqr(const GLUvec4 &); -#endif /* __cplusplus */ GLUvec4 gluMult4v_4v(const GLUvec4 *, const GLUvec4 *); GLUvec4 gluDiv4v_4v(const GLUvec4 *, const GLUvec4 *); @@ -133,8 +152,29 @@ GLUmat4 gluSub4m_4m(const GLUmat4 *, const GLUmat4 *); GLUmat4 gluMult4m_f(const GLUmat4 *, GLfloat); GLUmat4 gluScale4v(const GLUvec4 *); +GLUmat4 gluTranslate3(GLfloat x, GLfloat y, GLfloat z); GLUmat4 gluTranslate4v(const GLUvec4 *); -GLUmat4 gluLookAt(const GLUvec4 *, const GLUvec4 *, const GLUvec4 *); +GLUmat4 gluRotate4v(const GLUvec4 *axis, GLfloat angle); +GLUmat4 gluLookAt4v(const GLUvec4 *, const GLUvec4 *, const GLUvec4 *); +GLUmat4 gluPerspective4(GLfloat fovy, GLfloat aspect, GLfloat near, + GLfloat far); + +extern const GLUmat4 gluIdentityMatrix; + +#ifdef __cplusplus +}; +#endif + +#ifdef __cplusplus +GLfloat gluDot4(const GLUvec4 &, const GLUvec4 &); +GLfloat gluDot3(const GLUvec4 &, const GLUvec4 &); +GLfloat gluDot2(const GLUvec4 &, const GLUvec4 &); + +GLUvec4 gluCross(const GLUvec4 &, const GLUvec4 &); +GLUvec4 gluNormalize(const GLUvec4 &); +GLfloat gluLength(const GLUvec4 &); +GLfloat gluLengthSqr(const GLUvec4 &); +#endif /* __cplusplus */ #include "glu3_scalar.h" diff --git a/include/glu3_scalar.h b/include/glu3_scalar.h index 020952a..fdb01f7 100644 --- a/include/glu3_scalar.h +++ b/include/glu3_scalar.h @@ -104,6 +104,45 @@ extern inline GLUvec4 gluSub4v_f(const GLUvec4 *v1, GLfloat f) } +extern inline GLUmat4 gluMult4m_f(const GLUmat4 *m, GLfloat f) +{ + GLUmat4 result; + + result.col[0] = gluMult4v_f(& m->col[0], f); + result.col[1] = gluMult4v_f(& m->col[1], f); + result.col[2] = gluMult4v_f(& m->col[2], f); + result.col[3] = gluMult4v_f(& m->col[3], f); + + return result; +} + + +extern inline GLUvec4 gluMult4m_4v(const GLUmat4 *m, const GLUvec4 *v) +{ + GLUvec4 a = gluMult4v_f(& m->col[0], v->values[0]); + GLUvec4 b = gluMult4v_f(& m->col[1], v->values[1]); + GLUvec4 c = gluMult4v_f(& m->col[2], v->values[2]); + GLUvec4 d = gluMult4v_f(& m->col[3], v->values[3]); + + a = gluAdd4v_4v(&a, &b); + c = gluAdd4v_4v(&c, &d); + + return gluAdd4v_4v(&a, &c); +} + + +extern inline GLUmat4 gluAdd4m_4m(const GLUmat4 *m1, const GLUmat4 *m2) +{ + GLUmat4 result; + + result.col[0] = gluAdd4v_4v(& m1->col[0], & m2->col[0]); + result.col[1] = gluAdd4v_4v(& m1->col[1], & m2->col[1]); + result.col[2] = gluAdd4v_4v(& m1->col[2], & m2->col[2]); + result.col[3] = gluAdd4v_4v(& m1->col[3], & m2->col[3]); + + return result; +} + extern inline GLfloat gluDot4_4v(const GLUvec4 *v1, const GLUvec4 *v2) { return v1->values[0] * v2->values[0] @@ -192,6 +231,33 @@ extern inline GLUmat4 gluTranspose4(const GLUmat4 *m) } +extern inline GLUmat4 gluMult4m_4m(const GLUmat4 *m1, const GLUmat4 *m2) +{ + GLUmat4 result; + unsigned i; + + for (i = 0; i < 4; i++) { + result.col[i] = gluMult4m_4v(m1, & m2->col[i]); + } + + return result; +} + + + +extern inline GLUmat4 gluTranslate3(GLfloat x, GLfloat y, GLfloat z) +{ + GLUmat4 result; + + memcpy(&result, & gluIdentityMatrix, sizeof(result)); + result.col[3].values[0] = x; + result.col[3].values[1] = y; + result.col[3].values[2] = z; + + return result; +} + + #ifdef __cplusplus extern inline GLfloat gluDot4(const GLUvec4 &v1, const GLUvec4 &v2) { @@ -255,7 +321,7 @@ inline GLUvec4 GLUvec4::operator*(GLfloat f) const inline GLUvec4 GLUvec4::operator*(const GLUmat4 &m) const { - return GLUvev4(gluDot4(*this, m.col[0]), + return GLUvec4(gluDot4(*this, m.col[0]), gluDot4(*this, m.col[1]), gluDot4(*this, m.col[2]), gluDot4(*this, m.col[3])); @@ -264,10 +330,7 @@ inline GLUvec4 GLUvec4::operator*(const GLUmat4 &m) const inline GLUmat4 GLUmat4::operator+(const GLUmat4 &m) const { - return GLUmat4(col[0] + m.col[0], - col[1] + m.col[1], - col[2] + m.col[2], - col[3] + m.col[3]); + return gluAdd4m_4m(this, &m); } @@ -282,16 +345,22 @@ inline GLUmat4 GLUmat4::operator-(const GLUmat4 &m) const inline GLUmat4 GLUmat4::operator*(GLfloat f) const { - return GLUmat4(col[0] * f, col[1] * f, col[2] * f, col[3] * f); + return gluMult4m_f(this, f); } inline GLUvec4 GLUmat4::operator*(const GLUvec4 &v) const { - return (col[0] * v.value[0]) - + (col[1] * v.value[1]) - + (col[2] * v.value[2]) - + (col[3] * v.value[3]); + return (col[0] * v.values[0]) + + (col[1] * v.values[1]) + + (col[2] * v.values[2]) + + (col[3] * v.values[3]); +} + + +inline GLUmat4 GLUmat4::operator*(const GLUmat4 &m) const +{ + return gluMult4m_4m(this, &m); } diff --git a/src/Makefile.am b/src/Makefile.am index eaf2419..974ffbd 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -25,3 +25,6 @@ AM_CFLAGS=-I../include lib_LIBRARIES = libGLU3.a libGLU3_a_SOURCES = matrix.c + +libGLU3includedir = ${includedir} +libGLU3include_HEADERS = ../include/glu3.h ../include/glu3_scalar.h diff --git a/src/matrix.c b/src/matrix.c index 0ddedd8..bf6a9df 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -24,7 +24,9 @@ #include <string.h> #include "glu3.h" -static const GLUmat4 identity = { +#define DEG2RAD(d) ((d) * M_PI / 180.0) + +const GLUmat4 gluIdentityMatrix = { { { { 1.0f, 0.0f, 0.0f, 0.0f } }, { { 0.0f, 1.0f, 0.0f, 0.0f } }, @@ -38,7 +40,7 @@ GLUmat4 gluTranslate4v(const GLUvec4 *t) { GLUmat4 result; - memcpy(& result, & identity, sizeof(identity)); + memcpy(& result, & gluIdentityMatrix, sizeof(gluIdentityMatrix)); result.col[3] = *t; result.col[3].values[3] = 1.0f; @@ -50,7 +52,7 @@ GLUmat4 gluScale4v(const GLUvec4 *t) { GLUmat4 result; - memcpy(& result, & identity, sizeof(identity)); + memcpy(& result, & gluIdentityMatrix, sizeof(gluIdentityMatrix)); result.col[0].values[0] = t->values[0]; result.col[1].values[1] = t->values[1]; result.col[2].values[2] = t->values[2]; @@ -59,9 +61,9 @@ GLUmat4 gluScale4v(const GLUvec4 *t) } -GLUmat4 gluLookAt(const GLUvec4 *eye, - const GLUvec4 *center, - const GLUvec4 *up) +GLUmat4 gluLookAt4v(const GLUvec4 *eye, + const GLUvec4 *center, + const GLUvec4 *up) { static const GLUvec4 col3 = { { 0.0f, 0.0f, 0.0f, 1.0f } }; const GLUvec4 e = { @@ -84,7 +86,10 @@ GLUmat4 gluLookAt(const GLUvec4 *eye, rotate.col[0] = s; rotate.col[1] = u; - rotate.col[2] = s; + rotate.col[2].values[0] = -f.values[0]; + rotate.col[2].values[1] = -f.values[1]; + rotate.col[2].values[2] = -f.values[2]; + rotate.col[2].values[3] = 0.0f; rotate.col[3] = col3; rotate = gluTranspose4(& rotate); @@ -92,13 +97,15 @@ GLUmat4 gluLookAt(const GLUvec4 *eye, } -GLUmat4 gluRotate(const GLUvec4 *axis, GLfloat angle) +GLUmat4 gluRotate4v(const GLUvec4 *_axis, GLfloat angle) { +#if 0 + GLUvec4 axis = gluNormalize4v(_axis); GLUmat4 E = { { - { { 0.0f, -axis->values[2], axis->values[1], 0.0f } }, - { { axis->values[3], 0.0f, -axis->values[0], 0.0f } }, - { { -axis->values[1], axis->values[0], 0.0f, 0.0f } }, + { { 0.0f, -axis.values[2], axis.values[1], 0.0f } }, + { { axis.values[3], 0.0f, -axis.values[0], 0.0f } }, + { { -axis.values[1], axis.values[0], 0.0f, 0.0f } }, { { 0.0f, 0.0f, 0.0f, 1.0f } } } }; @@ -106,8 +113,8 @@ GLUmat4 gluRotate(const GLUvec4 *axis, GLfloat angle) const GLfloat cos_a = cos(angle); const GLfloat sin_a = sin(angle); - GLUmat4 O = gluOuter4v(axis, axis); - const GLUmat4 I = gluMult4m_f(& identity, cos_a); + GLUmat4 O = gluOuter4v(&axis, &axis); + const GLUmat4 I = gluMult4m_f(& gluIdentityMatrix, cos_a); GLUmat4 temp; @@ -116,4 +123,73 @@ GLUmat4 gluRotate(const GLUvec4 *axis, GLfloat angle) temp = gluAdd4m_4m(& I, & O); return gluAdd4m_4m(& temp, & E); +#else + GLUvec4 axis = gluNormalize4v(_axis); + GLUmat4 m; + const float c = cos(angle); + const float s = sin(angle); + const float one_c = 1.0 - c; + + const float xx = axis.values[0] * axis.values[0]; + const float yy = axis.values[1] * axis.values[1]; + const float zz = axis.values[2] * axis.values[2]; + + const float xs = axis.values[0] * s; + const float ys = axis.values[1] * s; + const float zs = axis.values[2] * s; + + const float xy = axis.values[0] * axis.values[1]; + const float xz = axis.values[0] * axis.values[2]; + const float yz = axis.values[1] * axis.values[2]; + + + m.col[0].values[0] = (one_c * xx) + c; + m.col[0].values[1] = (one_c * xy) + zs; + m.col[0].values[2] = (one_c * xz) - ys; + m.col[0].values[3] = 0.0; + + m.col[1].values[0] = (one_c * xy) - zs; + m.col[1].values[1] = (one_c * yy) + c; + m.col[1].values[2] = (one_c * yz) + xs; + m.col[1].values[3] = 0.0; + + + m.col[2].values[0] = (one_c * xz) + ys; + m.col[2].values[1] = (one_c * yz) - xs; + m.col[2].values[2] = (one_c * zz) + c; + m.col[2].values[3] = 0.0; + + m.col[3].values[0] = 0.0; + m.col[3].values[1] = 0.0; + m.col[3].values[2] = 0.0; + m.col[3].values[3] = 1.0; + + return m; +#endif +} + + +GLUmat4 +gluPerspective4(GLfloat fovy, GLfloat aspect, GLfloat near, GLfloat far) +{ + GLUmat4 result; + const double sine = sin(DEG2RAD(fovy / 2.0)); + const double cosine = cos(DEG2RAD(fovy / 2.0)); + const double sine_aspect = sine * aspect; + const double dz = far - near; + + + memcpy(&result, &gluIdentityMatrix, sizeof(result)); + if ((sine == 0.0) || (dz == 0.0) || (sine_aspect == 0.0)) { + return result; + } + + result.col[0].values[0] = cosine / sine_aspect; + result.col[1].values[1] = cosine / sine; + result.col[2].values[2] = -(far + near) / dz; + result.col[2].values[3] = -1.0; + result.col[3].values[2] = -2.0 * near * far / dz; + result.col[3].values[3] = 0.0; + + return result; } |