PageRenderTime 30ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/code/game/q_math.c

https://github.com/tario/fixedquake
C | 1287 lines | 936 code | 173 blank | 178 comment | 73 complexity | 1fa363a99ed67e62343af49287b6c02a MD5 | raw file
  1. // Copyright (C) 1999-2000 Id Software, Inc.
  2. //
  3. // q_math.c -- stateless support routines that are included in each code module
  4. #include "q_shared.h"
  5. vec3_t vec3_origin = {0,0,0};
  6. vec3_t axisDefault[3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
  7. vec4_t colorBlack = {0, 0, 0, 1};
  8. vec4_t colorRed = {1, 0, 0, 1};
  9. vec4_t colorGreen = {0, 1, 0, 1};
  10. vec4_t colorBlue = {0, 0, 1, 1};
  11. vec4_t colorYellow = {1, 1, 0, 1};
  12. vec4_t colorMagenta= {1, 0, 1, 1};
  13. vec4_t colorCyan = {0, 1, 1, 1};
  14. vec4_t colorWhite = {1, 1, 1, 1};
  15. vec4_t colorLtGrey = {0.75, 0.75, 0.75, 1};
  16. vec4_t colorMdGrey = {0.5, 0.5, 0.5, 1};
  17. vec4_t colorDkGrey = {0.25, 0.25, 0.25, 1};
  18. vec4_t g_color_table[8] =
  19. {
  20. {0.0, 0.0, 0.0, 1.0},
  21. {1.0, 0.0, 0.0, 1.0},
  22. {0.0, 1.0, 0.0, 1.0},
  23. {1.0, 1.0, 0.0, 1.0},
  24. {0.0, 0.0, 1.0, 1.0},
  25. {0.0, 1.0, 1.0, 1.0},
  26. {1.0, 0.0, 1.0, 1.0},
  27. {1.0, 1.0, 1.0, 1.0},
  28. };
  29. vec3_t bytedirs[NUMVERTEXNORMALS] =
  30. {
  31. {-0.525731f, 0.000000f, 0.850651f}, {-0.442863f, 0.238856f, 0.864188f},
  32. {-0.295242f, 0.000000f, 0.955423f}, {-0.309017f, 0.500000f, 0.809017f},
  33. {-0.162460f, 0.262866f, 0.951056f}, {0.000000f, 0.000000f, 1.000000f},
  34. {0.000000f, 0.850651f, 0.525731f}, {-0.147621f, 0.716567f, 0.681718f},
  35. {0.147621f, 0.716567f, 0.681718f}, {0.000000f, 0.525731f, 0.850651f},
  36. {0.309017f, 0.500000f, 0.809017f}, {0.525731f, 0.000000f, 0.850651f},
  37. {0.295242f, 0.000000f, 0.955423f}, {0.442863f, 0.238856f, 0.864188f},
  38. {0.162460f, 0.262866f, 0.951056f}, {-0.681718f, 0.147621f, 0.716567f},
  39. {-0.809017f, 0.309017f, 0.500000f},{-0.587785f, 0.425325f, 0.688191f},
  40. {-0.850651f, 0.525731f, 0.000000f},{-0.864188f, 0.442863f, 0.238856f},
  41. {-0.716567f, 0.681718f, 0.147621f},{-0.688191f, 0.587785f, 0.425325f},
  42. {-0.500000f, 0.809017f, 0.309017f}, {-0.238856f, 0.864188f, 0.442863f},
  43. {-0.425325f, 0.688191f, 0.587785f}, {-0.716567f, 0.681718f, -0.147621f},
  44. {-0.500000f, 0.809017f, -0.309017f}, {-0.525731f, 0.850651f, 0.000000f},
  45. {0.000000f, 0.850651f, -0.525731f}, {-0.238856f, 0.864188f, -0.442863f},
  46. {0.000000f, 0.955423f, -0.295242f}, {-0.262866f, 0.951056f, -0.162460f},
  47. {0.000000f, 1.000000f, 0.000000f}, {0.000000f, 0.955423f, 0.295242f},
  48. {-0.262866f, 0.951056f, 0.162460f}, {0.238856f, 0.864188f, 0.442863f},
  49. {0.262866f, 0.951056f, 0.162460f}, {0.500000f, 0.809017f, 0.309017f},
  50. {0.238856f, 0.864188f, -0.442863f},{0.262866f, 0.951056f, -0.162460f},
  51. {0.500000f, 0.809017f, -0.309017f},{0.850651f, 0.525731f, 0.000000f},
  52. {0.716567f, 0.681718f, 0.147621f}, {0.716567f, 0.681718f, -0.147621f},
  53. {0.525731f, 0.850651f, 0.000000f}, {0.425325f, 0.688191f, 0.587785f},
  54. {0.864188f, 0.442863f, 0.238856f}, {0.688191f, 0.587785f, 0.425325f},
  55. {0.809017f, 0.309017f, 0.500000f}, {0.681718f, 0.147621f, 0.716567f},
  56. {0.587785f, 0.425325f, 0.688191f}, {0.955423f, 0.295242f, 0.000000f},
  57. {1.000000f, 0.000000f, 0.000000f}, {0.951056f, 0.162460f, 0.262866f},
  58. {0.850651f, -0.525731f, 0.000000f},{0.955423f, -0.295242f, 0.000000f},
  59. {0.864188f, -0.442863f, 0.238856f}, {0.951056f, -0.162460f, 0.262866f},
  60. {0.809017f, -0.309017f, 0.500000f}, {0.681718f, -0.147621f, 0.716567f},
  61. {0.850651f, 0.000000f, 0.525731f}, {0.864188f, 0.442863f, -0.238856f},
  62. {0.809017f, 0.309017f, -0.500000f}, {0.951056f, 0.162460f, -0.262866f},
  63. {0.525731f, 0.000000f, -0.850651f}, {0.681718f, 0.147621f, -0.716567f},
  64. {0.681718f, -0.147621f, -0.716567f},{0.850651f, 0.000000f, -0.525731f},
  65. {0.809017f, -0.309017f, -0.500000f}, {0.864188f, -0.442863f, -0.238856f},
  66. {0.951056f, -0.162460f, -0.262866f}, {0.147621f, 0.716567f, -0.681718f},
  67. {0.309017f, 0.500000f, -0.809017f}, {0.425325f, 0.688191f, -0.587785f},
  68. {0.442863f, 0.238856f, -0.864188f}, {0.587785f, 0.425325f, -0.688191f},
  69. {0.688191f, 0.587785f, -0.425325f}, {-0.147621f, 0.716567f, -0.681718f},
  70. {-0.309017f, 0.500000f, -0.809017f}, {0.000000f, 0.525731f, -0.850651f},
  71. {-0.525731f, 0.000000f, -0.850651f}, {-0.442863f, 0.238856f, -0.864188f},
  72. {-0.295242f, 0.000000f, -0.955423f}, {-0.162460f, 0.262866f, -0.951056f},
  73. {0.000000f, 0.000000f, -1.000000f}, {0.295242f, 0.000000f, -0.955423f},
  74. {0.162460f, 0.262866f, -0.951056f}, {-0.442863f, -0.238856f, -0.864188f},
  75. {-0.309017f, -0.500000f, -0.809017f}, {-0.162460f, -0.262866f, -0.951056f},
  76. {0.000000f, -0.850651f, -0.525731f}, {-0.147621f, -0.716567f, -0.681718f},
  77. {0.147621f, -0.716567f, -0.681718f}, {0.000000f, -0.525731f, -0.850651f},
  78. {0.309017f, -0.500000f, -0.809017f}, {0.442863f, -0.238856f, -0.864188f},
  79. {0.162460f, -0.262866f, -0.951056f}, {0.238856f, -0.864188f, -0.442863f},
  80. {0.500000f, -0.809017f, -0.309017f}, {0.425325f, -0.688191f, -0.587785f},
  81. {0.716567f, -0.681718f, -0.147621f}, {0.688191f, -0.587785f, -0.425325f},
  82. {0.587785f, -0.425325f, -0.688191f}, {0.000000f, -0.955423f, -0.295242f},
  83. {0.000000f, -1.000000f, 0.000000f}, {0.262866f, -0.951056f, -0.162460f},
  84. {0.000000f, -0.850651f, 0.525731f}, {0.000000f, -0.955423f, 0.295242f},
  85. {0.238856f, -0.864188f, 0.442863f}, {0.262866f, -0.951056f, 0.162460f},
  86. {0.500000f, -0.809017f, 0.309017f}, {0.716567f, -0.681718f, 0.147621f},
  87. {0.525731f, -0.850651f, 0.000000f}, {-0.238856f, -0.864188f, -0.442863f},
  88. {-0.500000f, -0.809017f, -0.309017f}, {-0.262866f, -0.951056f, -0.162460f},
  89. {-0.850651f, -0.525731f, 0.000000f}, {-0.716567f, -0.681718f, -0.147621f},
  90. {-0.716567f, -0.681718f, 0.147621f}, {-0.525731f, -0.850651f, 0.000000f},
  91. {-0.500000f, -0.809017f, 0.309017f}, {-0.238856f, -0.864188f, 0.442863f},
  92. {-0.262866f, -0.951056f, 0.162460f}, {-0.864188f, -0.442863f, 0.238856f},
  93. {-0.809017f, -0.309017f, 0.500000f}, {-0.688191f, -0.587785f, 0.425325f},
  94. {-0.681718f, -0.147621f, 0.716567f}, {-0.442863f, -0.238856f, 0.864188f},
  95. {-0.587785f, -0.425325f, 0.688191f}, {-0.309017f, -0.500000f, 0.809017f},
  96. {-0.147621f, -0.716567f, 0.681718f}, {-0.425325f, -0.688191f, 0.587785f},
  97. {-0.162460f, -0.262866f, 0.951056f}, {0.442863f, -0.238856f, 0.864188f},
  98. {0.162460f, -0.262866f, 0.951056f}, {0.309017f, -0.500000f, 0.809017f},
  99. {0.147621f, -0.716567f, 0.681718f}, {0.000000f, -0.525731f, 0.850651f},
  100. {0.425325f, -0.688191f, 0.587785f}, {0.587785f, -0.425325f, 0.688191f},
  101. {0.688191f, -0.587785f, 0.425325f}, {-0.955423f, 0.295242f, 0.000000f},
  102. {-0.951056f, 0.162460f, 0.262866f}, {-1.000000f, 0.000000f, 0.000000f},
  103. {-0.850651f, 0.000000f, 0.525731f}, {-0.955423f, -0.295242f, 0.000000f},
  104. {-0.951056f, -0.162460f, 0.262866f}, {-0.864188f, 0.442863f, -0.238856f},
  105. {-0.951056f, 0.162460f, -0.262866f}, {-0.809017f, 0.309017f, -0.500000f},
  106. {-0.864188f, -0.442863f, -0.238856f}, {-0.951056f, -0.162460f, -0.262866f},
  107. {-0.809017f, -0.309017f, -0.500000f}, {-0.681718f, 0.147621f, -0.716567f},
  108. {-0.681718f, -0.147621f, -0.716567f}, {-0.850651f, 0.000000f, -0.525731f},
  109. {-0.688191f, 0.587785f, -0.425325f}, {-0.587785f, 0.425325f, -0.688191f},
  110. {-0.425325f, 0.688191f, -0.587785f}, {-0.425325f, -0.688191f, -0.587785f},
  111. {-0.587785f, -0.425325f, -0.688191f}, {-0.688191f, -0.587785f, -0.425325f}
  112. };
  113. //==============================================================
  114. int Q_rand( int *seed ) {
  115. *seed = (69069 * *seed + 1);
  116. return *seed;
  117. }
  118. float Q_random( int *seed ) {
  119. return ( Q_rand( seed ) & 0xffff ) / (float)0x10000;
  120. }
  121. float Q_crandom( int *seed ) {
  122. return 2.0 * ( Q_random( seed ) - 0.5 );
  123. }
  124. #ifdef __LCC__
  125. int VectorCompare( const vec3_t v1, const vec3_t v2 ) {
  126. if (v1[0] != v2[0] || v1[1] != v2[1] || v1[2] != v2[2]) {
  127. return 0;
  128. }
  129. return 1;
  130. }
  131. vec_t VectorLength( const vec3_t v ) {
  132. return (vec_t)sqrt (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  133. }
  134. vec_t VectorLengthSquared( const vec3_t v ) {
  135. return (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  136. }
  137. vec_t Distance( const vec3_t p1, const vec3_t p2 ) {
  138. vec3_t v;
  139. VectorSubtract (p2, p1, v);
  140. return VectorLength( v );
  141. }
  142. vec_t DistanceSquared( const vec3_t p1, const vec3_t p2 ) {
  143. vec3_t v;
  144. VectorSubtract (p2, p1, v);
  145. return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  146. }
  147. // fast vector normalize routine that does not check to make sure
  148. // that length != 0, nor does it return length, uses rsqrt approximation
  149. void VectorNormalizeFast( vec3_t v )
  150. {
  151. float ilength;
  152. ilength = Q_rsqrt( DotProduct( v, v ) );
  153. v[0] *= ilength;
  154. v[1] *= ilength;
  155. v[2] *= ilength;
  156. }
  157. void VectorInverse( vec3_t v ){
  158. v[0] = -v[0];
  159. v[1] = -v[1];
  160. v[2] = -v[2];
  161. }
  162. void CrossProduct( const vec3_t v1, const vec3_t v2, vec3_t cross ) {
  163. cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  164. cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  165. cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  166. }
  167. #endif
  168. //=======================================================
  169. signed char ClampChar( int i ) {
  170. if ( i < -128 ) {
  171. return -128;
  172. }
  173. if ( i > 127 ) {
  174. return 127;
  175. }
  176. return i;
  177. }
  178. signed short ClampShort( int i ) {
  179. if ( i < -32768 ) {
  180. return -32768;
  181. }
  182. if ( i > 0x7fff ) {
  183. return 0x7fff;
  184. }
  185. return i;
  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. if ( !dir ) {
  192. return 0;
  193. }
  194. bestd = 0;
  195. best = 0;
  196. for (i=0 ; i<NUMVERTEXNORMALS ; i++)
  197. {
  198. d = DotProduct (dir, bytedirs[i]);
  199. if (d > bestd)
  200. {
  201. bestd = d;
  202. best = i;
  203. }
  204. }
  205. return best;
  206. }
  207. void ByteToDir( int b, vec3_t dir ) {
  208. if ( b < 0 || b >= NUMVERTEXNORMALS ) {
  209. VectorCopy( vec3_origin, dir );
  210. return;
  211. }
  212. VectorCopy (bytedirs[b], dir);
  213. }
  214. unsigned ColorBytes3 (float r, float g, float b) {
  215. unsigned i;
  216. ( (byte *)&i )[0] = r * 255;
  217. ( (byte *)&i )[1] = g * 255;
  218. ( (byte *)&i )[2] = b * 255;
  219. return i;
  220. }
  221. unsigned ColorBytes4 (float r, float g, float b, float a) {
  222. unsigned i;
  223. ( (byte *)&i )[0] = r * 255;
  224. ( (byte *)&i )[1] = g * 255;
  225. ( (byte *)&i )[2] = b * 255;
  226. ( (byte *)&i )[3] = a * 255;
  227. return i;
  228. }
  229. float NormalizeColor( const vec3_t in, vec3_t out ) {
  230. float max;
  231. max = in[0];
  232. if ( in[1] > max ) {
  233. max = in[1];
  234. }
  235. if ( in[2] > max ) {
  236. max = in[2];
  237. }
  238. if ( !max ) {
  239. VectorClear( out );
  240. } else {
  241. out[0] = in[0] / max;
  242. out[1] = in[1] / max;
  243. out[2] = in[2] / max;
  244. }
  245. return max;
  246. }
  247. /*
  248. =====================
  249. PlaneFromPoints
  250. Returns false if the triangle is degenrate.
  251. The normal will point out of the clock for clockwise ordered points
  252. =====================
  253. */
  254. qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
  255. vec3_t d1, d2;
  256. VectorSubtract( b, a, d1 );
  257. VectorSubtract( c, a, d2 );
  258. CrossProduct( d2, d1, plane );
  259. if ( VectorNormalize( plane ) == 0 ) {
  260. return qfalse;
  261. }
  262. plane[3] = DotProduct( a, plane );
  263. return qtrue;
  264. }
  265. /*
  266. ===============
  267. RotatePointAroundVector
  268. This is not implemented very well...
  269. ===============
  270. */
  271. void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
  272. float degrees ) {
  273. float m[3][3];
  274. float im[3][3];
  275. float zrot[3][3];
  276. float tmpmat[3][3];
  277. float rot[3][3];
  278. int i;
  279. vec3_t vr, vup, vf;
  280. float rad;
  281. vf[0] = dir[0];
  282. vf[1] = dir[1];
  283. vf[2] = dir[2];
  284. PerpendicularVector( vr, dir );
  285. CrossProduct( vr, vf, vup );
  286. m[0][0] = vr[0];
  287. m[1][0] = vr[1];
  288. m[2][0] = vr[2];
  289. m[0][1] = vup[0];
  290. m[1][1] = vup[1];
  291. m[2][1] = vup[2];
  292. m[0][2] = vf[0];
  293. m[1][2] = vf[1];
  294. m[2][2] = vf[2];
  295. memcpy( im, m, sizeof( im ) );
  296. im[0][1] = m[1][0];
  297. im[0][2] = m[2][0];
  298. im[1][0] = m[0][1];
  299. im[1][2] = m[2][1];
  300. im[2][0] = m[0][2];
  301. im[2][1] = m[1][2];
  302. memset( zrot, 0, sizeof( zrot ) );
  303. zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
  304. rad = DEG2RAD( degrees );
  305. zrot[0][0] = cos( rad );
  306. zrot[0][1] = sin( rad );
  307. zrot[1][0] = -sin( rad );
  308. zrot[1][1] = cos( rad );
  309. MatrixMultiply( m, zrot, tmpmat );
  310. MatrixMultiply( tmpmat, im, rot );
  311. for ( i = 0; i < 3; i++ ) {
  312. dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
  313. }
  314. }
  315. /*
  316. ===============
  317. RotateAroundDirection
  318. ===============
  319. */
  320. void RotateAroundDirection( vec3_t axis[3], float yaw ) {
  321. // create an arbitrary axis[1]
  322. PerpendicularVector( axis[1], axis[0] );
  323. // rotate it around axis[0] by yaw
  324. if ( yaw ) {
  325. vec3_t temp;
  326. VectorCopy( axis[1], temp );
  327. RotatePointAroundVector( axis[1], axis[0], temp, yaw );
  328. }
  329. // cross to get axis[2]
  330. CrossProduct( axis[0], axis[1], axis[2] );
  331. }
  332. void vectoangles( const vec3_t value1, vec3_t angles ) {
  333. float forward;
  334. float yaw, pitch;
  335. if ( value1[1] == 0 && value1[0] == 0 ) {
  336. yaw = 0;
  337. if ( value1[2] > 0 ) {
  338. pitch = 90;
  339. }
  340. else {
  341. pitch = 270;
  342. }
  343. }
  344. else {
  345. if ( value1[0] ) {
  346. yaw = ( atan2 ( value1[1], value1[0] ) * 180 / M_PI );
  347. }
  348. else if ( value1[1] > 0 ) {
  349. yaw = 90;
  350. }
  351. else {
  352. yaw = 270;
  353. }
  354. if ( yaw < 0 ) {
  355. yaw += 360;
  356. }
  357. forward = sqrt ( value1[0]*value1[0] + value1[1]*value1[1] );
  358. pitch = ( atan2(value1[2], forward) * 180 / M_PI );
  359. if ( pitch < 0 ) {
  360. pitch += 360;
  361. }
  362. }
  363. angles[PITCH] = -pitch;
  364. angles[YAW] = yaw;
  365. angles[ROLL] = 0;
  366. }
  367. /*
  368. =================
  369. AnglesToAxis
  370. =================
  371. */
  372. void AnglesToAxis( const vec3_t angles, vec3_t axis[3] ) {
  373. vec3_t right;
  374. // angle vectors returns "right" instead of "y axis"
  375. AngleVectors( angles, axis[0], right, axis[2] );
  376. VectorSubtract( vec3_origin, right, axis[1] );
  377. }
  378. void AxisClear( vec3_t axis[3] ) {
  379. axis[0][0] = 1;
  380. axis[0][1] = 0;
  381. axis[0][2] = 0;
  382. axis[1][0] = 0;
  383. axis[1][1] = 1;
  384. axis[1][2] = 0;
  385. axis[2][0] = 0;
  386. axis[2][1] = 0;
  387. axis[2][2] = 1;
  388. }
  389. void AxisCopy( vec3_t in[3], vec3_t out[3] ) {
  390. VectorCopy( in[0], out[0] );
  391. VectorCopy( in[1], out[1] );
  392. VectorCopy( in[2], out[2] );
  393. }
  394. void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
  395. {
  396. float d;
  397. vec3_t n;
  398. float inv_denom;
  399. inv_denom = DotProduct( normal, normal );
  400. #ifndef Q3_VM
  401. assert( Q_fabs(inv_denom) != 0.0f ); // bk010122 - zero vectors get here
  402. #endif
  403. inv_denom = 1.0f / inv_denom;
  404. d = DotProduct( normal, p ) * inv_denom;
  405. n[0] = normal[0] * inv_denom;
  406. n[1] = normal[1] * inv_denom;
  407. n[2] = normal[2] * inv_denom;
  408. dst[0] = p[0] - d * n[0];
  409. dst[1] = p[1] - d * n[1];
  410. dst[2] = p[2] - d * n[2];
  411. }
  412. /*
  413. ================
  414. MakeNormalVectors
  415. Given a normalized forward vector, create two
  416. other perpendicular vectors
  417. ================
  418. */
  419. void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up) {
  420. float d;
  421. // this rotate and negate guarantees a vector
  422. // not colinear with the original
  423. right[1] = -forward[0];
  424. right[2] = forward[1];
  425. right[0] = forward[2];
  426. d = DotProduct (right, forward);
  427. VectorMA (right, -d, forward, right);
  428. VectorNormalize (right);
  429. CrossProduct (right, forward, up);
  430. }
  431. void VectorRotate( vec3_t in, vec3_t matrix[3], vec3_t out )
  432. {
  433. out[0] = DotProduct( in, matrix[0] );
  434. out[1] = DotProduct( in, matrix[1] );
  435. out[2] = DotProduct( in, matrix[2] );
  436. }
  437. //============================================================================
  438. #if !idppc
  439. /*
  440. ** float q_rsqrt( float number )
  441. */
  442. float Q_rsqrt( float number )
  443. {
  444. long i;
  445. float x2, y;
  446. const float threehalfs = 1.5F;
  447. x2 = number * 0.5F;
  448. y = number;
  449. i = * ( long * ) &y; // evil floating point bit level hacking
  450. i = 0x5f3759df - ( i >> 1 ); // what the fuck?
  451. y = * ( float * ) &i;
  452. y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  453. // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
  454. #ifndef Q3_VM
  455. #ifdef __linux__
  456. assert( !isnan(y) ); // bk010122 - FPE?
  457. #endif
  458. #endif
  459. return y;
  460. }
  461. float Q_fabs( float f ) {
  462. int tmp = * ( int * ) &f;
  463. tmp &= 0x7FFFFFFF;
  464. return * ( float * ) &tmp;
  465. }
  466. #endif
  467. //============================================================
  468. /*
  469. ===============
  470. LerpAngle
  471. ===============
  472. */
  473. float LerpAngle (float from, float to, float frac) {
  474. float a;
  475. if ( to - from > 180 ) {
  476. to -= 360;
  477. }
  478. if ( to - from < -180 ) {
  479. to += 360;
  480. }
  481. a = from + frac * (to - from);
  482. return a;
  483. }
  484. /*
  485. =================
  486. AngleSubtract
  487. Always returns a value from -180 to 180
  488. =================
  489. */
  490. float AngleSubtract( float a1, float a2 ) {
  491. float a;
  492. a = a1 - a2;
  493. while ( a > 180 ) {
  494. a -= 360;
  495. }
  496. while ( a < -180 ) {
  497. a += 360;
  498. }
  499. return a;
  500. }
  501. void AnglesSubtract( vec3_t v1, vec3_t v2, vec3_t v3 ) {
  502. v3[0] = AngleSubtract( v1[0], v2[0] );
  503. v3[1] = AngleSubtract( v1[1], v2[1] );
  504. v3[2] = AngleSubtract( v1[2], v2[2] );
  505. }
  506. float AngleMod(float a) {
  507. a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
  508. return a;
  509. }
  510. /*
  511. =================
  512. AngleNormalize360
  513. returns angle normalized to the range [0 <= angle < 360]
  514. =================
  515. */
  516. float AngleNormalize360 ( float angle ) {
  517. return (360.0 / 65536) * ((int)(angle * (65536 / 360.0)) & 65535);
  518. }
  519. /*
  520. =================
  521. AngleNormalize180
  522. returns angle normalized to the range [-180 < angle <= 180]
  523. =================
  524. */
  525. float AngleNormalize180 ( float angle ) {
  526. angle = AngleNormalize360( angle );
  527. if ( angle > 180.0 ) {
  528. angle -= 360.0;
  529. }
  530. return angle;
  531. }
  532. /*
  533. =================
  534. AngleDelta
  535. returns the normalized delta from angle1 to angle2
  536. =================
  537. */
  538. float AngleDelta ( float angle1, float angle2 ) {
  539. return AngleNormalize180( angle1 - angle2 );
  540. }
  541. //============================================================
  542. /*
  543. =================
  544. SetPlaneSignbits
  545. =================
  546. */
  547. void SetPlaneSignbits (cplane_t *out) {
  548. int bits, j;
  549. // for fast box on planeside test
  550. bits = 0;
  551. for (j=0 ; j<3 ; j++) {
  552. if (out->normal[j] < 0) {
  553. bits |= 1<<j;
  554. }
  555. }
  556. out->signbits = bits;
  557. }
  558. /*
  559. ==================
  560. BoxOnPlaneSide
  561. Returns 1, 2, or 1 + 2
  562. // this is the slow, general version
  563. int BoxOnPlaneSide2 (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  564. {
  565. int i;
  566. float dist1, dist2;
  567. int sides;
  568. vec3_t corners[2];
  569. for (i=0 ; i<3 ; i++)
  570. {
  571. if (p->normal[i] < 0)
  572. {
  573. corners[0][i] = emins[i];
  574. corners[1][i] = emaxs[i];
  575. }
  576. else
  577. {
  578. corners[1][i] = emins[i];
  579. corners[0][i] = emaxs[i];
  580. }
  581. }
  582. dist1 = DotProduct (p->normal, corners[0]) - p->dist;
  583. dist2 = DotProduct (p->normal, corners[1]) - p->dist;
  584. sides = 0;
  585. if (dist1 >= 0)
  586. sides = 1;
  587. if (dist2 < 0)
  588. sides |= 2;
  589. return sides;
  590. }
  591. ==================
  592. */
  593. #if !( (defined __linux__ || __FreeBSD__) && (defined __i386__) && (!defined C_ONLY)) // rb010123
  594. #if defined __LCC__ || defined C_ONLY || !id386 || defined __VECTORC
  595. int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  596. {
  597. float dist1, dist2;
  598. int sides;
  599. // fast axial cases
  600. if (p->type < 3)
  601. {
  602. if (p->dist <= emins[p->type])
  603. return 1;
  604. if (p->dist >= emaxs[p->type])
  605. return 2;
  606. return 3;
  607. }
  608. // general case
  609. switch (p->signbits)
  610. {
  611. case 0:
  612. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  613. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  614. break;
  615. case 1:
  616. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  617. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  618. break;
  619. case 2:
  620. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  621. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  622. break;
  623. case 3:
  624. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  625. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  626. break;
  627. case 4:
  628. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  629. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  630. break;
  631. case 5:
  632. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  633. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  634. break;
  635. case 6:
  636. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  637. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  638. break;
  639. case 7:
  640. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  641. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  642. break;
  643. default:
  644. dist1 = dist2 = 0; // shut up compiler
  645. break;
  646. }
  647. sides = 0;
  648. if (dist1 >= p->dist)
  649. sides = 1;
  650. if (dist2 < p->dist)
  651. sides |= 2;
  652. return sides;
  653. }
  654. #else
  655. #pragma warning( disable: 4035 )
  656. __declspec( naked ) int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  657. {
  658. static int bops_initialized;
  659. static int Ljmptab[8];
  660. __asm {
  661. push ebx
  662. cmp bops_initialized, 1
  663. je initialized
  664. mov bops_initialized, 1
  665. mov Ljmptab[0*4], offset Lcase0
  666. mov Ljmptab[1*4], offset Lcase1
  667. mov Ljmptab[2*4], offset Lcase2
  668. mov Ljmptab[3*4], offset Lcase3
  669. mov Ljmptab[4*4], offset Lcase4
  670. mov Ljmptab[5*4], offset Lcase5
  671. mov Ljmptab[6*4], offset Lcase6
  672. mov Ljmptab[7*4], offset Lcase7
  673. initialized:
  674. mov edx,dword ptr[4+12+esp]
  675. mov ecx,dword ptr[4+4+esp]
  676. xor eax,eax
  677. mov ebx,dword ptr[4+8+esp]
  678. mov al,byte ptr[17+edx]
  679. cmp al,8
  680. jge Lerror
  681. fld dword ptr[0+edx]
  682. fld st(0)
  683. jmp dword ptr[Ljmptab+eax*4]
  684. Lcase0:
  685. fmul dword ptr[ebx]
  686. fld dword ptr[0+4+edx]
  687. fxch st(2)
  688. fmul dword ptr[ecx]
  689. fxch st(2)
  690. fld st(0)
  691. fmul dword ptr[4+ebx]
  692. fld dword ptr[0+8+edx]
  693. fxch st(2)
  694. fmul dword ptr[4+ecx]
  695. fxch st(2)
  696. fld st(0)
  697. fmul dword ptr[8+ebx]
  698. fxch st(5)
  699. faddp st(3),st(0)
  700. fmul dword ptr[8+ecx]
  701. fxch st(1)
  702. faddp st(3),st(0)
  703. fxch st(3)
  704. faddp st(2),st(0)
  705. jmp LSetSides
  706. Lcase1:
  707. fmul dword ptr[ecx]
  708. fld dword ptr[0+4+edx]
  709. fxch st(2)
  710. fmul dword ptr[ebx]
  711. fxch st(2)
  712. fld st(0)
  713. fmul dword ptr[4+ebx]
  714. fld dword ptr[0+8+edx]
  715. fxch st(2)
  716. fmul dword ptr[4+ecx]
  717. fxch st(2)
  718. fld st(0)
  719. fmul dword ptr[8+ebx]
  720. fxch st(5)
  721. faddp st(3),st(0)
  722. fmul dword ptr[8+ecx]
  723. fxch st(1)
  724. faddp st(3),st(0)
  725. fxch st(3)
  726. faddp st(2),st(0)
  727. jmp LSetSides
  728. Lcase2:
  729. fmul dword ptr[ebx]
  730. fld dword ptr[0+4+edx]
  731. fxch st(2)
  732. fmul dword ptr[ecx]
  733. fxch st(2)
  734. fld st(0)
  735. fmul dword ptr[4+ecx]
  736. fld dword ptr[0+8+edx]
  737. fxch st(2)
  738. fmul dword ptr[4+ebx]
  739. fxch st(2)
  740. fld st(0)
  741. fmul dword ptr[8+ebx]
  742. fxch st(5)
  743. faddp st(3),st(0)
  744. fmul dword ptr[8+ecx]
  745. fxch st(1)
  746. faddp st(3),st(0)
  747. fxch st(3)
  748. faddp st(2),st(0)
  749. jmp LSetSides
  750. Lcase3:
  751. fmul dword ptr[ecx]
  752. fld dword ptr[0+4+edx]
  753. fxch st(2)
  754. fmul dword ptr[ebx]
  755. fxch st(2)
  756. fld st(0)
  757. fmul dword ptr[4+ecx]
  758. fld dword ptr[0+8+edx]
  759. fxch st(2)
  760. fmul dword ptr[4+ebx]
  761. fxch st(2)
  762. fld st(0)
  763. fmul dword ptr[8+ebx]
  764. fxch st(5)
  765. faddp st(3),st(0)
  766. fmul dword ptr[8+ecx]
  767. fxch st(1)
  768. faddp st(3),st(0)
  769. fxch st(3)
  770. faddp st(2),st(0)
  771. jmp LSetSides
  772. Lcase4:
  773. fmul dword ptr[ebx]
  774. fld dword ptr[0+4+edx]
  775. fxch st(2)
  776. fmul dword ptr[ecx]
  777. fxch st(2)
  778. fld st(0)
  779. fmul dword ptr[4+ebx]
  780. fld dword ptr[0+8+edx]
  781. fxch st(2)
  782. fmul dword ptr[4+ecx]
  783. fxch st(2)
  784. fld st(0)
  785. fmul dword ptr[8+ecx]
  786. fxch st(5)
  787. faddp st(3),st(0)
  788. fmul dword ptr[8+ebx]
  789. fxch st(1)
  790. faddp st(3),st(0)
  791. fxch st(3)
  792. faddp st(2),st(0)
  793. jmp LSetSides
  794. Lcase5:
  795. fmul dword ptr[ecx]
  796. fld dword ptr[0+4+edx]
  797. fxch st(2)
  798. fmul dword ptr[ebx]
  799. fxch st(2)
  800. fld st(0)
  801. fmul dword ptr[4+ebx]
  802. fld dword ptr[0+8+edx]
  803. fxch st(2)
  804. fmul dword ptr[4+ecx]
  805. fxch st(2)
  806. fld st(0)
  807. fmul dword ptr[8+ecx]
  808. fxch st(5)
  809. faddp st(3),st(0)
  810. fmul dword ptr[8+ebx]
  811. fxch st(1)
  812. faddp st(3),st(0)
  813. fxch st(3)
  814. faddp st(2),st(0)
  815. jmp LSetSides
  816. Lcase6:
  817. fmul dword ptr[ebx]
  818. fld dword ptr[0+4+edx]
  819. fxch st(2)
  820. fmul dword ptr[ecx]
  821. fxch st(2)
  822. fld st(0)
  823. fmul dword ptr[4+ecx]
  824. fld dword ptr[0+8+edx]
  825. fxch st(2)
  826. fmul dword ptr[4+ebx]
  827. fxch st(2)
  828. fld st(0)
  829. fmul dword ptr[8+ecx]
  830. fxch st(5)
  831. faddp st(3),st(0)
  832. fmul dword ptr[8+ebx]
  833. fxch st(1)
  834. faddp st(3),st(0)
  835. fxch st(3)
  836. faddp st(2),st(0)
  837. jmp LSetSides
  838. Lcase7:
  839. fmul dword ptr[ecx]
  840. fld dword ptr[0+4+edx]
  841. fxch st(2)
  842. fmul dword ptr[ebx]
  843. fxch st(2)
  844. fld st(0)
  845. fmul dword ptr[4+ecx]
  846. fld dword ptr[0+8+edx]
  847. fxch st(2)
  848. fmul dword ptr[4+ebx]
  849. fxch st(2)
  850. fld st(0)
  851. fmul dword ptr[8+ecx]
  852. fxch st(5)
  853. faddp st(3),st(0)
  854. fmul dword ptr[8+ebx]
  855. fxch st(1)
  856. faddp st(3),st(0)
  857. fxch st(3)
  858. faddp st(2),st(0)
  859. LSetSides:
  860. faddp st(2),st(0)
  861. fcomp dword ptr[12+edx]
  862. xor ecx,ecx
  863. fnstsw ax
  864. fcomp dword ptr[12+edx]
  865. and ah,1
  866. xor ah,1
  867. add cl,ah
  868. fnstsw ax
  869. and ah,1
  870. add ah,ah
  871. add cl,ah
  872. pop ebx
  873. mov eax,ecx
  874. ret
  875. Lerror:
  876. int 3
  877. }
  878. }
  879. #pragma warning( default: 4035 )
  880. #endif
  881. #endif
  882. /*
  883. =================
  884. RadiusFromBounds
  885. =================
  886. */
  887. float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) {
  888. int i;
  889. vec3_t corner;
  890. float a, b;
  891. for (i=0 ; i<3 ; i++) {
  892. a = fabs( mins[i] );
  893. b = fabs( maxs[i] );
  894. corner[i] = a > b ? a : b;
  895. }
  896. return VectorLength (corner);
  897. }
  898. void ClearBounds( vec3_t mins, vec3_t maxs ) {
  899. mins[0] = mins[1] = mins[2] = 99999;
  900. maxs[0] = maxs[1] = maxs[2] = -99999;
  901. }
  902. void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs ) {
  903. if ( v[0] < mins[0] ) {
  904. mins[0] = v[0];
  905. }
  906. if ( v[0] > maxs[0]) {
  907. maxs[0] = v[0];
  908. }
  909. if ( v[1] < mins[1] ) {
  910. mins[1] = v[1];
  911. }
  912. if ( v[1] > maxs[1]) {
  913. maxs[1] = v[1];
  914. }
  915. if ( v[2] < mins[2] ) {
  916. mins[2] = v[2];
  917. }
  918. if ( v[2] > maxs[2]) {
  919. maxs[2] = v[2];
  920. }
  921. }
  922. vec_t VectorNormalize( vec3_t v ) {
  923. float length, ilength;
  924. length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  925. length = sqrt (length);
  926. if ( length ) {
  927. ilength = 1/length;
  928. v[0] *= ilength;
  929. v[1] *= ilength;
  930. v[2] *= ilength;
  931. }
  932. return length;
  933. }
  934. vec_t VectorNormalize2( const vec3_t v, vec3_t out) {
  935. float length, ilength;
  936. length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  937. length = sqrt (length);
  938. if (length)
  939. {
  940. #ifndef Q3_VM // bk0101022 - FPE related
  941. // assert( ((Q_fabs(v[0])!=0.0f) || (Q_fabs(v[1])!=0.0f) || (Q_fabs(v[2])!=0.0f)) );
  942. #endif
  943. ilength = 1/length;
  944. out[0] = v[0]*ilength;
  945. out[1] = v[1]*ilength;
  946. out[2] = v[2]*ilength;
  947. } else {
  948. #ifndef Q3_VM // bk0101022 - FPE related
  949. // assert( ((Q_fabs(v[0])==0.0f) && (Q_fabs(v[1])==0.0f) && (Q_fabs(v[2])==0.0f)) );
  950. #endif
  951. VectorClear( out );
  952. }
  953. return length;
  954. }
  955. void _VectorMA( const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc) {
  956. vecc[0] = veca[0] + scale*vecb[0];
  957. vecc[1] = veca[1] + scale*vecb[1];
  958. vecc[2] = veca[2] + scale*vecb[2];
  959. }
  960. vec_t _DotProduct( const vec3_t v1, const vec3_t v2 ) {
  961. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  962. }
  963. void _VectorSubtract( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
  964. out[0] = veca[0]-vecb[0];
  965. out[1] = veca[1]-vecb[1];
  966. out[2] = veca[2]-vecb[2];
  967. }
  968. void _VectorAdd( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
  969. out[0] = veca[0]+vecb[0];
  970. out[1] = veca[1]+vecb[1];
  971. out[2] = veca[2]+vecb[2];
  972. }
  973. void _VectorCopy( const vec3_t in, vec3_t out ) {
  974. out[0] = in[0];
  975. out[1] = in[1];
  976. out[2] = in[2];
  977. }
  978. void _VectorScale( const vec3_t in, vec_t scale, vec3_t out ) {
  979. out[0] = in[0]*scale;
  980. out[1] = in[1]*scale;
  981. out[2] = in[2]*scale;
  982. }
  983. void Vector4Scale( const vec4_t in, vec_t scale, vec4_t out ) {
  984. out[0] = in[0]*scale;
  985. out[1] = in[1]*scale;
  986. out[2] = in[2]*scale;
  987. out[3] = in[3]*scale;
  988. }
  989. int Q_log2( int val ) {
  990. int answer;
  991. answer = 0;
  992. while ( ( val>>=1 ) != 0 ) {
  993. answer++;
  994. }
  995. return answer;
  996. }
  997. /*
  998. =================
  999. PlaneTypeForNormal
  1000. =================
  1001. */
  1002. /*
  1003. int PlaneTypeForNormal (vec3_t normal) {
  1004. if ( normal[0] == 1.0 )
  1005. return PLANE_X;
  1006. if ( normal[1] == 1.0 )
  1007. return PLANE_Y;
  1008. if ( normal[2] == 1.0 )
  1009. return PLANE_Z;
  1010. return PLANE_NON_AXIAL;
  1011. }
  1012. */
  1013. /*
  1014. ================
  1015. MatrixMultiply
  1016. ================
  1017. */
  1018. void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
  1019. out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  1020. in1[0][2] * in2[2][0];
  1021. out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  1022. in1[0][2] * in2[2][1];
  1023. out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  1024. in1[0][2] * in2[2][2];
  1025. out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  1026. in1[1][2] * in2[2][0];
  1027. out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  1028. in1[1][2] * in2[2][1];
  1029. out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  1030. in1[1][2] * in2[2][2];
  1031. out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  1032. in1[2][2] * in2[2][0];
  1033. out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  1034. in1[2][2] * in2[2][1];
  1035. out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  1036. in1[2][2] * in2[2][2];
  1037. }
  1038. void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) {
  1039. float angle;
  1040. static float sr, sp, sy, cr, cp, cy;
  1041. // static to help MS compiler fp bugs
  1042. angle = angles[YAW] * (M_PI*2 / 360);
  1043. sy = sin(angle);
  1044. cy = cos(angle);
  1045. angle = angles[PITCH] * (M_PI*2 / 360);
  1046. sp = sin(angle);
  1047. cp = cos(angle);
  1048. angle = angles[ROLL] * (M_PI*2 / 360);
  1049. sr = sin(angle);
  1050. cr = cos(angle);
  1051. if (forward)
  1052. {
  1053. forward[0] = cp*cy;
  1054. forward[1] = cp*sy;
  1055. forward[2] = -sp;
  1056. }
  1057. if (right)
  1058. {
  1059. right[0] = (-1*sr*sp*cy+-1*cr*-sy);
  1060. right[1] = (-1*sr*sp*sy+-1*cr*cy);
  1061. right[2] = -1*sr*cp;
  1062. }
  1063. if (up)
  1064. {
  1065. up[0] = (cr*sp*cy+-sr*-sy);
  1066. up[1] = (cr*sp*sy+-sr*cy);
  1067. up[2] = cr*cp;
  1068. }
  1069. }
  1070. /*
  1071. ** assumes "src" is normalized
  1072. */
  1073. void PerpendicularVector( vec3_t dst, const vec3_t src )
  1074. {
  1075. int pos;
  1076. int i;
  1077. float minelem = 1.0F;
  1078. vec3_t tempvec;
  1079. /*
  1080. ** find the smallest magnitude axially aligned vector
  1081. */
  1082. for ( pos = 0, i = 0; i < 3; i++ )
  1083. {
  1084. if ( fabs( src[i] ) < minelem )
  1085. {
  1086. pos = i;
  1087. minelem = fabs( src[i] );
  1088. }
  1089. }
  1090. tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
  1091. tempvec[pos] = 1.0F;
  1092. /*
  1093. ** project the point onto the plane defined by src
  1094. */
  1095. ProjectPointOnPlane( dst, tempvec, src );
  1096. /*
  1097. ** normalize the result
  1098. */
  1099. VectorNormalize( dst );
  1100. }