Debian Codegraph @

code/qcommon/q_math.c

raw
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23 // q_math.c -- stateless support routines that are included in each code module
24
25 // Some of the vector functions are static inline in q_shared.h. q3asm
26 // doesn't understand static functions though, so we only want them in
27 // one file. That's what this is about.
28 #ifdef Q3_VM
29 #define __Q3_VM_MATH
30 #endif
31
32 #include "q_shared.h"
33
34 vec3_t vec3_origin = {0,0,0};
35 vec3_t axisDefault[3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
36
37
38 vec4_t colorBlack = {0, 0, 0, 1};
39 vec4_t colorRed = {1, 0, 0, 1};
40 vec4_t colorGreen = {0, 1, 0, 1};
41 vec4_t colorBlue = {0, 0, 1, 1};
42 vec4_t colorYellow = {1, 1, 0, 1};
43 vec4_t colorMagenta= {1, 0, 1, 1};
44 vec4_t colorCyan = {0, 1, 1, 1};
45 vec4_t colorWhite = {1, 1, 1, 1};
46 vec4_t colorLtGrey = {0.75, 0.75, 0.75, 1};
47 vec4_t colorMdGrey = {0.5, 0.5, 0.5, 1};
48 vec4_t colorDkGrey = {0.25, 0.25, 0.25, 1};
49
50 vec4_t g_color_table[9] =
51 {
52 {0.0, 0.0, 0.0, 1.0},
53 {1.0, 0.0, 0.0, 1.0},
54 {0.0, 1.0, 0.0, 1.0},
55 {1.0, 1.0, 0.0, 1.0},
56 {0.0, 0.0, 1.0, 1.0},
57 {0.0, 1.0, 1.0, 1.0},
58 {1.0, 0.0, 1.0, 1.0},
59 {1.0, 1.0, 1.0, 1.0},
60 {1.00f, 0.43f, 0.00f, 1.00f},
61 };
62
63
64 vec3_t bytedirs[NUMVERTEXNORMALS] =
65 {
66 {-0.525731f, 0.000000f, 0.850651f}, {-0.442863f, 0.238856f, 0.864188f},
67 {-0.295242f, 0.000000f, 0.955423f}, {-0.309017f, 0.500000f, 0.809017f},
68 {-0.162460f, 0.262866f, 0.951056f}, {0.000000f, 0.000000f, 1.000000f},
69 {0.000000f, 0.850651f, 0.525731f}, {-0.147621f, 0.716567f, 0.681718f},
70 {0.147621f, 0.716567f, 0.681718f}, {0.000000f, 0.525731f, 0.850651f},
71 {0.309017f, 0.500000f, 0.809017f}, {0.525731f, 0.000000f, 0.850651f},
72 {0.295242f, 0.000000f, 0.955423f}, {0.442863f, 0.238856f, 0.864188f},
73 {0.162460f, 0.262866f, 0.951056f}, {-0.681718f, 0.147621f, 0.716567f},
74 {-0.809017f, 0.309017f, 0.500000f},{-0.587785f, 0.425325f, 0.688191f},
75 {-0.850651f, 0.525731f, 0.000000f},{-0.864188f, 0.442863f, 0.238856f},
76 {-0.716567f, 0.681718f, 0.147621f},{-0.688191f, 0.587785f, 0.425325f},
77 {-0.500000f, 0.809017f, 0.309017f}, {-0.238856f, 0.864188f, 0.442863f},
78 {-0.425325f, 0.688191f, 0.587785f}, {-0.716567f, 0.681718f, -0.147621f},
79 {-0.500000f, 0.809017f, -0.309017f}, {-0.525731f, 0.850651f, 0.000000f},
80 {0.000000f, 0.850651f, -0.525731f}, {-0.238856f, 0.864188f, -0.442863f},
81 {0.000000f, 0.955423f, -0.295242f}, {-0.262866f, 0.951056f, -0.162460f},
82 {0.000000f, 1.000000f, 0.000000f}, {0.000000f, 0.955423f, 0.295242f},
83 {-0.262866f, 0.951056f, 0.162460f}, {0.238856f, 0.864188f, 0.442863f},
84 {0.262866f, 0.951056f, 0.162460f}, {0.500000f, 0.809017f, 0.309017f},
85 {0.238856f, 0.864188f, -0.442863f},{0.262866f, 0.951056f, -0.162460f},
86 {0.500000f, 0.809017f, -0.309017f},{0.850651f, 0.525731f, 0.000000f},
87 {0.716567f, 0.681718f, 0.147621f}, {0.716567f, 0.681718f, -0.147621f},
88 {0.525731f, 0.850651f, 0.000000f}, {0.425325f, 0.688191f, 0.587785f},
89 {0.864188f, 0.442863f, 0.238856f}, {0.688191f, 0.587785f, 0.425325f},
90 {0.809017f, 0.309017f, 0.500000f}, {0.681718f, 0.147621f, 0.716567f},
91 {0.587785f, 0.425325f, 0.688191f}, {0.955423f, 0.295242f, 0.000000f},
92 {1.000000f, 0.000000f, 0.000000f}, {0.951056f, 0.162460f, 0.262866f},
93 {0.850651f, -0.525731f, 0.000000f},{0.955423f, -0.295242f, 0.000000f},
94 {0.864188f, -0.442863f, 0.238856f}, {0.951056f, -0.162460f, 0.262866f},
95 {0.809017f, -0.309017f, 0.500000f}, {0.681718f, -0.147621f, 0.716567f},
96 {0.850651f, 0.000000f, 0.525731f}, {0.864188f, 0.442863f, -0.238856f},
97 {0.809017f, 0.309017f, -0.500000f}, {0.951056f, 0.162460f, -0.262866f},
98 {0.525731f, 0.000000f, -0.850651f}, {0.681718f, 0.147621f, -0.716567f},
99 {0.681718f, -0.147621f, -0.716567f},{0.850651f, 0.000000f, -0.525731f},
100 {0.809017f, -0.309017f, -0.500000f}, {0.864188f, -0.442863f, -0.238856f},
101 {0.951056f, -0.162460f, -0.262866f}, {0.147621f, 0.716567f, -0.681718f},
102 {0.309017f, 0.500000f, -0.809017f}, {0.425325f, 0.688191f, -0.587785f},
103 {0.442863f, 0.238856f, -0.864188f}, {0.587785f, 0.425325f, -0.688191f},
104 {0.688191f, 0.587785f, -0.425325f}, {-0.147621f, 0.716567f, -0.681718f},
105 {-0.309017f, 0.500000f, -0.809017f}, {0.000000f, 0.525731f, -0.850651f},
106 {-0.525731f, 0.000000f, -0.850651f}, {-0.442863f, 0.238856f, -0.864188f},
107 {-0.295242f, 0.000000f, -0.955423f}, {-0.162460f, 0.262866f, -0.951056f},
108 {0.000000f, 0.000000f, -1.000000f}, {0.295242f, 0.000000f, -0.955423f},
109 {0.162460f, 0.262866f, -0.951056f}, {-0.442863f, -0.238856f, -0.864188f},
110 {-0.309017f, -0.500000f, -0.809017f}, {-0.162460f, -0.262866f, -0.951056f},
111 {0.000000f, -0.850651f, -0.525731f}, {-0.147621f, -0.716567f, -0.681718f},
112 {0.147621f, -0.716567f, -0.681718f}, {0.000000f, -0.525731f, -0.850651f},
113 {0.309017f, -0.500000f, -0.809017f}, {0.442863f, -0.238856f, -0.864188f},
114 {0.162460f, -0.262866f, -0.951056f}, {0.238856f, -0.864188f, -0.442863f},
115 {0.500000f, -0.809017f, -0.309017f}, {0.425325f, -0.688191f, -0.587785f},
116 {0.716567f, -0.681718f, -0.147621f}, {0.688191f, -0.587785f, -0.425325f},
117 {0.587785f, -0.425325f, -0.688191f}, {0.000000f, -0.955423f, -0.295242f},
118 {0.000000f, -1.000000f, 0.000000f}, {0.262866f, -0.951056f, -0.162460f},
119 {0.000000f, -0.850651f, 0.525731f}, {0.000000f, -0.955423f, 0.295242f},
120 {0.238856f, -0.864188f, 0.442863f}, {0.262866f, -0.951056f, 0.162460f},
121 {0.500000f, -0.809017f, 0.309017f}, {0.716567f, -0.681718f, 0.147621f},
122 {0.525731f, -0.850651f, 0.000000f}, {-0.238856f, -0.864188f, -0.442863f},
123 {-0.500000f, -0.809017f, -0.309017f}, {-0.262866f, -0.951056f, -0.162460f},
124 {-0.850651f, -0.525731f, 0.000000f}, {-0.716567f, -0.681718f, -0.147621f},
125 {-0.716567f, -0.681718f, 0.147621f}, {-0.525731f, -0.850651f, 0.000000f},
126 {-0.500000f, -0.809017f, 0.309017f}, {-0.238856f, -0.864188f, 0.442863f},
127 {-0.262866f, -0.951056f, 0.162460f}, {-0.864188f, -0.442863f, 0.238856f},
128 {-0.809017f, -0.309017f, 0.500000f}, {-0.688191f, -0.587785f, 0.425325f},
129 {-0.681718f, -0.147621f, 0.716567f}, {-0.442863f, -0.238856f, 0.864188f},
130 {-0.587785f, -0.425325f, 0.688191f}, {-0.309017f, -0.500000f, 0.809017f},
131 {-0.147621f, -0.716567f, 0.681718f}, {-0.425325f, -0.688191f, 0.587785f},
132 {-0.162460f, -0.262866f, 0.951056f}, {0.442863f, -0.238856f, 0.864188f},
133 {0.162460f, -0.262866f, 0.951056f}, {0.309017f, -0.500000f, 0.809017f},
134 {0.147621f, -0.716567f, 0.681718f}, {0.000000f, -0.525731f, 0.850651f},
135 {0.425325f, -0.688191f, 0.587785f}, {0.587785f, -0.425325f, 0.688191f},
136 {0.688191f, -0.587785f, 0.425325f}, {-0.955423f, 0.295242f, 0.000000f},
137 {-0.951056f, 0.162460f, 0.262866f}, {-1.000000f, 0.000000f, 0.000000f},
138 {-0.850651f, 0.000000f, 0.525731f}, {-0.955423f, -0.295242f, 0.000000f},
139 {-0.951056f, -0.162460f, 0.262866f}, {-0.864188f, 0.442863f, -0.238856f},
140 {-0.951056f, 0.162460f, -0.262866f}, {-0.809017f, 0.309017f, -0.500000f},
141 {-0.864188f, -0.442863f, -0.238856f}, {-0.951056f, -0.162460f, -0.262866f},
142 {-0.809017f, -0.309017f, -0.500000f}, {-0.681718f, 0.147621f, -0.716567f},
143 {-0.681718f, -0.147621f, -0.716567f}, {-0.850651f, 0.000000f, -0.525731f},
144 {-0.688191f, 0.587785f, -0.425325f}, {-0.587785f, 0.425325f, -0.688191f},
145 {-0.425325f, 0.688191f, -0.587785f}, {-0.425325f, -0.688191f, -0.587785f},
146 {-0.587785f, -0.425325f, -0.688191f}, {-0.688191f, -0.587785f, -0.425325f}
147 };
148
149 //==============================================================
150
151 int Q_rand( int *seed ) {
152 *seed = (69069 * *seed + 1);
153 return *seed;
154 }
155
156 float Q_random( int *seed ) {
157 return ( Q_rand( seed ) & 0xffff ) / (float)0x10000;
158 }
159
160 float Q_crandom( int *seed ) {
161 return 2.0 * ( Q_random( seed ) - 0.5 );
162 }
163
164 //=======================================================
165
166 signed char ClampChar( int i ) {
167 if ( i < -128 ) {
168 return -128;
169 }
170 if ( i > 127 ) {
171 return 127;
172 }
173 return i;
174 }
175
176 signed short ClampShort( int i ) {
177 if ( i < -32768 ) {
178 return -32768;
179 }
180 if ( i > 0x7fff ) {
181 return 0x7fff;
182 }
183 return i;
184 }
185
186
187 // this isn't a real cheap function to call!
188 int DirToByte( vec3_t dir ) {
189 int i, best;
190 float d, bestd;
191
192 if ( !dir ) {
193 return 0;
194 }
195
196 bestd = 0;
197 best = 0;
198 for (i=0 ; i<NUMVERTEXNORMALS ; i++)
199 {
200 d = DotProduct (dir, bytedirs[i]);
201 if (d > bestd)
202 {
203 bestd = d;
204 best = i;
205 }
206 }
207
208 return best;
209 }
210
211 void ByteToDir( int b, vec3_t dir ) {
212 if ( b < 0 || b >= NUMVERTEXNORMALS ) {
213 VectorCopy( vec3_origin, dir );
214 return;
215 }
216 VectorCopy (bytedirs[b], dir);
217 }
218
219
220 unsigned ColorBytes3 (float r, float g, float b) {
221 unsigned i;
222
223 ( (byte *)&i )[0] = r * 255;
224 ( (byte *)&i )[1] = g * 255;
225 ( (byte *)&i )[2] = b * 255;
226
227 return i;
228 }
229
230 unsigned ColorBytes4 (float r, float g, float b, float a) {
231 unsigned i;
232
233 ( (byte *)&i )[0] = r * 255;
234 ( (byte *)&i )[1] = g * 255;
235 ( (byte *)&i )[2] = b * 255;
236 ( (byte *)&i )[3] = a * 255;
237
238 return i;
239 }
240
241 float NormalizeColor( const vec3_t in, vec3_t out ) {
242 float max;
243
244 max = in[0];
245 if ( in[1] > max ) {
246 max = in[1];
247 }
248 if ( in[2] > max ) {
249 max = in[2];
250 }
251
252 if ( !max ) {
253 VectorClear( out );
254 } else {
255 out[0] = in[0] / max;
256 out[1] = in[1] / max;
257 out[2] = in[2] / max;
258 }
259 return max;
260 }
261
262
263 /*
264 =====================
265 PlaneFromPoints
266
267 Returns false if the triangle is degenrate.
268 The normal will point out of the clock for clockwise ordered points
269 =====================
270 */
271 qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
272 vec3_t d1, d2;
273
274 VectorSubtract( b, a, d1 );
275 VectorSubtract( c, a, d2 );
276 CrossProduct( d2, d1, plane );
277 if ( VectorNormalize( plane ) == 0 ) {
278 return qfalse;
279 }
280
281 plane[3] = DotProduct( a, plane );
282 return qtrue;
283 }
284
285 /*
286 ===============
287 RotatePointAroundVector
288
289 This is not implemented very well...
290 ===============
291 */
292 void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
293 float degrees ) {
294 float m[3][3];
295 float im[3][3];
296 float zrot[3][3];
297 float tmpmat[3][3];
298 float rot[3][3];
299 int i;
300 vec3_t vr, vup, vf;
301 float rad;
302
303 vf[0] = dir[0];
304 vf[1] = dir[1];
305 vf[2] = dir[2];
306
307 PerpendicularVector( vr, dir );
308 CrossProduct( vr, vf, vup );
309
310 m[0][0] = vr[0];
311 m[1][0] = vr[1];
312 m[2][0] = vr[2];
313
314 m[0][1] = vup[0];
315 m[1][1] = vup[1];
316 m[2][1] = vup[2];
317
318 m[0][2] = vf[0];
319 m[1][2] = vf[1];
320 m[2][2] = vf[2];
321
322 memcpy( im, m, sizeof( im ) );
323
324 im[0][1] = m[1][0];
325 im[0][2] = m[2][0];
326 im[1][0] = m[0][1];
327 im[1][2] = m[2][1];
328 im[2][0] = m[0][2];
329 im[2][1] = m[1][2];
330
331 memset( zrot, 0, sizeof( zrot ) );
332 zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
333
334 rad = DEG2RAD( degrees );
335 zrot[0][0] = cos( rad );
336 zrot[0][1] = sin( rad );
337 zrot[1][0] = -sin( rad );
338 zrot[1][1] = cos( rad );
339
340 MatrixMultiply( m, zrot, tmpmat );
341 MatrixMultiply( tmpmat, im, rot );
342
343 for ( i = 0; i < 3; i++ ) {
344 dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
345 }
346 }
347
348 /*
349 ===============
350 RotateAroundDirection
351 ===============
352 */
353 void RotateAroundDirection( vec3_t axis[3], float yaw ) {
354
355 // create an arbitrary axis[1]
356 PerpendicularVector( axis[1], axis[0] );
357
358 // rotate it around axis[0] by yaw
359 if ( yaw ) {
360 vec3_t temp;
361
362 VectorCopy( axis[1], temp );
363 RotatePointAroundVector( axis[1], axis[0], temp, yaw );
364 }
365
366 // cross to get axis[2]
367 CrossProduct( axis[0], axis[1], axis[2] );
368 }
369
370
371
372 void vectoangles( const vec3_t value1, vec3_t angles ) {
373 float forward;
374 float yaw, pitch;
375
376 if ( value1[1] == 0 && value1[0] == 0 ) {
377 yaw = 0;
378 if ( value1[2] > 0 ) {
379 pitch = 90;
380 }
381 else {
382 pitch = 270;
383 }
384 }
385 else {
386 if ( value1[0] ) {
387 yaw = ( atan2 ( value1[1], value1[0] ) * 180 / M_PI );
388 }
389 else if ( value1[1] > 0 ) {
390 yaw = 90;
391 }
392 else {
393 yaw = 270;
394 }
395 if ( yaw < 0 ) {
396 yaw += 360;
397 }
398
399 forward = sqrt ( value1[0]*value1[0] + value1[1]*value1[1] );
400 pitch = ( atan2(value1[2], forward) * 180 / M_PI );
401 if ( pitch < 0 ) {
402 pitch += 360;
403 }
404 }
405
406 angles[PITCH] = -pitch;
407 angles[YAW] = yaw;
408 angles[ROLL] = 0;
409 }
410
411
412 /*
413 =================
414 AnglesToAxis
415 =================
416 */
417 void AnglesToAxis( const vec3_t angles, vec3_t axis[3] ) {
418 vec3_t right;
419
420 // angle vectors returns "right" instead of "y axis"
421 AngleVectors( angles, axis[0], right, axis[2] );
422 VectorSubtract( vec3_origin, right, axis[1] );
423 }
424
425 void AxisClear( vec3_t axis[3] ) {
426 axis[0][0] = 1;
427 axis[0][1] = 0;
428 axis[0][2] = 0;
429 axis[1][0] = 0;
430 axis[1][1] = 1;
431 axis[1][2] = 0;
432 axis[2][0] = 0;
433 axis[2][1] = 0;
434 axis[2][2] = 1;
435 }
436
437 void AxisCopy( vec3_t in[3], vec3_t out[3] ) {
438 VectorCopy( in[0], out[0] );
439 VectorCopy( in[1], out[1] );
440 VectorCopy( in[2], out[2] );
441 }
442
443 void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
444 {
445 float d;
446 vec3_t n;
447 float inv_denom;
448
449 inv_denom = DotProduct( normal, normal );
450 #ifndef Q3_VM
451 assert( Q_fabs(inv_denom) != 0.0f ); // zero vectors get here
452 #endif
453 inv_denom = 1.0f / inv_denom;
454
455 d = DotProduct( normal, p ) * inv_denom;
456
457 n[0] = normal[0] * inv_denom;
458 n[1] = normal[1] * inv_denom;
459 n[2] = normal[2] * inv_denom;
460
461 dst[0] = p[0] - d * n[0];
462 dst[1] = p[1] - d * n[1];
463 dst[2] = p[2] - d * n[2];
464 }
465
466 /*
467 ================
468 MakeNormalVectors
469
470 Given a normalized forward vector, create two
471 other perpendicular vectors
472 ================
473 */
474 void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up) {
475 float d;
476
477 // this rotate and negate guarantees a vector
478 // not colinear with the original
479 right[1] = -forward[0];
480 right[2] = forward[1];
481 right[0] = forward[2];
482
483 d = DotProduct (right, forward);
484 VectorMA (right, -d, forward, right);
485 VectorNormalize (right);
486 CrossProduct (right, forward, up);
487 }
488
489
490 void VectorRotate( vec3_t in, vec3_t matrix[3], vec3_t out )
491 {
492 out[0] = DotProduct( in, matrix[0] );
493 out[1] = DotProduct( in, matrix[1] );
494 out[2] = DotProduct( in, matrix[2] );
495 }
496
497 //============================================================================
498
499 #if !idppc
500 /*
501 ** float q_rsqrt( float number )
502 */
503 float Q_rsqrt( float number )
504 {
505 union {
506 float f;
507 int i;
508 } t;
509 float x2, y;
510 const float threehalfs = 1.5F;
511
512 x2 = number * 0.5F;
513 t.f = number;
514 t.i = 0x5f3759df - ( t.i >> 1 ); // what the fuck?
515 y = t.f;
516 y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
517 // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
518
519 return y;
520 }
521
522 float Q_fabs( float f ) {
523 int tmp = * ( int * ) &f;
524 tmp &= 0x7FFFFFFF;
525 return * ( float * ) &tmp;
526 }
527 #endif
528
529 //============================================================
530
531 /*
532 ===============
533 LerpAngle
534
535 ===============
536 */
537 float LerpAngle (float from, float to, float frac) {
538 float a;
539
540 if ( to - from > 180 ) {
541 to -= 360;
542 }
543 if ( to - from < -180 ) {
544 to += 360;
545 }
546 a = from + frac * (to - from);
547
548 return a;
549 }
550
551
552 /*
553 =================
554 AngleSubtract
555
556 Always returns a value from -180 to 180
557 =================
558 */
559 float AngleSubtract( float a1, float a2 ) {
560 float a;
561
562 a = a1 - a2;
563 while ( a > 180 ) {
564 a -= 360;
565 }
566 while ( a < -180 ) {
567 a += 360;
568 }
569 return a;
570 }
571
572
573 void AnglesSubtract( vec3_t v1, vec3_t v2, vec3_t v3 ) {
574 v3[0] = AngleSubtract( v1[0], v2[0] );
575 v3[1] = AngleSubtract( v1[1], v2[1] );
576 v3[2] = AngleSubtract( v1[2], v2[2] );
577 }
578
579
580 float AngleMod(float a) {
581 a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
582 return a;
583 }
584
585
586 /*
587 =================
588 AngleNormalize360
589
590 returns angle normalized to the range [0 <= angle < 360]
591 =================
592 */
593 float AngleNormalize360 ( float angle ) {
594 return (360.0 / 65536) * ((int)(angle * (65536 / 360.0)) & 65535);
595 }
596
597
598 /*
599 =================
600 AngleNormalize180
601
602 returns angle normalized to the range [-180 < angle <= 180]
603 =================
604 */
605 float AngleNormalize180 ( float angle ) {
606 angle = AngleNormalize360( angle );
607 if ( angle > 180.0 ) {
608 angle -= 360.0;
609 }
610 return angle;
611 }
612
613
614 /*
615 =================
616 AngleDelta
617
618 returns the normalized delta from angle1 to angle2
619 =================
620 */
621 float AngleDelta ( float angle1, float angle2 ) {
622 return AngleNormalize180( angle1 - angle2 );
623 }
624
625
626 //============================================================
627
628
629 /*
630 =================
631 SetPlaneSignbits
632 =================
633 */
634 void SetPlaneSignbits (cplane_t *out) {
635 int bits, j;
636
637 // for fast box on planeside test
638 bits = 0;
639 for (j=0 ; j<3 ; j++) {
640 if (out->normal[j] < 0) {
641 bits |= 1<<j;
642 }
643 }
644 out->signbits = bits;
645 }
646
647
648 /*
649 ==================
650 BoxOnPlaneSide
651
652 Returns 1, 2, or 1 + 2
653
654 // this is the slow, general version
655 int BoxOnPlaneSide2 (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
656 {
657 int i;
658 float dist1, dist2;
659 int sides;
660 vec3_t corners[2];
661
662 for (i=0 ; i<3 ; i++)
663 {
664 if (p->normal[i] < 0)
665 {
666 corners[0][i] = emins[i];
667 corners[1][i] = emaxs[i];
668 }
669 else
670 {
671 corners[1][i] = emins[i];
672 corners[0][i] = emaxs[i];
673 }
674 }
675 dist1 = DotProduct (p->normal, corners[0]) - p->dist;
676 dist2 = DotProduct (p->normal, corners[1]) - p->dist;
677 sides = 0;
678 if (dist1 >= 0)
679 sides = 1;
680 if (dist2 < 0)
681 sides |= 2;
682
683 return sides;
684 }
685
686 ==================
687 */
688
689 #if !id386
690
691 int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
692 {
693 float dist1, dist2;
694 int sides;
695
696 // fast axial cases
697 if (p->type < 3)
698 {
699 if (p->dist <= emins[p->type])
700 return 1;
701 if (p->dist >= emaxs[p->type])
702 return 2;
703 return 3;
704 }
705
706 // general case
707 switch (p->signbits)
708 {
709 case 0:
710 dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
711 dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
712 break;
713 case 1:
714 dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
715 dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
716 break;
717 case 2:
718 dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
719 dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
720 break;
721 case 3:
722 dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
723 dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
724 break;
725 case 4:
726 dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
727 dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
728 break;
729 case 5:
730 dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
731 dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
732 break;
733 case 6:
734 dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
735 dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
736 break;
737 case 7:
738 dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
739 dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
740 break;
741 default:
742 dist1 = dist2 = 0; // shut up compiler
743 break;
744 }
745
746 sides = 0;
747 if (dist1 >= p->dist)
748 sides = 1;
749 if (dist2 < p->dist)
750 sides |= 2;
751
752 return sides;
753 }
754 #elif __GNUC__
755 // use matha.s
756 #else
757 #pragma warning( disable: 4035 )
758
759 __declspec( naked ) int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
760 {
761 static int bops_initialized;
762 static int Ljmptab[8];
763
764 __asm {
765
766 push ebx
767
768 cmp bops_initialized, 1
769 je initialized
770 mov bops_initialized, 1
771
772 mov Ljmptab[0*4], offset Lcase0
773 mov Ljmptab[1*4], offset Lcase1
774 mov Ljmptab[2*4], offset Lcase2
775 mov Ljmptab[3*4], offset Lcase3
776 mov Ljmptab[4*4], offset Lcase4
777 mov Ljmptab[5*4], offset Lcase5
778 mov Ljmptab[6*4], offset Lcase6
779 mov Ljmptab[7*4], offset Lcase7
780
781 initialized:
782
783 mov edx,dword ptr[4+12+esp]
784 mov ecx,dword ptr[4+4+esp]
785 xor eax,eax
786 mov ebx,dword ptr[4+8+esp]
787 mov al,byte ptr[17+edx]
788 cmp al,8
789 jge Lerror
790 fld dword ptr[0+edx]
791 fld st(0)
792 jmp dword ptr[Ljmptab+eax*4]
793 Lcase0:
794 fmul dword ptr[ebx]
795 fld dword ptr[0+4+edx]
796 fxch st(2)
797 fmul dword ptr[ecx]
798 fxch st(2)
799 fld st(0)
800 fmul dword ptr[4+ebx]
801 fld dword ptr[0+8+edx]
802 fxch st(2)
803 fmul dword ptr[4+ecx]
804 fxch st(2)
805 fld st(0)
806 fmul dword ptr[8+ebx]
807 fxch st(5)
808 faddp st(3),st(0)
809 fmul dword ptr[8+ecx]
810 fxch st(1)
811 faddp st(3),st(0)
812 fxch st(3)
813 faddp st(2),st(0)
814 jmp LSetSides
815 Lcase1:
816 fmul dword ptr[ecx]
817 fld dword ptr[0+4+edx]
818 fxch st(2)
819 fmul dword ptr[ebx]
820 fxch st(2)
821 fld st(0)
822 fmul dword ptr[4+ebx]
823 fld dword ptr[0+8+edx]
824 fxch st(2)
825 fmul dword ptr[4+ecx]
826 fxch st(2)
827 fld st(0)
828 fmul dword ptr[8+ebx]
829 fxch st(5)
830 faddp st(3),st(0)
831 fmul dword ptr[8+ecx]
832 fxch st(1)
833 faddp st(3),st(0)
834 fxch st(3)
835 faddp st(2),st(0)
836 jmp LSetSides
837 Lcase2:
838 fmul dword ptr[ebx]
839 fld dword ptr[0+4+edx]
840 fxch st(2)
841 fmul dword ptr[ecx]
842 fxch st(2)
843 fld st(0)
844 fmul dword ptr[4+ecx]
845 fld dword ptr[0+8+edx]
846 fxch st(2)
847 fmul dword ptr[4+ebx]
848 fxch st(2)
849 fld st(0)
850 fmul dword ptr[8+ebx]
851 fxch st(5)
852 faddp st(3),st(0)
853 fmul dword ptr[8+ecx]
854 fxch st(1)
855 faddp st(3),st(0)
856 fxch st(3)
857 faddp st(2),st(0)
858 jmp LSetSides
859 Lcase3:
860 fmul dword ptr[ecx]
861 fld dword ptr[0+4+edx]
862 fxch st(2)
863 fmul dword ptr[ebx]
864 fxch st(2)
865 fld st(0)
866 fmul dword ptr[4+ecx]
867 fld dword ptr[0+8+edx]
868 fxch st(2)
869 fmul dword ptr[4+ebx]
870 fxch st(2)
871 fld st(0)
872 fmul dword ptr[8+ebx]
873 fxch st(5)
874 faddp st(3),st(0)
875 fmul dword ptr[8+ecx]
876 fxch st(1)
877 faddp st(3),st(0)
878 fxch st(3)
879 faddp st(2),st(0)
880 jmp LSetSides
881 Lcase4:
882 fmul dword ptr[ebx]
883 fld dword ptr[0+4+edx]
884 fxch st(2)
885 fmul dword ptr[ecx]
886 fxch st(2)
887 fld st(0)
888 fmul dword ptr[4+ebx]
889 fld dword ptr[0+8+edx]
890 fxch st(2)
891 fmul dword ptr[4+ecx]
892 fxch st(2)
893 fld st(0)
894 fmul dword ptr[8+ecx]
895 fxch st(5)
896 faddp st(3),st(0)
897 fmul dword ptr[8+ebx]
898 fxch st(1)
899 faddp st(3),st(0)
900 fxch st(3)
901 faddp st(2),st(0)
902 jmp LSetSides
903 Lcase5:
904 fmul dword ptr[ecx]
905 fld dword ptr[0+4+edx]
906 fxch st(2)
907 fmul dword ptr[ebx]
908 fxch st(2)
909 fld st(0)
910 fmul dword ptr[4+ebx]
911 fld dword ptr[0+8+edx]
912 fxch st(2)
913 fmul dword ptr[4+ecx]
914 fxch st(2)
915 fld st(0)
916 fmul dword ptr[8+ecx]
917 fxch st(5)
918 faddp st(3),st(0)
919 fmul dword ptr[8+ebx]
920 fxch st(1)
921 faddp st(3),st(0)
922 fxch st(3)
923 faddp st(2),st(0)
924 jmp LSetSides
925 Lcase6:
926 fmul dword ptr[ebx]
927 fld dword ptr[0+4+edx]
928 fxch st(2)
929 fmul dword ptr[ecx]
930 fxch st(2)
931 fld st(0)
932 fmul dword ptr[4+ecx]
933 fld dword ptr[0+8+edx]
934 fxch st(2)
935 fmul dword ptr[4+ebx]
936 fxch st(2)
937 fld st(0)
938 fmul dword ptr[8+ecx]
939 fxch st(5)
940 faddp st(3),st(0)
941 fmul dword ptr[8+ebx]
942 fxch st(1)
943 faddp st(3),st(0)
944 fxch st(3)
945 faddp st(2),st(0)
946 jmp LSetSides
947 Lcase7:
948 fmul dword ptr[ecx]
949 fld dword ptr[0+4+edx]
950 fxch st(2)
951 fmul dword ptr[ebx]
952 fxch st(2)
953 fld st(0)
954 fmul dword ptr[4+ecx]
955 fld dword ptr[0+8+edx]
956 fxch st(2)
957 fmul dword ptr[4+ebx]
958 fxch st(2)
959 fld st(0)
960 fmul dword ptr[8+ecx]
961 fxch st(5)
962 faddp st(3),st(0)
963 fmul dword ptr[8+ebx]
964 fxch st(1)
965 faddp st(3),st(0)
966 fxch st(3)
967 faddp st(2),st(0)
968 LSetSides:
969 faddp st(2),st(0)
970 fcomp dword ptr[12+edx]
971 xor ecx,ecx
972 fnstsw ax
973 fcomp dword ptr[12+edx]
974 and ah,1
975 xor ah,1
976 add cl,ah
977 fnstsw ax
978 and ah,1
979 add ah,ah
980 add cl,ah
981 pop ebx
982 mov eax,ecx
983 ret
984 Lerror:
985 int 3
986 }
987 }
988 #pragma warning( default: 4035 )
989
990 #endif
991
992 /*
993 =================
994 RadiusFromBounds
995 =================
996 */
997 float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) {
998 int i;
999 vec3_t corner;
1000 float a, b;
1001
1002 for (i=0 ; i<3 ; i++) {
1003 a = fabs( mins[i] );
1004 b = fabs( maxs[i] );
1005 corner[i] = a > b ? a : b;
1006 }
1007
1008 return VectorLength (corner);
1009 }
1010
1011
1012 void ClearBounds( vec3_t mins, vec3_t maxs ) {
1013 mins[0] = mins[1] = mins[2] = 99999;
1014 maxs[0] = maxs[1] = maxs[2] = -99999;
1015 }
1016
1017 void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs ) {
1018 if ( v[0] < mins[0] ) {
1019 mins[0] = v[0];
1020 }
1021 if ( v[0] > maxs[0]) {
1022 maxs[0] = v[0];
1023 }
1024
1025 if ( v[1] < mins[1] ) {
1026 mins[1] = v[1];
1027 }
1028 if ( v[1] > maxs[1]) {
1029 maxs[1] = v[1];
1030 }
1031
1032 if ( v[2] < mins[2] ) {
1033 mins[2] = v[2];
1034 }
1035 if ( v[2] > maxs[2]) {
1036 maxs[2] = v[2];
1037 }
1038 }
1039
1040 qboolean BoundsIntersect(const vec3_t mins, const vec3_t maxs,
1041 const vec3_t mins2, const vec3_t maxs2)
1042 {
1043 if ( maxs[0] < mins2[0] ||
1044 maxs[1] < mins2[1] ||
1045 maxs[2] < mins2[2] ||
1046 mins[0] > maxs2[0] ||
1047 mins[1] > maxs2[1] ||
1048 mins[2] > maxs2[2])
1049 {
1050 return qfalse;
1051 }
1052
1053 return qtrue;
1054 }
1055
1056 qboolean BoundsIntersectSphere(const vec3_t mins, const vec3_t maxs,
1057 const vec3_t origin, vec_t radius)
1058 {
1059 if ( origin[0] - radius > maxs[0] ||
1060 origin[0] + radius < mins[0] ||
1061 origin[1] - radius > maxs[1] ||
1062 origin[1] + radius < mins[1] ||
1063 origin[2] - radius > maxs[2] ||
1064 origin[2] + radius < mins[2])
1065 {
1066 return qfalse;
1067 }
1068
1069 return qtrue;
1070 }
1071
1072 qboolean BoundsIntersectPoint(const vec3_t mins, const vec3_t maxs,
1073 const vec3_t origin)
1074 {
1075 if ( origin[0] > maxs[0] ||
1076 origin[0] < mins[0] ||
1077 origin[1] > maxs[1] ||
1078 origin[1] < mins[1] ||
1079 origin[2] > maxs[2] ||
1080 origin[2] < mins[2])
1081 {
1082 return qfalse;
1083 }
1084
1085 return qtrue;
1086 }
1087
1088 vec_t VectorNormalize( vec3_t v ) {
1089 // NOTE: TTimo - Apple G4 altivec source uses double?
1090 float length, ilength;
1091
1092 length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
1093 length = sqrt (length);
1094
1095 if ( length ) {
1096 ilength = 1/length;
1097 v[0] *= ilength;
1098 v[1] *= ilength;
1099 v[2] *= ilength;
1100 }
1101
1102 return length;
1103 }
1104
1105 vec_t VectorNormalize2( const vec3_t v, vec3_t out) {
1106 float length, ilength;
1107
1108 length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
1109 length = sqrt (length);
1110
1111 if (length)
1112 {
1113 ilength = 1/length;
1114 out[0] = v[0]*ilength;
1115 out[1] = v[1]*ilength;
1116 out[2] = v[2]*ilength;
1117 } else {
1118 VectorClear( out );
1119 }
1120
1121 return length;
1122
1123 }
1124
1125 void _VectorMA( const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc) {
1126 vecc[0] = veca[0] + scale*vecb[0];
1127 vecc[1] = veca[1] + scale*vecb[1];
1128 vecc[2] = veca[2] + scale*vecb[2];
1129 }
1130
1131
1132 vec_t _DotProduct( const vec3_t v1, const vec3_t v2 ) {
1133 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
1134 }
1135
1136 void _VectorSubtract( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
1137 out[0] = veca[0]-vecb[0];
1138 out[1] = veca[1]-vecb[1];
1139 out[2] = veca[2]-vecb[2];
1140 }
1141
1142 void _VectorAdd( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
1143 out[0] = veca[0]+vecb[0];
1144 out[1] = veca[1]+vecb[1];
1145 out[2] = veca[2]+vecb[2];
1146 }
1147
1148 void _VectorCopy( const vec3_t in, vec3_t out ) {
1149 out[0] = in[0];
1150 out[1] = in[1];
1151 out[2] = in[2];
1152 }
1153
1154 void _VectorScale( const vec3_t in, vec_t scale, vec3_t out ) {
1155 out[0] = in[0]*scale;
1156 out[1] = in[1]*scale;
1157 out[2] = in[2]*scale;
1158 }
1159
1160 void Vector4Scale( const vec4_t in, vec_t scale, vec4_t out ) {
1161 out[0] = in[0]*scale;
1162 out[1] = in[1]*scale;
1163 out[2] = in[2]*scale;
1164 out[3] = in[3]*scale;
1165 }
1166
1167
1168 int Q_log2( int val ) {
1169 int answer;
1170
1171 answer = 0;
1172 while ( ( val>>=1 ) != 0 ) {
1173 answer++;
1174 }
1175 return answer;
1176 }
1177
1178
1179
1180 /*
1181 =================
1182 PlaneTypeForNormal
1183 =================
1184 */
1185 /*
1186 int PlaneTypeForNormal (vec3_t normal) {
1187 if ( normal[0] == 1.0 )
1188 return PLANE_X;
1189 if ( normal[1] == 1.0 )
1190 return PLANE_Y;
1191 if ( normal[2] == 1.0 )
1192 return PLANE_Z;
1193
1194 return PLANE_NON_AXIAL;
1195 }
1196 */
1197
1198
1199 /*
1200 ================
1201 MatrixMultiply
1202 ================
1203 */
1204 void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
1205 out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
1206 in1[0][2] * in2[2][0];
1207 out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
1208 in1[0][2] * in2[2][1];
1209 out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
1210 in1[0][2] * in2[2][2];
1211 out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
1212 in1[1][2] * in2[2][0];
1213 out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
1214 in1[1][2] * in2[2][1];
1215 out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
1216 in1[1][2] * in2[2][2];
1217 out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
1218 in1[2][2] * in2[2][0];
1219 out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
1220 in1[2][2] * in2[2][1];
1221 out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
1222 in1[2][2] * in2[2][2];
1223 }
1224
1225
1226 void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) {
1227 float angle;
1228 static float sr, sp, sy, cr, cp, cy;
1229 // static to help MS compiler fp bugs
1230
1231 angle = angles[YAW] * (M_PI*2 / 360);
1232 sy = sin(angle);
1233 cy = cos(angle);
1234 angle = angles[PITCH] * (M_PI*2 / 360);
1235 sp = sin(angle);
1236 cp = cos(angle);
1237 angle = angles[ROLL] * (M_PI*2 / 360);
1238 sr = sin(angle);
1239 cr = cos(angle);
1240
1241 if (forward)
1242 {
1243 forward[0] = cp*cy;
1244 forward[1] = cp*sy;
1245 forward[2] = -sp;
1246 }
1247 if (right)
1248 {
1249 right[0] = (-1*sr*sp*cy+-1*cr*-sy);
1250 right[1] = (-1*sr*sp*sy+-1*cr*cy);
1251 right[2] = -1*sr*cp;
1252 }
1253 if (up)
1254 {
1255 up[0] = (cr*sp*cy+-sr*-sy);
1256 up[1] = (cr*sp*sy+-sr*cy);
1257 up[2] = cr*cp;
1258 }
1259 }
1260
1261 /*
1262 ** assumes "src" is normalized
1263 */
1264 void PerpendicularVector( vec3_t dst, const vec3_t src )
1265 {
1266 int pos;
1267 int i;
1268 float minelem = 1.0F;
1269 vec3_t tempvec;
1270
1271 /*
1272 ** find the smallest magnitude axially aligned vector
1273 */
1274 for ( pos = 0, i = 0; i < 3; i++ )
1275 {
1276 if ( fabs( src[i] ) < minelem )
1277 {
1278 pos = i;
1279 minelem = fabs( src[i] );
1280 }
1281 }
1282 tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
1283 tempvec[pos] = 1.0F;
1284
1285 /*
1286 ** project the point onto the plane defined by src
1287 */
1288 ProjectPointOnPlane( dst, tempvec, src );
1289
1290 /*
1291 ** normalize the result
1292 */
1293 VectorNormalize( dst );
1294 }
1295
1296 /*
1297 ================
1298 Q_isnan
1299
1300 Don't pass doubles to this
1301 ================
1302 */
1303 int Q_isnan( float x )
1304 {
1305 union
1306 {
1307 float f;
1308 unsigned int i;
1309 } t;
1310
1311 t.f = x;
1312 t.i &= 0x7FFFFFFF;
1313 t.i = 0x7F800000 - t.i;
1314
1315 return (int)( (unsigned int)t.i >> 31 );
1316 }