1 static inline void cgm_mcopy(float *dest, const float *src)
3 memcpy(dest, src, 16 * sizeof(float));
6 static inline void cgm_mzero(float *m)
12 static inline void cgm_midentity(float *m)
14 static float id[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
18 static inline void cgm_mmul(float *a, const float *b)
27 *resptr++ = arow[0] * b[j] + arow[1] * b[4 + j] +
28 arow[2] * b[8 + j] + arow[3] * b[12 + j];
35 static inline void cgm_msubmatrix(float *m, int row, int col)
44 if(i == row) continue;
48 if(j == col) continue;
50 m[subi * 4 + subj++] = orig[i * 4 + j];
58 static inline void cgm_mupper3(float *m)
60 m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0.0f;
64 static inline float cgm_msubdet(const float *m, int row, int col)
67 float subdet00, subdet01, subdet02;
70 cgm_msubmatrix(tmp, row, col);
72 subdet00 = tmp[5] * tmp[10] - tmp[6] * tmp[9];
73 subdet01 = tmp[4] * tmp[10] - tmp[6] * tmp[8];
74 subdet02 = tmp[4] * tmp[9] - tmp[5] * tmp[8];
76 return tmp[0] * subdet00 - tmp[1] * subdet01 + tmp[2] * subdet02;
79 static inline float cgm_mcofactor(const float *m, int row, int col)
81 float min = cgm_msubdet(m, row, col);
82 return (row + col) & 1 ? -min : min;
85 static inline float cgm_mdet(const float *m)
87 return m[0] * cgm_msubdet(m, 0, 0) - m[1] * cgm_msubdet(m, 0, 1) +
88 m[2] * cgm_msubdet(m, 0, 2) - m[3] * cgm_msubdet(m, 0, 3);
91 static inline void cgm_mtranspose(float *m)
105 static inline void cgm_mcofmatrix(float *m)
114 m[i * 4 + j] = cgm_mcofactor(tmp, i, j);
119 static inline int cgm_minverse(float *m)
124 float det = cgm_mdet(m);
125 if(det == 0.0f) return -1;
126 inv_det = 1.0f / det;
132 m[i * 4 + j] = cgm_mcofactor(tmp, j, i) * inv_det; /* transposed */
138 static inline void cgm_mtranslation(float *m, float x, float y, float z)
146 static inline void cgm_mscaling(float *m, float sx, float sy, float sz)
155 static inline void cgm_mrotation_x(float *m, float angle)
157 float sa = sin(angle);
158 float ca = cos(angle);
167 static inline void cgm_mrotation_y(float *m, float angle)
169 float sa = sin(angle);
170 float ca = cos(angle);
179 static inline void cgm_mrotation_z(float *m, float angle)
181 float sa = sin(angle);
182 float ca = cos(angle);
191 static inline void cgm_mrotation_axis(float *m, int idx, float angle)
195 cgm_mrotation_x(m, angle);
198 cgm_mrotation_y(m, angle);
201 cgm_mrotation_z(m, angle);
206 static inline void cgm_mrotation(float *m, float angle, float x, float y, float z)
208 float sa = sin(angle);
209 float ca = cos(angle);
210 float invca = 1.0f - ca;
218 m[0] = xsq + (1.0f - xsq) * ca;
219 m[4] = x * y * invca - z * sa;
220 m[8] = x * z * invca + y * sa;
222 m[1] = x * y * invca + z * sa;
223 m[5] = ysq + (1.0f - ysq) * ca;
224 m[9] = y * z * invca - x * sa;
226 m[2] = x * z * invca - y * sa;
227 m[6] = y * z * invca + x * sa;
228 m[10] = zsq + (1.0f - zsq) * ca;
231 static inline void cgm_mrotation_euler(float *m, float a, float b, float c, int mode)
233 /* this array must match the EulerMode enum */
234 static const int axis[][3] = {
235 {0, 1, 2}, {0, 2, 1},
236 {1, 0, 2}, {1, 2, 0},
237 {2, 0, 1}, {2, 1, 0},
238 {2, 0, 2}, {2, 1, 2},
239 {1, 0, 1}, {1, 2, 1},
243 float ma[16], mb[16];
244 cgm_mrotation_axis(ma, axis[mode][0], a);
245 cgm_mrotation_axis(mb, axis[mode][1], b);
246 cgm_mrotation_axis(m, axis[mode][2], c);
251 static inline void cgm_mtranslate(float *m, float x, float y, float z)
254 cgm_mtranslation(tm, x, y, z);
258 static inline void cgm_mscale(float *m, float sx, float sy, float sz)
261 cgm_mscaling(sm, sx, sy, sz);
265 static inline void cgm_mrotate_x(float *m, float angle)
268 cgm_mrotation_x(rm, angle);
272 static inline void cgm_mrotate_y(float *m, float angle)
275 cgm_mrotation_y(rm, angle);
279 static inline void cgm_mrotate_z(float *m, float angle)
282 cgm_mrotation_z(rm, angle);
286 static inline void cgm_mrotate_axis(float *m, int idx, float angle)
289 cgm_mrotation_axis(rm, idx, angle);
293 static inline void cgm_mrotate(float *m, float angle, float x, float y, float z)
296 cgm_mrotation(rm, angle, x, y, z);
300 static inline void cgm_mrotate_euler(float *m, float a, float b, float c, int mode)
303 cgm_mrotation_euler(rm, a, b, c, mode);
308 static inline void cgm_mpretranslate(float *m, float x, float y, float z)
312 cgm_mtranslation(m, x, y, z);
316 static inline void cgm_mprescale(float *m, float sx, float sy, float sz)
320 cgm_mscaling(m, sx, sy, sz);
324 static inline void cgm_mprerotate_x(float *m, float angle)
328 cgm_mrotation_x(m, angle);
332 static inline void cgm_mprerotate_y(float *m, float angle)
336 cgm_mrotation_y(m, angle);
340 static inline void cgm_mprerotate_z(float *m, float angle)
344 cgm_mrotation_z(m, angle);
348 static inline void cgm_mprerotate_axis(float *m, int idx, float angle)
352 cgm_mrotation_axis(m, idx, angle);
356 static inline void cgm_mprerotate(float *m, float angle, float x, float y, float z)
360 cgm_mrotation(m, angle, x, y, z);
364 static inline void cgm_mprerotate_euler(float *m, float a, float b, float c, int mode)
368 cgm_mrotation_euler(m, a, b, c, mode);
373 static inline void cgm_mget_translation(const float *m, cgm_vec3 *res)
380 /* Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
381 * article "Quaternion Calculus and Fast Animation".
382 * adapted from: http://www.geometrictools.com/LibMathematics/Algebra/Wm5Quaternion.inl
384 static inline void cgm_mget_rotation(const float *m, cgm_quat *res)
386 static const int next[3] = {1, 2, 0};
390 float trace = m[0] + m[5] + m[10];
395 root = sqrt(trace + 1.0f); // 2w
396 res->w = 0.5f * root;
397 root = 0.5f / root; // 1 / 4w
398 res->x = (m[6] - m[9]) * root;
399 res->y = (m[8] - m[2]) * root;
400 res->z = (m[1] - m[4]) * root;
407 if(m[10] > m[i * 4 + i]) {
413 root = sqrt(m[i * 4 + i] - m[j * 4 + j] - m[k * 4 + k] + 1.0f);
414 quat[i + 1] = 0.5f * root;
416 quat[0] = (m[j + 4 + k] - m[k * 4 + j]) * root;
417 quat[j + 1] = (m[i * 4 + j] - m[j * 4 + i]) * root;
418 quat[k + 1] = (m[i * 4 + k] - m[k * 4 + i]) * root;
426 static inline void cgm_mget_scaling(const float *m, cgm_vec3 *res)
428 res->x = sqrt(m[0] * m[0] + m[4] * m[4] + m[8] * m[8]);
429 res->y = sqrt(m[1] * m[1] + m[5] * m[5] + m[9] * m[9]);
430 res->z = sqrt(m[2] * m[2] + m[6] * m[6] + m[10] * m[10]);
433 static inline void cgm_mget_frustum_plane(const float *m, int p, cgm_vec4 *res)
436 const float *rowptr = m + row * 4;
439 res->x = m[12] + rowptr[0];
440 res->y = m[13] + rowptr[1];
441 res->z = m[14] + rowptr[2];
442 res->w = m[15] + rowptr[3];
444 res->x = m[12] - rowptr[0];
445 res->y = m[13] - rowptr[1];
446 res->z = m[14] - rowptr[2];
447 res->w = m[15] - rowptr[3];
451 static inline void cgm_mlookat(float *m, const cgm_vec3 *pos, const cgm_vec3 *targ,
454 float rot[16], trans[16];
455 cgm_vec3 dir = *targ, right, vup;
458 cgm_vnormalize(&dir);
459 cgm_vcross(&right, &dir, up);
460 cgm_vnormalize(&right);
461 cgm_vcross(&vup, &right, &dir);
462 cgm_vnormalize(&vup);
469 static inline void cgm_minv_lookat(float *m, const cgm_vec3 *pos, const cgm_vec3 *targ,
472 static inline void cgm_mortho(float *m, float left, float right, float bot, float top,
473 float znear, float zfar);
474 static inline void cgm_mfrustum(float *m, float left, float right, float bot, float top,
475 float znear, float zfar);
476 static inline void cgm_mperspective(float *m, float vfov, float aspect, float znear, float zfar);
478 static inline void cgm_mmirror(float *m, float a, float b, float c, float d);