Fix redundant glTexEnv calls
[neverball] / share / vec3.h
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
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.
8  *
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.
13  */
14
15 #ifndef VEC_H
16 #define VEC_H
17
18 #include <math.h>
19
20 #define V_PI 3.1415927f
21
22 #define V_RAD(d) (d * V_PI / 180.f)
23 #define V_DEG(r) (r * 180.f / V_PI)
24
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 fasinf(a)     ((float) asin((double) a))
31 #define facosf(a)     ((float) acos((double) a))
32 #define fmodf(x,y)    ((float) fmod((double) x, (double) y))
33 #define fatan2f(x, y) ((float) atan2((double) x, (double) y))
34
35 /*---------------------------------------------------------------------------*/
36
37 #define v_dot(u, v)  ((u)[0] * (v)[0] + (u)[1] * (v)[1] + (u)[2] * (v)[2])
38 #define v_len(u)     fsqrtf(v_dot(u, u))
39
40 #define v_cpy(u, v) do { \
41     (u)[0] = (v)[0];     \
42     (u)[1] = (v)[1];     \
43     (u)[2] = (v)[2];     \
44 } while (0)
45
46 #define v_inv(u, v) do { \
47     (u)[0] = -(v)[0];    \
48     (u)[1] = -(v)[1];    \
49     (u)[2] = -(v)[2];    \
50 } while (0)
51
52 #define v_scl(u, v, k) do { \
53     (u)[0] = (v)[0] * (k);  \
54     (u)[1] = (v)[1] * (k);  \
55     (u)[2] = (v)[2] * (k);  \
56 } while (0)
57
58 #define v_add(u, v, w) do {   \
59     (u)[0] = (v)[0] + (w)[0]; \
60     (u)[1] = (v)[1] + (w)[1]; \
61     (u)[2] = (v)[2] + (w)[2]; \
62 } while (0)
63
64 #define v_sub(u, v, w) do {   \
65     (u)[0] = (v)[0] - (w)[0]; \
66     (u)[1] = (v)[1] - (w)[1]; \
67     (u)[2] = (v)[2] - (w)[2]; \
68 } while (0)
69
70 #define v_mid(u, v, w) do {           \
71     (u)[0] = ((v)[0] + (w)[0]) / 2.f; \
72     (u)[1] = ((v)[1] + (w)[1]) / 2.f; \
73     (u)[2] = ((v)[2] + (w)[2]) / 2.f; \
74 } while (0)
75
76 #define v_mad(u, p, v, t) do {      \
77     (u)[0] = (p)[0] + (v)[0] * (t); \
78     (u)[1] = (p)[1] + (v)[1] * (t); \
79     (u)[2] = (p)[2] + (v)[2] * (t); \
80 } while (0)
81
82 #define v_lerp(u, v, w, a) do {                    \
83     (u)[0] = (v)[0] * (1.0f - (a)) + (w)[0] * (a); \
84     (u)[1] = (v)[1] * (1.0f - (a)) + (w)[1] * (a); \
85     (u)[2] = (v)[2] * (1.0f - (a)) + (w)[2] * (a); \
86 } while (0)
87
88 #define e_cpy(d, e) do {   \
89     v_cpy((d)[0], (e)[0]); \
90     v_cpy((d)[1], (e)[1]); \
91     v_cpy((d)[2], (e)[2]); \
92 } while (0)
93
94 #define e_orthonrm(e) do {         \
95     v_crs((e)[0], (e)[1], (e)[2]); \
96     v_crs((e)[2], (e)[0], (e)[1]); \
97     v_nrm((e)[0], (e)[0]);         \
98     v_nrm((e)[1], (e)[1]);         \
99     v_nrm((e)[2], (e)[2]);         \
100 } while (0)
101
102 #define e_lerp(c, d, e, a) do {        \
103     v_lerp((c)[0], (d)[0], (e)[0], a); \
104     v_lerp((c)[1], (d)[1], (e)[1], a); \
105     v_lerp((c)[2], (d)[2], (e)[2], a); \
106     e_orthonrm(c);                     \
107 } while (0)
108
109 /*---------------------------------------------------------------------------*/
110
111 void   v_nrm(float *, const float *);
112 void   v_crs(float *, const float *, const float *);
113
114 void   m_cpy(float *, const float *);
115 void   m_xps(float *, const float *);
116 int    m_inv(float *, const float *);
117
118 void   m_ident(float *);
119 void   m_basis(float *, const float *,
120                         const float *,
121                         const float *);
122 void   m_xlt(float *, const float *);
123 void   m_scl(float *, const float *);
124 void   m_rot(float *, const float *, float);
125
126 void   m_mult(float *, const float *, const float *);
127 void   m_pxfm(float *, const float *, const float *);
128 void   m_vxfm(float *, const float *, const float *);
129
130 void   m_view(float *, const float *,
131                        const float *,
132                        const float *);
133
134 /*---------------------------------------------------------------------------*/
135
136 #define q_dot(q, r) ((q)[0] * (r)[0] + v_dot((q) + 1, (r) + 1))
137 #define q_len(q)    fsqrtf(q_dot((q), (q)))
138
139 #define q_cpy(q, r) do { \
140     (q)[0] = (r)[0];     \
141     (q)[1] = (r)[1];     \
142     (q)[2] = (r)[2];     \
143     (q)[3] = (r)[3];     \
144 } while (0)
145
146 #define q_conj(q, r) do { \
147     (q)[0] =  (r)[0];     \
148     (q)[1] = -(r)[1];     \
149     (q)[2] = -(r)[2];     \
150     (q)[3] = -(r)[3];     \
151 } while (0)
152
153 void q_as_axisangle(const float q[4], float u[3], float *a);
154 void q_by_axisangle(float q[4], const float u[3], float a);
155
156 void q_nrm(float q[4], const float r[4]);
157 void q_mul(float q[4], const float a[4], const float b[4]);
158 void q_rot(float v[3], const float r[4], const float w[3]);
159
160 void q_euler(float v[3], const float q[4]);
161 void q_slerp(float q[4], const float a[4], const float b[4], float t);
162
163 #endif