2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
20 #define V_PI 3.1415927f
22 #define V_RAD(d) (d * V_PI / 180.f)
23 #define V_DEG(r) (r * 180.f / V_PI)
25 #define fsinf(a) ((float) sin((double) (a)))
26 #define fcosf(a) ((float) cos((double) (a)))
27 #define ftanf(a) ((float) tan((double) (a)))
28 #define fabsf(a) ((float) fabs((double) (a)))
29 #define fsqrtf(a) ((float) sqrt((double) (a)))
30 #define fpowf(x,y) ((float) pow((double) (x), (double) (y)))
31 #define fasinf(a) ((float) asin((double) (a)))
32 #define facosf(a) ((float) acos((double) (a)))
33 #define fmodf(x,y) ((float) fmod((double) (x), (double) (y)))
34 #define fatan2f(x, y) ((float) atan2((double) (x), (double) (y)))
36 #define flerp(f0, f1, a) ((f0) + ((f1) - (f0)) * (a))
38 /*---------------------------------------------------------------------------*/
40 #define v_dot(u, v) ((u)[0] * (v)[0] + (u)[1] * (v)[1] + (u)[2] * (v)[2])
41 #define v_len(u) fsqrtf(v_dot(u, u))
43 #define v_cpy(u, v) do { \
49 #define v_inv(u, v) do { \
55 #define v_scl(u, v, k) do { \
56 (u)[0] = (v)[0] * (k); \
57 (u)[1] = (v)[1] * (k); \
58 (u)[2] = (v)[2] * (k); \
61 #define v_add(u, v, w) do { \
62 (u)[0] = (v)[0] + (w)[0]; \
63 (u)[1] = (v)[1] + (w)[1]; \
64 (u)[2] = (v)[2] + (w)[2]; \
67 #define v_sub(u, v, w) do { \
68 (u)[0] = (v)[0] - (w)[0]; \
69 (u)[1] = (v)[1] - (w)[1]; \
70 (u)[2] = (v)[2] - (w)[2]; \
73 #define v_mid(u, v, w) do { \
74 (u)[0] = ((v)[0] + (w)[0]) / 2.f; \
75 (u)[1] = ((v)[1] + (w)[1]) / 2.f; \
76 (u)[2] = ((v)[2] + (w)[2]) / 2.f; \
79 #define v_mad(u, p, v, t) do { \
80 (u)[0] = (p)[0] + (v)[0] * (t); \
81 (u)[1] = (p)[1] + (v)[1] * (t); \
82 (u)[2] = (p)[2] + (v)[2] * (t); \
85 #define v_lerp(u, v, w, a) do { \
86 (u)[0] = flerp(v[0], w[0], a); \
87 (u)[1] = flerp(v[1], w[1], a); \
88 (u)[2] = flerp(v[2], w[2], a); \
91 #define e_cpy(d, e) do { \
92 v_cpy((d)[0], (e)[0]); \
93 v_cpy((d)[1], (e)[1]); \
94 v_cpy((d)[2], (e)[2]); \
97 #define e_orthonrm(e) do { \
98 v_crs((e)[0], (e)[1], (e)[2]); \
99 v_crs((e)[2], (e)[0], (e)[1]); \
100 v_nrm((e)[0], (e)[0]); \
101 v_nrm((e)[1], (e)[1]); \
102 v_nrm((e)[2], (e)[2]); \
105 #define e_lerp(c, d, e, a) do { \
106 v_lerp((c)[0], (d)[0], (e)[0], a); \
107 v_lerp((c)[1], (d)[1], (e)[1], a); \
108 v_lerp((c)[2], (d)[2], (e)[2], a); \
112 /*---------------------------------------------------------------------------*/
114 void v_nrm(float *, const float *);
115 void v_crs(float *, const float *, const float *);
117 void m_cpy(float *, const float *);
118 void m_xps(float *, const float *);
119 int m_inv(float *, const float *);
121 void m_ident(float *);
122 void m_basis(float *, const float *,
125 void m_xlt(float *, const float *);
126 void m_scl(float *, const float *);
127 void m_rot(float *, const float *, float);
129 void m_mult(float *, const float *, const float *);
130 void m_pxfm(float *, const float *, const float *);
131 void m_vxfm(float *, const float *, const float *);
133 /*---------------------------------------------------------------------------*/
135 #define q_dot(q, r) ((q)[0] * (r)[0] + v_dot((q) + 1, (r) + 1))
136 #define q_len(q) fsqrtf(q_dot((q), (q)))
138 #define q_cpy(q, r) do { \
145 #define q_conj(q, r) do { \
152 void q_as_axisangle(const float q
[4], float u
[3], float *a
);
153 void q_by_axisangle(float q
[4], const float u
[3], float a
);
155 void q_nrm(float q
[4], const float r
[4]);
156 void q_mul(float q
[4], const float a
[4], const float b
[4]);
157 void q_rot(float v
[3], const float r
[4], const float w
[3]);
159 void q_euler(float v
[3], const float q
[4]);
160 void q_slerp(float q
[4], const float a
[4], const float b
[4], float t
);