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. }
  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. 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. if(!newtris)
  97. return;
  98. dMemcpy(newtris, tris, ntris*3 * sizeof(S32));
  99. dFree(tris);
  100. tris = newtris;
  101. }
  102. }
  103. void RecastPolyList::plane(U32 v1, U32 v2, U32 v3)
  104. {
  105. }
  106. void RecastPolyList::plane(const PlaneF& p)
  107. {
  108. }
  109. void RecastPolyList::plane(const U32 index)
  110. {
  111. }
  112. void RecastPolyList::vertex(U32 vi)
  113. {
  114. if(vidx == 3)
  115. return;
  116. tris[ntris*3+2-vidx] = vi;
  117. vidx++;
  118. }
  119. void RecastPolyList::end()
  120. {
  121. ntris++;
  122. }
  123. U32 RecastPolyList::getVertCount() const
  124. {
  125. return nverts;
  126. }
  127. const F32 *RecastPolyList::getVerts() const
  128. {
  129. return verts;
  130. }
  131. U32 RecastPolyList::getTriCount() const
  132. {
  133. return ntris;
  134. }
  135. const S32 *RecastPolyList::getTris() const
  136. {
  137. return tris;
  138. }
  139. void RecastPolyList::renderWire() const
  140. {
  141. GFXStateBlockDesc desc;
  142. desc.setCullMode(GFXCullNone);
  143. desc.setZReadWrite(false, false);
  144. //desc.setBlend(true);
  145. GFXStateBlockRef sb = GFX->createStateBlock(desc);
  146. GFX->setStateBlock(sb);
  147. PrimBuild::color3i(255, 0, 255);
  148. for(U32 t = 0; t < getTriCount(); t++)
  149. {
  150. PrimBuild::begin(GFXLineStrip, 4);
  151. PrimBuild::vertex3f(verts[tris[t*3]*3], -verts[tris[t*3]*3+2], verts[tris[t*3]*3+1]);
  152. PrimBuild::vertex3f(verts[tris[t*3+1]*3], -verts[tris[t*3+1]*3+2], verts[tris[t*3+1]*3+1]);
  153. PrimBuild::vertex3f(verts[tris[t*3+2]*3], -verts[tris[t*3+2]*3+2], verts[tris[t*3+2]*3+1]);
  154. PrimBuild::vertex3f(verts[tris[t*3]*3], -verts[tris[t*3]*3+2], verts[tris[t*3]*3+1]);
  155. PrimBuild::end();
  156. }
  157. }