]> icculus.org git repositories - divverent/darkplaces.git/blob - mathlib.c
skyquality and mergesky are saved to the config now
[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 void Sys_Error (char *error, ...);
26
27 vec3_t vec3_origin = {0,0,0};
28 int nanmask = 255<<23;
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 byte NormalToByte(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(byte 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 #if 0
146 // LordHavoc: no longer used at all
147 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
148 {
149 #if 0
150         // LordHavoc: the old way...
151         float d;
152         vec3_t n;
153         float inv_denom;
154
155         inv_denom = 1.0F / DotProduct( normal, normal );
156
157         d = DotProduct( normal, p ) * inv_denom;
158
159         n[0] = normal[0] * inv_denom;
160         n[1] = normal[1] * inv_denom;
161         n[2] = normal[2] * inv_denom;
162
163         dst[0] = p[0] - d * n[0];
164         dst[1] = p[1] - d * n[1];
165         dst[2] = p[2] - d * n[2];
166 #else
167         // LordHavoc: optimized to death and beyond
168         float d;
169
170         // LordHavoc: the normal is a unit vector by definition,
171         //            therefore inv_denom was pointless.
172         d = DotProduct(normal, p);
173         dst[0] = p[0] - d * normal[0];
174         dst[1] = p[1] - d * normal[1];
175         dst[2] = p[2] - d * normal[2];
176 #endif
177 }
178 #endif
179
180 // assumes "src" is normalized
181 void PerpendicularVector( vec3_t dst, const vec3_t src )
182 {
183 #if 0
184         // LordHavoc: the old way...
185         int     pos;
186         int i;
187         float minelem, d;
188         vec3_t tempvec;
189
190         // find the smallest magnitude axially aligned vector
191         minelem = 1.0F;
192         for ( pos = 0, i = 0; i < 3; i++ )
193         {
194                 if ( fabs( src[i] ) < minelem )
195                 {
196                         pos = i;
197                         minelem = fabs( src[i] );
198                 }
199         }
200         VectorClear(tempvec);
201         tempvec[pos] = 1.0F;
202
203         // project the point onto the plane defined by src
204         ProjectPointOnPlane( dst, tempvec, src );
205
206         // normalize the result
207         VectorNormalize(dst);
208 #else
209         // LordHavoc: optimized to death and beyond
210         int     pos;
211         float minelem;
212
213         if (src[0])
214         {
215                 dst[0] = 0;
216                 if (src[1])
217                 {
218                         dst[1] = 0;
219                         if (src[2])
220                         {
221                                 dst[2] = 0;
222                                 pos = 0;
223                                 minelem = fabs(src[0]);
224                                 if (fabs(src[1]) < minelem)
225                                 {
226                                         pos = 1;
227                                         minelem = fabs(src[1]);
228                                 }
229                                 if (fabs(src[2]) < minelem)
230                                         pos = 2;
231
232                                 dst[pos] = 1;
233                                 dst[0] -= src[pos] * src[0];
234                                 dst[1] -= src[pos] * src[1];
235                                 dst[2] -= src[pos] * src[2];
236
237                                 // normalize the result
238                                 VectorNormalize(dst);
239                         }
240                         else
241                                 dst[2] = 1;
242                 }
243                 else
244                 {
245                         dst[1] = 1;
246                         dst[2] = 0;
247                 }
248         }
249         else
250         {
251                 dst[0] = 1;
252                 dst[1] = 0;
253                 dst[2] = 0;
254         }
255 #endif
256 }
257
258
259 #ifdef _WIN32
260 #pragma optimize( "", off )
261 #endif
262
263
264 // LordHavoc: like AngleVectors, but taking a forward vector instead of angles, useful!
265 void VectorVectors(const vec3_t forward, vec3_t right, vec3_t up)
266 {
267         float d;
268
269         right[0] = forward[2];
270         right[1] = -forward[0];
271         right[2] = forward[1];
272
273         d = DotProduct(forward, right);
274         right[0] -= d * forward[0];
275         right[1] -= d * forward[1];
276         right[2] -= d * forward[2];
277         VectorNormalize(right);
278         CrossProduct(right, forward, up);
279 }
280
281 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
282 {
283 #if 0
284         // LordHavoc: the old way, cryptic brute force...
285         float   m[3][3];
286         float   im[3][3];
287         float   zrot[3][3];
288         float   tmpmat[3][3];
289         float   rot[3][3];
290         int     i;
291         vec3_t vr, vup, vf;
292
293         vf[0] = dir[0];
294         vf[1] = dir[1];
295         vf[2] = dir[2];
296
297         PerpendicularVector( vr, dir );
298         CrossProduct( vr, vf, vup );
299
300         m[0][0] = vr[0];
301         m[1][0] = vr[1];
302         m[2][0] = vr[2];
303
304         m[0][1] = vup[0];
305         m[1][1] = vup[1];
306         m[2][1] = vup[2];
307
308         m[0][2] = vf[0];
309         m[1][2] = vf[1];
310         m[2][2] = vf[2];
311
312         memcpy( im, m, sizeof( im ) );
313
314         im[0][1] = m[1][0];
315         im[0][2] = m[2][0];
316         im[1][0] = m[0][1];
317         im[1][2] = m[2][1];
318         im[2][0] = m[0][2];
319         im[2][1] = m[1][2];
320
321         memset( zrot, 0, sizeof( zrot ) );
322         zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
323
324         zrot[0][0] = cos( DEG2RAD( degrees ) );
325         zrot[0][1] = sin( DEG2RAD( degrees ) );
326         zrot[1][0] = -sin( DEG2RAD( degrees ) );
327         zrot[1][1] = cos( DEG2RAD( degrees ) );
328
329         R_ConcatRotations( m, zrot, tmpmat );
330         R_ConcatRotations( tmpmat, im, rot );
331
332         for ( i = 0; i < 3; i++ )
333                 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
334 #elif 0
335         // LordHavoc: on the path to unintelligible code...
336 //      float   m[3][3];
337 //      float   im[3][3];
338 //      float   zrot[3][3];
339         float   tmpmat[3][3];
340 //      float   rot[3][3];
341         float   angle, c, s;
342 //      int     i;
343         vec3_t vr, vu, vf;
344
345         angle = DEG2RAD(degrees);
346
347         c = cos(angle);
348         s = sin(angle);
349
350         vf[0] = dir[0];
351         vf[1] = dir[1];
352         vf[2] = dir[2];
353
354         PerpendicularVector(vr, dir);
355         CrossProduct(vr, vf, vu);
356
357 //      m   [0][0] = vr[0];m   [0][1] = vu[0];m   [0][2] = vf[0];
358 //      m   [1][0] = vr[1];m   [1][1] = vu[1];m   [1][2] = vf[1];
359 //      m   [2][0] = vr[2];m   [2][1] = vu[2];m   [2][2] = vf[2];
360 //      im  [0][0] = vr[0];im  [0][1] = vr[1];im  [0][2] = vr[2];
361 //      im  [1][0] = vu[0];im  [1][1] = vu[1];im  [1][2] = vu[2];
362 //      im  [2][0] = vf[0];im  [2][1] = vf[1];im  [2][2] = vf[2];
363 //      zrot[0][0] =     c;zrot[0][1] =     s;zrot[0][2] =     0;
364 //      zrot[1][0] =    -s;zrot[1][1] =     c;zrot[1][2] =     0;
365 //      zrot[2][0] =     0;zrot[2][1] =     0;zrot[2][2] =     1;
366
367 //      tmpmat[0][0] = m[0][0] * zrot[0][0] + m[0][1] * zrot[1][0] + m[0][2] * zrot[2][0];
368 //      tmpmat[0][1] = m[0][0] * zrot[0][1] + m[0][1] * zrot[1][1] + m[0][2] * zrot[2][1];
369 //      tmpmat[0][2] = m[0][0] * zrot[0][2] + m[0][1] * zrot[1][2] + m[0][2] * zrot[2][2];
370 //      tmpmat[1][0] = m[1][0] * zrot[0][0] + m[1][1] * zrot[1][0] + m[1][2] * zrot[2][0];
371 //      tmpmat[1][1] = m[1][0] * zrot[0][1] + m[1][1] * zrot[1][1] + m[1][2] * zrot[2][1];
372 //      tmpmat[1][2] = m[1][0] * zrot[0][2] + m[1][1] * zrot[1][2] + m[1][2] * zrot[2][2];
373 //      tmpmat[2][0] = m[2][0] * zrot[0][0] + m[2][1] * zrot[1][0] + m[2][2] * zrot[2][0];
374 //      tmpmat[2][1] = m[2][0] * zrot[0][1] + m[2][1] * zrot[1][1] + m[2][2] * zrot[2][1];
375 //      tmpmat[2][2] = m[2][0] * zrot[0][2] + m[2][1] * zrot[1][2] + m[2][2] * zrot[2][2];
376
377         tmpmat[0][0] = vr[0] *  c + vu[0] * -s;
378         tmpmat[0][1] = vr[0] *  s + vu[0] *  c;
379 //      tmpmat[0][2] =                           vf[0];
380         tmpmat[1][0] = vr[1] *  c + vu[1] * -s;
381         tmpmat[1][1] = vr[1] *  s + vu[1] *  c;
382 //      tmpmat[1][2] =                           vf[1];
383         tmpmat[2][0] = vr[2] *  c + vu[2] * -s;
384         tmpmat[2][1] = vr[2] *  s + vu[2] *  c;
385 //      tmpmat[2][2] =                           vf[2];
386
387 //      rot[0][0] = tmpmat[0][0] * vr[0] + tmpmat[0][1] * vu[0] + tmpmat[0][2] * vf[0];
388 //      rot[0][1] = tmpmat[0][0] * vr[1] + tmpmat[0][1] * vu[1] + tmpmat[0][2] * vf[1];
389 //      rot[0][2] = tmpmat[0][0] * vr[2] + tmpmat[0][1] * vu[2] + tmpmat[0][2] * vf[2];
390 //      rot[1][0] = tmpmat[1][0] * vr[0] + tmpmat[1][1] * vu[0] + tmpmat[1][2] * vf[0];
391 //      rot[1][1] = tmpmat[1][0] * vr[1] + tmpmat[1][1] * vu[1] + tmpmat[1][2] * vf[1];
392 //      rot[1][2] = tmpmat[1][0] * vr[2] + tmpmat[1][1] * vu[2] + tmpmat[1][2] * vf[2];
393 //      rot[2][0] = tmpmat[2][0] * vr[0] + tmpmat[2][1] * vu[0] + tmpmat[2][2] * vf[0];
394 //      rot[2][1] = tmpmat[2][0] * vr[1] + tmpmat[2][1] * vu[1] + tmpmat[2][2] * vf[1];
395 //      rot[2][2] = tmpmat[2][0] * vr[2] + tmpmat[2][1] * vu[2] + tmpmat[2][2] * vf[2];
396
397 //      rot[0][0] = tmpmat[0][0] * vr[0] + tmpmat[0][1] * vu[0] + vf[0] * vf[0];
398 //      rot[0][1] = tmpmat[0][0] * vr[1] + tmpmat[0][1] * vu[1] + vf[0] * vf[1];
399 //      rot[0][2] = tmpmat[0][0] * vr[2] + tmpmat[0][1] * vu[2] + vf[0] * vf[2];
400 //      rot[1][0] = tmpmat[1][0] * vr[0] + tmpmat[1][1] * vu[0] + vf[1] * vf[0];
401 //      rot[1][1] = tmpmat[1][0] * vr[1] + tmpmat[1][1] * vu[1] + vf[1] * vf[1];
402 //      rot[1][2] = tmpmat[1][0] * vr[2] + tmpmat[1][1] * vu[2] + vf[1] * vf[2];
403 //      rot[2][0] = tmpmat[2][0] * vr[0] + tmpmat[2][1] * vu[0] + vf[2] * vf[0];
404 //      rot[2][1] = tmpmat[2][0] * vr[1] + tmpmat[2][1] * vu[1] + vf[2] * vf[1];
405 //      rot[2][2] = tmpmat[2][0] * vr[2] + tmpmat[2][1] * vu[2] + vf[2] * vf[2];
406
407 //      dst[0] = rot[0][0] * point[0] + rot[0][1] * point[1] + rot[0][2] * point[2];
408 //      dst[1] = rot[1][0] * point[0] + rot[1][1] * point[1] + rot[1][2] * point[2];
409 //      dst[2] = rot[2][0] * point[0] + rot[2][1] * point[1] + rot[2][2] * point[2];
410
411         dst[0] = (tmpmat[0][0] * vr[0] + tmpmat[0][1] * vu[0] + vf[0] * vf[0]) * point[0]
412                + (tmpmat[0][0] * vr[1] + tmpmat[0][1] * vu[1] + vf[0] * vf[1]) * point[1]
413                + (tmpmat[0][0] * vr[2] + tmpmat[0][1] * vu[2] + vf[0] * vf[2]) * point[2];
414         dst[1] = (tmpmat[1][0] * vr[0] + tmpmat[1][1] * vu[0] + vf[1] * vf[0]) * point[0]
415                + (tmpmat[1][0] * vr[1] + tmpmat[1][1] * vu[1] + vf[1] * vf[1]) * point[1]
416                + (tmpmat[1][0] * vr[2] + tmpmat[1][1] * vu[2] + vf[1] * vf[2]) * point[2];
417         dst[2] = (tmpmat[2][0] * vr[0] + tmpmat[2][1] * vu[0] + vf[2] * vf[0]) * point[0]
418                + (tmpmat[2][0] * vr[1] + tmpmat[2][1] * vu[1] + vf[2] * vf[1]) * point[1]
419                + (tmpmat[2][0] * vr[2] + tmpmat[2][1] * vu[2] + vf[2] * vf[2]) * point[2];
420 #else
421         // LordHavoc: optimized to death and beyond, cryptic in an entirely new way
422         float   t0, t1;
423         float   angle, c, s;
424         vec3_t  vr, vu, vf;
425
426         angle = DEG2RAD(degrees);
427
428         c = cos(angle);
429         s = sin(angle);
430
431         vf[0] = dir[0];
432         vf[1] = dir[1];
433         vf[2] = dir[2];
434
435 //      PerpendicularVector(vr, dir);
436 //      CrossProduct(vr, vf, vu);
437         VectorVectors(vf, vr, vu);
438
439         t0 = vr[0] *  c + vu[0] * -s;
440         t1 = vr[0] *  s + vu[0] *  c;
441         dst[0] = (t0 * vr[0] + t1 * vu[0] + vf[0] * vf[0]) * point[0]
442                + (t0 * vr[1] + t1 * vu[1] + vf[0] * vf[1]) * point[1]
443                + (t0 * vr[2] + t1 * vu[2] + vf[0] * vf[2]) * point[2];
444
445         t0 = vr[1] *  c + vu[1] * -s;
446         t1 = vr[1] *  s + vu[1] *  c;
447         dst[1] = (t0 * vr[0] + t1 * vu[0] + vf[1] * vf[0]) * point[0]
448                + (t0 * vr[1] + t1 * vu[1] + vf[1] * vf[1]) * point[1]
449                + (t0 * vr[2] + t1 * vu[2] + vf[1] * vf[2]) * point[2];
450
451         t0 = vr[2] *  c + vu[2] * -s;
452         t1 = vr[2] *  s + vu[2] *  c;
453         dst[2] = (t0 * vr[0] + t1 * vu[0] + vf[2] * vf[0]) * point[0]
454                + (t0 * vr[1] + t1 * vu[1] + vf[2] * vf[1]) * point[1]
455                + (t0 * vr[2] + t1 * vu[2] + vf[2] * vf[2]) * point[2];
456 #endif
457 }
458
459 #ifdef _WIN32
460 #pragma optimize( "", on )
461 #endif
462
463 /*-----------------------------------------------------------------*/
464
465
466 // LordHavoc note 1:
467 // BoxOnPlaneSide did a switch on a 'signbits' value and had optimized
468 // assembly in an attempt to accelerate it further, very inefficient
469 // considering that signbits of the frustum planes only changed each
470 // frame, and the world planes changed only at load time.
471 // So, to optimize it further I took the obvious route of storing a function
472 // pointer in the plane struct itself, and shrunk each of the individual
473 // cases to a single return statement.
474 // LordHavoc note 2:
475 // realized axial cases would be a nice speedup for world geometry, although
476 // never useful for the frustum planes.
477 int BoxOnPlaneSideX (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[0] ? 1 : (p->dist >= emaxs[0] ? 2 : 3);}
478 int BoxOnPlaneSideY (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[1] ? 1 : (p->dist >= emaxs[1] ? 2 : 3);}
479 int BoxOnPlaneSideZ (vec3_t emins, vec3_t emaxs, mplane_t *p) {return p->dist <= emins[2] ? 1 : (p->dist >= emaxs[2] ? 2 : 3);}
480 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));}
481 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));}
482 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));}
483 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));}
484 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));}
485 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));}
486 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));}
487 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));}
488
489 void BoxOnPlaneSideClassify(mplane_t *p)
490 {
491         switch(p->type)
492         {
493         case 0: // x axis
494                 p->BoxOnPlaneSideFunc = BoxOnPlaneSideX;
495                 break;
496         case 1: // y axis
497                 p->BoxOnPlaneSideFunc = BoxOnPlaneSideY;
498                 break;
499         case 2: // z axis
500                 p->BoxOnPlaneSideFunc = BoxOnPlaneSideZ;
501                 break;
502         default:
503                 if (p->normal[2] < 0) // 4
504                 {
505                         if (p->normal[1] < 0) // 2
506                         {
507                                 if (p->normal[0] < 0) // 1
508                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide7;
509                                 else
510                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide6;
511                         }
512                         else
513                         {
514                                 if (p->normal[0] < 0) // 1
515                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide5;
516                                 else
517                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide4;
518                         }
519                 }
520                 else
521                 {
522                         if (p->normal[1] < 0) // 2
523                         {
524                                 if (p->normal[0] < 0) // 1
525                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide3;
526                                 else
527                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide2;
528                         }
529                         else
530                         {
531                                 if (p->normal[0] < 0) // 1
532                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide1;
533                                 else
534                                         p->BoxOnPlaneSideFunc = BoxOnPlaneSide0;
535                         }
536                 }
537                 break;
538         }
539 }
540
541 void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
542 {
543         float           angle;
544         float           sr, sp, sy, cr, cp, cy;
545         
546         angle = angles[YAW] * (M_PI*2 / 360);
547         sy = sin(angle);
548         cy = cos(angle);
549         angle = angles[PITCH] * (M_PI*2 / 360);
550         sp = sin(angle);
551         cp = cos(angle);
552         // LordHavoc: this is only to hush up gcc complaining about 'might be used uninitialized' variables
553         // (they are NOT used uninitialized, but oh well)
554         cr = 0;
555         sr = 0;
556         if (right || up)
557         {
558                 angle = angles[ROLL] * (M_PI*2 / 360);
559                 sr = sin(angle);
560                 cr = cos(angle);
561         }
562
563         if (forward)
564         {
565                 forward[0] = cp*cy;
566                 forward[1] = cp*sy;
567                 forward[2] = -sp;
568         }
569         if (right)
570         {
571                 right[0] = (-1*sr*sp*cy+-1*cr*-sy);
572                 right[1] = (-1*sr*sp*sy+-1*cr*cy);
573                 right[2] = -1*sr*cp;
574         }
575         if (up)
576         {
577                 up[0] = (cr*sp*cy+-sr*-sy);
578                 up[1] = (cr*sp*sy+-sr*cy);
579                 up[2] = cr*cp;
580         }
581 }
582
583 int VectorCompare (vec3_t v1, vec3_t v2)
584 {
585         int             i;
586         
587         for (i=0 ; i<3 ; i++)
588                 if (v1[i] != v2[i])
589                         return 0;
590                         
591         return 1;
592 }
593
594 void VectorMA (vec3_t veca, float scale, vec3_t vecb, vec3_t vecc)
595 {
596         vecc[0] = veca[0] + scale*vecb[0];
597         vecc[1] = veca[1] + scale*vecb[1];
598         vecc[2] = veca[2] + scale*vecb[2];
599 }
600
601
602 vec_t _DotProduct (vec3_t v1, vec3_t v2)
603 {
604         return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
605 }
606
607 void _VectorSubtract (vec3_t veca, vec3_t vecb, vec3_t out)
608 {
609         out[0] = veca[0]-vecb[0];
610         out[1] = veca[1]-vecb[1];
611         out[2] = veca[2]-vecb[2];
612 }
613
614 void _VectorAdd (vec3_t veca, vec3_t vecb, vec3_t out)
615 {
616         out[0] = veca[0]+vecb[0];
617         out[1] = veca[1]+vecb[1];
618         out[2] = veca[2]+vecb[2];
619 }
620
621 void _VectorCopy (vec3_t in, vec3_t out)
622 {
623         out[0] = in[0];
624         out[1] = in[1];
625         out[2] = in[2];
626 }
627
628 // LordHavoc: changed CrossProduct to a #define
629 /*
630 void CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
631 {
632         cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
633         cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
634         cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
635 }
636 */
637
638 double sqrt(double x);
639
640 vec_t Length(vec3_t v)
641 {
642         int             i;
643         float   length;
644         
645         length = 0;
646         for (i=0 ; i< 3 ; i++)
647                 length += v[i]*v[i];
648         length = sqrt (length);         // FIXME
649
650         return length;
651 }
652
653 // LordHavoc: renamed these to Length, and made the normal ones #define
654 float VectorNormalizeLength (vec3_t v)
655 {
656         float   length, ilength;
657
658         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
659         length = sqrt (length);         // FIXME
660
661         if (length)
662         {
663                 ilength = 1/length;
664                 v[0] *= ilength;
665                 v[1] *= ilength;
666                 v[2] *= ilength;
667         }
668                 
669         return length;
670
671 }
672
673 float VectorNormalizeLength2 (vec3_t v, vec3_t dest) // LordHavoc: added to allow copying while doing the calculation...
674 {
675         float   length, ilength;
676
677         length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
678         length = sqrt (length);         // FIXME
679
680         if (length)
681         {
682                 ilength = 1/length;
683                 dest[0] = v[0] * ilength;
684                 dest[1] = v[1] * ilength;
685                 dest[2] = v[2] * ilength;
686         }
687         else
688                 dest[0] = dest[1] = dest[2] = 0;
689                 
690         return length;
691
692 }
693
694 void VectorInverse (vec3_t v)
695 {
696         v[0] = -v[0];
697         v[1] = -v[1];
698         v[2] = -v[2];
699 }
700
701 void VectorScale (vec3_t in, vec_t scale, vec3_t out)
702 {
703         out[0] = in[0]*scale;
704         out[1] = in[1]*scale;
705         out[2] = in[2]*scale;
706 }
707
708
709 int Q_log2(int val)
710 {
711         int answer=0;
712         while (val>>=1)
713                 answer++;
714         return answer;
715 }
716
717
718 /*
719 ================
720 R_ConcatRotations
721 ================
722 */
723 void R_ConcatRotations (float in1[3][3], float in2[3][3], float out[3][3])
724 {
725         out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0];
726         out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1];
727         out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2];
728         out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0];
729         out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1];
730         out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2];
731         out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0];
732         out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1];
733         out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2];
734 }
735
736
737 /*
738 ================
739 R_ConcatTransforms
740 ================
741 */
742 void R_ConcatTransforms (float in1[3][4], float in2[3][4], float out[3][4])
743 {
744         out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + in1[0][2] * in2[2][0];
745         out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + in1[0][2] * in2[2][1];
746         out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + in1[0][2] * in2[2][2];
747         out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + in1[0][2] * in2[2][3] + in1[0][3];
748         out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + in1[1][2] * in2[2][0];
749         out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + in1[1][2] * in2[2][1];
750         out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + in1[1][2] * in2[2][2];
751         out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + in1[1][2] * in2[2][3] + in1[1][3];
752         out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + in1[2][2] * in2[2][0];
753         out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + in1[2][2] * in2[2][1];
754         out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + in1[2][2] * in2[2][2];
755         out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + in1[2][2] * in2[2][3] + in1[2][3];
756 }
757
758
759 /*
760 ===================
761 FloorDivMod
762
763 Returns mathematically correct (floor-based) quotient and remainder for
764 numer and denom, both of which should contain no fractional part. The
765 quotient must fit in 32 bits.
766 ====================
767 */
768
769 void FloorDivMod (double numer, double denom, int *quotient,
770                 int *rem)
771 {
772         int             q, r;
773         double  x;
774
775 #ifndef PARANOID
776         if (denom <= 0.0)
777                 Sys_Error ("FloorDivMod: bad denominator %d\n", denom);
778
779 //      if ((floor(numer) != numer) || (floor(denom) != denom))
780 //              Sys_Error ("FloorDivMod: non-integer numer or denom %f %f\n",
781 //                              numer, denom);
782 #endif
783
784         if (numer >= 0.0)
785         {
786
787                 x = floor(numer / denom);
788                 q = (int)x;
789                 r = (int)floor(numer - (x * denom));
790         }
791         else
792         {
793         //
794         // perform operations with positive values, and fix mod to make floor-based
795         //
796                 x = floor(-numer / denom);
797                 q = -(int)x;
798                 r = (int)floor(-numer - (x * denom));
799                 if (r != 0)
800                 {
801                         q--;
802                         r = (int)denom - r;
803                 }
804         }
805
806         *quotient = q;
807         *rem = r;
808 }
809
810
811 /*
812 ===================
813 GreatestCommonDivisor
814 ====================
815 */
816 int GreatestCommonDivisor (int i1, int i2)
817 {
818         if (i1 > i2)
819         {
820                 if (i2 == 0)
821                         return (i1);
822                 return GreatestCommonDivisor (i2, i1 % i2);
823         }
824         else
825         {
826                 if (i1 == 0)
827                         return (i2);
828                 return GreatestCommonDivisor (i1, i2 % i1);
829         }
830 }