AllegroGL  0.4.4
math.c
Go to the documentation of this file.
1 /* This code is (C) AllegroGL contributors, and double licensed under
2  * the GPL and zlib licenses. See gpl.txt or zlib.txt for details.
3  */
15 #include <math.h>
16 #include <allegro.h>
17 #include "alleggl.h"
18 #include "allglint.h"
19 
20 
21 #ifndef M_PI
22  #define M_PI 3.14159265358979323846
23 #endif
24 
25 
26 #define ALGL_NOCONV(x) x
27 
28 #define TRANSLATE_AL_TO_GL(al_type, gl_type, convertor) \
29  void allegro_gl_##al_type##_to_##gl_type (al_type *m, gl_type gl[16]) \
30  { \
31  int col, row; \
32  for (col = 0; col < 3; col++) \
33  for (row = 0; row < 3; row++) \
34  gl[col*4+row] = convertor (m->v[col][row]); \
35  for (row = 0; row < 3; row++) \
36  gl[12+row] = convertor (m->t[row]); \
37  for (col = 0; col < 3; col++) \
38  gl[4*col + 3] = 0; \
39  gl[15] = 1; \
40  }
41 
42 
43 
66 TRANSLATE_AL_TO_GL(MATRIX, GLfloat, fixtof)
67 
68 
69 
70 
82 TRANSLATE_AL_TO_GL(MATRIX, GLdouble, fixtof)
83 
84 
85 
98 TRANSLATE_AL_TO_GL(MATRIX_f, GLfloat, ALGL_NOCONV)
99 
100 
101 
114 TRANSLATE_AL_TO_GL(MATRIX_f, GLdouble, ALGL_NOCONV)
115 
116 
117 
118 #define TRANSLATE_GL_TO_AL(gl_type, al_type, convertor) \
119  void allegro_gl_##gl_type##_to_##al_type (gl_type gl[16], al_type *m) \
120  { \
121  int col, row; \
122  for (col = 0; col < 3; col++) \
123  for (row = 0; row < 3; row++) \
124  m->v[col][row] = convertor (gl[col*4+row]); \
125  for (row = 0; row < 3; row++) \
126  m->t[row] = convertor (gl[12+row]); \
127  }
128 
129 
130 
143 TRANSLATE_GL_TO_AL(GLfloat, MATRIX, ftofix)
144 
145 
146 
147 
159 TRANSLATE_GL_TO_AL(GLdouble, MATRIX, ftofix)
160 
161 
162 
175 TRANSLATE_GL_TO_AL(GLfloat, MATRIX_f, ALGL_NOCONV)
176 
177 
178 
192 TRANSLATE_GL_TO_AL(GLdouble, MATRIX_f, ALGL_NOCONV)
193 
194 
195 #undef ALGL_NOCONV
196 
197 
198 #ifndef RAD_2_DEG
199  #define RAD_2_DEG(a) ((a) * 180 / M_PI)
200 #endif
201 
202 
203 
204 /* void allegro_gl_apply_quat(QUAT *q) */
222 void allegro_gl_apply_quat(QUAT *q) {
223 
224  float theta;
225  ASSERT(q);
226  ASSERT(__allegro_gl_valid_context);
227 
228  theta = RAD_2_DEG(2 * acos(q->w));
229  if (q->w < 1.0f && q->w > -1.0f)
230  glRotatef(theta, q->x, q->y, q->z);
231 
232  return;
233 }
234 
235 
236 
237 /* void allegro_gl_quat_to_glrotatef(QUAT *q, float *angle, float *x, float *y, float *z) */
258 void allegro_gl_quat_to_glrotatef(QUAT *q, float *angle, float *x, float *y, float *z) {
259 
260  ASSERT(q);
261  ASSERT(angle);
262  ASSERT(x);
263  ASSERT(y);
264  ASSERT(z);
265 
266  *angle = RAD_2_DEG(2 * acos(q->w));
267  *x = q->x;
268  *y = q->y;
269  *z = q->z;
270 
271  return;
272 }
273 
274 
275 
276 /* void allegro_gl_quat_to_glrotated(QUAT *q, double *angle, double *x, double *y, double *z) */
290 void allegro_gl_quat_to_glrotated(QUAT *q, double *angle, double *x, double *y, double *z) {
291 
292  ASSERT(q);
293  ASSERT(angle);
294  ASSERT(x);
295  ASSERT(y);
296  ASSERT(z);
297 
298  *angle = RAD_2_DEG(2 * acos(q->w));
299  *x = q->x;
300  *y = q->y;
301  *z = q->z;
302 
303  return;
304 }
305 
void allegro_gl_apply_quat(QUAT *q)
Converts a quaternion to a vector/angle, which can be used with glRotate*().
void allegro_gl_quat_to_glrotatef(QUAT *q, float *angle, float *x, float *y, float *z)
Converts a quaternion to a vector/angle, which can be used with glRotate*().
Definition: math.c:250
Main header file for AllegroGL.
void allegro_gl_quat_to_glrotated(QUAT *q, double *angle, double *x, double *y, double *z)
Converts a quaternion to a vector/angle, which can be used with glRotate*().
Definition: math.c:282