recastPolyList.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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()
  28. {
  29. nverts = 0;
  30. verts = NULL;
  31. vertcap = 0;
  32. ntris = 0;
  33. tris = NULL;
  34. tricap = 0;
  35. }
  36. RecastPolyList::~RecastPolyList()
  37. {
  38. clear();
  39. }
  40. void RecastPolyList::clear()
  41. {
  42. nverts = 0;
  43. delete[] verts;
  44. verts = NULL;
  45. vertcap = 0;
  46. ntris = 0;
  47. delete[] tris;
  48. tris = NULL;
  49. tricap = 0;
  50. }
  51. bool RecastPolyList::isEmpty() const
  52. {
  53. return getTriCount() == 0;
  54. }
  55. U32 RecastPolyList::addPoint(const Point3F &p)
  56. {
  57. // If we've reached the vertex cap, double the array size.
  58. if(nverts == vertcap)
  59. {
  60. // vertcap starts at 64, otherwise it doubles.
  61. if(vertcap == 0) vertcap = 16;
  62. else vertcap *= 2;
  63. // Allocate new vertex storage.
  64. F32 *newverts = new F32[vertcap*3];
  65. if(!newverts)
  66. return 0;
  67. dMemcpy(newverts, verts, nverts*3 * sizeof(F32));
  68. dFree(verts);
  69. verts = newverts;
  70. }
  71. Point3F v = p;
  72. v.convolve(mScale);
  73. mMatrix.mulP(v);
  74. // Insert the new vertex.
  75. verts[nverts*3] = v.x;
  76. verts[nverts*3+1] = v.z;
  77. verts[nverts*3+2] = -v.y;
  78. // Return nverts before incrementing it.
  79. return nverts++;
  80. }
  81. U32 RecastPolyList::addPlane(const PlaneF &plane)
  82. {
  83. planes.increment();
  84. mPlaneTransformer.transform(plane, planes.last());
  85. return planes.size() - 1;
  86. }
  87. void RecastPolyList::begin(BaseMatInstance *material, U32 surfaceKey)
  88. {
  89. vidx = 0;
  90. // If we've reached the tri cap, grow the array.
  91. if(ntris == tricap)
  92. {
  93. if(tricap == 0) tricap = 16;
  94. else tricap *= 2;
  95. // Allocate new vertex storage.
  96. S32 *newtris = new S32[tricap*3];
  97. if(!newtris)
  98. return;
  99. dMemcpy(newtris, tris, ntris*3 * sizeof(S32));
  100. dFree(tris);
  101. tris = newtris;
  102. }
  103. }
  104. void RecastPolyList::plane(U32 v1, U32 v2, U32 v3)
  105. {
  106. }
  107. void RecastPolyList::plane(const PlaneF& p)
  108. {
  109. }
  110. void RecastPolyList::plane(const U32 index)
  111. {
  112. }
  113. void RecastPolyList::vertex(U32 vi)
  114. {
  115. if(vidx == 3)
  116. return;
  117. tris[ntris*3+2-vidx] = vi;
  118. vidx++;
  119. }
  120. void RecastPolyList::end()
  121. {
  122. ntris++;
  123. }
  124. U32 RecastPolyList::getVertCount() const
  125. {
  126. return nverts;
  127. }
  128. const F32 *RecastPolyList::getVerts() const
  129. {
  130. return verts;
  131. }
  132. U32 RecastPolyList::getTriCount() const
  133. {
  134. return ntris;
  135. }
  136. const S32 *RecastPolyList::getTris() const
  137. {
  138. return tris;
  139. }
  140. void RecastPolyList::renderWire() const
  141. {
  142. GFXStateBlockDesc desc;
  143. desc.setCullMode(GFXCullNone);
  144. desc.setZReadWrite(false, false);
  145. //desc.setBlend(true);
  146. GFXStateBlockRef sb = GFX->createStateBlock(desc);
  147. GFX->setStateBlock(sb);
  148. PrimBuild::color3i(255, 0, 255);
  149. for(U32 t = 0; t < getTriCount(); t++)
  150. {
  151. PrimBuild::begin(GFXLineStrip, 4);
  152. PrimBuild::vertex3f(verts[tris[t*3]*3], -verts[tris[t*3]*3+2], verts[tris[t*3]*3+1]);
  153. PrimBuild::vertex3f(verts[tris[t*3+1]*3], -verts[tris[t*3+1]*3+2], verts[tris[t*3+1]*3+1]);
  154. PrimBuild::vertex3f(verts[tris[t*3+2]*3], -verts[tris[t*3+2]*3+2], verts[tris[t*3+2]*3+1]);
  155. PrimBuild::vertex3f(verts[tris[t*3]*3], -verts[tris[t*3]*3+2], verts[tris[t*3]*3+1]);
  156. PrimBuild::end();
  157. }
  158. }