recastPolyList.cpp 4.4 KB

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