recastPolyList.cpp 4.4 KB

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