math.c

Go to the documentation of this file.
00001 /* This code is (C) AllegroGL contributors, and double licensed under
00002  * the GPL and zlib licenses. See gpl.txt or zlib.txt for details.
00003  */
00015 #include <math.h>
00016 #include <allegro.h>
00017 #include "alleggl.h"
00018 #include "allglint.h"
00019 
00020 
00021 #ifndef M_PI
00022    #define M_PI   3.14159265358979323846
00023 #endif
00024 
00025 
00026 #define ALGL_NOCONV(x) x
00027 
00028 #define TRANSLATE_AL_TO_GL(al_type, gl_type, convertor) \
00029     void allegro_gl_##al_type##_to_##gl_type (al_type *m, gl_type gl[16]) \
00030     { \
00031         int col, row; \
00032         for (col = 0; col < 3; col++) \
00033             for (row = 0; row < 3; row++) \
00034                 gl[col*4+row] = convertor (m->v[col][row]); \
00035         for (row = 0; row < 3; row++) \
00036             gl[12+row] = convertor (m->t[row]); \
00037         for (col = 0; col < 3; col++) \
00038             gl[4*col + 3] = 0; \
00039         gl[15] = 1; \
00040     }
00041 
00042 
00043 
00066 TRANSLATE_AL_TO_GL(MATRIX, GLfloat, fixtof)
00067 
00068 
00069     
00070 
00082 TRANSLATE_AL_TO_GL(MATRIX, GLdouble, fixtof)
00083 
00084     
00085 
00098 TRANSLATE_AL_TO_GL(MATRIX_f, GLfloat, ALGL_NOCONV)
00099 
00100 
00101 
00114 TRANSLATE_AL_TO_GL(MATRIX_f, GLdouble, ALGL_NOCONV)
00115 
00116 
00117     
00118 #define TRANSLATE_GL_TO_AL(gl_type, al_type, convertor) \
00119     void allegro_gl_##gl_type##_to_##al_type (gl_type gl[16], al_type *m) \
00120     { \
00121         int col, row; \
00122         for (col = 0; col < 3; col++) \
00123             for (row = 0; row < 3; row++) \
00124                 m->v[col][row] = convertor (gl[col*4+row]); \
00125         for (row = 0; row < 3; row++) \
00126             m->t[row] = convertor (gl[12+row]); \
00127     }
00128 
00129 
00130 
00143 TRANSLATE_GL_TO_AL(GLfloat, MATRIX, ftofix)
00144 
00145 
00146 
00147 
00159 TRANSLATE_GL_TO_AL(GLdouble, MATRIX, ftofix)
00160 
00161 
00162 
00175 TRANSLATE_GL_TO_AL(GLfloat, MATRIX_f, ALGL_NOCONV)
00176 
00177     
00178 
00192 TRANSLATE_GL_TO_AL(GLdouble, MATRIX_f, ALGL_NOCONV)
00193 
00194 
00195 #undef ALGL_NOCONV
00196 
00197 
00198 #ifndef RAD_2_DEG
00199     #define RAD_2_DEG(a) ((a) * 180 / M_PI)
00200 #endif
00201 
00202     
00203 
00204 /* void allegro_gl_apply_quat(QUAT *q) */
00222 void allegro_gl_apply_quat(QUAT *q) {
00223 
00224     float theta;
00225     ASSERT(q);
00226     ASSERT(__allegro_gl_valid_context);
00227 
00228     theta = RAD_2_DEG(2 * acos(q->w));
00229     if (q->w < 1.0f && q->w > -1.0f) 
00230         glRotatef(theta, q->x, q->y, q->z); 
00231 
00232     return;
00233 }
00234 
00235 
00236 
00237 /* void allegro_gl_quat_to_glrotatef(QUAT *q, float *angle, float *x, float *y, float *z) */
00258 void allegro_gl_quat_to_glrotatef(QUAT *q, float *angle, float *x, float *y, float *z) {
00259 
00260     ASSERT(q);
00261     ASSERT(angle);
00262     ASSERT(x);
00263     ASSERT(y);
00264     ASSERT(z);
00265 
00266     *angle = RAD_2_DEG(2 * acos(q->w));
00267     *x = q->x;
00268     *y = q->y;
00269     *z = q->z;
00270 
00271     return;
00272 }
00273 
00274 
00275 
00276 /* void allegro_gl_quat_to_glrotated(QUAT *q, double *angle, double *x, double *y, double *z) */
00290 void allegro_gl_quat_to_glrotated(QUAT *q, double *angle, double *x, double *y, double *z) {
00291 
00292     ASSERT(q);
00293     ASSERT(angle);
00294     ASSERT(x);
00295     ASSERT(y);
00296     ASSERT(z);
00297 
00298     *angle = RAD_2_DEG(2 * acos(q->w));
00299     *x = q->x;
00300     *y = q->y;
00301     *z = q->z;
00302 
00303     return;
00304 }
00305 

Generated on Sun Nov 11 15:52:55 2007 for AllegroGL by  doxygen 1.5.2