disable double/float conversion warnings in matrixlib on MSVS
[divverent/darkplaces.git] / matrixlib.c
1
2 #include <math.h>
3 #include "matrixlib.h"
4
5 #ifdef _MSC_VER
6 #pragma warning(disable : 4244)     // LordHavoc: MSVC++ 4 x86, double/float
7 #pragma warning(disable : 4305)         // LordHavoc: MSVC++ 6 x86, double/float
8 #endif
9
10 const matrix4x4_t identitymatrix =
11 {
12         {
13                 {1, 0, 0, 0},
14                 {0, 1, 0, 0},
15                 {0, 0, 1, 0},
16                 {0, 0, 0, 1}
17         }
18 };
19
20 void Matrix4x4_Copy (matrix4x4_t *out, const matrix4x4_t *in)
21 {
22         *out = *in;
23 }
24
25 void Matrix4x4_CopyRotateOnly (matrix4x4_t *out, const matrix4x4_t *in)
26 {
27         out->m[0][0] = in->m[0][0];
28         out->m[0][1] = in->m[0][1];
29         out->m[0][2] = in->m[0][2];
30         out->m[0][3] = 0.0f;
31         out->m[1][0] = in->m[1][0];
32         out->m[1][1] = in->m[1][1];
33         out->m[1][2] = in->m[1][2];
34         out->m[1][3] = 0.0f;
35         out->m[2][0] = in->m[2][0];
36         out->m[2][1] = in->m[2][1];
37         out->m[2][2] = in->m[2][2];
38         out->m[2][3] = 0.0f;
39         out->m[3][0] = 0.0f;
40         out->m[3][1] = 0.0f;
41         out->m[3][2] = 0.0f;
42         out->m[3][3] = 1.0f;
43 }
44
45 void Matrix4x4_CopyTranslateOnly (matrix4x4_t *out, const matrix4x4_t *in)
46 {
47 #ifdef MATRIX4x4_OPENGLORIENTATION
48         out->m[0][0] = 1.0f;
49         out->m[1][0] = 0.0f;
50         out->m[2][0] = 0.0f;
51         out->m[3][0] = in->m[0][3];
52         out->m[0][1] = 0.0f;
53         out->m[1][1] = 1.0f;
54         out->m[2][1] = 0.0f;
55         out->m[3][1] = in->m[1][3];
56         out->m[0][2] = 0.0f;
57         out->m[1][2] = 0.0f;
58         out->m[2][2] = 1.0f;
59         out->m[3][2] = in->m[2][3];
60         out->m[0][3] = 0.0f;
61         out->m[1][3] = 0.0f;
62         out->m[2][3] = 0.0f;
63         out->m[3][3] = 1.0f;
64 #else
65         out->m[0][0] = 1.0f;
66         out->m[0][1] = 0.0f;
67         out->m[0][2] = 0.0f;
68         out->m[0][3] = in->m[0][3];
69         out->m[1][0] = 0.0f;
70         out->m[1][1] = 1.0f;
71         out->m[1][2] = 0.0f;
72         out->m[1][3] = in->m[1][3];
73         out->m[2][0] = 0.0f;
74         out->m[2][1] = 0.0f;
75         out->m[2][2] = 1.0f;
76         out->m[2][3] = in->m[2][3];
77         out->m[3][0] = 0.0f;
78         out->m[3][1] = 0.0f;
79         out->m[3][2] = 0.0f;
80         out->m[3][3] = 1.0f;
81 #endif
82 }
83
84 void Matrix4x4_Concat (matrix4x4_t *out, const matrix4x4_t *in1, const matrix4x4_t *in2)
85 {
86 #ifdef MATRIX4x4_OPENGLORIENTATION
87         out->m[0][0] = in1->m[0][0] * in2->m[0][0] + in1->m[1][0] * in2->m[0][1] + in1->m[2][0] * in2->m[0][2] + in1->m[3][0] * in2->m[0][3];
88         out->m[1][0] = in1->m[0][0] * in2->m[1][0] + in1->m[1][0] * in2->m[1][1] + in1->m[2][0] * in2->m[1][2] + in1->m[3][0] * in2->m[1][3];
89         out->m[2][0] = in1->m[0][0] * in2->m[2][0] + in1->m[1][0] * in2->m[2][1] + in1->m[2][0] * in2->m[2][2] + in1->m[3][0] * in2->m[2][3];
90         out->m[3][0] = in1->m[0][0] * in2->m[3][0] + in1->m[1][0] * in2->m[3][1] + in1->m[2][0] * in2->m[3][2] + in1->m[3][0] * in2->m[3][3];
91         out->m[0][1] = in1->m[0][1] * in2->m[0][0] + in1->m[1][1] * in2->m[0][1] + in1->m[2][1] * in2->m[0][2] + in1->m[3][1] * in2->m[0][3];
92         out->m[1][1] = in1->m[0][1] * in2->m[1][0] + in1->m[1][1] * in2->m[1][1] + in1->m[2][1] * in2->m[1][2] + in1->m[3][1] * in2->m[1][3];
93         out->m[2][1] = in1->m[0][1] * in2->m[2][0] + in1->m[1][1] * in2->m[2][1] + in1->m[2][1] * in2->m[2][2] + in1->m[3][1] * in2->m[2][3];
94         out->m[3][1] = in1->m[0][1] * in2->m[3][0] + in1->m[1][1] * in2->m[3][1] + in1->m[2][1] * in2->m[3][2] + in1->m[3][1] * in2->m[3][3];
95         out->m[0][2] = in1->m[0][2] * in2->m[0][0] + in1->m[1][2] * in2->m[0][1] + in1->m[2][2] * in2->m[0][2] + in1->m[3][2] * in2->m[0][3];
96         out->m[1][2] = in1->m[0][2] * in2->m[1][0] + in1->m[1][2] * in2->m[1][1] + in1->m[2][2] * in2->m[1][2] + in1->m[3][2] * in2->m[1][3];
97         out->m[2][2] = in1->m[0][2] * in2->m[2][0] + in1->m[1][2] * in2->m[2][1] + in1->m[2][2] * in2->m[2][2] + in1->m[3][2] * in2->m[2][3];
98         out->m[3][2] = in1->m[0][2] * in2->m[3][0] + in1->m[1][2] * in2->m[3][1] + in1->m[2][2] * in2->m[3][2] + in1->m[3][2] * in2->m[3][3];
99         out->m[0][3] = in1->m[0][3] * in2->m[0][0] + in1->m[1][3] * in2->m[0][1] + in1->m[2][3] * in2->m[0][2] + in1->m[3][3] * in2->m[0][3];
100         out->m[1][3] = in1->m[0][3] * in2->m[1][0] + in1->m[1][3] * in2->m[1][1] + in1->m[2][3] * in2->m[1][2] + in1->m[3][3] * in2->m[1][3];
101         out->m[2][3] = in1->m[0][3] * in2->m[2][0] + in1->m[1][3] * in2->m[2][1] + in1->m[2][3] * in2->m[2][2] + in1->m[3][3] * in2->m[2][3];
102         out->m[3][3] = in1->m[0][3] * in2->m[3][0] + in1->m[1][3] * in2->m[3][1] + in1->m[2][3] * in2->m[3][2] + in1->m[3][3] * in2->m[3][3];
103 #else
104         out->m[0][0] = in1->m[0][0] * in2->m[0][0] + in1->m[0][1] * in2->m[1][0] + in1->m[0][2] * in2->m[2][0] + in1->m[0][3] * in2->m[3][0];
105         out->m[0][1] = in1->m[0][0] * in2->m[0][1] + in1->m[0][1] * in2->m[1][1] + in1->m[0][2] * in2->m[2][1] + in1->m[0][3] * in2->m[3][1];
106         out->m[0][2] = in1->m[0][0] * in2->m[0][2] + in1->m[0][1] * in2->m[1][2] + in1->m[0][2] * in2->m[2][2] + in1->m[0][3] * in2->m[3][2];
107         out->m[0][3] = in1->m[0][0] * in2->m[0][3] + in1->m[0][1] * in2->m[1][3] + in1->m[0][2] * in2->m[2][3] + in1->m[0][3] * in2->m[3][3];
108         out->m[1][0] = in1->m[1][0] * in2->m[0][0] + in1->m[1][1] * in2->m[1][0] + in1->m[1][2] * in2->m[2][0] + in1->m[1][3] * in2->m[3][0];
109         out->m[1][1] = in1->m[1][0] * in2->m[0][1] + in1->m[1][1] * in2->m[1][1] + in1->m[1][2] * in2->m[2][1] + in1->m[1][3] * in2->m[3][1];
110         out->m[1][2] = in1->m[1][0] * in2->m[0][2] + in1->m[1][1] * in2->m[1][2] + in1->m[1][2] * in2->m[2][2] + in1->m[1][3] * in2->m[3][2];
111         out->m[1][3] = in1->m[1][0] * in2->m[0][3] + in1->m[1][1] * in2->m[1][3] + in1->m[1][2] * in2->m[2][3] + in1->m[1][3] * in2->m[3][3];
112         out->m[2][0] = in1->m[2][0] * in2->m[0][0] + in1->m[2][1] * in2->m[1][0] + in1->m[2][2] * in2->m[2][0] + in1->m[2][3] * in2->m[3][0];
113         out->m[2][1] = in1->m[2][0] * in2->m[0][1] + in1->m[2][1] * in2->m[1][1] + in1->m[2][2] * in2->m[2][1] + in1->m[2][3] * in2->m[3][1];
114         out->m[2][2] = in1->m[2][0] * in2->m[0][2] + in1->m[2][1] * in2->m[1][2] + in1->m[2][2] * in2->m[2][2] + in1->m[2][3] * in2->m[3][2];
115         out->m[2][3] = in1->m[2][0] * in2->m[0][3] + in1->m[2][1] * in2->m[1][3] + in1->m[2][2] * in2->m[2][3] + in1->m[2][3] * in2->m[3][3];
116         out->m[3][0] = in1->m[3][0] * in2->m[0][0] + in1->m[3][1] * in2->m[1][0] + in1->m[3][2] * in2->m[2][0] + in1->m[3][3] * in2->m[3][0];
117         out->m[3][1] = in1->m[3][0] * in2->m[0][1] + in1->m[3][1] * in2->m[1][1] + in1->m[3][2] * in2->m[2][1] + in1->m[3][3] * in2->m[3][1];
118         out->m[3][2] = in1->m[3][0] * in2->m[0][2] + in1->m[3][1] * in2->m[1][2] + in1->m[3][2] * in2->m[2][2] + in1->m[3][3] * in2->m[3][2];
119         out->m[3][3] = in1->m[3][0] * in2->m[0][3] + in1->m[3][1] * in2->m[1][3] + in1->m[3][2] * in2->m[2][3] + in1->m[3][3] * in2->m[3][3];
120 #endif
121 }
122
123 void Matrix4x4_Transpose (matrix4x4_t *out, const matrix4x4_t *in1)
124 {
125         out->m[0][0] = in1->m[0][0];
126         out->m[0][1] = in1->m[1][0];
127         out->m[0][2] = in1->m[2][0];
128         out->m[0][3] = in1->m[3][0];
129         out->m[1][0] = in1->m[0][1];
130         out->m[1][1] = in1->m[1][1];
131         out->m[1][2] = in1->m[2][1];
132         out->m[1][3] = in1->m[3][1];
133         out->m[2][0] = in1->m[0][2];
134         out->m[2][1] = in1->m[1][2];
135         out->m[2][2] = in1->m[2][2];
136         out->m[2][3] = in1->m[3][2];
137         out->m[3][0] = in1->m[0][3];
138         out->m[3][1] = in1->m[1][3];
139         out->m[3][2] = in1->m[2][3];
140         out->m[3][3] = in1->m[3][3];
141 }
142
143 int Matrix4x4_Invert_Full (matrix4x4_t *out, const matrix4x4_t *in1)
144 {
145         double  *temp;
146         double  *r[4];
147         double  rtemp[4][8];
148         double  m[4];
149         double  s;
150
151         r[0]    = rtemp[0];
152         r[1]    = rtemp[1];
153         r[2]    = rtemp[2];
154         r[3]    = rtemp[3];
155
156 #ifdef MATRIX4x4_OPENGLORIENTATION
157         r[0][0] = in1->m[0][0]; r[0][1] = in1->m[1][0]; r[0][2] = in1->m[2][0]; r[0][3] = in1->m[3][0];
158         r[0][4] = 1.0;                  r[0][5] =                               r[0][6] =                               r[0][7] = 0.0;
159
160         r[1][0] = in1->m[0][1]; r[1][1] = in1->m[1][1]; r[1][2] = in1->m[2][1]; r[1][3] = in1->m[3][1];
161         r[1][5] = 1.0;                  r[1][4] =                               r[1][6] =                               r[1][7] = 0.0;
162
163         r[2][0] = in1->m[0][2]; r[2][1] = in1->m[1][2]; r[2][2] = in1->m[2][2]; r[2][3] = in1->m[3][2];
164         r[2][6] = 1.0;                  r[2][4] =                               r[2][5] =                               r[2][7] = 0.0;
165
166         r[3][0] = in1->m[0][3]; r[3][1] = in1->m[1][3]; r[3][2] = in1->m[2][3]; r[3][3] = in1->m[3][3];
167         r[3][7] = 1.0;                  r[3][4] =                               r[3][5] =                               r[3][6] = 0.0;
168 #else
169         r[0][0] = in1->m[0][0]; r[0][1] = in1->m[0][1]; r[0][2] = in1->m[0][2]; r[0][3] = in1->m[0][3];
170         r[0][4] = 1.0;                  r[0][5] =                               r[0][6] =                               r[0][7] = 0.0;
171
172         r[1][0] = in1->m[1][0]; r[1][1] = in1->m[1][1]; r[1][2] = in1->m[1][2]; r[1][3] = in1->m[1][3];
173         r[1][5] = 1.0;                  r[1][4] =                               r[1][6] =                               r[1][7] = 0.0;
174
175         r[2][0] = in1->m[2][0]; r[2][1] = in1->m[2][1]; r[2][2] = in1->m[2][2]; r[2][3] = in1->m[2][3];
176         r[2][6] = 1.0;                  r[2][4] =                               r[2][5] =                               r[2][7] = 0.0;
177
178         r[3][0] = in1->m[3][0]; r[3][1] = in1->m[3][1]; r[3][2] = in1->m[3][2]; r[3][3] = in1->m[3][3];
179         r[3][7] = 1.0;                  r[3][4] =                               r[3][5] =                               r[3][6] = 0.0;
180 #endif
181
182         if (fabs (r[3][0]) > fabs (r[2][0])) { temp = r[3]; r[3] = r[2]; r[2] = temp; }
183         if (fabs (r[2][0]) > fabs (r[1][0])) { temp = r[2]; r[2] = r[1]; r[1] = temp; }
184         if (fabs (r[1][0]) > fabs (r[0][0])) { temp = r[1]; r[1] = r[0]; r[0] = temp; }
185
186         if (r[0][0])
187         {
188                 m[1]    = r[1][0] / r[0][0];
189                 m[2]    = r[2][0] / r[0][0];
190                 m[3]    = r[3][0] / r[0][0];
191
192                 s       = r[0][1]; r[1][1] -= m[1] * s; r[2][1] -= m[2] * s; r[3][1] -= m[3] * s;
193                 s       = r[0][2]; r[1][2] -= m[1] * s; r[2][2] -= m[2] * s; r[3][2] -= m[3] * s;
194                 s       = r[0][3]; r[1][3] -= m[1] * s; r[2][3] -= m[2] * s; r[3][3] -= m[3] * s;
195
196                 s       = r[0][4]; if (s) { r[1][4] -= m[1] * s; r[2][4] -= m[2] * s; r[3][4] -= m[3] * s; }
197                 s       = r[0][5]; if (s) { r[1][5] -= m[1] * s; r[2][5] -= m[2] * s; r[3][5] -= m[3] * s; }
198                 s       = r[0][6]; if (s) { r[1][6] -= m[1] * s; r[2][6] -= m[2] * s; r[3][6] -= m[3] * s; }
199                 s       = r[0][7]; if (s) { r[1][7] -= m[1] * s; r[2][7] -= m[2] * s; r[3][7] -= m[3] * s; }
200
201                 if (fabs (r[3][1]) > fabs (r[2][1])) { temp = r[3]; r[3] = r[2]; r[2] = temp; }
202                 if (fabs (r[2][1]) > fabs (r[1][1])) { temp = r[2]; r[2] = r[1]; r[1] = temp; }
203
204                 if (r[1][1])
205                 {
206                         m[2]            = r[2][1] / r[1][1];
207                         m[3]            = r[3][1] / r[1][1];
208                         r[2][2] -= m[2] * r[1][2];
209                         r[3][2] -= m[3] * r[1][2];
210                         r[2][3] -= m[2] * r[1][3];
211                         r[3][3] -= m[3] * r[1][3];
212
213                         s       = r[1][4]; if (s) { r[2][4] -= m[2] * s; r[3][4] -= m[3] * s; }
214                         s       = r[1][5]; if (s) { r[2][5] -= m[2] * s; r[3][5] -= m[3] * s; }
215                         s       = r[1][6]; if (s) { r[2][6] -= m[2] * s; r[3][6] -= m[3] * s; }
216                         s       = r[1][7]; if (s) { r[2][7] -= m[2] * s; r[3][7] -= m[3] * s; }
217
218                         if (fabs (r[3][2]) > fabs (r[2][2])) { temp = r[3]; r[3] = r[2]; r[2] = temp; }
219
220                         if (r[2][2])
221                         {
222                                 m[3]            = r[3][2] / r[2][2];
223                                 r[3][3] -= m[3] * r[2][3];
224                                 r[3][4] -= m[3] * r[2][4];
225                                 r[3][5] -= m[3] * r[2][5];
226                                 r[3][6] -= m[3] * r[2][6];
227                                 r[3][7] -= m[3] * r[2][7];
228
229                                 if (r[3][3])
230                                 {
231                                         s                       = 1.0 / r[3][3];
232                                         r[3][4] *= s;
233                                         r[3][5] *= s;
234                                         r[3][6] *= s;
235                                         r[3][7] *= s;
236
237                                         m[2]            = r[2][3];
238                                         s                       = 1.0 / r[2][2];
239                                         r[2][4] = s * (r[2][4] - r[3][4] * m[2]);
240                                         r[2][5] = s * (r[2][5] - r[3][5] * m[2]);
241                                         r[2][6] = s * (r[2][6] - r[3][6] * m[2]);
242                                         r[2][7] = s * (r[2][7] - r[3][7] * m[2]);
243
244                                         m[1]            = r[1][3];
245                                         r[1][4] -= r[3][4] * m[1], r[1][5] -= r[3][5] * m[1];
246                                         r[1][6] -= r[3][6] * m[1], r[1][7] -= r[3][7] * m[1];
247
248                                         m[0]            = r[0][3];
249                                         r[0][4] -= r[3][4] * m[0], r[0][5] -= r[3][5] * m[0];
250                                         r[0][6] -= r[3][6] * m[0], r[0][7] -= r[3][7] * m[0];
251
252                                         m[1]            = r[1][2];
253                                         s                       = 1.0 / r[1][1];
254                                         r[1][4] = s * (r[1][4] - r[2][4] * m[1]), r[1][5] = s * (r[1][5] - r[2][5] * m[1]);
255                                         r[1][6] = s * (r[1][6] - r[2][6] * m[1]), r[1][7] = s * (r[1][7] - r[2][7] * m[1]);
256
257                                         m[0]            = r[0][2];
258                                         r[0][4] -= r[2][4] * m[0], r[0][5] -= r[2][5] * m[0];
259                                         r[0][6] -= r[2][6] * m[0], r[0][7] -= r[2][7] * m[0];
260
261                                         m[0]            = r[0][1];
262                                         s                       = 1.0 / r[0][0];
263                                         r[0][4] = s * (r[0][4] - r[1][4] * m[0]), r[0][5] = s * (r[0][5] - r[1][5] * m[0]);
264                                         r[0][6] = s * (r[0][6] - r[1][6] * m[0]), r[0][7] = s * (r[0][7] - r[1][7] * m[0]);
265
266 #ifdef MATRIX4x4_OPENGLORIENTATION
267                                         out->m[0][0]    = r[0][4];
268                                         out->m[0][1]    = r[1][4];
269                                         out->m[0][2]    = r[2][4];
270                                         out->m[0][3]    = r[3][4];
271                                         out->m[1][0]    = r[0][5];
272                                         out->m[1][1]    = r[1][5];
273                                         out->m[1][2]    = r[2][5];
274                                         out->m[1][3]    = r[3][5];
275                                         out->m[2][0]    = r[0][6];
276                                         out->m[2][1]    = r[1][6];
277                                         out->m[2][2]    = r[2][6];
278                                         out->m[2][3]    = r[3][6];
279                                         out->m[3][0]    = r[0][7];
280                                         out->m[3][1]    = r[1][7];
281                                         out->m[3][2]    = r[2][7];
282                                         out->m[3][3]    = r[3][7];
283 #else
284                                         out->m[0][0]    = r[0][4];
285                                         out->m[0][1]    = r[0][5];
286                                         out->m[0][2]    = r[0][6];
287                                         out->m[0][3]    = r[0][7];
288                                         out->m[1][0]    = r[1][4];
289                                         out->m[1][1]    = r[1][5];
290                                         out->m[1][2]    = r[1][6];
291                                         out->m[1][3]    = r[1][7];
292                                         out->m[2][0]    = r[2][4];
293                                         out->m[2][1]    = r[2][5];
294                                         out->m[2][2]    = r[2][6];
295                                         out->m[2][3]    = r[2][7];
296                                         out->m[3][0]    = r[3][4];
297                                         out->m[3][1]    = r[3][5];
298                                         out->m[3][2]    = r[3][6];
299                                         out->m[3][3]    = r[3][7];
300 #endif
301
302                                         return 1;
303                                 }
304                         }
305                 }
306         }
307
308         return 0;
309 }
310
311 void Matrix4x4_Invert_Simple (matrix4x4_t *out, const matrix4x4_t *in1)
312 {
313         // we only support uniform scaling, so assume the first row is enough
314         // (note the lack of sqrt here, because we're trying to undo the scaling,
315         // this means multiplying by the inverse scale twice - squaring it, which
316         // makes the sqrt a waste of time)
317 #if 1
318         double scale = 1.0 / (in1->m[0][0] * in1->m[0][0] + in1->m[0][1] * in1->m[0][1] + in1->m[0][2] * in1->m[0][2]);
319 #else
320         double scale = 3.0 / sqrt
321                  (in1->m[0][0] * in1->m[0][0] + in1->m[0][1] * in1->m[0][1] + in1->m[0][2] * in1->m[0][2]
322                 + in1->m[1][0] * in1->m[1][0] + in1->m[1][1] * in1->m[1][1] + in1->m[1][2] * in1->m[1][2]
323                 + in1->m[2][0] * in1->m[2][0] + in1->m[2][1] * in1->m[2][1] + in1->m[2][2] * in1->m[2][2]);
324         scale *= scale;
325 #endif
326
327         // invert the rotation by transposing and multiplying by the squared
328         // recipricol of the input matrix scale as described above
329         out->m[0][0] = in1->m[0][0] * scale;
330         out->m[0][1] = in1->m[1][0] * scale;
331         out->m[0][2] = in1->m[2][0] * scale;
332         out->m[1][0] = in1->m[0][1] * scale;
333         out->m[1][1] = in1->m[1][1] * scale;
334         out->m[1][2] = in1->m[2][1] * scale;
335         out->m[2][0] = in1->m[0][2] * scale;
336         out->m[2][1] = in1->m[1][2] * scale;
337         out->m[2][2] = in1->m[2][2] * scale;
338
339 #ifdef MATRIX4x4_OPENGLORIENTATION
340         // invert the translate
341         out->m[3][0] = -(in1->m[3][0] * out->m[0][0] + in1->m[3][1] * out->m[1][0] + in1->m[3][2] * out->m[2][0]);
342         out->m[3][1] = -(in1->m[3][0] * out->m[0][1] + in1->m[3][1] * out->m[1][1] + in1->m[3][2] * out->m[2][1]);
343         out->m[3][2] = -(in1->m[3][0] * out->m[0][2] + in1->m[3][1] * out->m[1][2] + in1->m[3][2] * out->m[2][2]);
344
345         // don't know if there's anything worth doing here
346         out->m[0][3] = 0;
347         out->m[1][3] = 0;
348         out->m[2][3] = 0;
349         out->m[3][3] = 1;
350 #else
351         // invert the translate
352         out->m[0][3] = -(in1->m[0][3] * out->m[0][0] + in1->m[1][3] * out->m[0][1] + in1->m[2][3] * out->m[0][2]);
353         out->m[1][3] = -(in1->m[0][3] * out->m[1][0] + in1->m[1][3] * out->m[1][1] + in1->m[2][3] * out->m[1][2]);
354         out->m[2][3] = -(in1->m[0][3] * out->m[2][0] + in1->m[1][3] * out->m[2][1] + in1->m[2][3] * out->m[2][2]);
355
356         // don't know if there's anything worth doing here
357         out->m[3][0] = 0;
358         out->m[3][1] = 0;
359         out->m[3][2] = 0;
360         out->m[3][3] = 1;
361 #endif
362 }
363
364 void Matrix4x4_Normalize (matrix4x4_t *out, matrix4x4_t *in1)
365 {
366         // scale rotation matrix vectors to a length of 1
367         // note: this is only designed to undo uniform scaling
368         double scale = 1.0 / sqrt(in1->m[0][0] * in1->m[0][0] + in1->m[0][1] * in1->m[0][1] + in1->m[0][2] * in1->m[0][2]);
369         *out = *in1;
370         Matrix4x4_Scale(out, scale, 1);
371 }
372
373 void Matrix4x4_Reflect (matrix4x4_t *out, double normalx, double normaly, double normalz, double dist, double axisscale)
374 {
375         int i;
376         double d;
377         double p[4], p2[4];
378         p[0] = normalx;
379         p[1] = normaly;
380         p[2] = normalz;
381         p[3] = -dist;
382         p2[0] = p[0] * axisscale;
383         p2[1] = p[1] * axisscale;
384         p2[2] = p[2] * axisscale;
385         p2[3] = 0;
386         for (i = 0;i < 4;i++)
387         {
388 #ifdef MATRIX4x4_OPENGLORIENTATION
389                 d = out->m[i][0] * p[0] + out->m[i][1] * p[1] + out->m[i][2] * p[2] + out->m[i][3] * p[3];
390                 out->m[i][0] += p2[0] * d;
391                 out->m[i][1] += p2[1] * d;
392                 out->m[i][2] += p2[2] * d;
393 #else
394                 d = out->m[0][i] * p[0] + out->m[1][i] * p[1] + out->m[2][i] * p[2] + out->m[3][i] * p[3];
395                 out->m[0][i] += p2[0] * d;
396                 out->m[1][i] += p2[1] * d;
397                 out->m[2][i] += p2[2] * d;
398 #endif
399         }
400 }
401
402 void Matrix4x4_CreateIdentity (matrix4x4_t *out)
403 {
404         out->m[0][0]=1.0f;
405         out->m[0][1]=0.0f;
406         out->m[0][2]=0.0f;
407         out->m[0][3]=0.0f;
408         out->m[1][0]=0.0f;
409         out->m[1][1]=1.0f;
410         out->m[1][2]=0.0f;
411         out->m[1][3]=0.0f;
412         out->m[2][0]=0.0f;
413         out->m[2][1]=0.0f;
414         out->m[2][2]=1.0f;
415         out->m[2][3]=0.0f;
416         out->m[3][0]=0.0f;
417         out->m[3][1]=0.0f;
418         out->m[3][2]=0.0f;
419         out->m[3][3]=1.0f;
420 }
421
422 void Matrix4x4_CreateTranslate (matrix4x4_t *out, double x, double y, double z)
423 {
424 #ifdef MATRIX4x4_OPENGLORIENTATION
425         out->m[0][0]=1.0f;
426         out->m[1][0]=0.0f;
427         out->m[2][0]=0.0f;
428         out->m[3][0]=x;
429         out->m[0][1]=0.0f;
430         out->m[1][1]=1.0f;
431         out->m[2][1]=0.0f;
432         out->m[3][1]=y;
433         out->m[0][2]=0.0f;
434         out->m[1][2]=0.0f;
435         out->m[2][2]=1.0f;
436         out->m[3][2]=z;
437         out->m[0][3]=0.0f;
438         out->m[1][3]=0.0f;
439         out->m[2][3]=0.0f;
440         out->m[3][3]=1.0f;
441 #else
442         out->m[0][0]=1.0f;
443         out->m[0][1]=0.0f;
444         out->m[0][2]=0.0f;
445         out->m[0][3]=x;
446         out->m[1][0]=0.0f;
447         out->m[1][1]=1.0f;
448         out->m[1][2]=0.0f;
449         out->m[1][3]=y;
450         out->m[2][0]=0.0f;
451         out->m[2][1]=0.0f;
452         out->m[2][2]=1.0f;
453         out->m[2][3]=z;
454         out->m[3][0]=0.0f;
455         out->m[3][1]=0.0f;
456         out->m[3][2]=0.0f;
457         out->m[3][3]=1.0f;
458 #endif
459 }
460
461 void Matrix4x4_CreateRotate (matrix4x4_t *out, double angle, double x, double y, double z)
462 {
463         double len, c, s;
464
465         len = x*x+y*y+z*z;
466         if (len != 0.0f)
467                 len = 1.0f / sqrt(len);
468         x *= len;
469         y *= len;
470         z *= len;
471
472         angle *= (-M_PI / 180.0);
473         c = cos(angle);
474         s = sin(angle);
475
476 #ifdef MATRIX4x4_OPENGLORIENTATION
477         out->m[0][0]=x * x + c * (1 - x * x);
478         out->m[1][0]=x * y * (1 - c) + z * s;
479         out->m[2][0]=z * x * (1 - c) - y * s;
480         out->m[3][0]=0.0f;
481         out->m[0][1]=x * y * (1 - c) - z * s;
482         out->m[1][1]=y * y + c * (1 - y * y);
483         out->m[2][1]=y * z * (1 - c) + x * s;
484         out->m[3][1]=0.0f;
485         out->m[0][2]=z * x * (1 - c) + y * s;
486         out->m[1][2]=y * z * (1 - c) - x * s;
487         out->m[2][2]=z * z + c * (1 - z * z);
488         out->m[3][2]=0.0f;
489         out->m[0][3]=0.0f;
490         out->m[1][3]=0.0f;
491         out->m[2][3]=0.0f;
492         out->m[3][3]=1.0f;
493 #else
494         out->m[0][0]=x * x + c * (1 - x * x);
495         out->m[0][1]=x * y * (1 - c) + z * s;
496         out->m[0][2]=z * x * (1 - c) - y * s;
497         out->m[0][3]=0.0f;
498         out->m[1][0]=x * y * (1 - c) - z * s;
499         out->m[1][1]=y * y + c * (1 - y * y);
500         out->m[1][2]=y * z * (1 - c) + x * s;
501         out->m[1][3]=0.0f;
502         out->m[2][0]=z * x * (1 - c) + y * s;
503         out->m[2][1]=y * z * (1 - c) - x * s;
504         out->m[2][2]=z * z + c * (1 - z * z);
505         out->m[2][3]=0.0f;
506         out->m[3][0]=0.0f;
507         out->m[3][1]=0.0f;
508         out->m[3][2]=0.0f;
509         out->m[3][3]=1.0f;
510 #endif
511 }
512
513 void Matrix4x4_CreateScale (matrix4x4_t *out, double x)
514 {
515         out->m[0][0]=x;
516         out->m[0][1]=0.0f;
517         out->m[0][2]=0.0f;
518         out->m[0][3]=0.0f;
519         out->m[1][0]=0.0f;
520         out->m[1][1]=x;
521         out->m[1][2]=0.0f;
522         out->m[1][3]=0.0f;
523         out->m[2][0]=0.0f;
524         out->m[2][1]=0.0f;
525         out->m[2][2]=x;
526         out->m[2][3]=0.0f;
527         out->m[3][0]=0.0f;
528         out->m[3][1]=0.0f;
529         out->m[3][2]=0.0f;
530         out->m[3][3]=1.0f;
531 }
532
533 void Matrix4x4_CreateScale3 (matrix4x4_t *out, double x, double y, double z)
534 {
535         out->m[0][0]=x;
536         out->m[0][1]=0.0f;
537         out->m[0][2]=0.0f;
538         out->m[0][3]=0.0f;
539         out->m[1][0]=0.0f;
540         out->m[1][1]=y;
541         out->m[1][2]=0.0f;
542         out->m[1][3]=0.0f;
543         out->m[2][0]=0.0f;
544         out->m[2][1]=0.0f;
545         out->m[2][2]=z;
546         out->m[2][3]=0.0f;
547         out->m[3][0]=0.0f;
548         out->m[3][1]=0.0f;
549         out->m[3][2]=0.0f;
550         out->m[3][3]=1.0f;
551 }
552
553 void Matrix4x4_CreateFromQuakeEntity(matrix4x4_t *out, double x, double y, double z, double pitch, double yaw, double roll, double scale)
554 {
555         double angle, sr, sp, sy, cr, cp, cy;
556
557         if (roll)
558         {
559                 angle = yaw * (M_PI*2 / 360);
560                 sy = sin(angle);
561                 cy = cos(angle);
562                 angle = pitch * (M_PI*2 / 360);
563                 sp = sin(angle);
564                 cp = cos(angle);
565                 angle = roll * (M_PI*2 / 360);
566                 sr = sin(angle);
567                 cr = cos(angle);
568 #ifdef MATRIX4x4_OPENGLORIENTATION
569                 out->m[0][0] = (cp*cy) * scale;
570                 out->m[1][0] = (sr*sp*cy+cr*-sy) * scale;
571                 out->m[2][0] = (cr*sp*cy+-sr*-sy) * scale;
572                 out->m[3][0] = x;
573                 out->m[0][1] = (cp*sy) * scale;
574                 out->m[1][1] = (sr*sp*sy+cr*cy) * scale;
575                 out->m[2][1] = (cr*sp*sy+-sr*cy) * scale;
576                 out->m[3][1] = y;
577                 out->m[0][2] = (-sp) * scale;
578                 out->m[1][2] = (sr*cp) * scale;
579                 out->m[2][2] = (cr*cp) * scale;
580                 out->m[3][2] = z;
581                 out->m[0][3] = 0;
582                 out->m[1][3] = 0;
583                 out->m[2][3] = 0;
584                 out->m[3][3] = 1;
585 #else
586                 out->m[0][0] = (cp*cy) * scale;
587                 out->m[0][1] = (sr*sp*cy+cr*-sy) * scale;
588                 out->m[0][2] = (cr*sp*cy+-sr*-sy) * scale;
589                 out->m[0][3] = x;
590                 out->m[1][0] = (cp*sy) * scale;
591                 out->m[1][1] = (sr*sp*sy+cr*cy) * scale;
592                 out->m[1][2] = (cr*sp*sy+-sr*cy) * scale;
593                 out->m[1][3] = y;
594                 out->m[2][0] = (-sp) * scale;
595                 out->m[2][1] = (sr*cp) * scale;
596                 out->m[2][2] = (cr*cp) * scale;
597                 out->m[2][3] = z;
598                 out->m[3][0] = 0;
599                 out->m[3][1] = 0;
600                 out->m[3][2] = 0;
601                 out->m[3][3] = 1;
602 #endif
603         }
604         else if (pitch)
605         {
606                 angle = yaw * (M_PI*2 / 360);
607                 sy = sin(angle);
608                 cy = cos(angle);
609                 angle = pitch * (M_PI*2 / 360);
610                 sp = sin(angle);
611                 cp = cos(angle);
612 #ifdef MATRIX4x4_OPENGLORIENTATION
613                 out->m[0][0] = (cp*cy) * scale;
614                 out->m[1][0] = (-sy) * scale;
615                 out->m[2][0] = (sp*cy) * scale;
616                 out->m[3][0] = x;
617                 out->m[0][1] = (cp*sy) * scale;
618                 out->m[1][1] = (cy) * scale;
619                 out->m[2][1] = (sp*sy) * scale;
620                 out->m[3][1] = y;
621                 out->m[0][2] = (-sp) * scale;
622                 out->m[1][2] = 0;
623                 out->m[2][2] = (cp) * scale;
624                 out->m[3][2] = z;
625                 out->m[0][3] = 0;
626                 out->m[1][3] = 0;
627                 out->m[2][3] = 0;
628                 out->m[3][3] = 1;
629 #else
630                 out->m[0][0] = (cp*cy) * scale;
631                 out->m[0][1] = (-sy) * scale;
632                 out->m[0][2] = (sp*cy) * scale;
633                 out->m[0][3] = x;
634                 out->m[1][0] = (cp*sy) * scale;
635                 out->m[1][1] = (cy) * scale;
636                 out->m[1][2] = (sp*sy) * scale;
637                 out->m[1][3] = y;
638                 out->m[2][0] = (-sp) * scale;
639                 out->m[2][1] = 0;
640                 out->m[2][2] = (cp) * scale;
641                 out->m[2][3] = z;
642                 out->m[3][0] = 0;
643                 out->m[3][1] = 0;
644                 out->m[3][2] = 0;
645                 out->m[3][3] = 1;
646 #endif
647         }
648         else if (yaw)
649         {
650                 angle = yaw * (M_PI*2 / 360);
651                 sy = sin(angle);
652                 cy = cos(angle);
653 #ifdef MATRIX4x4_OPENGLORIENTATION
654                 out->m[0][0] = (cy) * scale;
655                 out->m[1][0] = (-sy) * scale;
656                 out->m[2][0] = 0;
657                 out->m[3][0] = x;
658                 out->m[0][1] = (sy) * scale;
659                 out->m[1][1] = (cy) * scale;
660                 out->m[2][1] = 0;
661                 out->m[3][1] = y;
662                 out->m[0][2] = 0;
663                 out->m[1][2] = 0;
664                 out->m[2][2] = scale;
665                 out->m[3][2] = z;
666                 out->m[0][3] = 0;
667                 out->m[1][3] = 0;
668                 out->m[2][3] = 0;
669                 out->m[3][3] = 1;
670 #else
671                 out->m[0][0] = (cy) * scale;
672                 out->m[0][1] = (-sy) * scale;
673                 out->m[0][2] = 0;
674                 out->m[0][3] = x;
675                 out->m[1][0] = (sy) * scale;
676                 out->m[1][1] = (cy) * scale;
677                 out->m[1][2] = 0;
678                 out->m[1][3] = y;
679                 out->m[2][0] = 0;
680                 out->m[2][1] = 0;
681                 out->m[2][2] = scale;
682                 out->m[2][3] = z;
683                 out->m[3][0] = 0;
684                 out->m[3][1] = 0;
685                 out->m[3][2] = 0;
686                 out->m[3][3] = 1;
687 #endif
688         }
689         else
690         {
691 #ifdef MATRIX4x4_OPENGLORIENTATION
692                 out->m[0][0] = scale;
693                 out->m[1][0] = 0;
694                 out->m[2][0] = 0;
695                 out->m[3][0] = x;
696                 out->m[0][1] = 0;
697                 out->m[1][1] = scale;
698                 out->m[2][1] = 0;
699                 out->m[3][1] = y;
700                 out->m[0][2] = 0;
701                 out->m[1][2] = 0;
702                 out->m[2][2] = scale;
703                 out->m[3][2] = z;
704                 out->m[0][3] = 0;
705                 out->m[1][3] = 0;
706                 out->m[2][3] = 0;
707                 out->m[3][3] = 1;
708 #else
709                 out->m[0][0] = scale;
710                 out->m[0][1] = 0;
711                 out->m[0][2] = 0;
712                 out->m[0][3] = x;
713                 out->m[1][0] = 0;
714                 out->m[1][1] = scale;
715                 out->m[1][2] = 0;
716                 out->m[1][3] = y;
717                 out->m[2][0] = 0;
718                 out->m[2][1] = 0;
719                 out->m[2][2] = scale;
720                 out->m[2][3] = z;
721                 out->m[3][0] = 0;
722                 out->m[3][1] = 0;
723                 out->m[3][2] = 0;
724                 out->m[3][3] = 1;
725 #endif
726         }
727 }
728
729 void Matrix4x4_ToVectors(const matrix4x4_t *in, float vx[3], float vy[3], float vz[3], float t[3])
730 {
731 #ifdef MATRIX4x4_OPENGLORIENTATION
732         vx[0] = in->m[0][0];
733         vx[1] = in->m[0][1];
734         vx[2] = in->m[0][2];
735         vy[0] = in->m[1][0];
736         vy[1] = in->m[1][1];
737         vy[2] = in->m[1][2];
738         vz[0] = in->m[2][0];
739         vz[1] = in->m[2][1];
740         vz[2] = in->m[2][2];
741         t [0] = in->m[3][0];
742         t [1] = in->m[3][1];
743         t [2] = in->m[3][2];
744 #else
745         vx[0] = in->m[0][0];
746         vx[1] = in->m[1][0];
747         vx[2] = in->m[2][0];
748         vy[0] = in->m[0][1];
749         vy[1] = in->m[1][1];
750         vy[2] = in->m[2][1];
751         vz[0] = in->m[0][2];
752         vz[1] = in->m[1][2];
753         vz[2] = in->m[2][2];
754         t [0] = in->m[0][3];
755         t [1] = in->m[1][3];
756         t [2] = in->m[2][3];
757 #endif
758 }
759
760 void Matrix4x4_FromVectors(matrix4x4_t *out, const float vx[3], const float vy[3], const float vz[3], const float t[3])
761 {
762 #ifdef MATRIX4x4_OPENGLORIENTATION
763         out->m[0][0] = vx[0];
764         out->m[1][0] = vy[0];
765         out->m[2][0] = vz[0];
766         out->m[3][0] = t[0];
767         out->m[0][1] = vx[1];
768         out->m[1][1] = vy[1];
769         out->m[2][1] = vz[1];
770         out->m[3][1] = t[1];
771         out->m[0][2] = vx[2];
772         out->m[1][2] = vy[2];
773         out->m[2][2] = vz[2];
774         out->m[3][2] = t[2];
775         out->m[0][3] = 0.0f;
776         out->m[1][3] = 0.0f;
777         out->m[2][3] = 0.0f;
778         out->m[3][3] = 1.0f;
779 #else
780         out->m[0][0] = vx[0];
781         out->m[0][1] = vy[0];
782         out->m[0][2] = vz[0];
783         out->m[0][3] = t[0];
784         out->m[1][0] = vx[1];
785         out->m[1][1] = vy[1];
786         out->m[1][2] = vz[1];
787         out->m[1][3] = t[1];
788         out->m[2][0] = vx[2];
789         out->m[2][1] = vy[2];
790         out->m[2][2] = vz[2];
791         out->m[2][3] = t[2];
792         out->m[3][0] = 0.0f;
793         out->m[3][1] = 0.0f;
794         out->m[3][2] = 0.0f;
795         out->m[3][3] = 1.0f;
796 #endif
797 }
798
799 void Matrix4x4_ToArrayDoubleGL(const matrix4x4_t *in, double out[16])
800 {
801 #ifdef MATRIX4x4_OPENGLORIENTATION
802         out[ 0] = in->m[0][0];
803         out[ 1] = in->m[0][1];
804         out[ 2] = in->m[0][2];
805         out[ 3] = in->m[0][3];
806         out[ 4] = in->m[1][0];
807         out[ 5] = in->m[1][1];
808         out[ 6] = in->m[1][2];
809         out[ 7] = in->m[1][3];
810         out[ 8] = in->m[2][0];
811         out[ 9] = in->m[2][1];
812         out[10] = in->m[2][2];
813         out[11] = in->m[2][3];
814         out[12] = in->m[3][0];
815         out[13] = in->m[3][1];
816         out[14] = in->m[3][2];
817         out[15] = in->m[3][3];
818 #else
819         out[ 0] = in->m[0][0];
820         out[ 1] = in->m[1][0];
821         out[ 2] = in->m[2][0];
822         out[ 3] = in->m[3][0];
823         out[ 4] = in->m[0][1];
824         out[ 5] = in->m[1][1];
825         out[ 6] = in->m[2][1];
826         out[ 7] = in->m[3][1];
827         out[ 8] = in->m[0][2];
828         out[ 9] = in->m[1][2];
829         out[10] = in->m[2][2];
830         out[11] = in->m[3][2];
831         out[12] = in->m[0][3];
832         out[13] = in->m[1][3];
833         out[14] = in->m[2][3];
834         out[15] = in->m[3][3];
835 #endif
836 }
837
838 void Matrix4x4_FromArrayDoubleGL (matrix4x4_t *out, const double in[16])
839 {
840 #ifdef MATRIX4x4_OPENGLORIENTATION
841         out->m[0][0] = in[0];
842         out->m[0][1] = in[1];
843         out->m[0][2] = in[2];
844         out->m[0][3] = in[3];
845         out->m[1][0] = in[4];
846         out->m[1][1] = in[5];
847         out->m[1][2] = in[6];
848         out->m[1][3] = in[7];
849         out->m[2][0] = in[8];
850         out->m[2][1] = in[9];
851         out->m[2][2] = in[10];
852         out->m[2][3] = in[11];
853         out->m[3][0] = in[12];
854         out->m[3][1] = in[13];
855         out->m[3][2] = in[14];
856         out->m[3][3] = in[15];
857 #else
858         out->m[0][0] = in[0];
859         out->m[1][0] = in[1];
860         out->m[2][0] = in[2];
861         out->m[3][0] = in[3];
862         out->m[0][1] = in[4];
863         out->m[1][1] = in[5];
864         out->m[2][1] = in[6];
865         out->m[3][1] = in[7];
866         out->m[0][2] = in[8];
867         out->m[1][2] = in[9];
868         out->m[2][2] = in[10];
869         out->m[3][2] = in[11];
870         out->m[0][3] = in[12];
871         out->m[1][3] = in[13];
872         out->m[2][3] = in[14];
873         out->m[3][3] = in[15];
874 #endif
875 }
876
877 void Matrix4x4_ToArrayDoubleD3D(const matrix4x4_t *in, double out[16])
878 {
879 #ifdef MATRIX4x4_OPENGLORIENTATION
880         out[ 0] = in->m[0][0];
881         out[ 1] = in->m[1][0];
882         out[ 2] = in->m[2][0];
883         out[ 3] = in->m[3][0];
884         out[ 4] = in->m[0][1];
885         out[ 5] = in->m[1][1];
886         out[ 6] = in->m[2][1];
887         out[ 7] = in->m[3][1];
888         out[ 8] = in->m[0][2];
889         out[ 9] = in->m[1][2];
890         out[10] = in->m[2][2];
891         out[11] = in->m[3][2];
892         out[12] = in->m[0][3];
893         out[13] = in->m[1][3];
894         out[14] = in->m[2][3];
895         out[15] = in->m[3][3];
896 #else
897         out[ 0] = in->m[0][0];
898         out[ 1] = in->m[0][1];
899         out[ 2] = in->m[0][2];
900         out[ 3] = in->m[0][3];
901         out[ 4] = in->m[1][0];
902         out[ 5] = in->m[1][1];
903         out[ 6] = in->m[1][2];
904         out[ 7] = in->m[1][3];
905         out[ 8] = in->m[2][0];
906         out[ 9] = in->m[2][1];
907         out[10] = in->m[2][2];
908         out[11] = in->m[2][3];
909         out[12] = in->m[3][0];
910         out[13] = in->m[3][1];
911         out[14] = in->m[3][2];
912         out[15] = in->m[3][3];
913 #endif
914 }
915
916 void Matrix4x4_FromArrayDoubleD3D (matrix4x4_t *out, const double in[16])
917 {
918 #ifdef MATRIX4x4_OPENGLORIENTATION
919         out->m[0][0] = in[0];
920         out->m[1][0] = in[1];
921         out->m[2][0] = in[2];
922         out->m[3][0] = in[3];
923         out->m[0][1] = in[4];
924         out->m[1][1] = in[5];
925         out->m[2][1] = in[6];
926         out->m[3][1] = in[7];
927         out->m[0][2] = in[8];
928         out->m[1][2] = in[9];
929         out->m[2][2] = in[10];
930         out->m[3][2] = in[11];
931         out->m[0][3] = in[12];
932         out->m[1][3] = in[13];
933         out->m[2][3] = in[14];
934         out->m[3][3] = in[15];
935 #else
936         out->m[0][0] = in[0];
937         out->m[0][1] = in[1];
938         out->m[0][2] = in[2];
939         out->m[0][3] = in[3];
940         out->m[1][0] = in[4];
941         out->m[1][1] = in[5];
942         out->m[1][2] = in[6];
943         out->m[1][3] = in[7];
944         out->m[2][0] = in[8];
945         out->m[2][1] = in[9];
946         out->m[2][2] = in[10];
947         out->m[2][3] = in[11];
948         out->m[3][0] = in[12];
949         out->m[3][1] = in[13];
950         out->m[3][2] = in[14];
951         out->m[3][3] = in[15];
952 #endif
953 }
954
955 void Matrix4x4_ToArray12FloatGL(const matrix4x4_t *in, float out[12])
956 {
957 #ifdef MATRIX4x4_OPENGLORIENTATION
958         out[ 0] = in->m[0][0];
959         out[ 1] = in->m[0][1];
960         out[ 2] = in->m[0][2];
961         out[ 3] = in->m[1][0];
962         out[ 4] = in->m[1][1];
963         out[ 5] = in->m[1][2];
964         out[ 6] = in->m[2][0];
965         out[ 7] = in->m[2][1];
966         out[ 8] = in->m[2][2];
967         out[ 9] = in->m[3][0];
968         out[10] = in->m[3][1];
969         out[11] = in->m[3][2];
970 #else
971         out[ 0] = in->m[0][0];
972         out[ 1] = in->m[1][0];
973         out[ 2] = in->m[2][0];
974         out[ 3] = in->m[0][1];
975         out[ 4] = in->m[1][1];
976         out[ 5] = in->m[2][1];
977         out[ 6] = in->m[0][2];
978         out[ 7] = in->m[1][2];
979         out[ 8] = in->m[2][2];
980         out[ 9] = in->m[0][3];
981         out[10] = in->m[1][3];
982         out[11] = in->m[2][3];
983 #endif
984 }
985
986 void Matrix4x4_FromArray12FloatGL(matrix4x4_t *out, const float in[12])
987 {
988 #ifdef MATRIX4x4_OPENGLORIENTATION
989         out->m[0][0] = in[0];
990         out->m[0][1] = in[1];
991         out->m[0][2] = in[2];
992         out->m[0][3] = 0;
993         out->m[1][0] = in[3];
994         out->m[1][1] = in[4];
995         out->m[1][2] = in[5];
996         out->m[1][3] = 0;
997         out->m[2][0] = in[6];
998         out->m[2][1] = in[7];
999         out->m[2][2] = in[8];
1000         out->m[2][3] = 0;
1001         out->m[3][0] = in[9];
1002         out->m[3][1] = in[10];
1003         out->m[3][2] = in[11];
1004         out->m[3][3] = 1;
1005 #else
1006         out->m[0][0] = in[0];
1007         out->m[1][0] = in[1];
1008         out->m[2][0] = in[2];
1009         out->m[3][0] = 0;
1010         out->m[0][1] = in[3];
1011         out->m[1][1] = in[4];
1012         out->m[2][1] = in[5];
1013         out->m[3][1] = 0;
1014         out->m[0][2] = in[6];
1015         out->m[1][2] = in[7];
1016         out->m[2][2] = in[8];
1017         out->m[3][2] = 0;
1018         out->m[0][3] = in[9];
1019         out->m[1][3] = in[10];
1020         out->m[2][3] = in[11];
1021         out->m[3][3] = 1;
1022 #endif
1023 }
1024
1025 void Matrix4x4_ToArray12FloatD3D(const matrix4x4_t *in, float out[12])
1026 {
1027 #ifdef MATRIX4x4_OPENGLORIENTATION
1028         out[ 0] = in->m[0][0];
1029         out[ 1] = in->m[1][0];
1030         out[ 2] = in->m[2][0];
1031         out[ 3] = in->m[3][0];
1032         out[ 4] = in->m[0][1];
1033         out[ 5] = in->m[1][1];
1034         out[ 6] = in->m[2][1];
1035         out[ 7] = in->m[3][1];
1036         out[ 8] = in->m[0][2];
1037         out[ 9] = in->m[1][2];
1038         out[10] = in->m[2][2];
1039         out[11] = in->m[3][2];
1040 #else
1041         out[ 0] = in->m[0][0];
1042         out[ 1] = in->m[0][1];
1043         out[ 2] = in->m[0][2];
1044         out[ 3] = in->m[0][3];
1045         out[ 4] = in->m[1][0];
1046         out[ 5] = in->m[1][1];
1047         out[ 6] = in->m[1][2];
1048         out[ 7] = in->m[1][3];
1049         out[ 8] = in->m[2][0];
1050         out[ 9] = in->m[2][1];
1051         out[10] = in->m[2][2];
1052         out[11] = in->m[2][3];
1053 #endif
1054 }
1055
1056 void Matrix4x4_FromArray12FloatD3D(matrix4x4_t *out, const float in[12])
1057 {
1058 #ifdef MATRIX4x4_OPENGLORIENTATION
1059         out->m[0][0] = in[0];
1060         out->m[1][0] = in[1];
1061         out->m[2][0] = in[2];
1062         out->m[3][0] = in[3];
1063         out->m[0][1] = in[4];
1064         out->m[1][1] = in[5];
1065         out->m[2][1] = in[6];
1066         out->m[3][1] = in[7];
1067         out->m[0][2] = in[8];
1068         out->m[1][2] = in[9];
1069         out->m[2][2] = in[10];
1070         out->m[3][2] = in[11];
1071         out->m[0][3] = 0;
1072         out->m[1][3] = 0;
1073         out->m[2][3] = 0;
1074         out->m[3][3] = 1;
1075 #else
1076         out->m[0][0] = in[0];
1077         out->m[0][1] = in[1];
1078         out->m[0][2] = in[2];
1079         out->m[0][3] = in[3];
1080         out->m[1][0] = in[4];
1081         out->m[1][1] = in[5];
1082         out->m[1][2] = in[6];
1083         out->m[1][3] = in[7];
1084         out->m[2][0] = in[8];
1085         out->m[2][1] = in[9];
1086         out->m[2][2] = in[10];
1087         out->m[2][3] = in[11];
1088         out->m[3][0] = 0;
1089         out->m[3][1] = 0;
1090         out->m[3][2] = 0;
1091         out->m[3][3] = 1;
1092 #endif
1093 }
1094
1095 void Matrix4x4_FromOriginQuat(matrix4x4_t *m, double ox, double oy, double oz, double x, double y, double z, double w)
1096 {
1097 #ifdef MATRIX4x4_OPENGLORIENTATION
1098         m->m[0][0]=1-2*(y*y+z*z);m->m[1][0]=  2*(x*y-z*w);m->m[2][0]=  2*(x*z+y*w);m->m[3][0]=ox;
1099         m->m[0][1]=  2*(x*y+z*w);m->m[1][1]=1-2*(x*x+z*z);m->m[2][1]=  2*(y*z-x*w);m->m[3][1]=oy;
1100         m->m[0][2]=  2*(x*z-y*w);m->m[1][2]=  2*(y*z+x*w);m->m[2][2]=1-2*(x*x+y*y);m->m[3][2]=oz;
1101         m->m[0][3]=  0          ;m->m[1][3]=  0          ;m->m[2][3]=  0          ;m->m[3][3]=1;
1102 #else
1103         m->m[0][0]=1-2*(y*y+z*z);m->m[0][1]=  2*(x*y-z*w);m->m[0][2]=  2*(x*z+y*w);m->m[0][3]=ox;
1104         m->m[1][0]=  2*(x*y+z*w);m->m[1][1]=1-2*(x*x+z*z);m->m[1][2]=  2*(y*z-x*w);m->m[1][3]=oy;
1105         m->m[2][0]=  2*(x*z-y*w);m->m[2][1]=  2*(y*z+x*w);m->m[2][2]=1-2*(x*x+y*y);m->m[2][3]=oz;
1106         m->m[3][0]=  0          ;m->m[3][1]=  0          ;m->m[3][2]=  0          ;m->m[3][3]=1;
1107 #endif
1108 }
1109
1110 // LordHavoc: I got this code from:
1111 //http://www.doom3world.org/phpbb2/viewtopic.php?t=2884
1112 void Matrix4x4_FromDoom3Joint(matrix4x4_t *m, double ox, double oy, double oz, double x, double y, double z)
1113 {
1114         double w = 1.0 - (x*x+y*y+z*z);
1115         w = w > 0.0 ? -sqrt(w) : 0.0;
1116 #ifdef MATRIX4x4_OPENGLORIENTATION
1117         m->m[0][0]=1-2*(y*y+z*z);m->m[1][0]=  2*(x*y-z*w);m->m[2][0]=  2*(x*z+y*w);m->m[3][0]=ox;
1118         m->m[0][1]=  2*(x*y+z*w);m->m[1][1]=1-2*(x*x+z*z);m->m[2][1]=  2*(y*z-x*w);m->m[3][1]=oy;
1119         m->m[0][2]=  2*(x*z-y*w);m->m[1][2]=  2*(y*z+x*w);m->m[2][2]=1-2*(x*x+y*y);m->m[3][2]=oz;
1120         m->m[0][3]=  0          ;m->m[1][3]=  0          ;m->m[2][3]=  0          ;m->m[3][3]=1;
1121 #else
1122         m->m[0][0]=1-2*(y*y+z*z);m->m[0][1]=  2*(x*y-z*w);m->m[0][2]=  2*(x*z+y*w);m->m[0][3]=ox;
1123         m->m[1][0]=  2*(x*y+z*w);m->m[1][1]=1-2*(x*x+z*z);m->m[1][2]=  2*(y*z-x*w);m->m[1][3]=oy;
1124         m->m[2][0]=  2*(x*z-y*w);m->m[2][1]=  2*(y*z+x*w);m->m[2][2]=1-2*(x*x+y*y);m->m[2][3]=oz;
1125         m->m[3][0]=  0          ;m->m[3][1]=  0          ;m->m[3][2]=  0          ;m->m[3][3]=1;
1126 #endif
1127 }
1128
1129 void Matrix4x4_Blend (matrix4x4_t *out, const matrix4x4_t *in1, const matrix4x4_t *in2, double blend)
1130 {
1131         double iblend = 1 - blend;
1132         out->m[0][0] = in1->m[0][0] * iblend + in2->m[0][0] * blend;
1133         out->m[0][1] = in1->m[0][1] * iblend + in2->m[0][1] * blend;
1134         out->m[0][2] = in1->m[0][2] * iblend + in2->m[0][2] * blend;
1135         out->m[0][3] = in1->m[0][3] * iblend + in2->m[0][3] * blend;
1136         out->m[1][0] = in1->m[1][0] * iblend + in2->m[1][0] * blend;
1137         out->m[1][1] = in1->m[1][1] * iblend + in2->m[1][1] * blend;
1138         out->m[1][2] = in1->m[1][2] * iblend + in2->m[1][2] * blend;
1139         out->m[1][3] = in1->m[1][3] * iblend + in2->m[1][3] * blend;
1140         out->m[2][0] = in1->m[2][0] * iblend + in2->m[2][0] * blend;
1141         out->m[2][1] = in1->m[2][1] * iblend + in2->m[2][1] * blend;
1142         out->m[2][2] = in1->m[2][2] * iblend + in2->m[2][2] * blend;
1143         out->m[2][3] = in1->m[2][3] * iblend + in2->m[2][3] * blend;
1144         out->m[3][0] = in1->m[3][0] * iblend + in2->m[3][0] * blend;
1145         out->m[3][1] = in1->m[3][1] * iblend + in2->m[3][1] * blend;
1146         out->m[3][2] = in1->m[3][2] * iblend + in2->m[3][2] * blend;
1147         out->m[3][3] = in1->m[3][3] * iblend + in2->m[3][3] * blend;
1148 }
1149
1150
1151 void Matrix4x4_Transform (const matrix4x4_t *in, const float v[3], float out[3])
1152 {
1153 #ifdef MATRIX4x4_OPENGLORIENTATION
1154         out[0] = v[0] * in->m[0][0] + v[1] * in->m[1][0] + v[2] * in->m[2][0] + in->m[3][0];
1155         out[1] = v[0] * in->m[0][1] + v[1] * in->m[1][1] + v[2] * in->m[2][1] + in->m[3][1];
1156         out[2] = v[0] * in->m[0][2] + v[1] * in->m[1][2] + v[2] * in->m[2][2] + in->m[3][2];
1157 #else
1158         out[0] = v[0] * in->m[0][0] + v[1] * in->m[0][1] + v[2] * in->m[0][2] + in->m[0][3];
1159         out[1] = v[0] * in->m[1][0] + v[1] * in->m[1][1] + v[2] * in->m[1][2] + in->m[1][3];
1160         out[2] = v[0] * in->m[2][0] + v[1] * in->m[2][1] + v[2] * in->m[2][2] + in->m[2][3];
1161 #endif
1162 }
1163
1164 void Matrix4x4_Transform4 (const matrix4x4_t *in, const float v[4], float out[4])
1165 {
1166 #ifdef MATRIX4x4_OPENGLORIENTATION
1167         out[0] = v[0] * in->m[0][0] + v[1] * in->m[1][0] + v[2] * in->m[2][0] + v[3] * in->m[3][0];
1168         out[1] = v[0] * in->m[0][1] + v[1] * in->m[1][1] + v[2] * in->m[2][1] + v[3] * in->m[3][1];
1169         out[2] = v[0] * in->m[0][2] + v[1] * in->m[1][2] + v[2] * in->m[2][2] + v[3] * in->m[3][2];
1170         out[3] = v[0] * in->m[0][3] + v[1] * in->m[1][3] + v[2] * in->m[2][3] + v[3] * in->m[3][3];
1171 #else
1172         out[0] = v[0] * in->m[0][0] + v[1] * in->m[0][1] + v[2] * in->m[0][2] + v[3] * in->m[0][3];
1173         out[1] = v[0] * in->m[1][0] + v[1] * in->m[1][1] + v[2] * in->m[1][2] + v[3] * in->m[1][3];
1174         out[2] = v[0] * in->m[2][0] + v[1] * in->m[2][1] + v[2] * in->m[2][2] + v[3] * in->m[2][3];
1175         out[3] = v[0] * in->m[3][0] + v[1] * in->m[3][1] + v[2] * in->m[3][2] + v[3] * in->m[3][3];
1176 #endif
1177 }
1178
1179 void Matrix4x4_Transform3x3 (const matrix4x4_t *in, const float v[3], float out[3])
1180 {
1181 #ifdef MATRIX4x4_OPENGLORIENTATION
1182         out[0] = v[0] * in->m[0][0] + v[1] * in->m[1][0] + v[2] * in->m[2][0];
1183         out[1] = v[0] * in->m[0][1] + v[1] * in->m[1][1] + v[2] * in->m[2][1];
1184         out[2] = v[0] * in->m[0][2] + v[1] * in->m[1][2] + v[2] * in->m[2][2];
1185 #else
1186         out[0] = v[0] * in->m[0][0] + v[1] * in->m[0][1] + v[2] * in->m[0][2];
1187         out[1] = v[0] * in->m[1][0] + v[1] * in->m[1][1] + v[2] * in->m[1][2];
1188         out[2] = v[0] * in->m[2][0] + v[1] * in->m[2][1] + v[2] * in->m[2][2];
1189 #endif
1190 }
1191
1192 /*
1193 void Matrix4x4_SimpleUntransform (const matrix4x4_t *in, const float v[3], float out[3])
1194 {
1195         double t[3];
1196 #ifdef MATRIX4x4_OPENGLORIENTATION
1197         t[0] = v[0] - in->m[3][0];
1198         t[1] = v[1] - in->m[3][1];
1199         t[2] = v[2] - in->m[3][2];
1200         out[0] = t[0] * in->m[0][0] + t[1] * in->m[0][1] + t[2] * in->m[0][2];
1201         out[1] = t[0] * in->m[1][0] + t[1] * in->m[1][1] + t[2] * in->m[1][2];
1202         out[2] = t[0] * in->m[2][0] + t[1] * in->m[2][1] + t[2] * in->m[2][2];
1203 #else
1204         t[0] = v[0] - in->m[0][3];
1205         t[1] = v[1] - in->m[1][3];
1206         t[2] = v[2] - in->m[2][3];
1207         out[0] = t[0] * in->m[0][0] + t[1] * in->m[1][0] + t[2] * in->m[2][0];
1208         out[1] = t[0] * in->m[0][1] + t[1] * in->m[1][1] + t[2] * in->m[2][1];
1209         out[2] = t[0] * in->m[0][2] + t[1] * in->m[1][2] + t[2] * in->m[2][2];
1210 #endif
1211 }
1212 */
1213
1214 // FIXME: optimize
1215 void Matrix4x4_ConcatTranslate (matrix4x4_t *out, double x, double y, double z)
1216 {
1217         matrix4x4_t base, temp;
1218         base = *out;
1219         Matrix4x4_CreateTranslate(&temp, x, y, z);
1220         Matrix4x4_Concat(out, &base, &temp);
1221 }
1222
1223 // FIXME: optimize
1224 void Matrix4x4_ConcatRotate (matrix4x4_t *out, double angle, double x, double y, double z)
1225 {
1226         matrix4x4_t base, temp;
1227         base = *out;
1228         Matrix4x4_CreateRotate(&temp, angle, x, y, z);
1229         Matrix4x4_Concat(out, &base, &temp);
1230 }
1231
1232 // FIXME: optimize
1233 void Matrix4x4_ConcatScale (matrix4x4_t *out, double x)
1234 {
1235         matrix4x4_t base, temp;
1236         base = *out;
1237         Matrix4x4_CreateScale(&temp, x);
1238         Matrix4x4_Concat(out, &base, &temp);
1239 }
1240
1241 // FIXME: optimize
1242 void Matrix4x4_ConcatScale3 (matrix4x4_t *out, double x, double y, double z)
1243 {
1244         matrix4x4_t base, temp;
1245         base = *out;
1246         Matrix4x4_CreateScale3(&temp, x, y, z);
1247         Matrix4x4_Concat(out, &base, &temp);
1248 }
1249
1250 void Matrix4x4_OriginFromMatrix (const matrix4x4_t *in, float *out)
1251 {
1252 #ifdef MATRIX4x4_OPENGLORIENTATION
1253         out[0] = in->m[3][0];
1254         out[1] = in->m[3][1];
1255         out[2] = in->m[3][2];
1256 #else
1257         out[0] = in->m[0][3];
1258         out[1] = in->m[1][3];
1259         out[2] = in->m[2][3];
1260 #endif
1261 }
1262
1263 double Matrix4x4_ScaleFromMatrix (const matrix4x4_t *in)
1264 {
1265         // we only support uniform scaling, so assume the first row is enough
1266         return sqrt(in->m[0][0] * in->m[0][0] + in->m[0][1] * in->m[0][1] + in->m[0][2] * in->m[0][2]);
1267 }
1268
1269 void Matrix4x4_SetOrigin (matrix4x4_t *out, double x, double y, double z)
1270 {
1271 #ifdef MATRIX4x4_OPENGLORIENTATION
1272         out->m[3][0] = x;
1273         out->m[3][1] = y;
1274         out->m[3][2] = z;
1275 #else
1276         out->m[0][3] = x;
1277         out->m[1][3] = y;
1278         out->m[2][3] = z;
1279 #endif
1280 }
1281
1282 void Matrix4x4_AdjustOrigin (matrix4x4_t *out, double x, double y, double z)
1283 {
1284 #ifdef MATRIX4x4_OPENGLORIENTATION
1285         out->m[3][0] += x;
1286         out->m[3][1] += y;
1287         out->m[3][2] += z;
1288 #else
1289         out->m[0][3] += x;
1290         out->m[1][3] += y;
1291         out->m[2][3] += z;
1292 #endif
1293 }
1294
1295 void Matrix4x4_Scale (matrix4x4_t *out, double rotatescale, double originscale)
1296 {
1297         out->m[0][0] *= rotatescale;
1298         out->m[0][1] *= rotatescale;
1299         out->m[0][2] *= rotatescale;
1300         out->m[1][0] *= rotatescale;
1301         out->m[1][1] *= rotatescale;
1302         out->m[1][2] *= rotatescale;
1303         out->m[2][0] *= rotatescale;
1304         out->m[2][1] *= rotatescale;
1305         out->m[2][2] *= rotatescale;
1306 #ifdef MATRIX4x4_OPENGLORIENTATION
1307         out->m[3][0] *= originscale;
1308         out->m[3][1] *= originscale;
1309         out->m[3][2] *= originscale;
1310 #else
1311         out->m[0][3] *= originscale;
1312         out->m[1][3] *= originscale;
1313         out->m[2][3] *= originscale;
1314 #endif
1315 }