quadTreeTracer.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "util/quadTreeTracer.h"
  23. #include "platform/profiler.h"
  24. #include "core/frameAllocator.h"
  25. static F32 calcInterceptV(F32 vStart, F32 invDeltaV, F32 intercept)
  26. {
  27. return (intercept - vStart) * invDeltaV;
  28. }
  29. static F32 calcInterceptNone(F32, F32, F32)
  30. {
  31. return F32_MAX;
  32. }
  33. static F32 (*calcInterceptX)(F32, F32, F32);
  34. static F32 (*calcInterceptY)(F32, F32, F32);
  35. bool QuadTreeTracer::castRay(const Point3F &start, const Point3F &end, RayInfo *info)
  36. {
  37. PROFILE_START(QuadTreeTracer_castRay);
  38. // Do some precalculations we'll use for the rest of this routine.
  39. // Set up our intercept calculation methods.
  40. F32 invDeltaX;
  41. if(end.x == start.x)
  42. {
  43. calcInterceptX = calcInterceptNone;
  44. invDeltaX = 0;
  45. }
  46. else
  47. {
  48. invDeltaX = 1.f / (end.x - start.x);
  49. calcInterceptX = calcInterceptV;
  50. }
  51. F32 invDeltaY;
  52. if(end.y == start.y)
  53. {
  54. calcInterceptY = calcInterceptNone;
  55. invDeltaY = 0;
  56. }
  57. else
  58. {
  59. invDeltaY = 1.f / (end.y - start.y);
  60. calcInterceptY = calcInterceptV;
  61. }
  62. // Subdivide our space based on the size of the lowest level of the tree...
  63. const F32 invSize = 1.f / F32(BIT(mTreeDepth-1));
  64. // Grab this off the frame allocator, we don't want to do a proper alloc
  65. // on every ray!
  66. FrameAllocatorMarker stackAlloc;
  67. RayStackNode *stack = (RayStackNode*)stackAlloc.alloc(sizeof(RayStackNode) * (mTreeDepth * 3 + 1));
  68. U32 stackSize = 1;
  69. // Kick off the stack with the root node.
  70. stack[0].startT = 0;
  71. stack[0].endT = 1;
  72. stack[0].squarePos.set(0,0);
  73. stack[0].level = mTreeDepth - 1;
  74. //Con::printf("QuadTreeTracer::castRay(%x)", this);
  75. // Aright, now let's do some raycasting!
  76. while(stackSize--)
  77. {
  78. // Get the current node for easy access...
  79. RayStackNode *sn = stack + stackSize;
  80. const U32 level = sn->level;
  81. const F32 startT = sn->startT;
  82. const F32 endT = sn->endT;
  83. const Point2I squarePos = sn->squarePos;
  84. AssertFatal((startT >= 0.f) && (startT <= 1.f), "QuadTreeTracer::castRay - out of range startT on stack!");
  85. AssertFatal((endT >= 0.f) && (endT <= 1.f), "QuadTreeTracer::castRay - out of range endT on stack!");
  86. //Con::printf(" -- node(%d, %d @ %d), sT=%f, eT=%f", squarePos.x, squarePos.y, level, startT, endT);
  87. // Figure our start and end Z.
  88. const F32 startZ = startT * (end.z - start.z) + start.z;
  89. const F32 endZ = endT * (end.z - start.z) + start.z;
  90. // Ok, now let's see if we hit the lower bound
  91. const F32 squareMin = getSquareMin(level, squarePos);
  92. if(startZ < squareMin && endZ < squareMin)
  93. continue; //Nope, skip out.
  94. // Hmm, let's check the upper bound.
  95. const F32 squareMax = getSquareMax(level, squarePos);
  96. if(startZ > squareMax && endZ > squareMax)
  97. continue; //Nope, skip out.
  98. // We might be intersecting something... If we've hit
  99. // the tree depth let's deal with the leaf intersection.
  100. if(level == 0)
  101. {
  102. //Con::printf(" ++ check node(%d, %d @ %d), sT=%f, eT=%f", squarePos.x, squarePos.y, level, startT, endT);
  103. if(castLeafRay(squarePos, start, end, startT, endT, info))
  104. {
  105. PROFILE_END();
  106. return true; // We hit, tell 'em so!
  107. }
  108. continue; // Otherwise, keep looking.
  109. }
  110. else
  111. {
  112. // Ok, we have to push our children as we're an inner node.
  113. // First, figure out some widths...
  114. U32 subSqSize = BIT(level - 1);
  115. // Now, calculate intercepts so we know how to deal with this
  116. // situation... (intercept = position, int = t value for that pos)
  117. const F32 xIntercept = (squarePos.x + subSqSize) * invSize;
  118. F32 xInt = calcInterceptX(start.x, invDeltaX, xIntercept);
  119. const F32 yIntercept = (squarePos.y + subSqSize) * invSize;
  120. F32 yInt = calcInterceptY(start.y, invDeltaY, yIntercept);
  121. // Our starting position for this subray...
  122. const F32 startX = startT * (end.x - start.x) + start.x;
  123. const F32 startY = startT * (end.y - start.y) + start.y;
  124. // Deal with squares that might be "behind" the ray.
  125. if(xInt < startT) xInt = F32_MAX;
  126. if(yInt < startT) yInt = F32_MAX;
  127. // Do a little magic to calculate our next checks...
  128. const U32 x0 = (startX > xIntercept) * subSqSize;
  129. const U32 y0 = (startY > yIntercept) * subSqSize;
  130. const U32 x1 = subSqSize - x0;
  131. const U32 y1 = subSqSize - y0;
  132. const U32 nextLevel = level - 1;
  133. // Ok, now let's figure out what nodes, in what order, need to go
  134. // on the stack. We push things on in reverse order of processing.
  135. if(xInt > endT && yInt > endT)
  136. {
  137. stack[stackSize].squarePos.set(squarePos.x + x0, squarePos.y + y0);
  138. stack[stackSize].level = nextLevel;
  139. stackSize++;
  140. }
  141. else if(xInt < yInt)
  142. {
  143. F32 nextIntersect = endT;
  144. if(yInt <= endT)
  145. {
  146. stack[stackSize].squarePos.set(squarePos.x + x1, squarePos.y + y1);
  147. stack[stackSize].startT = yInt;
  148. stack[stackSize].endT = endT;
  149. stack[stackSize].level = nextLevel;
  150. nextIntersect = yInt;
  151. stackSize++;
  152. }
  153. // Do middle two, order doesn't matter.
  154. stack[stackSize].squarePos.set(squarePos.x + x1, squarePos.y + y0);
  155. stack[stackSize].startT = xInt;
  156. stack[stackSize].endT = nextIntersect;
  157. stack[stackSize].level = nextLevel;
  158. stack[stackSize+1].squarePos.set(squarePos.x + x0, squarePos.y + y0);
  159. stack[stackSize+1].startT = startT;
  160. stack[stackSize+1].endT = xInt;
  161. stack[stackSize+1].level = nextLevel;
  162. stackSize += 2;
  163. }
  164. else if(yInt < xInt)
  165. {
  166. F32 nextIntersect = endT;
  167. if(xInt <= endT)
  168. {
  169. stack[stackSize].squarePos.set(squarePos.x + x1, squarePos.y + y1);
  170. stack[stackSize].startT = xInt;
  171. stack[stackSize].endT = endT;
  172. stack[stackSize].level = nextLevel;
  173. nextIntersect = xInt;
  174. stackSize++;
  175. }
  176. stack[stackSize].squarePos.set(squarePos.x + x0, squarePos.y + y1);
  177. stack[stackSize].startT = yInt;
  178. stack[stackSize].endT = nextIntersect;
  179. stack[stackSize].level = nextLevel;
  180. stack[stackSize+1].squarePos.set(squarePos.x + x0, squarePos.y + y0);
  181. stack[stackSize+1].startT = startT;
  182. stack[stackSize+1].endT = yInt;
  183. stack[stackSize+1].level = nextLevel;
  184. stackSize += 2;
  185. }
  186. else
  187. {
  188. stack[stackSize].squarePos.set(squarePos.x + x1, squarePos.y + y1);
  189. stack[stackSize].startT = xInt;
  190. stack[stackSize].endT = endT;
  191. stack[stackSize].level = nextLevel;
  192. stack[stackSize+1].squarePos.set(squarePos.x + x0, squarePos.y + y0);
  193. stack[stackSize+1].startT = startT;
  194. stack[stackSize+1].endT = xInt;
  195. stack[stackSize+1].level = nextLevel;
  196. stackSize += 2;
  197. }
  198. }
  199. }
  200. // Nothing found, so give up.
  201. PROFILE_END();
  202. return false;
  203. }