earlyOutPolyList.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "math/mMath.h"
  23. #include "console/console.h"
  24. #include "collision/earlyOutPolyList.h"
  25. //----------------------------------------------------------------------------
  26. EarlyOutPolyList::EarlyOutPolyList()
  27. {
  28. VECTOR_SET_ASSOCIATION(mPolyList);
  29. VECTOR_SET_ASSOCIATION(mVertexList);
  30. VECTOR_SET_ASSOCIATION(mIndexList);
  31. VECTOR_SET_ASSOCIATION(mPolyPlaneList);
  32. VECTOR_SET_ASSOCIATION(mPlaneList);
  33. mNormal.set(0, 0, 0);
  34. mIndexList.reserve(100);
  35. mEarlyOut = false;
  36. }
  37. EarlyOutPolyList::~EarlyOutPolyList()
  38. {
  39. }
  40. //----------------------------------------------------------------------------
  41. void EarlyOutPolyList::clear()
  42. {
  43. // Only clears internal data
  44. mPolyList.clear();
  45. mVertexList.clear();
  46. mIndexList.clear();
  47. mPolyPlaneList.clear();
  48. mEarlyOut = false;
  49. }
  50. bool EarlyOutPolyList::isEmpty() const
  51. {
  52. return mEarlyOut == false;
  53. }
  54. //----------------------------------------------------------------------------
  55. U32 EarlyOutPolyList::addPoint(const Point3F& p)
  56. {
  57. if (mEarlyOut == true)
  58. return 0;
  59. mVertexList.increment();
  60. Vertex& v = mVertexList.last();
  61. v.point.x = p.x * mScale.x;
  62. v.point.y = p.y * mScale.y;
  63. v.point.z = p.z * mScale.z;
  64. mMatrix.mulP(v.point);
  65. // Build the plane mask
  66. v.mask = 0;
  67. for (U32 i = 0; i < mPlaneList.size(); i++)
  68. if (mPlaneList[i].distToPlane(v.point) > 0)
  69. v.mask |= 1 << i;
  70. // If the point is inside all the planes, then we're done!
  71. if (v.mask == 0)
  72. mEarlyOut = true;
  73. return mVertexList.size() - 1;
  74. }
  75. U32 EarlyOutPolyList::addPlane(const PlaneF& plane)
  76. {
  77. mPolyPlaneList.increment();
  78. mPlaneTransformer.transform(plane, mPolyPlaneList.last());
  79. return mPolyPlaneList.size() - 1;
  80. }
  81. //----------------------------------------------------------------------------
  82. void EarlyOutPolyList::begin(BaseMatInstance* material,U32 surfaceKey)
  83. {
  84. if (mEarlyOut == true)
  85. return;
  86. mPolyList.increment();
  87. Poly& poly = mPolyList.last();
  88. poly.object = mCurrObject;
  89. poly.material = material;
  90. poly.vertexStart = mIndexList.size();
  91. poly.surfaceKey = surfaceKey;
  92. }
  93. //----------------------------------------------------------------------------
  94. void EarlyOutPolyList::plane(U32 v1,U32 v2,U32 v3)
  95. {
  96. if (mEarlyOut == true)
  97. return;
  98. mPolyList.last().plane.set(mVertexList[v1].point,
  99. mVertexList[v2].point,mVertexList[v3].point);
  100. }
  101. void EarlyOutPolyList::plane(const PlaneF& p)
  102. {
  103. if (mEarlyOut == true)
  104. return;
  105. mPlaneTransformer.transform(p, mPolyList.last().plane);
  106. }
  107. void EarlyOutPolyList::plane(const U32 index)
  108. {
  109. if (mEarlyOut == true)
  110. return;
  111. AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
  112. mPolyList.last().plane = mPolyPlaneList[index];
  113. }
  114. const PlaneF& EarlyOutPolyList::getIndexedPlane(const U32 index)
  115. {
  116. AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
  117. return mPolyPlaneList[index];
  118. }
  119. //----------------------------------------------------------------------------
  120. void EarlyOutPolyList::vertex(U32 vi)
  121. {
  122. if (mEarlyOut == true)
  123. return;
  124. mIndexList.push_back(vi);
  125. }
  126. //----------------------------------------------------------------------------
  127. void EarlyOutPolyList::end()
  128. {
  129. if (mEarlyOut == true)
  130. return;
  131. Poly& poly = mPolyList.last();
  132. // Anything facing away from the mNormal is rejected
  133. if (mDot(poly.plane,mNormal) > 0) {
  134. mIndexList.setSize(poly.vertexStart);
  135. mPolyList.decrement();
  136. return;
  137. }
  138. // Build intial inside/outside plane masks
  139. U32 indexStart = poly.vertexStart;
  140. U32 vertexCount = mIndexList.size() - indexStart;
  141. U32 frontMask = 0,backMask = 0;
  142. U32 i;
  143. for (i = indexStart; i < mIndexList.size(); i++) {
  144. U32 mask = mVertexList[mIndexList[i]].mask;
  145. frontMask |= mask;
  146. backMask |= ~mask;
  147. }
  148. // Trivial accept if all the vertices are on the backsides of
  149. // all the planes.
  150. if (!frontMask) {
  151. poly.vertexCount = vertexCount;
  152. mEarlyOut = true;
  153. return;
  154. }
  155. // Trivial reject if any plane not crossed has all it's points
  156. // on the front.
  157. U32 crossMask = frontMask & backMask;
  158. if (~crossMask & frontMask) {
  159. mIndexList.setSize(poly.vertexStart);
  160. mPolyList.decrement();
  161. return;
  162. }
  163. // Need to do some clipping
  164. for (U32 p = 0; p < mPlaneList.size(); p++) {
  165. U32 pmask = 1 << p;
  166. // Only test against this plane if we have something
  167. // on both sides
  168. if (crossMask & pmask) {
  169. U32 indexEnd = mIndexList.size();
  170. U32 i1 = indexEnd - 1;
  171. U32 mask1 = mVertexList[mIndexList[i1]].mask;
  172. for (U32 i2 = indexStart; i2 < indexEnd; i2++) {
  173. U32 mask2 = mVertexList[mIndexList[i2]].mask;
  174. if ((mask1 ^ mask2) & pmask) {
  175. //
  176. mVertexList.increment();
  177. VectorF& v1 = mVertexList[mIndexList[i1]].point;
  178. VectorF& v2 = mVertexList[mIndexList[i2]].point;
  179. VectorF vv = v2 - v1;
  180. F32 t = -mPlaneList[p].distToPlane(v1) / mDot(mPlaneList[p],vv);
  181. mIndexList.push_back(mVertexList.size() - 1);
  182. Vertex& iv = mVertexList.last();
  183. iv.point.x = v1.x + vv.x * t;
  184. iv.point.y = v1.y + vv.y * t;
  185. iv.point.z = v1.z + vv.z * t;
  186. iv.mask = 0;
  187. // Test against the remaining planes
  188. for (i = p + 1; i < mPlaneList.size(); i++)
  189. if (mPlaneList[i].distToPlane(iv.point) > 0) {
  190. iv.mask = 1 << i;
  191. break;
  192. }
  193. }
  194. if (!(mask2 & pmask)) {
  195. U32 index = mIndexList[i2];
  196. mIndexList.push_back(index);
  197. }
  198. mask1 = mask2;
  199. i1 = i2;
  200. }
  201. // Check for degenerate
  202. indexStart = indexEnd;
  203. if (mIndexList.size() - indexStart < 3) {
  204. mIndexList.setSize(poly.vertexStart);
  205. mPolyList.decrement();
  206. return;
  207. }
  208. }
  209. }
  210. // If we reach here, then there's a poly!
  211. mEarlyOut = true;
  212. // Emit what's left and compress the index list.
  213. poly.vertexCount = mIndexList.size() - indexStart;
  214. memcpy(&mIndexList[poly.vertexStart],
  215. &mIndexList[indexStart],poly.vertexCount);
  216. mIndexList.setSize(poly.vertexStart + poly.vertexCount);
  217. }
  218. //----------------------------------------------------------------------------
  219. void EarlyOutPolyList::memcpy(U32* dst, U32* src,U32 size)
  220. {
  221. U32* end = src + size;
  222. while (src != end)
  223. *dst++ = *src++;
  224. }