]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - mathlib.c
80c09fa13c308344b88ac95a7a700eb0e7dc0ec2
[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 void Sys_Error (char *error, ...);
26
27 vec3_t vec3_origin = {0,0,0};
28 float ixtable[4096];
29
30 /*-----------------------------------------------------------------*/
31
32 float m_bytenormals[NUMVERTEXNORMALS][3] =
33 {
34 {-0.525731, 0.000000, 0.850651}, {-0.442863, 0.238856, 0.864188}, 
35 {-0.295242, 0.000000, 0.955423}, {-0.309017, 0.500000, 0.809017}, 
36 {-0.162460, 0.262866, 0.951056}, {0.000000, 0.000000, 1.000000}, 
37 {0.000000, 0.850651, 0.525731}, {-0.147621, 0.716567, 0.681718}, 
38 {0.147621, 0.716567, 0.681718}, {0.000000, 0.525731, 0.850651}, 
39 {0.309017, 0.500000, 0.809017}, {0.525731, 0.000000, 0.850651}, 
40 {0.295242, 0.000000, 0.955423}, {0.442863, 0.238856, 0.864188}, 
41 {0.162460, 0.262866, 0.951056}, {-0.681718, 0.147621, 0.716567}, 
42 {-0.809017, 0.309017, 0.500000}, {-0.587785, 0.425325, 0.688191}, 
43 {-0.850651, 0.525731, 0.000000}, {-0.864188, 0.442863, 0.238856},
44 {-0.716567, 0.681718, 0.147621}, {-0.688191, 0.587785, 0.425325}, 
45 {-0.500000, 0.809017, 0.309017}, {-0.238856, 0.864188, 0.442863}, 
46 {-0.425325, 0.688191, 0.587785}, {-0.716567, 0.681718, -0.147621}, 
47 {-0.500000, 0.809017, -0.309017}, {-0.525731, 0.850651, 0.000000}, 
48 {0.000000, 0.850651, -0.525731}, {-0.238856, 0.864188, -0.442863},
49 {0.000000, 0.955423, -0.295242}, {-0.262866, 0.951056, -0.162460}, 
50 {0.000000, 1.000000, 0.000000}, {0.000000, 0.955423, 0.295242}, 
51 {-0.262866, 0.951056, 0.162460}, {0.238856, 0.864188, 0.442863}, 
52 {0.262866, 0.951056, 0.162460}, {0.500000, 0.809017, 0.309017}, 
53 {0.238856, 0.864188, -0.442863}, {0.262866, 0.951056, -0.162460}, 
54 {0.500000, 0.809017, -0.309017}, {0.850651, 0.525731, 0.000000}, 
55 {0.716567, 0.681718, 0.147621}, {0.716567, 0.681718, -0.147621}, 
56 {0.525731, 0.850651, 0.000000}, {0.425325, 0.688191, 0.587785}, 
57 {0.864188, 0.442863, 0.238856}, {0.688191, 0.587785, 0.425325}, 
58 {0.809017, 0.309017, 0.500000}, {0.681718, 0.147621, 0.716567}, 
59 {0.587785, 0.425325, 0.688191}, {0.955423, 0.295242, 0.000000}, 
60 {1.000000, 0.000000, 0.000000}, {0.951056, 0.162460, 0.262866},
61 {0.850651, -0.525731, 0.000000}, {0.955423, -0.295242, 0.000000}, 
62 {0.864188, -0.442863, 0.238856}, {0.951056, -0.162460, 0.262866}, 
63 {0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567},
64 {0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856},
65 {0.809017, 0.309017, -0.500000}, {0.951056, 0.162460, -0.262866}, 
66 {0.525731, 0.000000, -0.850651}, {0.681718, 0.147621, -0.716567}, 
67 {0.681718, -0.147621, -0.716567}, {0.850651, 0.000000, -0.525731},
68 {0.809017, -0.309017, -0.500000}, {0.864188, -0.442863, -0.238856}, 
69 {0.951056, -0.162460, -0.262866}, {0.147621, 0.716567, -0.681718}, 
70 {0.309017, 0.500000, -0.809017}, {0.425325, 0.688191, -0.587785}, 
71 {0.442863, 0.238856, -0.864188}, {0.587785, 0.425325, -0.688191}, 
72 {0.688191, 0.587785, -0.425325}, {-0.147621, 0.716567, -0.681718}, 
73 {-0.309017, 0.500000, -0.809017}, {0.000000, 0.525731, -0.850651}, 
74 {-0.525731, 0.000000, -0.850651}, {-0.442863, 0.238856, -0.864188}, 
75 {-0.295242, 0.000000, -0.955423}, {-0.162460, 0.262866, -0.951056}, 
76 {0.000000, 0.000000, -1.000000}, {0.295242, 0.000000, -0.955423}, 
77 {0.162460, 0.262866, -0.951056}, {-0.442863, -0.238856, -0.864188}, 
78 {-0.309017, -0.500000, -0.809017}, {-0.162460, -0.262866, -0.951056}, 
79 {0.000000, -0.850651, -0.525731}, {-0.147621, -0.716567, -0.681718}, 
80 {0.147621, -0.716567, -0.681718}, {0.000000, -0.525731, -0.850651}, 
81 {0.309017, -0.500000, -0.809017}, {0.442863, -0.238856, -0.864188}, 
82 {0.162460, -0.262866, -0.951056}, {0.238856, -0.864188, -0.442863}, 
83 {0.500000, -0.809017, -0.309017}, {0.425325, -0.688191, -0.587785}, 
84 {0.716567, -0.681718, -0.147621}, {0.688191, -0.587785, -0.425325}, 
85 {0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242},
86 {0.000000, -1.000000, 0.000000}, {0.262866, -0.951056, -0.162460}, 
87 {0.000000, -0.850651, 0.525731}, {0.000000, -0.955423, 0.295242}, 
88 {0.238856, -0.864188, 0.442863}, {0.262866, -0.951056, 0.162460}, 
89 {0.500000, -0.809017, 0.309017}, {0.716567, -0.681718, 0.147621}, 
90 {0.525731, -0.850651, 0.000000}, {-0.238856, -0.864188, -0.442863}, 
91 {-0.500000, -0.809017, -0.309017}, {-0.262866, -0.951056, -0.162460}, 
92 {-0.850651, -0.525731, 0.000000}, {-0.716567, -0.681718, -0.147621},
93 {-0.716567, -0.681718, 0.147621}, {-0.525731, -0.850651, 0.000000}, 
94 {-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863},
95 {-0.262866, -0.951056, 0.162460}, {-0.864188, -0.442863, 0.238856},
96 {-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325},
97 {-0.681718, -0.147621, 0.716567}, {-0.442863, -0.238856, 0.864188}, 
98 {-0.587785, -0.425325, 0.688191}, {-0.309017, -0.500000, 0.809017}, 
99 {-0.147621, -0.716567, 0.681718}, {-0.425325, -0.688191, 0.587785}, 
100 {-0.162460, -0.262866, 0.951056}, {0.442863, -0.238856, 0.864188}, 
101 {0.162460, -0.262866, 0.951056}, {0.309017, -0.500000, 0.809017}, 
102 {0.147621, -0.716567, 0.681718}, {0.000000, -0.525731, 0.850651}, 
103 {0.425325, -0.688191, 0.587785}, {0.587785, -0.425325, 0.688191}, 
104 {0.688191, -0.587785, 0.425325}, {-0.955423, 0.295242, 0.000000}, 
105 {-0.951056, 0.162460, 0.262866}, {-1.000000, 0.000000, 0.000000}, 
106 {-0.850651, 0.000000, 0.525731}, {-0.955423, -0.295242, 0.000000}, 
107 {-0.951056, -0.162460, 0.262866}, {-0.864188, 0.442863, -0.238856}, 
108 {-0.951056, 0.162460, -0.262866}, {-0.809017, 0.309017, -0.500000}, 
109 {-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866},
110 {-0.809017, -0.309017, -0.500000}, {-0.681718, 0.147621, -0.716567},
111 {-0.681718, -0.147621, -0.716567}, {-0.850651, 0.000000, -0.525731}, 
112 {-0.688191, 0.587785, -0.425325}, {-0.587785, 0.425325, -0.688191}, 
113 {-0.425325, 0.688191, -0.587785}, {-0.425325, -0.688191, -0.587785}, 
114 {-0.587785, -0.425325, -0.688191}, {-0.688191, -0.587785, -0.425325}, 
115 };
116
117 qbyte NormalToByte(const vec3_t n)
118 {
119         int i, best;
120         float bestdistance, distance;
121
122         best = 0;
123         bestdistance = DotProduct (n, m_bytenormals[0]);
124         for (i = 1;i < NUMVERTEXNORMALS;i++)
125         {
126                 distance = DotProduct (n, m_bytenormals[i]);
127                 if (distance > bestdistance)
128                 {
129                         bestdistance = distance;
130                         best = i;
131                 }
132         }
133         return best;
134 }
135
136 // note: uses byte partly to force unsigned for the validity check
137 void ByteToNormal(qbyte num, vec3_t n)
138 {
139         if (num < NUMVERTEXNORMALS)
140                 VectorCopy(m_bytenormals[num], n);
141         else
142                 VectorClear(n); // FIXME: complain?
143 }
144
145 float Q_RSqrt(float number)
146 {
147         float y;
148
149         if (number == 0.0f)
150                 return 0.0f;
151
152         *((int *)&y) = 0x5f3759df - ((* (int *) &number) >> 1);
153         return y * (1.5f - (number * 0.5f * y * y));
154 }
155
156
157 // assumes "src" is normalized
158 void PerpendicularVector( vec3_t dst, const vec3_t src )
159 {
160         // LordHavoc: optimized to death and beyond
161         int     pos;
162         float minelem;
163
164         if (src[0])
165         {
166                 dst[0] = 0;
167                 if (src[1])
168                 {
169                         dst[1] = 0;
170                         if (src[2])
171                         {
172                                 dst[2] = 0;
173                                 pos = 0;
174                                 minelem = fabs(src[0]);
175                                 if (fabs(src[1]) < minelem)
176                                 {
177                                         pos = 1;
178                                         minelem = fabs(src[1]);
179                                 }
180                                 if (fabs(src[2]) < minelem)
181                                         pos = 2;
182
183                                 dst[pos] = 1;
184                                 dst[0] -= src[pos] * src[0];
185                                 dst[1] -= src[pos] * src[1];
186                                 dst[2] -= src[pos] * src[2];
187
188                                 // normalize the result
189                                 VectorNormalize(dst);
190                         }
191                         else
192                                 dst[2] = 1;
193                 }
194                 else
195                 {
196                         dst[1] = 1;
197                         dst[2] = 0;
198                 }
199         }
200         else
201         {
202                 dst[0] = 1;
203                 dst[1] = 0;
204                 dst[2] = 0;
205         }
206 }
207
208
209 // LordHavoc: like AngleVectors, but taking a forward vector instead of angles, useful!
210 void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up)
211 {
212         float d;
213
214         right[0] = forward[2];
215         right[1] = -forward[0];
216         right[2] = forward[1];
217
218         d = DotProduct(forward, right);
219         right[0] -= d * forward[0];
220         right[1] -= d * forward[1];
221         right[2] -= d * forward[2];
222         VectorNormalizeFast(right);
223         CrossProduct(right, forward, up);
224 }
225
226 void VectorVectorsDouble(const double *forward, double *right, double *up)
227 {
228         double d;
229
230         right[0] = forward[2];
231         right[1] = -forward[0];
232         right[2] = forward[1];
233
234         d = DotProduct(forward, right);
235         right[0] -= d * forward[0];
236         right[1] -= d * forward[1];
237         right[2] -= d * forward[2];
238         VectorNormalize(right);
239         CrossProduct(right, forward, up);
240 }
241
242 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
243 {
244         float   t0, t1;
245         float   angle, c, s;
246         vec3_t  vr, vu, vf;
247
248         angle = DEG2RAD(degrees);
249
250         c = cos(angle);
251         s = sin(angle);
252
253         vf[0] = dir[0];
254         vf[1] = dir[1];
255         vf[2] = dir[2];
256
257         VectorVectors(vf, vr, vu);
258
259         t0 = vr[0] *  c + vu[0] * -s;
260         t1 = vr[0] *  s + vu[0] *  c;
261         dst[0] = (t0 * vr[0] + t1 * vu[0] + vf[0] * vf[0]) * point[0]
262                + (t0 * vr[1] + t1 * vu[1] + vf[0] * vf[1]) * point[1]
263                + (t0 * vr[2] + t1 * vu[2] + vf[0] * vf[2]) * point[2];
264
265         t0 = vr[1] *  c + vu[1] * -s;
266         t1 = vr[1] *  s + vu[1] *  c;
267         dst[1] = (t0 * vr[0] + t1 * vu[0] + vf[1] * vf[0]) * point[0]
268                + (t0 * vr[1] + t1 * vu[1] + vf[1] * vf[1]) * point[1]
269                + (t0 * vr[2] + t1 * vu[2] + vf[1] * vf[2]) * point[2];
270
271         t0 = vr[2] *  c + vu[2] * -s;
272         t1 = vr[2] *  s + vu[2] *  c;
273         dst[2] = (t0 * vr[0] + t1 * vu[0] + vf[2] * vf[0]) * point[0]
274                + (t0 * vr[1] + t1 * vu[1] + vf[2] * vf[1]) * point[1]
275                + (t0 * vr[2] + t1 * vu[2] + vf[2] * vf[2]) * point[2];
276 }
277
278 /*-----------------------------------------------------------------*/
279
280
281 // LordHavoc note 1:
282 // BoxOnPlaneSide did a switch on a 'signbits' value and had optimized
283 // assembly in an attempt to accelerate it further, very inefficient
284 // considering that signbits of the frustum planes only changed each
285 // frame, and the world planes changed only at load time.
286 // So, to optimize it further I took the obvious route of storing a function
287 // pointer in the plane struct itself, and shrunk each of the individual
288 // cases to a single return statement.
289 // LordHavoc note 2:
290 // realized axial cases would be a nice speedup for world geometry, although
291 // never useful for the frustum planes.
292 int BoxOnPlaneSideX (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[0] ? 1 : (p->dist >= emaxs[0] ? 2 : 3);}
293 int BoxOnPlaneSideY (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[1] ? 1 : (p->dist >= emaxs[1] ? 2 : 3);}
294 int BoxOnPlaneSideZ (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[2] ? 1 : (p->dist >= emaxs[2] ? 2 : 3);}
295 int BoxOnPlaneSide0 (vec3_t emins, vec3_t emaxs, mplane_t *p) {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));}
296 int BoxOnPlaneSide1 (vec3_t emins, vec3_t emaxs, mplane_t *p) {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));}
297 int BoxOnPlaneSide2 (vec3_t emins, vec3_t emaxs, mplane_t *p) {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));}
298 int BoxOnPlaneSide3 (vec3_t emins, vec3_t emaxs, mplane_t *p) {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));}
299 int BoxOnPlaneSide4 (vec3_t emins, vec3_t emaxs, mplane_t *p) {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));}
300 int BoxOnPlaneSide5 (vec3_t emins, vec3_t emaxs, mplane_t *p) {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));}
301 int BoxOnPlaneSide6 (vec3_t emins, vec3_t emaxs, mplane_t *p) {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));}
302 int BoxOnPlaneSide7 (vec3_t emins, vec3_t emaxs, mplane_t *p) {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));}
303
304 void BoxOnPlaneSideClassify(mplane_t *p)
305 {
306         switch(p->type)
307         {
308         case 0: // x axis
309                 p->BoxOnPlaneSideFunc = BoxOnPlaneSideX;
310                 break;
311         case 1: // y axis
312                 p->BoxOnPlaneSideFunc = BoxOnPlaneSideY;
313                 break;
314         case 2: // z axis
315                 p->BoxOnPlaneSideFunc = BoxOnPlaneSideZ;
316                 break;
317         default:
318                 if (p->normal[2] < 0) // 4
319                 {
320                         if (p->normal[1] < 0) // 2
321                         {
322                                 if (p->normal[0] < 0) // 1
323                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide7;
324                                 else
325                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide6;
326                         }
327                         else
328                         {
329                                 if (p->normal[0] < 0) // 1
330                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide5;
331                                 else
332                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide4;
333                         }
334                 }
335                 else
336                 {
337                         if (p->normal[1] < 0) // 2
338                         {
339                                 if (p->normal[0] < 0) // 1
340                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide3;
341                                 else
342                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide2;
343                         }
344                         else
345                         {
346                                 if (p->normal[0] < 0) // 1
347                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide1;
348                                 else
349                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide0;
350                         }
351                 }
352                 break;
353         }
354 }
355
356 void PlaneClassify(mplane_t *p)
357 {
358         if (p->normal[0] == 1)
359                 p->type = 0;
360         else if (p->normal[1] == 1)
361                 p->type = 1;
362         else if (p->normal[2] == 1)
363                 p->type = 2;
364         else
365                 p->type = 3;
366         BoxOnPlaneSideClassify(p);
367 }
368
369 void AngleVectors (const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
370 {
371         double angle, sr, sp, sy, cr, cp, cy;
372
373         angle = angles[YAW] * (M_PI*2 / 360);
374         sy = sin(angle);
375         cy = cos(angle);
376         angle = angles[PITCH] * (M_PI*2 / 360);
377         sp = sin(angle);
378         cp = cos(angle);
379         if (forward)
380         {
381                 forward[0] = cp*cy;
382                 forward[1] = cp*sy;
383                 forward[2] = -sp;
384         }
385         if (right || up)
386         {
387                 angle = angles[ROLL] * (M_PI*2 / 360);
388                 sr = sin(angle);
389                 cr = cos(angle);
390                 if (right)
391                 {
392                         right[0] = -1*(sr*sp*cy+cr*-sy);
393                         right[1] = -1*(sr*sp*sy+cr*cy);
394                         right[2] = -1*(sr*cp);
395                 }
396                 if (up)
397                 {
398                         up[0] = (cr*sp*cy+-sr*-sy);
399                         up[1] = (cr*sp*sy+-sr*cy);
400                         up[2] = cr*cp;
401                 }
402         }
403 }
404
405 void AngleVectorsFLU (const vec3_t angles, vec3_t forward, vec3_t left, vec3_t up)
406 {
407         double angle, sr, sp, sy, cr, cp, cy;
408
409         angle = angles[YAW] * (M_PI*2 / 360);
410         sy = sin(angle);
411         cy = cos(angle);
412         angle = angles[PITCH] * (M_PI*2 / 360);
413         sp = sin(angle);
414         cp = cos(angle);
415         if (forward)
416         {
417                 forward[0] = cp*cy;
418                 forward[1] = cp*sy;
419                 forward[2] = -sp;
420         }
421         if (left || up)
422         {
423                 angle = angles[ROLL] * (M_PI*2 / 360);
424                 sr = sin(angle);
425                 cr = cos(angle);
426                 if (left)
427                 {
428                         left[0] = sr*sp*cy+cr*-sy;
429                         left[1] = sr*sp*sy+cr*cy;
430                         left[2] = 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 }
440
441 void AngleMatrix (const vec3_t angles, const vec3_t translate, vec_t matrix[][4])
442 {
443         double angle, sr, sp, sy, cr, cp, cy;
444
445         angle = angles[YAW] * (M_PI*2 / 360);
446         sy = sin(angle);
447         cy = cos(angle);
448         angle = angles[PITCH] * (M_PI*2 / 360);
449         sp = sin(angle);
450         cp = cos(angle);
451         angle = angles[ROLL] * (M_PI*2 / 360);
452         sr = sin(angle);
453         cr = cos(angle);
454         matrix[0][0] = cp*cy;
455         matrix[0][1] = sr*sp*cy+cr*-sy;
456         matrix[0][2] = cr*sp*cy+-sr*-sy;
457         matrix[0][3] = translate[0];
458         matrix[1][0] = cp*sy;
459         matrix[1][1] = sr*sp*sy+cr*cy;
460         matrix[1][2] = cr*sp*sy+-sr*cy;
461         matrix[1][3] = translate[1];
462         matrix[2][0] = -sp;
463         matrix[2][1] = sr*cp;
464         matrix[2][2] = cr*cp;
465         matrix[2][3] = translate[2];
466 }
467
468
469 // LordHavoc: renamed these to Length, and made the normal ones #define
470 float VectorNormalizeLength (vec3_t v)
471 {
472         float   length, ilength;
473
474         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
475         length = sqrt (length);         // FIXME
476
477         if (length)
478         {
479                 ilength = 1/length;
480                 v[0] *= ilength;
481                 v[1] *= ilength;
482                 v[2] *= ilength;
483         }
484                 
485         return length;
486
487 }
488
489
490 /*
491 ================
492 R_ConcatRotations
493 ================
494 */
495 void R_ConcatRotations (const float in1[3*3], const float in2[3*3], float out[3*3])
496 {
497         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];
498         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];
499         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];
500         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];
501         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];
502         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];
503         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];
504         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];
505         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];
506 }
507
508
509 /*
510 ================
511 R_ConcatTransforms
512 ================
513 */
514 void R_ConcatTransforms (const float in1[3*4], const float in2[3*4], float out[3*4])
515 {
516         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];
517         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];
518         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];
519         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];
520         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];
521         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];
522         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];
523         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];
524         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];
525         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];
526         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];
527         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];
528 }
529
530
531 void Mathlib_Init(void)
532 {
533         int a;
534
535         // LordHavoc: setup 1.0f / N table for quick recipricols of integers
536         ixtable[0] = 0;
537         for (a = 1;a < 4096;a++)
538                 ixtable[a] = 1.0f / a;
539 }
540