]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - mathlib.c
fix PBLEND_MOD in fog
[xonotic/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 float Q_RSqrt(float number)
145 {
146         float y;
147
148         if (number == 0.0f)
149                 return 0.0f;
150
151         *((int *)&y) = 0x5f3759df - ((* (int *) &number) >> 1);
152         return y * (1.5f - (number * 0.5f * y * y));
153 }
154
155 // assumes "src" is normalized
156 void PerpendicularVector( vec3_t dst, const vec3_t src )
157 {
158         // LordHavoc: optimized to death and beyond
159         int pos;
160         float minelem;
161
162         if (src[0])
163         {
164                 dst[0] = 0;
165                 if (src[1])
166                 {
167                         dst[1] = 0;
168                         if (src[2])
169                         {
170                                 dst[2] = 0;
171                                 pos = 0;
172                                 minelem = fabs(src[0]);
173                                 if (fabs(src[1]) < minelem)
174                                 {
175                                         pos = 1;
176                                         minelem = fabs(src[1]);
177                                 }
178                                 if (fabs(src[2]) < minelem)
179                                         pos = 2;
180
181                                 dst[pos] = 1;
182                                 dst[0] -= src[pos] * src[0];
183                                 dst[1] -= src[pos] * src[1];
184                                 dst[2] -= src[pos] * src[2];
185
186                                 // normalize the result
187                                 VectorNormalize(dst);
188                         }
189                         else
190                                 dst[2] = 1;
191                 }
192                 else
193                 {
194                         dst[1] = 1;
195                         dst[2] = 0;
196                 }
197         }
198         else
199         {
200                 dst[0] = 1;
201                 dst[1] = 0;
202                 dst[2] = 0;
203         }
204 }
205 #endif
206
207
208 // LordHavoc: like AngleVectors, but taking a forward vector instead of angles, useful!
209 void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up)
210 {
211         float d;
212
213         right[0] = forward[2];
214         right[1] = -forward[0];
215         right[2] = forward[1];
216
217         d = DotProduct(forward, right);
218         VectorMA(right, -d, forward, right);
219         VectorNormalizeFast(right);
220         CrossProduct(right, forward, up);
221 }
222
223 void VectorVectorsDouble(const double *forward, double *right, double *up)
224 {
225         double d;
226
227         right[0] = forward[2];
228         right[1] = -forward[0];
229         right[2] = forward[1];
230
231         d = DotProduct(forward, right);
232         VectorMA(right, -d, forward, right);
233         VectorNormalize(right);
234         CrossProduct(right, forward, up);
235 }
236
237 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
238 {
239         float t0, t1;
240         float angle, c, s;
241         vec3_t vr, vu, vf;
242
243         angle = DEG2RAD(degrees);
244         c = cos(angle);
245         s = sin(angle);
246         VectorCopy(dir, vf);
247         VectorVectors(vf, vr, vu);
248
249         t0 = vr[0] *  c + vu[0] * -s;
250         t1 = vr[0] *  s + vu[0] *  c;
251         dst[0] = (t0 * vr[0] + t1 * vu[0] + vf[0] * vf[0]) * point[0]
252                + (t0 * vr[1] + t1 * vu[1] + vf[0] * vf[1]) * point[1]
253                + (t0 * vr[2] + t1 * vu[2] + vf[0] * vf[2]) * point[2];
254
255         t0 = vr[1] *  c + vu[1] * -s;
256         t1 = vr[1] *  s + vu[1] *  c;
257         dst[1] = (t0 * vr[0] + t1 * vu[0] + vf[1] * vf[0]) * point[0]
258                + (t0 * vr[1] + t1 * vu[1] + vf[1] * vf[1]) * point[1]
259                + (t0 * vr[2] + t1 * vu[2] + vf[1] * vf[2]) * point[2];
260
261         t0 = vr[2] *  c + vu[2] * -s;
262         t1 = vr[2] *  s + vu[2] *  c;
263         dst[2] = (t0 * vr[0] + t1 * vu[0] + vf[2] * vf[0]) * point[0]
264                + (t0 * vr[1] + t1 * vu[1] + vf[2] * vf[1]) * point[1]
265                + (t0 * vr[2] + t1 * vu[2] + vf[2] * vf[2]) * point[2];
266 }
267
268 /*-----------------------------------------------------------------*/
269
270
271 void PlaneClassify(mplane_t *p)
272 {
273         // for optimized plane comparisons
274         if (p->normal[0] == 1)
275                 p->type = 0;
276         else if (p->normal[1] == 1)
277                 p->type = 1;
278         else if (p->normal[2] == 1)
279                 p->type = 2;
280         else
281                 p->type = 3;
282         // for BoxOnPlaneSide
283         p->signbits = 0;
284         if (p->normal[0] < 0) // 1
285                 p->signbits |= 1;
286         if (p->normal[1] < 0) // 2
287                 p->signbits |= 2;
288         if (p->normal[2] < 0) // 4
289                 p->signbits |= 4;
290 }
291
292 int BoxOnPlaneSide(const vec3_t emins, const vec3_t emaxs, const mplane_t *p)
293 {
294         if (p->type < 3)
295                 return ((emaxs[p->type] >= p->dist) | ((emins[p->type] < p->dist) << 1));
296         switch(p->signbits)
297         {
298         default:
299         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));
300         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));
301         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));
302         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));
303         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));
304         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));
305         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));
306         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));
307         }
308 }
309
310 int BoxOnPlaneSide_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, const vec_t dist)
311 {
312         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
313         {
314         default:
315         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));
316         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));
317         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));
318         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));
319         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));
320         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));
321         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));
322         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));
323         }
324 }
325
326 void BoxPlaneCorners(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec3_t outnear, vec3_t outfar)
327 {
328         if (p->type < 3)
329         {
330                 outnear[0] = outnear[1] = outnear[2] = outfar[0] = outfar[1] = outfar[2] = 0;
331                 outnear[p->type] = emins[p->type];
332                 outfar[p->type] = emaxs[p->type];
333                 return;
334         }
335         switch(p->signbits)
336         {
337         default:
338         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;
339         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;
340         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;
341         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;
342         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;
343         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;
344         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;
345         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;
346         }
347 }
348
349 void BoxPlaneCorners_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec3_t outnear, vec3_t outfar)
350 {
351         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
352         {
353         default:
354         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;
355         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;
356         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;
357         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;
358         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;
359         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;
360         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;
361         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;
362         }
363 }
364
365 void BoxPlaneCornerDistances(const vec3_t emins, const vec3_t emaxs, const mplane_t *p, vec_t *outneardist, vec_t *outfardist)
366 {
367         if (p->type < 3)
368         {
369                 *outneardist = emins[p->type] - p->dist;
370                 *outfardist = emaxs[p->type] - p->dist;
371                 return;
372         }
373         switch(p->signbits)
374         {
375         default:
376         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;
377         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;
378         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;
379         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;
380         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;
381         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;
382         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;
383         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;
384         }
385 }
386
387 void BoxPlaneCornerDistances_Separate(const vec3_t emins, const vec3_t emaxs, const vec3_t normal, vec_t *outneardist, vec_t *outfardist)
388 {
389         switch((normal[0] < 0) | ((normal[1] < 0) << 1) | ((normal[2] < 0) << 2))
390         {
391         default:
392         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;
393         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;
394         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;
395         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;
396         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;
397         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;
398         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;
399         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;
400         }
401 }
402
403 void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
404 {
405         double angle, sr, sp, sy, cr, cp, cy;
406
407         angle = angles[YAW] * (M_PI*2 / 360);
408         sy = sin(angle);
409         cy = cos(angle);
410         angle = angles[PITCH] * (M_PI*2 / 360);
411         sp = sin(angle);
412         cp = cos(angle);
413         if (forward)
414         {
415                 forward[0] = cp*cy;
416                 forward[1] = cp*sy;
417                 forward[2] = -sp;
418         }
419         if (right || up)
420         {
421                 if (angles[ROLL])
422                 {
423                         angle = angles[ROLL] * (M_PI*2 / 360);
424                         sr = sin(angle);
425                         cr = cos(angle);
426                         if (right)
427                         {
428                                 right[0] = -1*(sr*sp*cy+cr*-sy);
429                                 right[1] = -1*(sr*sp*sy+cr*cy);
430                                 right[2] = -1*(sr*cp);
431                         }
432                         if (up)
433                         {
434                                 up[0] = (cr*sp*cy+-sr*-sy);
435                                 up[1] = (cr*sp*sy+-sr*cy);
436                                 up[2] = cr*cp;
437                         }
438                 }
439                 else
440                 {
441                         if (right)
442                         {
443                                 right[0] = sy;
444                                 right[1] = -cy;
445                                 right[2] = 0;
446                         }
447                         if (up)
448                         {
449                                 up[0] = (sp*cy);
450                                 up[1] = (sp*sy);
451                                 up[2] = cp;
452                         }
453                 }
454         }
455 }
456
457 void AngleVectorsFLU (const vec3_t angles, vec3_t forward, vec3_t left, vec3_t up)
458 {
459         double angle, sr, sp, sy, cr, cp, cy;
460
461         angle = angles[YAW] * (M_PI*2 / 360);
462         sy = sin(angle);
463         cy = cos(angle);
464         angle = angles[PITCH] * (M_PI*2 / 360);
465         sp = sin(angle);
466         cp = cos(angle);
467         if (forward)
468         {
469                 forward[0] = cp*cy;
470                 forward[1] = cp*sy;
471                 forward[2] = -sp;
472         }
473         if (left || up)
474         {
475                 if (angles[ROLL])
476                 {
477                         angle = angles[ROLL] * (M_PI*2 / 360);
478                         sr = sin(angle);
479                         cr = cos(angle);
480                         if (left)
481                         {
482                                 left[0] = sr*sp*cy+cr*-sy;
483                                 left[1] = sr*sp*sy+cr*cy;
484                                 left[2] = sr*cp;
485                         }
486                         if (up)
487                         {
488                                 up[0] = cr*sp*cy+-sr*-sy;
489                                 up[1] = cr*sp*sy+-sr*cy;
490                                 up[2] = cr*cp;
491                         }
492                 }
493                 else
494                 {
495                         if (left)
496                         {
497                                 left[0] = -sy;
498                                 left[1] = cy;
499                                 left[2] = 0;
500                         }
501                         if (up)
502                         {
503                                 up[0] = sp*cy;
504                                 up[1] = sp*sy;
505                                 up[2] = cp;
506                         }
507                 }
508         }
509 }
510
511 #if 0
512 void AngleMatrix (const vec3_t angles, const vec3_t translate, vec_t matrix[][4])
513 {
514         double angle, sr, sp, sy, cr, cp, cy;
515
516         angle = angles[YAW] * (M_PI*2 / 360);
517         sy = sin(angle);
518         cy = cos(angle);
519         angle = angles[PITCH] * (M_PI*2 / 360);
520         sp = sin(angle);
521         cp = cos(angle);
522         angle = angles[ROLL] * (M_PI*2 / 360);
523         sr = sin(angle);
524         cr = cos(angle);
525         matrix[0][0] = cp*cy;
526         matrix[0][1] = sr*sp*cy+cr*-sy;
527         matrix[0][2] = cr*sp*cy+-sr*-sy;
528         matrix[0][3] = translate[0];
529         matrix[1][0] = cp*sy;
530         matrix[1][1] = sr*sp*sy+cr*cy;
531         matrix[1][2] = cr*sp*sy+-sr*cy;
532         matrix[1][3] = translate[1];
533         matrix[2][0] = -sp;
534         matrix[2][1] = sr*cp;
535         matrix[2][2] = cr*cp;
536         matrix[2][3] = translate[2];
537 }
538 #endif
539
540
541 // LordHavoc: renamed this to Length, and made the normal one a #define
542 float VectorNormalizeLength (vec3_t v)
543 {
544         float length, ilength;
545
546         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
547         length = sqrt (length);
548
549         if (length)
550         {
551                 ilength = 1/length;
552                 v[0] *= ilength;
553                 v[1] *= ilength;
554                 v[2] *= ilength;
555         }
556
557         return length;
558
559 }
560
561
562 /*
563 ================
564 R_ConcatRotations
565 ================
566 */
567 void R_ConcatRotations (const float in1[3*3], const float in2[3*3], float out[3*3])
568 {
569         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];
570         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];
571         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];
572         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];
573         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];
574         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];
575         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];
576         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];
577         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];
578 }
579
580
581 /*
582 ================
583 R_ConcatTransforms
584 ================
585 */
586 void R_ConcatTransforms (const float in1[3*4], const float in2[3*4], float out[3*4])
587 {
588         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];
589         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];
590         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];
591         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];
592         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];
593         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];
594         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];
595         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];
596         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];
597         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];
598         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];
599         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];
600 }
601
602 float RadiusFromBounds (const vec3_t mins, const vec3_t maxs)
603 {
604         vec3_t m1, m2;
605         VectorMultiply(mins, mins, m1);
606         VectorMultiply(maxs, maxs, m2);
607         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
608 }
609
610 float RadiusFromBoundsAndOrigin (const vec3_t mins, const vec3_t maxs, const vec3_t origin)
611 {
612         vec3_t m1, m2;
613         VectorSubtract(mins, origin, m1);VectorMultiply(m1, m1, m1);
614         VectorSubtract(maxs, origin, m2);VectorMultiply(m2, m2, m2);
615         return sqrt(max(m1[0], m2[0]) + max(m1[1], m2[1]) + max(m1[2], m2[2]));
616 }
617
618 void Mathlib_Init(void)
619 {
620         int a;
621
622         // LordHavoc: setup 1.0f / N table for quick recipricols of integers
623         ixtable[0] = 0;
624         for (a = 1;a < 4096;a++)
625                 ixtable[a] = 1.0f / a;
626 }
627
628 #include "matrixlib.h"
629
630 void Matrix4x4_Print(const matrix4x4_t *in)
631 {
632         Con_Printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
633         , in->m[0][0], in->m[0][1], in->m[0][2], in->m[0][3]
634         , in->m[1][0], in->m[1][1], in->m[1][2], in->m[1][3]
635         , in->m[2][0], in->m[2][1], in->m[2][2], in->m[2][3]
636         , in->m[3][0], in->m[3][1], in->m[3][2], in->m[3][3]);
637 }
638
639 int Math_atov(const char *s, vec3_t out)
640 {
641         int i;
642         VectorClear(out);
643         if (*s == '\'')
644                 s++;
645         for (i = 0;i < 3;i++)
646         {
647                 while (*s == ' ' || *s == '\t')
648                         s++;
649                 out[i] = atof (s);
650                 if (out[i] == 0 && *s != '-' && *s != '+' && (*s < '0' || *s > '9'))
651                         break; // not a number
652                 while (*s && *s != ' ' && *s !='\t' && *s != '\'')
653                         s++;
654                 if (*s == '\'')
655                         break;
656         }
657         return i;
658 }
659
660 void BoxFromPoints(vec3_t mins, vec3_t maxs, int numpoints, vec_t *point3f)
661 {
662         int i;
663         VectorCopy(point3f, mins);
664         VectorCopy(point3f, maxs);
665         for (i = 1, point3f += 3;i < numpoints;i++, point3f += 3)
666         {
667                 mins[0] = min(mins[0], point3f[0]);maxs[0] = max(maxs[0], point3f[0]);
668                 mins[1] = min(mins[1], point3f[1]);maxs[1] = max(maxs[1], point3f[1]);
669                 mins[2] = min(mins[2], point3f[2]);maxs[2] = max(maxs[2], point3f[2]);
670         }
671 }
672