2 gph-cmath - C graphics math library
3 Copyright (C) 2018 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software. Feel free to use, modify, and/or redistribute
6 it under the terms of the MIT/X11 license. See LICENSE for details.
7 If you intend to redistribute parts of the code without the LICENSE file
8 replace this paragraph with the full contents of the LICENSE file.
10 static inline void cgm_mcopy(float *dest, const float *src)
12 memcpy(dest, src, 16 * sizeof(float));
15 static inline void cgm_mzero(float *m)
21 static inline void cgm_midentity(float *m)
23 static float id[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
27 static inline void cgm_mmul(float *a, const float *b)
36 *resptr++ = arow[0] * b[j] + arow[1] * b[4 + j] +
37 arow[2] * b[8 + j] + arow[3] * b[12 + j];
44 static inline void cgm_msubmatrix(float *m, int row, int col)
53 if(i == row) continue;
57 if(j == col) continue;
59 m[subi * 4 + subj++] = orig[i * 4 + j];
67 static inline void cgm_mupper3(float *m)
69 m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0.0f;
73 static inline float cgm_msubdet(const float *m, int row, int col)
76 float subdet00, subdet01, subdet02;
79 cgm_msubmatrix(tmp, row, col);
81 subdet00 = tmp[5] * tmp[10] - tmp[6] * tmp[9];
82 subdet01 = tmp[4] * tmp[10] - tmp[6] * tmp[8];
83 subdet02 = tmp[4] * tmp[9] - tmp[5] * tmp[8];
85 return tmp[0] * subdet00 - tmp[1] * subdet01 + tmp[2] * subdet02;
88 static inline float cgm_mcofactor(const float *m, int row, int col)
90 float min = cgm_msubdet(m, row, col);
91 return (row + col) & 1 ? -min : min;
94 static inline float cgm_mdet(const float *m)
96 return m[0] * cgm_msubdet(m, 0, 0) - m[1] * cgm_msubdet(m, 0, 1) +
97 m[2] * cgm_msubdet(m, 0, 2) - m[3] * cgm_msubdet(m, 0, 3);
100 static inline void cgm_mtranspose(float *m)
114 static inline void cgm_mcofmatrix(float *m)
123 m[i * 4 + j] = cgm_mcofactor(tmp, i, j);
128 static inline int cgm_minverse(float *m)
133 float det = cgm_mdet(m);
134 if(det == 0.0f) return -1;
135 inv_det = 1.0f / det;
141 m[i * 4 + j] = cgm_mcofactor(tmp, j, i) * inv_det; /* transposed */
147 static inline void cgm_mtranslation(float *m, float x, float y, float z)
155 static inline void cgm_mscaling(float *m, float sx, float sy, float sz)
164 static inline void cgm_mrotation_x(float *m, float angle)
166 float sa = sin(angle);
167 float ca = cos(angle);
176 static inline void cgm_mrotation_y(float *m, float angle)
178 float sa = sin(angle);
179 float ca = cos(angle);
188 static inline void cgm_mrotation_z(float *m, float angle)
190 float sa = sin(angle);
191 float ca = cos(angle);
200 static inline void cgm_mrotation_axis(float *m, int idx, float angle)
204 cgm_mrotation_x(m, angle);
207 cgm_mrotation_y(m, angle);
210 cgm_mrotation_z(m, angle);
215 static inline void cgm_mrotation(float *m, float angle, float x, float y, float z)
217 float sa = sin(angle);
218 float ca = cos(angle);
219 float invca = 1.0f - ca;
227 m[0] = xsq + (1.0f - xsq) * ca;
228 m[4] = x * y * invca - z * sa;
229 m[8] = x * z * invca + y * sa;
231 m[1] = x * y * invca + z * sa;
232 m[5] = ysq + (1.0f - ysq) * ca;
233 m[9] = y * z * invca - x * sa;
235 m[2] = x * z * invca - y * sa;
236 m[6] = y * z * invca + x * sa;
237 m[10] = zsq + (1.0f - zsq) * ca;
240 static inline void cgm_mrotation_euler(float *m, float a, float b, float c, int mode)
242 /* this array must match the EulerMode enum */
243 static const int axis[][3] = {
244 {0, 1, 2}, {0, 2, 1},
245 {1, 0, 2}, {1, 2, 0},
246 {2, 0, 1}, {2, 1, 0},
247 {2, 0, 2}, {2, 1, 2},
248 {1, 0, 1}, {1, 2, 1},
252 float ma[16], mb[16];
253 cgm_mrotation_axis(ma, axis[mode][0], a);
254 cgm_mrotation_axis(mb, axis[mode][1], b);
255 cgm_mrotation_axis(m, axis[mode][2], c);
260 static inline void cgm_mtranslate(float *m, float x, float y, float z)
263 cgm_mtranslation(tm, x, y, z);
267 static inline void cgm_mscale(float *m, float sx, float sy, float sz)
270 cgm_mscaling(sm, sx, sy, sz);
274 static inline void cgm_mrotate_x(float *m, float angle)
277 cgm_mrotation_x(rm, angle);
281 static inline void cgm_mrotate_y(float *m, float angle)
284 cgm_mrotation_y(rm, angle);
288 static inline void cgm_mrotate_z(float *m, float angle)
291 cgm_mrotation_z(rm, angle);
295 static inline void cgm_mrotate_axis(float *m, int idx, float angle)
298 cgm_mrotation_axis(rm, idx, angle);
302 static inline void cgm_mrotate(float *m, float angle, float x, float y, float z)
305 cgm_mrotation(rm, angle, x, y, z);
309 static inline void cgm_mrotate_euler(float *m, float a, float b, float c, int mode)
312 cgm_mrotation_euler(rm, a, b, c, mode);
317 static inline void cgm_mpretranslate(float *m, float x, float y, float z)
321 cgm_mtranslation(m, x, y, z);
325 static inline void cgm_mprescale(float *m, float sx, float sy, float sz)
329 cgm_mscaling(m, sx, sy, sz);
333 static inline void cgm_mprerotate_x(float *m, float angle)
337 cgm_mrotation_x(m, angle);
341 static inline void cgm_mprerotate_y(float *m, float angle)
345 cgm_mrotation_y(m, angle);
349 static inline void cgm_mprerotate_z(float *m, float angle)
353 cgm_mrotation_z(m, angle);
357 static inline void cgm_mprerotate_axis(float *m, int idx, float angle)
361 cgm_mrotation_axis(m, idx, angle);
365 static inline void cgm_mprerotate(float *m, float angle, float x, float y, float z)
369 cgm_mrotation(m, angle, x, y, z);
373 static inline void cgm_mprerotate_euler(float *m, float a, float b, float c, int mode)
377 cgm_mrotation_euler(m, a, b, c, mode);
382 static inline void cgm_mget_translation(const float *m, cgm_vec3 *res)
389 /* Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
390 * article "Quaternion Calculus and Fast Animation".
391 * adapted from: http://www.geometrictools.com/LibMathematics/Algebra/Wm5Quaternion.inl
393 static inline void cgm_mget_rotation(const float *m, cgm_quat *res)
395 static const int next[3] = {1, 2, 0};
399 float trace = m[0] + m[5] + m[10];
404 root = sqrt(trace + 1.0f); // 2w
405 res->w = 0.5f * root;
406 root = 0.5f / root; // 1 / 4w
407 res->x = (m[6] - m[9]) * root;
408 res->y = (m[8] - m[2]) * root;
409 res->z = (m[1] - m[4]) * root;
416 if(m[10] > m[i * 4 + i]) {
422 root = sqrt(m[i * 4 + i] - m[j * 4 + j] - m[k * 4 + k] + 1.0f);
423 quat[i + 1] = 0.5f * root;
425 quat[0] = (m[j + 4 + k] - m[k * 4 + j]) * root;
426 quat[j + 1] = (m[i * 4 + j] - m[j * 4 + i]) * root;
427 quat[k + 1] = (m[i * 4 + k] - m[k * 4 + i]) * root;
435 static inline void cgm_mget_scaling(const float *m, cgm_vec3 *res)
437 res->x = sqrt(m[0] * m[0] + m[4] * m[4] + m[8] * m[8]);
438 res->y = sqrt(m[1] * m[1] + m[5] * m[5] + m[9] * m[9]);
439 res->z = sqrt(m[2] * m[2] + m[6] * m[6] + m[10] * m[10]);
442 static inline void cgm_mget_frustum_plane(const float *m, int p, cgm_vec4 *res)
445 const float *rowptr = m + row * 4;
448 res->x = m[12] + rowptr[0];
449 res->y = m[13] + rowptr[1];
450 res->z = m[14] + rowptr[2];
451 res->w = m[15] + rowptr[3];
453 res->x = m[12] - rowptr[0];
454 res->y = m[13] - rowptr[1];
455 res->z = m[14] - rowptr[2];
456 res->w = m[15] - rowptr[3];
460 static inline void cgm_mlookat(float *m, const cgm_vec3 *pos, const cgm_vec3 *targ,
464 cgm_vec3 dir = *targ, right, vup;
467 cgm_vnormalize(&dir);
468 cgm_vcross(&right, &dir, up);
469 cgm_vnormalize(&right);
470 cgm_vcross(&vup, &right, &dir);
471 cgm_vnormalize(&vup);
484 cgm_mtranslation(trans, pos->x, pos->y, pos->z);
488 static inline void cgm_minv_lookat(float *m, const cgm_vec3 *pos, const cgm_vec3 *targ,
492 cgm_vec3 dir = *targ, right, vup;
495 cgm_vnormalize(&dir);
496 cgm_vcross(&right, &dir, up);
497 cgm_vnormalize(&right);
498 cgm_vcross(&vup, &right, &dir);
499 cgm_vnormalize(&vup);
512 cgm_mtranslation(m, -pos->x, -pos->y, -pos->z);
516 static inline void cgm_mortho(float *m, float left, float right, float bot, float top,
517 float znear, float zfar)
519 float dx = right - left;
520 float dy = top - bot;
521 float dz = zfar - znear;
527 m[12] = -(right + left) / dx;
528 m[13] = -(top + bot) / dy;
529 m[14] = -(zfar + znear) / dz;
532 static inline void cgm_mfrustum(float *m, float left, float right, float bot, float top,
533 float znear, float zfar)
535 float dx = right - left;
536 float dy = top - bot;
537 float dz = zfar - znear;
540 m[0] = 2.0f * znear / dx;
541 m[5] = 2.0f * znear / dy;
542 m[8] = (right + left) / dx;
543 m[9] = (top + bot) / dy;
544 m[10] = -(zfar + znear) / dz;
545 m[14] = -2.0f * zfar * znear / dz;
549 static inline void cgm_mperspective(float *m, float vfov, float aspect, float znear, float zfar)
551 float s = 1.0f / (float)tan(vfov / 2.0f);
552 float range = znear - zfar;
557 m[10] = (znear + zfar) / range;
558 m[14] = 2.0f * znear * zfar / range;
562 static inline void cgm_mmirror(float *m, float a, float b, float c, float d)
564 m[0] = 1.0f - 2.0f * a * a;
565 m[5] = 1.0f - 2.0f * b * b;
566 m[10] = 1.0f - 2.0f * c * c;
569 m[1] = m[4] = -2.0f * a * b;
570 m[2] = m[8] = -2.0f * a * c;
571 m[6] = m[9] = -2.0f * b * c;
573 m[12] = -2.0f * a * d;
574 m[13] = -2.0f * b * d;
575 m[14] = -2.0f * c * d;
577 m[3] = m[7] = m[11] = 0.0f;