Advertisement
Guest User

Untitled

a guest
Feb 19th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. Index: libs/mathlib.h
  2. ===================================================================
  3. --- libs/mathlib.h (revision 491)
  4. +++ libs/mathlib.h (working copy)
  5. @@ -93,7 +93,13 @@
  6. // I need this define in order to test some of the regression tests from time to time.
  7. // This define affect the precision of VectorNormalize() function only.
  8. #define MATHLIB_VECTOR_NORMALIZE_PRECISION_FIX 1
  9. -vec_t VectorNormalize (const vec3_t in, vec3_t out);
  10. +vec_t VectorAccurateNormalize (const vec3_t in, vec3_t out);
  11. +vec_t VectorFastNormalize (const vec3_t in, vec3_t out);
  12. +#if MATHLIB_VECTOR_NORMALIZE_PRECISION_FIX
  13. +#define VectorNormalize VectorAccurateNormalize
  14. +#else
  15. +#define VectorNormalize VectorFastNormalize
  16. +#endif
  17. vec_t ColorNormalize( const vec3_t in, vec3_t out );
  18. void VectorInverse (vec3_t v);
  19. void VectorPolar(vec3_t v, float radius, float theta, float phi);
  20. Index: libs/mathlib/mathlib.c
  21. ===================================================================
  22. --- libs/mathlib/mathlib.c (revision 491)
  23. +++ libs/mathlib/mathlib.c (working copy)
  24. @@ -174,10 +174,8 @@
  25. out[2] = in[2];
  26. }
  27.  
  28. -vec_t VectorNormalize( const vec3_t in, vec3_t out ) {
  29. +vec_t VectorAccurateNormalize( const vec3_t in, vec3_t out ) {
  30.  
  31. -#if MATHLIB_VECTOR_NORMALIZE_PRECISION_FIX
  32. -
  33. // The sqrt() function takes double as an input and returns double as an
  34. // output according the the man pages on Debian and on FreeBSD. Therefore,
  35. // I don't see a reason why using a double outright (instead of using the
  36. @@ -201,27 +199,30 @@
  37. out[2] = (vec_t) (z / length);
  38.  
  39. return (vec_t) length;
  40. +}
  41.  
  42. -#else
  43. +vec_t VectorFastNormalize( const vec3_t in, vec3_t out ) {
  44.  
  45. - vec_t length, ilength;
  46. + // SmileTheory: This is ioquake3's VectorNormalize2
  47. + // for when accuracy matters less than speed
  48. + float length, ilength;
  49.  
  50. - length = (vec_t)sqrt (in[0]*in[0] + in[1]*in[1] + in[2]*in[2]);
  51. - if (length == 0)
  52. - {
  53. - VectorClear (out);
  54. - return 0;
  55. - }
  56. + length = in[0]*in[0] + in[1]*in[1] + in[2]*in[2];
  57.  
  58. - ilength = 1.0f/length;
  59. - out[0] = in[0]*ilength;
  60. - out[1] = in[1]*ilength;
  61. - out[2] = in[2]*ilength;
  62. + if (length)
  63. + {
  64. + /* writing it this way allows gcc to recognize that rsqrt can be used */
  65. + ilength = 1/(float)sqrt (length);
  66. + /* sqrt(length) = length * (1 / sqrt(length)) */
  67. + length *= ilength;
  68. + out[0] = in[0]*ilength;
  69. + out[1] = in[1]*ilength;
  70. + out[2] = in[2]*ilength;
  71. + } else {
  72. + VectorClear( out );
  73. + }
  74.  
  75. - return length;
  76. -
  77. -#endif
  78. -
  79. + return length;
  80. }
  81.  
  82. vec_t ColorNormalize( const vec3_t in, vec3_t out ) {
  83. Index: tools/quake3/q3map2/light.c
  84. ===================================================================
  85. --- tools/quake3/q3map2/light.c (revision 491)
  86. +++ tools/quake3/q3map2/light.c (working copy)
  87. @@ -680,7 +680,7 @@
  88. for( i = 0; i < w->numpoints; i++ )
  89. {
  90. VectorSubtract( w->p[ i ], point, dirs[ i ] );
  91. - VectorNormalize( dirs[ i ], dirs[ i ] );
  92. + VectorFastNormalize( dirs[ i ], dirs[ i ] );
  93. }
  94.  
  95. /* duplicate first vertex to avoid mod operation */
  96. @@ -704,7 +704,7 @@
  97. angle = acos( dot );
  98.  
  99. CrossProduct( dirs[ i ], dirs[ j ], triVector );
  100. - if( VectorNormalize( triVector, triNormal ) < 0.0001f )
  101. + if( VectorFastNormalize( triVector, triNormal ) < 0.0001f )
  102. continue;
  103.  
  104. facing = DotProduct( normal, triNormal );
  105. Index: tools/quake3/q3map2/light_trace.c
  106. ===================================================================
  107. --- tools/quake3/q3map2/light_trace.c (revision 491)
  108. +++ tools/quake3/q3map2/light_trace.c (working copy)
  109. @@ -1581,102 +1581,134 @@
  110. /*
  111. TraceLine_r()
  112. returns qtrue if something is hit and tracing can stop
  113. +
  114. +SmileTheory: made half-iterative
  115. */
  116.  
  117. -static qboolean TraceLine_r( int nodeNum, vec3_t origin, vec3_t end, trace_t *trace )
  118. +#define TRACELINE_R_HALF_ITERATIVE 1
  119. +
  120. +#if TRACELINE_R_HALF_ITERATIVE
  121. +static qboolean TraceLine_r( int nodeNum, const vec3_t start, const vec3_t end, trace_t *trace )
  122. +#else
  123. +static qboolean TraceLine_r( int nodeNum, const vec3_t origin, const vec3_t end, trace_t *trace )
  124. +#endif
  125. {
  126. traceNode_t *node;
  127. int side;
  128. float front, back, frac;
  129. - vec3_t mid;
  130. + vec3_t origin, mid;
  131. qboolean r;
  132. +
  133. +#if TRACELINE_R_HALF_ITERATIVE
  134. + VectorCopy( start, origin );
  135.  
  136. -
  137. - /* bogus node number means solid, end tracing unless testing all */
  138. - if( nodeNum < 0 )
  139. + while( 1 )
  140. +#endif
  141. {
  142. - VectorCopy( origin, trace->hit );
  143. - trace->passSolid = qtrue;
  144. - return qtrue;
  145. - }
  146. -
  147. - /* get node */
  148. - node = &traceNodes[ nodeNum ];
  149. -
  150. - /* solid? */
  151. - if( node->type == TRACE_LEAF_SOLID )
  152. - {
  153. - VectorCopy( origin, trace->hit );
  154. - trace->passSolid = qtrue;
  155. - return qtrue;
  156. - }
  157. -
  158. - /* leafnode? */
  159. - if( node->type < 0 )
  160. - {
  161. - /* note leaf and return */
  162. - if( node->numItems > 0 && trace->numTestNodes < MAX_TRACE_TEST_NODES )
  163. - trace->testNodes[ trace->numTestNodes++ ] = nodeNum;
  164. - return qfalse;
  165. - }
  166. -
  167. - /* ydnar 2003-09-07: don't test branches of the bsp with nothing in them when testall is enabled */
  168. - if( trace->testAll && node->numItems == 0 )
  169. - return qfalse;
  170. -
  171. - /* classify beginning and end points */
  172. - switch( node->type )
  173. - {
  174. - case PLANE_X:
  175. - front = origin[ 0 ] - node->plane[ 3 ];
  176. - back = end[ 0 ] - node->plane[ 3 ];
  177. - break;
  178. + /* bogus node number means solid, end tracing unless testing all */
  179. + if( nodeNum < 0 )
  180. + {
  181. + VectorCopy( origin, trace->hit );
  182. + trace->passSolid = qtrue;
  183. + return qtrue;
  184. + }
  185.  
  186. - case PLANE_Y:
  187. - front = origin[ 1 ] - node->plane[ 3 ];
  188. - back = end[ 1 ] - node->plane[ 3 ];
  189. - break;
  190. + /* get node */
  191. + node = &traceNodes[ nodeNum ];
  192.  
  193. - case PLANE_Z:
  194. - front = origin[ 2 ] - node->plane[ 3 ];
  195. - back = end[ 2 ] - node->plane[ 3 ];
  196. - break;
  197. + /* solid? */
  198. + if( node->type == TRACE_LEAF_SOLID )
  199. + {
  200. + VectorCopy( origin, trace->hit );
  201. + trace->passSolid = qtrue;
  202. + return qtrue;
  203. + }
  204.  
  205. - default:
  206. - front = DotProduct( origin, node->plane ) - node->plane[ 3 ];
  207. - back = DotProduct( end, node->plane ) - node->plane[ 3 ];
  208. - break;
  209. + /* leafnode? */
  210. + if( node->type < 0 )
  211. + {
  212. + /* note leaf and return */
  213. + if( node->numItems > 0 && trace->numTestNodes < MAX_TRACE_TEST_NODES )
  214. + trace->testNodes[ trace->numTestNodes++ ] = nodeNum;
  215. + return qfalse;
  216. + }
  217. +
  218. + /* ydnar 2003-09-07: don't test branches of the bsp with nothing in them when testall is enabled */
  219. + if( trace->testAll && node->numItems == 0 )
  220. + return qfalse;
  221. +
  222. + /* classify beginning and end points */
  223. + switch( node->type )
  224. + {
  225. + case PLANE_X:
  226. + front = origin[ 0 ] - node->plane[ 3 ];
  227. + back = end[ 0 ] - node->plane[ 3 ];
  228. + break;
  229. +
  230. + case PLANE_Y:
  231. + front = origin[ 1 ] - node->plane[ 3 ];
  232. + back = end[ 1 ] - node->plane[ 3 ];
  233. + break;
  234. +
  235. + case PLANE_Z:
  236. + front = origin[ 2 ] - node->plane[ 3 ];
  237. + back = end[ 2 ] - node->plane[ 3 ];
  238. + break;
  239. +
  240. + default:
  241. + front = DotProduct( origin, node->plane ) - node->plane[ 3 ];
  242. + back = DotProduct( end, node->plane ) - node->plane[ 3 ];
  243. + break;
  244. + }
  245. +
  246. + /* entirely in front side? */
  247. + if( front >= -TRACE_ON_EPSILON && back >= -TRACE_ON_EPSILON )
  248. + {
  249. +#if TRACELINE_R_HALF_ITERATIVE
  250. + nodeNum = node->children[ 0 ];
  251. + continue;
  252. +#else
  253. + return TraceLine_r( node->children[ 0 ], origin, end, trace );
  254. +#endif
  255. + }
  256. +
  257. + /* entirely on back side? */
  258. + if( front < TRACE_ON_EPSILON && back < TRACE_ON_EPSILON )
  259. + {
  260. +#if TRACELINE_R_HALF_ITERATIVE
  261. + nodeNum = node->children[ 1 ];
  262. + continue;
  263. +#else
  264. + return TraceLine_r( node->children[ 1 ], origin, end, trace );
  265. +#endif
  266. + }
  267. +
  268. + /* select side */
  269. + side = front < 0;
  270. +
  271. + /* calculate intercept point */
  272. + frac = front / (front - back);
  273. + mid[ 0 ] = origin[ 0 ] + (end[ 0 ] - origin[ 0 ]) * frac;
  274. + mid[ 1 ] = origin[ 1 ] + (end[ 1 ] - origin[ 1 ]) * frac;
  275. + mid[ 2 ] = origin[ 2 ] + (end[ 2 ] - origin[ 2 ]) * frac;
  276. +
  277. + /* fixme: check inhibit radius, then solid nodes and ignore */
  278. +
  279. + /* set trace hit here */
  280. + //% VectorCopy( mid, trace->hit );
  281. +
  282. + /* trace first side */
  283. + if( TraceLine_r( node->children[ side ], origin, mid, trace ) )
  284. + return qtrue;
  285. +
  286. + /* trace other side */
  287. +#if TRACELINE_R_HALF_ITERATIVE
  288. + nodeNum = node->children[ !side ];
  289. + VectorCopy( mid, origin );
  290. +#else
  291. + return TraceLine_r( node->children[ !side ], mid, end, trace );
  292. +#endif
  293. }
  294. -
  295. - /* entirely in front side? */
  296. - if( front >= -TRACE_ON_EPSILON && back >= -TRACE_ON_EPSILON )
  297. - return TraceLine_r( node->children[ 0 ], origin, end, trace );
  298. -
  299. - /* entirely on back side? */
  300. - if( front < TRACE_ON_EPSILON && back < TRACE_ON_EPSILON )
  301. - return TraceLine_r( node->children[ 1 ], origin, end, trace );
  302. -
  303. - /* select side */
  304. - side = front < 0;
  305. -
  306. - /* calculate intercept point */
  307. - frac = front / (front - back);
  308. - mid[ 0 ] = origin[ 0 ] + (end[ 0 ] - origin[ 0 ]) * frac;
  309. - mid[ 1 ] = origin[ 1 ] + (end[ 1 ] - origin[ 1 ]) * frac;
  310. - mid[ 2 ] = origin[ 2 ] + (end[ 2 ] - origin[ 2 ]) * frac;
  311. -
  312. - /* fixme: check inhibit radius, then solid nodes and ignore */
  313. -
  314. - /* set trace hit here */
  315. - //% VectorCopy( mid, trace->hit );
  316. -
  317. - /* trace first side */
  318. - r = TraceLine_r( node->children[ side ], origin, mid, trace );
  319. - if( r )
  320. - return r;
  321. -
  322. - /* trace other side */
  323. - return TraceLine_r( node->children[ !side ], mid, end, trace );
  324. }
  325.  
  326.  
  327. @@ -1754,7 +1786,7 @@
  328. float SetupTrace( trace_t *trace )
  329. {
  330. VectorSubtract( trace->end, trace->origin, trace->displacement );
  331. - trace->distance = VectorNormalize( trace->displacement, trace->direction );
  332. + trace->distance = VectorFastNormalize( trace->displacement, trace->direction );
  333. VectorCopy( trace->origin, trace->hit );
  334. return trace->distance;
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement