]> icculus.org git repositories - divverent/darkplaces.git/blob - mathlib.c
removed old surface renderer
[divverent/darkplaces.git] / mathlib.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // mathlib.c -- math primitives
21
22 #include <math.h>
23 #include "quakedef.h"
24
25 vec3_t vec3_origin = {0,0,0};
26 float ixtable[4096];
27
28 /*-----------------------------------------------------------------*/
29
30 float m_bytenormals[NUMVERTEXNORMALS][3] =
31 {
32 {-0.525731, 0.000000, 0.850651}, {-0.442863, 0.238856, 0.864188},
33 {-0.295242, 0.000000, 0.955423}, {-0.309017, 0.500000, 0.809017},
34 {-0.162460, 0.262866, 0.951056}, {0.000000, 0.000000, 1.000000},
35 {0.000000, 0.850651, 0.525731}, {-0.147621, 0.716567, 0.681718},
36 {0.147621, 0.716567, 0.681718}, {0.000000, 0.525731, 0.850651},
37 {0.309017, 0.500000, 0.809017}, {0.525731, 0.000000, 0.850651},
38 {0.295242, 0.000000, 0.955423}, {0.442863, 0.238856, 0.864188},
39 {0.162460, 0.262866, 0.951056}, {-0.681718, 0.147621, 0.716567},
40 {-0.809017, 0.309017, 0.500000}, {-0.587785, 0.425325, 0.688191},
41 {-0.850651, 0.525731, 0.000000}, {-0.864188, 0.442863, 0.238856},
42 {-0.716567, 0.681718, 0.147621}, {-0.688191, 0.587785, 0.425325},
43 {-0.500000, 0.809017, 0.309017}, {-0.238856, 0.864188, 0.442863},
44 {-0.425325, 0.688191, 0.587785}, {-0.716567, 0.681718, -0.147621},
45 {-0.500000, 0.809017, -0.309017}, {-0.525731, 0.850651, 0.000000},
46 {0.000000, 0.850651, -0.525731}, {-0.238856, 0.864188, -0.442863},
47 {0.000000, 0.955423, -0.295242}, {-0.262866, 0.951056, -0.162460},
48 {0.000000, 1.000000, 0.000000}, {0.000000, 0.955423, 0.295242},
49 {-0.262866, 0.951056, 0.162460}, {0.238856, 0.864188, 0.442863},
50 {0.262866, 0.951056, 0.162460}, {0.500000, 0.809017, 0.309017},
51 {0.238856, 0.864188, -0.442863}, {0.262866, 0.951056, -0.162460},
52 {0.500000, 0.809017, -0.309017}, {0.850651, 0.525731, 0.000000},
53 {0.716567, 0.681718, 0.147621}, {0.716567, 0.681718, -0.147621},
54 {0.525731, 0.850651, 0.000000}, {0.425325, 0.688191, 0.587785},
55 {0.864188, 0.442863, 0.238856}, {0.688191, 0.587785, 0.425325},
56 {0.809017, 0.309017, 0.500000}, {0.681718, 0.147621, 0.716567},
57 {0.587785, 0.425325, 0.688191}, {0.955423, 0.295242, 0.000000},
58 {1.000000, 0.000000, 0.000000}, {0.951056, 0.162460, 0.262866},
59 {0.850651, -0.525731, 0.000000}, {0.955423, -0.295242, 0.000000},
60 {0.864188, -0.442863, 0.238856}, {0.951056, -0.162460, 0.262866},
61 {0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567},
62 {0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856},
63 {0.809017, 0.309017, -0.500000}, {0.951056, 0.162460, -0.262866},
64 {0.525731, 0.000000, -0.850651}, {0.681718, 0.147621, -0.716567},
65 {0.681718, -0.147621, -0.716567}, {0.850651, 0.000000, -0.525731},
66 {0.809017, -0.309017, -0.500000}, {0.864188, -0.442863, -0.238856},
67 {0.951056, -0.162460, -0.262866}, {0.147621, 0.716567, -0.681718},
68 {0.309017, 0.500000, -0.809017}, {0.425325, 0.688191, -0.587785},
69 {0.442863, 0.238856, -0.864188}, {0.587785, 0.425325, -0.688191},
70 {0.688191, 0.587785, -0.425325}, {-0.147621, 0.716567, -0.681718},
71 {-0.309017, 0.500000, -0.809017}, {0.000000, 0.525731, -0.850651},
72 {-0.525731, 0.000000, -0.850651}, {-0.442863, 0.238856, -0.864188},
73 {-0.295242, 0.000000, -0.955423}, {-0.162460, 0.262866, -0.951056},
74 {0.000000, 0.000000, -1.000000}, {0.295242, 0.000000, -0.955423},
75 {0.162460, 0.262866, -0.951056}, {-0.442863, -0.238856, -0.864188},
76 {-0.309017, -0.500000, -0.809017}, {-0.162460, -0.262866, -0.951056},
77 {0.000000, -0.850651, -0.525731}, {-0.147621, -0.716567, -0.681718},
78 {0.147621, -0.716567, -0.681718}, {0.000000, -0.525731, -0.850651},
79 {0.309017, -0.500000, -0.809017}, {0.442863, -0.238856, -0.864188},
80 {0.162460, -0.262866, -0.951056}, {0.238856, -0.864188, -0.442863},
81 {0.500000, -0.809017, -0.309017}, {0.425325, -0.688191, -0.587785},
82 {0.716567, -0.681718, -0.147621}, {0.688191, -0.587785, -0.425325},
83 {0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242},
84 {0.000000, -1.000000, 0.000000}, {0.262866, -0.951056, -0.162460},
85 {0.000000, -0.850651, 0.525731}, {0.000000, -0.955423, 0.295242},
86 {0.238856, -0.864188, 0.442863}, {0.262866, -0.951056, 0.162460},
87 {0.500000, -0.809017, 0.309017}, {0.716567, -0.681718, 0.147621},
88 {0.525731, -0.850651, 0.000000}, {-0.238856, -0.864188, -0.442863},
89 {-0.500000, -0.809017, -0.309017}, {-0.262866, -0.951056, -0.162460},
90 {-0.850651, -0.525731, 0.000000}, {-0.716567, -0.681718, -0.147621},
91 {-0.716567, -0.681718, 0.147621}, {-0.525731, -0.850651, 0.000000},
92 {-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863},
93 {-0.262866, -0.951056, 0.162460}, {-0.864188, -0.442863, 0.238856},
94 {-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325},
95 {-0.681718, -0.147621, 0.716567}, {-0.442863, -0.238856, 0.864188},
96 {-0.587785, -0.425325, 0.688191}, {-0.309017, -0.500000, 0.809017},
97 {-0.147621, -0.716567, 0.681718}, {-0.425325, -0.688191, 0.587785},
98 {-0.162460, -0.262866, 0.951056}, {0.442863, -0.238856, 0.864188},
99 {0.162460, -0.262866, 0.951056}, {0.309017, -0.500000, 0.809017},
100 {0.147621, -0.716567, 0.681718}, {0.000000, -0.525731, 0.850651},
101 {0.425325, -0.688191, 0.587785}, {0.587785, -0.425325, 0.688191},
102 {0.688191, -0.587785, 0.425325}, {-0.955423, 0.295242, 0.000000},
103 {-0.951056, 0.162460, 0.262866}, {-1.000000, 0.000000, 0.000000},
104 {-0.850651, 0.000000, 0.525731}, {-0.955423, -0.295242, 0.000000},
105 {-0.951056, -0.162460, 0.262866}, {-0.864188, 0.442863, -0.238856},
106 {-0.951056, 0.162460, -0.262866}, {-0.809017, 0.309017, -0.500000},
107 {-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866},
108 {-0.809017, -0.309017, -0.500000}, {-0.681718, 0.147621, -0.716567},
109 {-0.681718, -0.147621, -0.716567}, {-0.850651, 0.000000, -0.525731},
110 {-0.688191, 0.587785, -0.425325}, {-0.587785, 0.425325, -0.688191},
111 {-0.425325, 0.688191, -0.587785}, {-0.425325, -0.688191, -0.587785},
112 {-0.587785, -0.425325, -0.688191}, {-0.688191, -0.587785, -0.425325},
113 };
114
115 #if 0
116 qbyte NormalToByte(const vec3_t n)
117 {
118         int i, best;
119         float bestdistance, distance;
120
121         best = 0;
122         bestdistance = DotProduct (n, m_bytenormals[0]);
123         for (i = 1;i < NUMVERTEXNORMALS;i++)
124         {
125                 distance = DotProduct (n, m_bytenormals[i]);
126                 if (distance > bestdistance)
127                 {
128                         bestdistance = distance;
129                         best = i;
130                 }
131         }
132         return best;
133 }
134
135 // note: uses byte partly to force unsigned for the validity check
136 void ByteToNormal(qbyte num, vec3_t n)
137 {
138         if (num < NUMVERTEXNORMALS)
139                 VectorCopy(m_bytenormals[num], n);
140         else
141                 VectorClear(n); // FIXME: complain?
142 }
143
144 // assumes "src" is normalized
145 void PerpendicularVector( vec3_t dst, const vec3_t src )
146 {
147         // LordHavoc: optimized to death and beyond
148         int pos;
149         float minelem;
150
151         if (src[0])
152         {
153                 dst[0] = 0;
154                 if (src[1])
155                 {
156                         dst[1] = 0;
157                         if (src[2])
158                         {
159                                 dst[2] = 0;
160                                 pos = 0;
161                                 minelem = fabs(src[0]);
162                                 if (fabs(src[1]) < minelem)
163                                 {
164                                         pos = 1;
165                                         minelem = fabs(src[1]);
166                                 }
167                                 if (fabs(src[2]) < minelem)
168                                         pos = 2;
169
170                                 dst[pos] = 1;
171                                 dst[0] -= src[pos] * src[0];
172                                 dst[1] -= src[pos] * src[1];
173                                 dst[2] -= src[pos] * src[2];
174
175                                 // normalize the result
176                                 VectorNormalize(dst);
177                         }
178                         else
179                                 dst[2] = 1;
180                 }
181                 else
182                 {
183                         dst[1] = 1;
184                         dst[2] = 0;
185                 }
186         }
187         else
188         {
189                 dst[0] = 1;
190                 dst[1] = 0;
191                 dst[2] = 0;
192         }
193 }
194 #endif
195
196
197 // LordHavoc: like AngleVectors, but taking a forward vector instead of angles, useful!
198 void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up)
199 {
200         float d;
201
202         right[0] = forward[2];
203         right[1] = -forward[0];
204         right[2] = forward[1];
205
206         d = DotProduct(forward, right);
207         VectorMA(right, -d, forward, right);
208         VectorNormalize(right);
209         CrossProduct(right, forward, up);
210 }
211
212 void VectorVectorsDouble(const double *forward, double *right, double *up)
213 {
214         double d;
215
216         right[0] = forward[2];
217         right[1] = -forward[0];
218         right[2] = forward[1];
219
220         d = DotProduct(forward, right);
221         VectorMA(right, -d, forward, right);
222         VectorNormalize(right);
223         CrossProduct(right, forward, up);
224 }
225
226 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
227 {
228         float t0, t1;
229         float angle, c, s;
230         vec3_t vr, vu, vf;
231
232         angle = DEG2RAD(degrees);
233         c = cos(angle);
234         s = sin(angle);
235         VectorCopy(dir, vf);
236         VectorVectors(vf, vr, vu);
237
238         t0 = vr[0] *  c + vu[0] * -s;
239         t1 = vr[0] *  s + vu[0] *  c;
240         dst[0] = (t0 * vr[0] + t1 * vu[0] + vf[0] * vf[0]) * point[0]
241                + (t0 * vr[1] + t1 * vu[1] + vf[0] * vf[1]) * point[1]
242                + (t0 * vr[2] + t1 * vu[2] + vf[0] * vf[2]) * point[2];
243
244         t0 = vr[1] *  c + vu[1] * -s;
245         t1 = vr[1] *  s + vu[1] *  c;
246         dst[1] = (t0 * vr[0] + t1 * vu[0] + vf[1] * vf[0]) * point[0]
247                + (t0 * vr[1] + t1 * vu[1] + vf[1] * vf[1]) * point[1]
248                + (t0 * vr[2] + t1 * vu[2] + vf[1] * vf[2]) * point[2];
249
250         t0 = vr[2] *  c + vu[2] * -s;
251         t1 = vr[2] *  s + vu[2] *  c;
252         dst[2] = (t0 * vr[0] + t1 * vu[0] + vf[2] * vf[0]) * point[0]
253                + (t0 * vr[1] + t1 * vu[1] + vf[2] * vf[1]) * point[1]
254                + (t0 * vr[2] + t1 * vu[2] + vf[2] * vf[2]) * point[2];
255 }
256
257 /*-----------------------------------------------------------------*/
258
259
260 void PlaneClassify(mplane_t *p)
261 {
262         // for optimized plane comparisons
263         if (p->normal[0] == 1)
264                 p->type = 0;
265         else if (p->normal[1] == 1)
266                 p->type = 1;
267         else if (p->normal[2] == 1)
268                 p->type = 2;
269         else
270                 p->type = 3;
271         // for BoxOnPlaneSide
272         p->signbits = 0;
273         if (p->normal[0] < 0) // 1
274                 p->signbits |= 1;
275         if (p->normal[1] < 0) // 2
276                 p->signbits |= 2;
277         if (p->normal[2] < 0) // 4
278                 p->signbits |= 4;
279 }
280
281 int BoxOnPlaneSide(const vec3_t emins, const vec3_t emaxs, const mplane_t *p)
282 {
283         if (p->type < 3)
284                 return ((emaxs[p->type] >= p->dist) | ((emins[p->type] < p->dist) << 1));
285         switch(p->signbits)
286         {
287         default:
288         case 0: return (((p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]) >= p->dist) | (((p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]) < p->dist) << 1));
289         case 1: return (((p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]) >= p->dist) | (((p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]) < p->dist) << 1));
290         case 2: return (((p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]) >= p->dist) | (((p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]) < p->dist) << 1));
291         case 3: return (((p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]) >= p->dist) | (((p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]) < p->dist) << 1));
292         case 4: return (((p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]) >= p->dist) | (((p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]) < p->dist) << 1));
293         case 5: return (((p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2]) >= p->dist) | (((p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2]) < p->dist) << 1));
294         case 6: return (((p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]) >= p->dist) | (((p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]) < p->dist) << 1));
295         case 7: return (((p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2]) >= p->dist) | (((p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2]) < p->dist) << 1));
296         }
297 }
298
299 int BoxOnPlaneSide_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, const vec_t dist)
300 {
301         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
302         {
303         default:
304         case 0: return (((normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2]) >= dist) | (((normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emins[2]) < dist) << 1));
305         case 1: return (((normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2]) >= dist) | (((normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emins[2]) < dist) << 1));
306         case 2: return (((normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emaxs[2]) >= dist) | (((normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emins[2]) < dist) << 1));
307         case 3: return (((normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emaxs[2]) >= dist) | (((normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emins[2]) < dist) << 1));
308         case 4: return (((normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emins[2]) >= dist) | (((normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emaxs[2]) < dist) << 1));
309         case 5: return (((normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emins[2]) >= dist) | (((normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emaxs[2]) < dist) << 1));
310         case 6: return (((normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emins[2]) >= dist) | (((normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2]) < dist) << 1));
311         case 7: return (((normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emins[2]) >= dist) | (((normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2]) < dist) << 1));
312         }
313 }
314
315 void BoxPlaneCorners(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec3_t outnear, vec3_t outfar)
316 {
317         if (p->type < 3)
318         {
319                 outnear[0] = outnear[1] = outnear[2] = outfar[0] = outfar[1] = outfar[2] = 0;
320                 outnear[p->type] = emins[p->type];
321                 outfar[p->type] = emaxs[p->type];
322                 return;
323         }
324         switch(p->signbits)
325         {
326         default:
327         case 0: outnear[0] = emaxs[0];outnear[1] = emaxs[1];outnear[2] = emaxs[2];outfar[0] = emins[0];outfar[1] = emins[1];outfar[2] = emins[2];break;
328         case 1: outnear[0] = emins[0];outnear[1] = emaxs[1];outnear[2] = emaxs[2];outfar[0] = emaxs[0];outfar[1] = emins[1];outfar[2] = emins[2];break;
329         case 2: outnear[0] = emaxs[0];outnear[1] = emins[1];outnear[2] = emaxs[2];outfar[0] = emins[0];outfar[1] = emaxs[1];outfar[2] = emins[2];break;
330         case 3: outnear[0] = emins[0];outnear[1] = emins[1];outnear[2] = emaxs[2];outfar[0] = emaxs[0];outfar[1] = emaxs[1];outfar[2] = emins[2];break;
331         case 4: outnear[0] = emaxs[0];outnear[1] = emaxs[1];outnear[2] = emins[2];outfar[0] = emins[0];outfar[1] = emins[1];outfar[2] = emaxs[2];break;
332         case 5: outnear[0] = emins[0];outnear[1] = emaxs[1];outnear[2] = emins[2];outfar[0] = emaxs[0];outfar[1] = emins[1];outfar[2] = emaxs[2];break;
333         case 6: outnear[0] = emaxs[0];outnear[1] = emins[1];outnear[2] = emins[2];outfar[0] = emins[0];outfar[1] = emaxs[1];outfar[2] = emaxs[2];break;
334         case 7: outnear[0] = emins[0];outnear[1] = emins[1];outnear[2] = emins[2];outfar[0] = emaxs[0];outfar[1] = emaxs[1];outfar[2] = emaxs[2];break;
335         }
336 }
337
338 void BoxPlaneCorners_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec3_t outnear, vec3_t outfar)
339 {
340         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
341         {
342         default:
343         case 0: outnear[0] = emaxs[0];outnear[1] = emaxs[1];outnear[2] = emaxs[2];outfar[0] = emins[0];outfar[1] = emins[1];outfar[2] = emins[2];break;
344         case 1: outnear[0] = emins[0];outnear[1] = emaxs[1];outnear[2] = emaxs[2];outfar[0] = emaxs[0];outfar[1] = emins[1];outfar[2] = emins[2];break;
345         case 2: outnear[0] = emaxs[0];outnear[1] = emins[1];outnear[2] = emaxs[2];outfar[0] = emins[0];outfar[1] = emaxs[1];outfar[2] = emins[2];break;
346         case 3: outnear[0] = emins[0];outnear[1] = emins[1];outnear[2] = emaxs[2];outfar[0] = emaxs[0];outfar[1] = emaxs[1];outfar[2] = emins[2];break;
347         case 4: outnear[0] = emaxs[0];outnear[1] = emaxs[1];outnear[2] = emins[2];outfar[0] = emins[0];outfar[1] = emins[1];outfar[2] = emaxs[2];break;
348         case 5: outnear[0] = emins[0];outnear[1] = emaxs[1];outnear[2] = emins[2];outfar[0] = emaxs[0];outfar[1] = emins[1];outfar[2] = emaxs[2];break;
349         case 6: outnear[0] = emaxs[0];outnear[1] = emins[1];outnear[2] = emins[2];outfar[0] = emins[0];outfar[1] = emaxs[1];outfar[2] = emaxs[2];break;
350         case 7: outnear[0] = emins[0];outnear[1] = emins[1];outnear[2] = emins[2];outfar[0] = emaxs[0];outfar[1] = emaxs[1];outfar[2] = emaxs[2];break;
351         }
352 }
353
354 void BoxPlaneCornerDistances(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec_t *outneardist, vec_t *outfardist)
355 {
356         if (p->type < 3)
357         {
358                 *outneardist = emins[p->type] - p->dist;
359                 *outfardist = emaxs[p->type] - p->dist;
360                 return;
361         }
362         switch(p->signbits)
363         {
364         default:
365         case 0: *outneardist = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2] - p->dist;*outfardist = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2] - p->dist;break;
366         case 1: *outneardist = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2] - p->dist;*outfardist = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2] - p->dist;break;
367         case 2: *outneardist = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2] - p->dist;*outfardist = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2] - p->dist;break;
368         case 3: *outneardist = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2] - p->dist;*outfardist = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2] - p->dist;break;
369         case 4: *outneardist = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2] - p->dist;*outfardist = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2] - p->dist;break;
370         case 5: *outneardist = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emins[2] - p->dist;*outfardist = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emaxs[2] - p->dist;break;
371         case 6: *outneardist = p->normal[0] * emaxs[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2] - p->dist;*outfardist = p->normal[0] * emins[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2] - p->dist;break;
372         case 7: *outneardist = p->normal[0] * emins[0] + p->normal[1] * emins[1] + p->normal[2] * emins[2] - p->dist;*outfardist = p->normal[0] * emaxs[0] + p->normal[1] * emaxs[1] + p->normal[2] * emaxs[2] - p->dist;break;
373         }
374 }
375
376 void BoxPlaneCornerDistances_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec_t *outneardist, vec_t *outfardist)
377 {
378         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
379         {
380         default:
381         case 0: *outneardist = normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2];*outfardist = normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emins[2];break;
382         case 1: *outneardist = normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2];*outfardist = normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emins[2];break;
383         case 2: *outneardist = normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emaxs[2];*outfardist = normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emins[2];break;
384         case 3: *outneardist = normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emaxs[2];*outfardist = normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emins[2];break;
385         case 4: *outneardist = normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emins[2];*outfardist = normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emaxs[2];break;
386         case 5: *outneardist = normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emins[2];*outfardist = normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emaxs[2];break;
387         case 6: *outneardist = normal[0] * emaxs[0] + normal[1] * emins[1] + normal[2] * emins[2];*outfardist = normal[0] * emins[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2];break;
388         case 7: *outneardist = normal[0] * emins[0] + normal[1] * emins[1] + normal[2] * emins[2];*outfardist = normal[0] * emaxs[0] + normal[1] * emaxs[1] + normal[2] * emaxs[2];break;
389         }
390 }
391
392 void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
393 {
394         double angle, sr, sp, sy, cr, cp, cy;
395
396         angle = angles[YAW] * (M_PI*2 / 360);
397         sy = sin(angle);
398         cy = cos(angle);
399         angle = angles[PITCH] * (M_PI*2 / 360);
400         sp = sin(angle);
401         cp = cos(angle);
402         if (forward)
403         {
404                 forward[0] = cp*cy;
405                 forward[1] = cp*sy;
406                 forward[2] = -sp;
407         }
408         if (right || up)
409         {
410                 if (angles[ROLL])
411                 {
412                         angle = angles[ROLL] * (M_PI*2 / 360);
413                         sr = sin(angle);
414                         cr = cos(angle);
415                         if (right)
416                         {
417                                 right[0] = -1*(sr*sp*cy+cr*-sy);
418                                 right[1] = -1*(sr*sp*sy+cr*cy);
419                                 right[2] = -1*(sr*cp);
420                         }
421                         if (up)
422                         {
423                                 up[0] = (cr*sp*cy+-sr*-sy);
424                                 up[1] = (cr*sp*sy+-sr*cy);
425                                 up[2] = cr*cp;
426                         }
427                 }
428                 else
429                 {
430                         if (right)
431                         {
432                                 right[0] = sy;
433                                 right[1] = -cy;
434                                 right[2] = 0;
435                         }
436                         if (up)
437                         {
438                                 up[0] = (sp*cy);
439                                 up[1] = (sp*sy);
440                                 up[2] = cp;
441                         }
442                 }
443         }
444 }
445
446 void AngleVectorsFLU (const vec3_t angles, vec3_t forward, vec3_t left, vec3_t up)
447 {
448         double angle, sr, sp, sy, cr, cp, cy;
449
450         angle = angles[YAW] * (M_PI*2 / 360);
451         sy = sin(angle);
452         cy = cos(angle);
453         angle = angles[PITCH] * (M_PI*2 / 360);
454         sp = sin(angle);
455         cp = cos(angle);
456         if (forward)
457         {
458                 forward[0] = cp*cy;
459                 forward[1] = cp*sy;
460                 forward[2] = -sp;
461         }
462         if (left || up)
463         {
464                 if (angles[ROLL])
465                 {
466                         angle = angles[ROLL] * (M_PI*2 / 360);
467                         sr = sin(angle);
468                         cr = cos(angle);
469                         if (left)
470                         {
471                                 left[0] = sr*sp*cy+cr*-sy;
472                                 left[1] = sr*sp*sy+cr*cy;
473                                 left[2] = sr*cp;
474                         }
475                         if (up)
476                         {
477                                 up[0] = cr*sp*cy+-sr*-sy;
478                                 up[1] = cr*sp*sy+-sr*cy;
479                                 up[2] = cr*cp;
480                         }
481                 }
482                 else
483                 {
484                         if (left)
485                         {
486                                 left[0] = -sy;
487                                 left[1] = cy;
488                                 left[2] = 0;
489                         }
490                         if (up)
491                         {
492                                 up[0] = sp*cy;
493                                 up[1] = sp*sy;
494                                 up[2] = cp;
495                         }
496                 }
497         }
498 }
499
500 #if 0
501 void AngleMatrix (const vec3_t angles, const vec3_t translate, vec_t matrix[][4])
502 {
503         double angle, sr, sp, sy, cr, cp, cy;
504
505         angle = angles[YAW] * (M_PI*2 / 360);
506         sy = sin(angle);
507         cy = cos(angle);
508         angle = angles[PITCH] * (M_PI*2 / 360);
509         sp = sin(angle);
510         cp = cos(angle);
511         angle = angles[ROLL] * (M_PI*2 / 360);
512         sr = sin(angle);
513         cr = cos(angle);
514         matrix[0][0] = cp*cy;
515         matrix[0][1] = sr*sp*cy+cr*-sy;
516         matrix[0][2] = cr*sp*cy+-sr*-sy;
517         matrix[0][3] = translate[0];
518         matrix[1][0] = cp*sy;
519         matrix[1][1] = sr*sp*sy+cr*cy;
520         matrix[1][2] = cr*sp*sy+-sr*cy;
521         matrix[1][3] = translate[1];
522         matrix[2][0] = -sp;
523         matrix[2][1] = sr*cp;
524         matrix[2][2] = cr*cp;
525         matrix[2][3] = translate[2];
526 }
527 #endif
528
529
530 // LordHavoc: renamed this to Length, and made the normal one a #define
531 float VectorNormalizeLength (vec3_t v)
532 {
533         float length, ilength;
534
535         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
536         length = sqrt (length);
537
538         if (length)
539         {
540                 ilength = 1/length;
541                 v[0] *= ilength;
542                 v[1] *= ilength;
543                 v[2] *= ilength;
544         }
545
546         return length;
547
548 }
549
550
551 /*
552 ================
553 R_ConcatRotations
554 ================
555 */
556 void R_ConcatRotations (const float in1[3*3], const float in2[3*3], float out[3*3])
557 {
558         out[0*3+0] = in1[0*3+0] * in2[0*3+0] + in1[0*3+1] * in2[1*3+0] + in1[0*3+2] * in2[2*3+0];
559         out[0*3+1] = in1[0*3+0] * in2[0*3+1] + in1[0*3+1] * in2[1*3+1] + in1[0*3+2] * in2[2*3+1];
560         out[0*3+2] = in1[0*3+0] * in2[0*3+2] + in1[0*3+1] * in2[1*3+2] + in1[0*3+2] * in2[2*3+2];
561         out[1*3+0] = in1[1*3+0] * in2[0*3+0] + in1[1*3+1] * in2[1*3+0] + in1[1*3+2] * in2[2*3+0];
562         out[1*3+1] = in1[1*3+0] * in2[0*3+1] + in1[1*3+1] * in2[1*3+1] + in1[1*3+2] * in2[2*3+1];
563         out[1*3+2] = in1[1*3+0] * in2[0*3+2] + in1[1*3+1] * in2[1*3+2] + in1[1*3+2] * in2[2*3+2];
564         out[2*3+0] = in1[2*3+0] * in2[0*3+0] + in1[2*3+1] * in2[1*3+0] + in1[2*3+2] * in2[2*3+0];
565         out[2*3+1] = in1[2*3+0] * in2[0*3+1] + in1[2*3+1] * in2[1*3+1] + in1[2*3+2] * in2[2*3+1];
566         out[2*3+2] = in1[2*3+0] * in2[0*3+2] + in1[2*3+1] * in2[1*3+2] + in1[2*3+2] * in2[2*3+2];
567 }
568
569
570 /*
571 ================
572 R_ConcatTransforms
573 ================
574 */
575 void R_ConcatTransforms (const float in1[3*4], const float in2[3*4], float out[3*4])
576 {
577         out[0*4+0] = in1[0*4+0] * in2[0*4+0] + in1[0*4+1] * in2[1*4+0] + in1[0*4+2] * in2[2*4+0];
578         out[0*4+1] = in1[0*4+0] * in2[0*4+1] + in1[0*4+1] * in2[1*4+1] + in1[0*4+2] * in2[2*4+1];
579         out[0*4+2] = in1[0*4+0] * in2[0*4+2] + in1[0*4+1] * in2[1*4+2] + in1[0*4+2] * in2[2*4+2];
580         out[0*4+3] = in1[0*4+0] * in2[0*4+3] + in1[0*4+1] * in2[1*4+3] + in1[0*4+2] * in2[2*4+3] + in1[0*4+3];
581         out[1*4+0] = in1[1*4+0] * in2[0*4+0] + in1[1*4+1] * in2[1*4+0] + in1[1*4+2] * in2[2*4+0];
582         out[1*4+1] = in1[1*4+0] * in2[0*4+1] + in1[1*4+1] * in2[1*4+1] + in1[1*4+2] * in2[2*4+1];
583         out[1*4+2] = in1[1*4+0] * in2[0*4+2] + in1[1*4+1] * in2[1*4+2] + in1[1*4+2] * in2[2*4+2];
584         out[1*4+3] = in1[1*4+0] * in2[0*4+3] + in1[1*4+1] * in2[1*4+3] + in1[1*4+2] * in2[2*4+3] + in1[1*4+3];
585         out[2*4+0] = in1[2*4+0] * in2[0*4+0] + in1[2*4+1] * in2[1*4+0] + in1[2*4+2] * in2[2*4+0];
586         out[2*4+1] = in1[2*4+0] * in2[0*4+1] + in1[2*4+1] * in2[1*4+1] + in1[2*4+2] * in2[2*4+1];
587         out[2*4+2] = in1[2*4+0] * in2[0*4+2] + in1[2*4+1] * in2[1*4+2] + in1[2*4+2] * in2[2*4+2];
588         out[2*4+3] = in1[2*4+0] * in2[0*4+3] + in1[2*4+1] * in2[1*4+3] + in1[2*4+2] * in2[2*4+3] + in1[2*4+3];
589 }
590
591 float RadiusFromBounds (const vec3_t mins, const vec3_t maxs)
592 {
593         vec3_t m1, m2;
594         VectorMultiply(mins, mins, m1);
595         VectorMultiply(maxs, maxs, m2);
596         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
597 }
598
599 float RadiusFromBoundsAndOrigin (const vec3_t mins, const vec3_t maxs, const vec3_t origin)
600 {
601         vec3_t m1, m2;
602         VectorSubtract(mins, origin, m1);VectorMultiply(m1, m1, m1);
603         VectorSubtract(maxs, origin, m2);VectorMultiply(m2, m2, m2);
604         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
605 }
606
607 void Mathlib_Init(void)
608 {
609         int a;
610
611         // LordHavoc: setup 1.0f / N table for quick recipricols of integers
612         ixtable[0] = 0;
613         for (a = 1;a < 4096;a++)
614                 ixtable[a] = 1.0f / a;
615 }
616
617 #include "matrixlib.h"
618
619 void Matrix4x4_Print(const matrix4x4_t *in)
620 {
621         Con_Printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
622         , in->m[0][0], in->m[0][1], in->m[0][2], in->m[0][3]
623         , in->m[1][0], in->m[1][1], in->m[1][2], in->m[1][3]
624         , in->m[2][0], in->m[2][1], in->m[2][2], in->m[2][3]
625         , in->m[3][0], in->m[3][1], in->m[3][2], in->m[3][3]);
626 }
627
628 int Math_atov(const char *s, vec3_t out)
629 {
630         int i;
631         VectorClear(out);
632         if (*s == '\'')
633                 s++;
634         for (i = 0;i < 3;i++)
635         {
636                 while (*s == ' ' || *s == '\t')
637                         s++;
638                 out[i] = atof (s);
639                 if (out[i] == 0 && *s != '-' && *s != '+' && (*s < '0' || *s > '9'))
640                         break; // not a number
641                 while (*s && *s != ' ' && *s !='\t' && *s != '\'')
642                         s++;
643                 if (*s == '\'')
644                         break;
645         }
646         return i;
647 }
648
649 void BoxFromPoints(vec3_t mins, vec3_t maxs, int numpoints, vec_t *point3f)
650 {
651         int i;
652         VectorCopy(point3f, mins);
653         VectorCopy(point3f, maxs);
654         for (i = 1, point3f += 3;i < numpoints;i++, point3f += 3)
655         {
656                 mins[0] = min(mins[0], point3f[0]);maxs[0] = max(maxs[0], point3f[0]);
657                 mins[1] = min(mins[1], point3f[1]);maxs[1] = max(maxs[1], point3f[1]);
658                 mins[2] = min(mins[2], point3f[2]);maxs[2] = max(maxs[2], point3f[2]);
659         }
660 }
661