concretePolyList.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "platform/platform.h"
  23. #include "collision/concretePolyList.h"
  24. #include "math/mMath.h"
  25. #include "console/console.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "gfx/primBuilder.h"
  28. #include "gfx/gfxStateBlock.h"
  29. //----------------------------------------------------------------------------
  30. ConcretePolyList::ConcretePolyList()
  31. {
  32. VECTOR_SET_ASSOCIATION(mPolyList);
  33. VECTOR_SET_ASSOCIATION(mVertexList);
  34. VECTOR_SET_ASSOCIATION(mIndexList);
  35. VECTOR_SET_ASSOCIATION(mPolyPlaneList);
  36. mIndexList.reserve(100);
  37. }
  38. ConcretePolyList::~ConcretePolyList()
  39. {
  40. }
  41. //----------------------------------------------------------------------------
  42. void ConcretePolyList::clear()
  43. {
  44. // Only clears internal data
  45. mPolyList.clear();
  46. mVertexList.clear();
  47. mIndexList.clear();
  48. mPolyPlaneList.clear();
  49. }
  50. //----------------------------------------------------------------------------
  51. U32 ConcretePolyList::addPoint(const Point3F& p)
  52. {
  53. mVertexList.increment();
  54. Point3F& v = mVertexList.last();
  55. v.x = p.x * mScale.x;
  56. v.y = p.y * mScale.y;
  57. v.z = p.z * mScale.z;
  58. mMatrix.mulP(v);
  59. return mVertexList.size() - 1;
  60. }
  61. U32 ConcretePolyList::addPlane(const PlaneF& plane)
  62. {
  63. mPolyPlaneList.increment();
  64. mPlaneTransformer.transform(plane, mPolyPlaneList.last());
  65. return mPolyPlaneList.size() - 1;
  66. }
  67. //----------------------------------------------------------------------------
  68. void ConcretePolyList::begin(BaseMatInstance* material,U32 surfaceKey)
  69. {
  70. mPolyList.increment();
  71. Poly& poly = mPolyList.last();
  72. poly.object = mCurrObject;
  73. poly.material = material;
  74. poly.vertexStart = mIndexList.size();
  75. poly.surfaceKey = surfaceKey;
  76. }
  77. //----------------------------------------------------------------------------
  78. void ConcretePolyList::plane(U32 v1,U32 v2,U32 v3)
  79. {
  80. mPolyList.last().plane.set(mVertexList[v1],
  81. mVertexList[v2],mVertexList[v3]);
  82. }
  83. void ConcretePolyList::plane(const PlaneF& p)
  84. {
  85. mPlaneTransformer.transform(p, mPolyList.last().plane);
  86. }
  87. void ConcretePolyList::plane(const U32 index)
  88. {
  89. AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
  90. mPolyList.last().plane = mPolyPlaneList[index];
  91. }
  92. const PlaneF& ConcretePolyList::getIndexedPlane(const U32 index)
  93. {
  94. AssertFatal(index < mPolyPlaneList.size(), "Out of bounds index!");
  95. return mPolyPlaneList[index];
  96. }
  97. //----------------------------------------------------------------------------
  98. void ConcretePolyList::vertex(U32 vi)
  99. {
  100. mIndexList.push_back(vi);
  101. }
  102. //----------------------------------------------------------------------------
  103. bool ConcretePolyList::isEmpty() const
  104. {
  105. return mPolyList.empty();
  106. }
  107. void ConcretePolyList::end()
  108. {
  109. Poly& poly = mPolyList.last();
  110. poly.vertexCount = mIndexList.size() - poly.vertexStart;
  111. }
  112. void ConcretePolyList::render()
  113. {
  114. GFXStateBlockDesc solidZDisable;
  115. solidZDisable.setCullMode( GFXCullNone );
  116. solidZDisable.setZReadWrite( false, false );
  117. GFXStateBlockRef sb = GFX->createStateBlock( solidZDisable );
  118. GFX->setStateBlock( sb );
  119. Poly *p;
  120. Point3F *pnt;
  121. for ( p = mPolyList.begin(); p < mPolyList.end(); p++ )
  122. {
  123. PrimBuild::color3i(255, 0, 255);
  124. PrimBuild::begin( GFXLineStrip, p->vertexCount + 1 );
  125. for ( U32 i = 0; i < p->vertexCount; i++ )
  126. {
  127. pnt = &mVertexList[mIndexList[p->vertexStart + i]];
  128. PrimBuild::vertex3fv( pnt );
  129. }
  130. pnt = &mVertexList[mIndexList[p->vertexStart]];
  131. PrimBuild::vertex3fv( pnt );
  132. PrimBuild::end();
  133. // Calculate the center of the polygon
  134. Point3F centroid(0, 0, 0);
  135. for (U32 i = 0; i < p->vertexCount; i++)
  136. {
  137. pnt = &mVertexList[mIndexList[p->vertexStart + i]];
  138. centroid += *pnt;
  139. }
  140. centroid /= p->vertexCount;
  141. // Calculate the end point of the normal line
  142. Point3F norm = p->plane.getNormal();
  143. U8 red = static_cast<U8>((norm.x + 1.0f) * 0.5f * 255);
  144. U8 green = static_cast<U8>((norm.y + 1.0f) * 0.5f * 255);
  145. U8 blue = static_cast<U8>((norm.z + 1.0f) * 0.5f * 255);
  146. PrimBuild::color3i(red, green, blue);
  147. Point3F normalEnd = centroid + norm;
  148. // Draw the normal line
  149. PrimBuild::begin(GFXLineList, 2);
  150. PrimBuild::vertex3fv(centroid);
  151. PrimBuild::vertex3fv(normalEnd);
  152. PrimBuild::end();
  153. }
  154. }
  155. void ConcretePolyList::triangulate()
  156. {
  157. PROFILE_SCOPE( ConcretePolyList_Triangulate );
  158. // Build into a new polylist and index list.
  159. //
  160. // TODO: There are potential performance issues
  161. // here as we're not reserving enough space for
  162. // new generated triangles.
  163. //
  164. // We need to either over estimate and shrink or
  165. // better yet fix vector to internally grow in
  166. // large chunks.
  167. //
  168. PolyList polyList;
  169. polyList.reserve( mPolyList.size() );
  170. IndexList indexList;
  171. indexList.reserve( mIndexList.size() );
  172. U32 j, numTriangles;
  173. //
  174. PolyList::const_iterator polyIter = mPolyList.begin();
  175. for ( ; polyIter != mPolyList.end(); polyIter++ )
  176. {
  177. const Poly &poly = *polyIter;
  178. // How many triangles in this poly?
  179. numTriangles = poly.vertexCount - 2;
  180. // Build out the triangles.
  181. for ( j = 0; j < numTriangles; j++ )
  182. {
  183. polyList.increment();
  184. Poly &triangle = polyList.last();
  185. triangle = poly;
  186. triangle.vertexCount = 3;
  187. triangle.vertexStart = indexList.size();
  188. indexList.push_back( mIndexList[ poly.vertexStart ] );
  189. indexList.push_back( mIndexList[ poly.vertexStart + 1 + j ] );
  190. indexList.push_back( mIndexList[ poly.vertexStart + 2 + j ] );
  191. }
  192. }
  193. mPolyList = polyList;
  194. mIndexList = indexList;
  195. }