Quake II RTX doxygen  1.0 dev
math.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (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. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #include "shared/shared.h"
20 #include "common/math.h"
21 
22 #if USE_CLIENT
23 
24 /*
25 ======
26 vectoangles2 - this is duplicated in the game DLL, but I need it here.
27 ======
28 */
29 void vectoangles2(const vec3_t value1, vec3_t angles)
30 {
31  float forward;
32  float yaw, pitch;
33 
34  if (value1[1] == 0 && value1[0] == 0) {
35  yaw = 0;
36  if (value1[2] > 0)
37  pitch = 90;
38  else
39  pitch = 270;
40  } else {
41  if (value1[0])
42  yaw = atan2(value1[1], value1[0]) * 180 / M_PI;
43  else if (value1[1] > 0)
44  yaw = 90;
45  else
46  yaw = 270;
47 
48  if (yaw < 0)
49  yaw += 360;
50 
51  forward = sqrt(value1[0] * value1[0] + value1[1] * value1[1]);
52  pitch = atan2(value1[2], forward) * 180 / M_PI;
53  if (pitch < 0)
54  pitch += 360;
55  }
56 
57  angles[PITCH] = -pitch;
58  angles[YAW] = yaw;
59  angles[ROLL] = 0;
60 }
61 
62 void MakeNormalVectors(const vec3_t forward, vec3_t right, vec3_t up)
63 {
64  float d;
65 
66  // this rotate and negate guarantees a vector
67  // not colinear with the original
68  right[1] = -forward[0];
69  right[2] = forward[1];
70  right[0] = forward[2];
71 
72  d = DotProduct(right, forward);
73  VectorMA(right, -d, forward, right);
75  CrossProduct(right, forward, up);
76 }
77 
78 #endif // USE_CLIENT
79 
80 const vec3_t bytedirs[NUMVERTEXNORMALS] = {
81  {-0.525731, 0.000000, 0.850651},
82  {-0.442863, 0.238856, 0.864188},
83  {-0.295242, 0.000000, 0.955423},
84  {-0.309017, 0.500000, 0.809017},
85  {-0.162460, 0.262866, 0.951056},
86  {0.000000, 0.000000, 1.000000},
87  {0.000000, 0.850651, 0.525731},
88  {-0.147621, 0.716567, 0.681718},
89  {0.147621, 0.716567, 0.681718},
90  {0.000000, 0.525731, 0.850651},
91  {0.309017, 0.500000, 0.809017},
92  {0.525731, 0.000000, 0.850651},
93  {0.295242, 0.000000, 0.955423},
94  {0.442863, 0.238856, 0.864188},
95  {0.162460, 0.262866, 0.951056},
96  {-0.681718, 0.147621, 0.716567},
97  {-0.809017, 0.309017, 0.500000},
98  {-0.587785, 0.425325, 0.688191},
99  {-0.850651, 0.525731, 0.000000},
100  {-0.864188, 0.442863, 0.238856},
101  {-0.716567, 0.681718, 0.147621},
102  {-0.688191, 0.587785, 0.425325},
103  {-0.500000, 0.809017, 0.309017},
104  {-0.238856, 0.864188, 0.442863},
105  {-0.425325, 0.688191, 0.587785},
106  {-0.716567, 0.681718, -0.147621},
107  {-0.500000, 0.809017, -0.309017},
108  {-0.525731, 0.850651, 0.000000},
109  {0.000000, 0.850651, -0.525731},
110  {-0.238856, 0.864188, -0.442863},
111  {0.000000, 0.955423, -0.295242},
112  {-0.262866, 0.951056, -0.162460},
113  {0.000000, 1.000000, 0.000000},
114  {0.000000, 0.955423, 0.295242},
115  {-0.262866, 0.951056, 0.162460},
116  {0.238856, 0.864188, 0.442863},
117  {0.262866, 0.951056, 0.162460},
118  {0.500000, 0.809017, 0.309017},
119  {0.238856, 0.864188, -0.442863},
120  {0.262866, 0.951056, -0.162460},
121  {0.500000, 0.809017, -0.309017},
122  {0.850651, 0.525731, 0.000000},
123  {0.716567, 0.681718, 0.147621},
124  {0.716567, 0.681718, -0.147621},
125  {0.525731, 0.850651, 0.000000},
126  {0.425325, 0.688191, 0.587785},
127  {0.864188, 0.442863, 0.238856},
128  {0.688191, 0.587785, 0.425325},
129  {0.809017, 0.309017, 0.500000},
130  {0.681718, 0.147621, 0.716567},
131  {0.587785, 0.425325, 0.688191},
132  {0.955423, 0.295242, 0.000000},
133  {1.000000, 0.000000, 0.000000},
134  {0.951056, 0.162460, 0.262866},
135  {0.850651, -0.525731, 0.000000},
136  {0.955423, -0.295242, 0.000000},
137  {0.864188, -0.442863, 0.238856},
138  {0.951056, -0.162460, 0.262866},
139  {0.809017, -0.309017, 0.500000},
140  {0.681718, -0.147621, 0.716567},
141  {0.850651, 0.000000, 0.525731},
142  {0.864188, 0.442863, -0.238856},
143  {0.809017, 0.309017, -0.500000},
144  {0.951056, 0.162460, -0.262866},
145  {0.525731, 0.000000, -0.850651},
146  {0.681718, 0.147621, -0.716567},
147  {0.681718, -0.147621, -0.716567},
148  {0.850651, 0.000000, -0.525731},
149  {0.809017, -0.309017, -0.500000},
150  {0.864188, -0.442863, -0.238856},
151  {0.951056, -0.162460, -0.262866},
152  {0.147621, 0.716567, -0.681718},
153  {0.309017, 0.500000, -0.809017},
154  {0.425325, 0.688191, -0.587785},
155  {0.442863, 0.238856, -0.864188},
156  {0.587785, 0.425325, -0.688191},
157  {0.688191, 0.587785, -0.425325},
158  {-0.147621, 0.716567, -0.681718},
159  {-0.309017, 0.500000, -0.809017},
160  {0.000000, 0.525731, -0.850651},
161  {-0.525731, 0.000000, -0.850651},
162  {-0.442863, 0.238856, -0.864188},
163  {-0.295242, 0.000000, -0.955423},
164  {-0.162460, 0.262866, -0.951056},
165  {0.000000, 0.000000, -1.000000},
166  {0.295242, 0.000000, -0.955423},
167  {0.162460, 0.262866, -0.951056},
168  {-0.442863, -0.238856, -0.864188},
169  {-0.309017, -0.500000, -0.809017},
170  {-0.162460, -0.262866, -0.951056},
171  {0.000000, -0.850651, -0.525731},
172  {-0.147621, -0.716567, -0.681718},
173  {0.147621, -0.716567, -0.681718},
174  {0.000000, -0.525731, -0.850651},
175  {0.309017, -0.500000, -0.809017},
176  {0.442863, -0.238856, -0.864188},
177  {0.162460, -0.262866, -0.951056},
178  {0.238856, -0.864188, -0.442863},
179  {0.500000, -0.809017, -0.309017},
180  {0.425325, -0.688191, -0.587785},
181  {0.716567, -0.681718, -0.147621},
182  {0.688191, -0.587785, -0.425325},
183  {0.587785, -0.425325, -0.688191},
184  {0.000000, -0.955423, -0.295242},
185  {0.000000, -1.000000, 0.000000},
186  {0.262866, -0.951056, -0.162460},
187  {0.000000, -0.850651, 0.525731},
188  {0.000000, -0.955423, 0.295242},
189  {0.238856, -0.864188, 0.442863},
190  {0.262866, -0.951056, 0.162460},
191  {0.500000, -0.809017, 0.309017},
192  {0.716567, -0.681718, 0.147621},
193  {0.525731, -0.850651, 0.000000},
194  {-0.238856, -0.864188, -0.442863},
195  {-0.500000, -0.809017, -0.309017},
196  {-0.262866, -0.951056, -0.162460},
197  {-0.850651, -0.525731, 0.000000},
198  {-0.716567, -0.681718, -0.147621},
199  {-0.716567, -0.681718, 0.147621},
200  {-0.525731, -0.850651, 0.000000},
201  {-0.500000, -0.809017, 0.309017},
202  {-0.238856, -0.864188, 0.442863},
203  {-0.262866, -0.951056, 0.162460},
204  {-0.864188, -0.442863, 0.238856},
205  {-0.809017, -0.309017, 0.500000},
206  {-0.688191, -0.587785, 0.425325},
207  {-0.681718, -0.147621, 0.716567},
208  {-0.442863, -0.238856, 0.864188},
209  {-0.587785, -0.425325, 0.688191},
210  {-0.309017, -0.500000, 0.809017},
211  {-0.147621, -0.716567, 0.681718},
212  {-0.425325, -0.688191, 0.587785},
213  {-0.162460, -0.262866, 0.951056},
214  {0.442863, -0.238856, 0.864188},
215  {0.162460, -0.262866, 0.951056},
216  {0.309017, -0.500000, 0.809017},
217  {0.147621, -0.716567, 0.681718},
218  {0.000000, -0.525731, 0.850651},
219  {0.425325, -0.688191, 0.587785},
220  {0.587785, -0.425325, 0.688191},
221  {0.688191, -0.587785, 0.425325},
222  {-0.955423, 0.295242, 0.000000},
223  {-0.951056, 0.162460, 0.262866},
224  {-1.000000, 0.000000, 0.000000},
225  {-0.850651, 0.000000, 0.525731},
226  {-0.955423, -0.295242, 0.000000},
227  {-0.951056, -0.162460, 0.262866},
228  {-0.864188, 0.442863, -0.238856},
229  {-0.951056, 0.162460, -0.262866},
230  {-0.809017, 0.309017, -0.500000},
231  {-0.864188, -0.442863, -0.238856},
232  {-0.951056, -0.162460, -0.262866},
233  {-0.809017, -0.309017, -0.500000},
234  {-0.681718, 0.147621, -0.716567},
235  {-0.681718, -0.147621, -0.716567},
236  {-0.850651, 0.000000, -0.525731},
237  {-0.688191, 0.587785, -0.425325},
238  {-0.587785, 0.425325, -0.688191},
239  {-0.425325, 0.688191, -0.587785},
240  {-0.425325, -0.688191, -0.587785},
241  {-0.587785, -0.425325, -0.688191},
242  {-0.688191, -0.587785, -0.425325},
243 };
244 
245 int DirToByte(const vec3_t dir)
246 {
247  int i, best;
248  float d, bestd;
249 
250  if (!dir) {
251  return 0;
252  }
253 
254  bestd = 0;
255  best = 0;
256  for (i = 0; i < NUMVERTEXNORMALS; i++) {
257  d = DotProduct(dir, bytedirs[i]);
258  if (d > bestd) {
259  bestd = d;
260  best = i;
261  }
262  }
263 
264  return best;
265 }
266 
267 #if 0
268 void ByteToDir(int index, vec3_t dir)
269 {
270  if (index < 0 || index >= NUMVERTEXNORMALS) {
271  Com_Error(ERR_FATAL, "ByteToDir: illegal index");
272  }
273 
274  VectorCopy(bytedirs[index], dir);
275 }
276 #endif
277 
278 void SetPlaneType(cplane_t *plane)
279 {
280  vec_t *normal = plane->normal;
281 
282  if (normal[0] == 1) {
283  plane->type = PLANE_X;
284  return;
285  }
286  if (normal[1] == 1) {
287  plane->type = PLANE_Y;
288  return;
289  }
290  if (normal[2] == 1) {
291  plane->type = PLANE_Z;
292  return;
293  }
294 
295  plane->type = PLANE_NON_AXIAL;
296 }
297 
298 void SetPlaneSignbits(cplane_t *plane)
299 {
300  int bits = 0;
301 
302  if (plane->normal[0] < 0) {
303  bits |= 1;
304  }
305  if (plane->normal[1] < 0) {
306  bits |= 2;
307  }
308  if (plane->normal[2] < 0) {
309  bits |= 4;
310  }
311 
312  plane->signbits = bits;
313 }
314 
315 /*
316 ==================
317 BoxOnPlaneSide
318 
319 Returns 1, 2, or 1 + 2
320 ==================
321 */
322 int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, cplane_t *p)
323 {
324  vec_t *bounds[2] = { emins, emaxs };
325  int i = p->signbits & 1;
326  int j = (p->signbits >> 1) & 1;
327  int k = (p->signbits >> 2) & 1;
328 
329 #define P(i, j, k) \
330  p->normal[0] * bounds[i][0] + \
331  p->normal[1] * bounds[j][1] + \
332  p->normal[2] * bounds[k][2]
333 
334  vec_t dist1 = P(i ^ 1, j ^ 1, k ^ 1);
335  vec_t dist2 = P(i, j, k);
336  int sides = 0;
337 
338 #undef P
339 
340  if (dist1 >= p->dist)
341  sides = BOX_INFRONT;
342  if (dist2 < p->dist)
343  sides |= BOX_BEHIND;
344 
345  return sides;
346 }
347 
348 void RotatePointAroundVector(vec3_t dst, const vec3_t dir, const vec3_t point, float degrees)
349 {
350  vec3_t matrix[3];
351 
352  SetupRotationMatrix(matrix, dir, degrees);
353 
354  dst[0] = DotProduct(matrix[0], point);
355  dst[1] = DotProduct(matrix[1], point);
356  dst[2] = DotProduct(matrix[2], point);
357 }
358 
359 /*
360 ==================
361 SetupRotationMatrix
362 
363 Setup rotation matrix given the normalized direction vector and angle to rotate
364 around this vector. Adapted from Mesa 3D implementation of _math_matrix_rotate.
365 ==================
366 */
367 void SetupRotationMatrix(vec3_t matrix[3], const vec3_t dir, float degrees)
368 {
369  vec_t angle, s, c, one_c, xx, yy, zz, xy, yz, zx, xs, ys, zs;
370 
371  angle = DEG2RAD(degrees);
372  s = sin(angle);
373  c = cos(angle);
374  one_c = 1.0F - c;
375 
376  xx = dir[0] * dir[0];
377  yy = dir[1] * dir[1];
378  zz = dir[2] * dir[2];
379  xy = dir[0] * dir[1];
380  yz = dir[1] * dir[2];
381  zx = dir[2] * dir[0];
382  xs = dir[0] * s;
383  ys = dir[1] * s;
384  zs = dir[2] * s;
385 
386  matrix[0][0] = (one_c * xx) + c;
387  matrix[0][1] = (one_c * xy) - zs;
388  matrix[0][2] = (one_c * zx) + ys;
389 
390  matrix[1][0] = (one_c * xy) + zs;
391  matrix[1][1] = (one_c * yy) + c;
392  matrix[1][2] = (one_c * yz) - xs;
393 
394  matrix[2][0] = (one_c * zx) - ys;
395  matrix[2][1] = (one_c * yz) + xs;
396  matrix[2][2] = (one_c * zz) + c;
397 }
398 
399 
400 #if USE_REF
401 
402 #if USE_REF == REF_SOFT
403 
404 void ProjectPointOnPlane(vec3_t dst, const vec3_t p, const vec3_t normal)
405 {
406  float d;
407  vec3_t n;
408  float inv_denom;
409 
410  inv_denom = 1.0F / DotProduct(normal, normal);
411 
412  d = DotProduct(normal, p) * inv_denom;
413 
414  n[0] = normal[0] * inv_denom;
415  n[1] = normal[1] * inv_denom;
416  n[2] = normal[2] * inv_denom;
417 
418  dst[0] = p[0] - d * n[0];
419  dst[1] = p[1] - d * n[1];
420  dst[2] = p[2] - d * n[2];
421 }
422 
423 /*
424 ** assumes "src" is normalized
425 */
426 void PerpendicularVector(vec3_t dst, const vec3_t src)
427 {
428  int pos;
429  int i;
430  float minelem = 1.0F;
431  vec3_t tempvec;
432 
433  /*
434  ** find the smallest magnitude axially aligned vector
435  */
436  for (pos = 0, i = 0; i < 3; i++) {
437  if (fabs(src[i]) < minelem) {
438  pos = i;
439  minelem = fabs(src[i]);
440  }
441  }
442  tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
443  tempvec[pos] = 1.0F;
444 
445  /*
446  ** project the point onto the plane defined by src
447  */
448  ProjectPointOnPlane(dst, tempvec, src);
449 
450  /*
451  ** normalize the result
452  */
453  VectorNormalize(dst);
454 }
455 
456 #endif // USE_REF == REF_SOFT
457 
458 #endif // USE_REF
Com_Error
void Com_Error(error_type_t type, const char *fmt,...)
Definition: g_main.c:258
forward
static vec3_t forward
Definition: p_view.c:27
DirToByte
int DirToByte(const vec3_t dir)
Definition: math.c:245
SetupRotationMatrix
void SetupRotationMatrix(vec3_t matrix[3], const vec3_t dir, float degrees)
Definition: math.c:367
P
#define P(i, j, k)
SetPlaneSignbits
void SetPlaneSignbits(cplane_t *plane)
Definition: math.c:298
RotatePointAroundVector
void RotatePointAroundVector(vec3_t dst, const vec3_t dir, const vec3_t point, float degrees)
Definition: math.c:348
c
statCounters_t c
Definition: main.c:30
up
static vec3_t up
Definition: p_view.c:27
right
static vec3_t right
Definition: p_view.c:27
SetPlaneType
void SetPlaneType(cplane_t *plane)
Definition: math.c:278
bytedirs
const vec3_t bytedirs[NUMVERTEXNORMALS]
Definition: math.c:80
VectorNormalize
vec_t VectorNormalize(vec3_t v)
Definition: shared.c:55
BoxOnPlaneSide
int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, cplane_t *p)
Definition: math.c:322