concretePolyList.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. PrimBuild::color3i( 255, 0, 255 );
  120. Poly *p;
  121. Point3F *pnt;
  122. for ( p = mPolyList.begin(); p < mPolyList.end(); p++ )
  123. {
  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. }
  134. }
  135. void ConcretePolyList::triangulate()
  136. {
  137. PROFILE_SCOPE( ConcretePolyList_Triangulate );
  138. // Build into a new polylist and index list.
  139. //
  140. // TODO: There are potential performance issues
  141. // here as we're not reserving enough space for
  142. // new generated triangles.
  143. //
  144. // We need to either over estimate and shrink or
  145. // better yet fix vector to internally grow in
  146. // large chunks.
  147. //
  148. PolyList polyList;
  149. polyList.reserve( mPolyList.size() );
  150. IndexList indexList;
  151. indexList.reserve( mIndexList.size() );
  152. U32 j, numTriangles;
  153. //
  154. PolyList::const_iterator polyIter = mPolyList.begin();
  155. for ( ; polyIter != mPolyList.end(); polyIter++ )
  156. {
  157. const Poly &poly = *polyIter;
  158. // How many triangles in this poly?
  159. numTriangles = poly.vertexCount - 2;
  160. // Build out the triangles.
  161. for ( j = 0; j < numTriangles; j++ )
  162. {
  163. polyList.increment();
  164. Poly &triangle = polyList.last();
  165. triangle = poly;
  166. triangle.vertexCount = 3;
  167. triangle.vertexStart = indexList.size();
  168. indexList.push_back( mIndexList[ poly.vertexStart ] );
  169. indexList.push_back( mIndexList[ poly.vertexStart + 1 + j ] );
  170. indexList.push_back( mIndexList[ poly.vertexStart + 2 + j ] );
  171. }
  172. }
  173. mPolyList = polyList;
  174. mIndexList = indexList;
  175. }