recastPolyList.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "recastPolyList.h"
  23. #include "platform/platform.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "gfx/primBuilder.h"
  26. #include "gfx/gfxStateBlock.h"
  27. RecastPolyList::RecastPolyList() : mChunkyMesh(0)
  28. {
  29. nverts = 0;
  30. verts = NULL;
  31. vertcap = 0;
  32. nnormals = 0;
  33. normals = NULL;
  34. normalcap = 0;
  35. ntris = 0;
  36. tris = NULL;
  37. tricap = 0;
  38. vidx = 0;
  39. }
  40. RecastPolyList::~RecastPolyList()
  41. {
  42. clear();
  43. }
  44. rcChunkyTriMesh* RecastPolyList::getChunkyMesh()
  45. {
  46. if (!mChunkyMesh)
  47. {
  48. mChunkyMesh = new rcChunkyTriMesh;
  49. if (!mChunkyMesh)
  50. {
  51. Con::errorf("Build tile navigation: out of memory");
  52. return NULL;
  53. }
  54. if (!rcCreateChunkyTriMesh(getVerts(), getTris(), getTriCount(), 256, mChunkyMesh))
  55. {
  56. Con::errorf("Build tile navigation: out of memory");
  57. return NULL;
  58. }
  59. }
  60. return mChunkyMesh;
  61. }
  62. void RecastPolyList::clear()
  63. {
  64. nverts = 0;
  65. delete[] verts;
  66. verts = NULL;
  67. vertcap = 0;
  68. nnormals = 0;
  69. delete[] normals;
  70. normals = NULL;
  71. normalcap = 0;
  72. ntris = 0;
  73. delete[] tris;
  74. tris = NULL;
  75. tricap = 0;
  76. }
  77. bool RecastPolyList::isEmpty() const
  78. {
  79. return getTriCount() == 0;
  80. }
  81. U32 RecastPolyList::addPoint(const Point3F &p)
  82. {
  83. // If we've reached the vertex cap, double the array size.
  84. if(nverts == vertcap)
  85. {
  86. // vertcap starts at 64, otherwise it doubles.
  87. if(vertcap == 0) vertcap = 16;
  88. else vertcap *= 2;
  89. // Allocate new vertex storage.
  90. F32 *newverts = new F32[vertcap*3];
  91. dMemcpy(newverts, verts, nverts*3 * sizeof(F32));
  92. dFree(verts);
  93. verts = newverts;
  94. }
  95. Point3F v = p;
  96. v.convolve(mScale);
  97. mMatrix.mulP(v);
  98. // Insert the new vertex.
  99. verts[nverts*3] = v.x;
  100. verts[nverts*3+1] = v.z;
  101. verts[nverts*3+2] = -v.y;
  102. // Return nverts before incrementing it.
  103. return nverts++;
  104. }
  105. U32 RecastPolyList::addPlane(const PlaneF &plane)
  106. {
  107. planes.increment();
  108. mPlaneTransformer.transform(plane, planes.last());
  109. return planes.size() - 1;
  110. }
  111. void RecastPolyList::begin(BaseMatInstance *material, U32 surfaceKey)
  112. {
  113. vidx = 0;
  114. // If we've reached the tri cap, grow the array.
  115. if(ntris == tricap)
  116. {
  117. if(tricap == 0) tricap = 16;
  118. else tricap *= 2;
  119. // Allocate new vertex storage.
  120. S32 *newtris = new S32[tricap*3];
  121. dMemcpy(newtris, tris, ntris*3 * sizeof(S32));
  122. dFree(tris);
  123. tris = newtris;
  124. }
  125. }
  126. void RecastPolyList::plane(U32 v1, U32 v2, U32 v3)
  127. {
  128. }
  129. void RecastPolyList::plane(const PlaneF& p)
  130. {
  131. }
  132. void RecastPolyList::plane(const U32 index)
  133. {
  134. }
  135. void RecastPolyList::vertex(U32 vi)
  136. {
  137. if(vidx == 3)
  138. return;
  139. tris[ntris*3+2-vidx] = vi;
  140. vidx++;
  141. }
  142. void RecastPolyList::end()
  143. {
  144. // Fetch current triangle indices
  145. const U32 i0 = tris[ntris * 3 + 0];
  146. const U32 i1 = tris[ntris * 3 + 1];
  147. const U32 i2 = tris[ntris * 3 + 2];
  148. // Rebuild vertices
  149. Point3F v0(verts[i0 * 3 + 0], verts[i0 * 3 + 1], verts[i0 * 3 + 2]);
  150. Point3F v1(verts[i1 * 3 + 0], verts[i1 * 3 + 1], verts[i1 * 3 + 2]);
  151. Point3F v2(verts[i2 * 3 + 0], verts[i2 * 3 + 1], verts[i2 * 3 + 2]);
  152. // Compute normal
  153. Point3F edge1 = v1 - v0;
  154. Point3F edge2 = v2 - v0;
  155. Point3F normal = mCross(edge1, edge2);
  156. normal.normalizeSafe();
  157. // Allocate/resize normal buffer if needed
  158. if (nnormals == normalcap)
  159. {
  160. normalcap = (normalcap == 0) ? 16 : normalcap * 2;
  161. F32* newNormals = new F32[normalcap * 3];
  162. if (normals)
  163. dMemcpy(newNormals, normals, nnormals * 3 * sizeof(F32));
  164. delete[] normals;
  165. normals = newNormals;
  166. }
  167. // Store normal
  168. normals[nnormals * 3 + 0] = normal.x;
  169. normals[nnormals * 3 + 1] = normal.y;
  170. normals[nnormals * 3 + 2] = normal.z;
  171. nnormals++;
  172. ntris++;
  173. }
  174. U32 RecastPolyList::getVertCount() const
  175. {
  176. return nverts;
  177. }
  178. const F32 *RecastPolyList::getVerts() const
  179. {
  180. return verts;
  181. }
  182. const F32* RecastPolyList::getNormals() const
  183. {
  184. return normals;
  185. }
  186. U32 RecastPolyList::getTriCount() const
  187. {
  188. return ntris;
  189. }
  190. const S32 *RecastPolyList::getTris() const
  191. {
  192. return tris;
  193. }
  194. void RecastPolyList::renderWire() const
  195. {
  196. GFXStateBlockDesc desc;
  197. desc.setCullMode(GFXCullNone);
  198. desc.setZReadWrite(false, false);
  199. //desc.setBlend(true);
  200. GFXStateBlockRef sb = GFX->createStateBlock(desc);
  201. GFX->setStateBlock(sb);
  202. PrimBuild::color3i(255, 0, 255);
  203. for(U32 t = 0; t < getTriCount(); t++)
  204. {
  205. PrimBuild::begin(GFXLineStrip, 4);
  206. PrimBuild::vertex3f(verts[tris[t*3]*3], -verts[tris[t*3]*3+2], verts[tris[t*3]*3+1]);
  207. PrimBuild::vertex3f(verts[tris[t*3+1]*3], -verts[tris[t*3+1]*3+2], verts[tris[t*3+1]*3+1]);
  208. PrimBuild::vertex3f(verts[tris[t*3+2]*3], -verts[tris[t*3+2]*3+2], verts[tris[t*3+2]*3+1]);
  209. PrimBuild::vertex3f(verts[tris[t*3]*3], -verts[tris[t*3]*3+2], verts[tris[t*3]*3+1]);
  210. PrimBuild::end();
  211. }
  212. }