duDebugDrawTorque.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "torqueRecast.h"
  23. #include "duDebugDrawTorque.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "gfx/primBuilder.h"
  26. #include "gfx/gfxStateBlock.h"
  27. /// @class duDebugDrawTorque
  28. /// This class uses the primitive builder (gfx/primBuild.h) to render navmeshes
  29. /// and other Recast data. To facilitate the primbuilder's requirement to know
  30. /// the number of vertices to render beforehand, this class stores all vertices
  31. /// in a buffer of its own, then passes on that known-size buffer.
  32. /// This means that you only need to call the duDebugDraw functions when your
  33. /// data changes. At other times, you can cache the duDebugDrawTorque object
  34. /// and call its render() method, which actually renders its buffered data.
  35. duDebugDrawTorque::duDebugDrawTorque()
  36. {
  37. VECTOR_SET_ASSOCIATION(mVertList);
  38. mPrimType = 0;
  39. mQuadsMode = false;
  40. mVertCount = 0;
  41. dMemset(&mStore, 0, sizeof(mStore));
  42. }
  43. duDebugDrawTorque::~duDebugDrawTorque()
  44. {
  45. }
  46. void duDebugDrawTorque::depthMask(bool state)
  47. {
  48. mDesc.setZReadWrite(state, state);
  49. }
  50. void duDebugDrawTorque::texture(bool state)
  51. {
  52. // need a checker texture?...... if(state is true) then set first slot to that texture.
  53. }
  54. unsigned int duDebugDrawTorque::areaToCol(unsigned int area)
  55. {
  56. switch (area)
  57. {
  58. // Ground (0) : light blue
  59. case GroundArea: return duRGBA(0, 192, 255, 255);
  60. // Water : blue
  61. case WaterArea: return duRGBA(0, 0, 255, 255);
  62. // Road : brown
  63. case OffMeshArea: return duRGBA(50, 20, 12, 255);
  64. // Unexpected : red
  65. default: return duRGBA(255, 0, 0, 255);
  66. }
  67. }
  68. /// Begin drawing primitives.
  69. /// @param prim [in] primitive type to draw, one of rcDebugDrawPrimitives.
  70. /// @param size [in] size of a primitive, applies to point size and line width only.
  71. void duDebugDrawTorque::begin(duDebugDrawPrimitives prim, float size)
  72. {
  73. if (!mVertList.empty())
  74. mVertList.clear();
  75. mQuadsMode = false;
  76. mVertCount = 0;
  77. mPrimType = 0;
  78. switch (prim)
  79. {
  80. case DU_DRAW_POINTS: mPrimType = GFXPointList; break;
  81. case DU_DRAW_LINES: mPrimType = GFXLineList; break;
  82. case DU_DRAW_TRIS: mPrimType = GFXTriangleList; break;
  83. case DU_DRAW_QUADS: mPrimType = GFXTriangleList; mQuadsMode = true; break;
  84. }
  85. mDesc.setCullMode(GFXCullNone);
  86. mDesc.setBlend(true);
  87. }
  88. /// Submit a vertex
  89. /// @param pos [in] position of the verts.
  90. /// @param color [in] color of the verts.
  91. void duDebugDrawTorque::vertex(const float* pos, unsigned int color)
  92. {
  93. vertex(pos[0], pos[1], pos[2], color);
  94. }
  95. /// Submit a vertex
  96. /// @param x,y,z [in] position of the verts.
  97. /// @param color [in] color of the verts.
  98. void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color)
  99. {
  100. _vertex(x, -z, y, color);
  101. }
  102. /// Submit a vertex
  103. /// @param pos [in] position of the verts.
  104. /// @param color [in] color of the verts.
  105. void duDebugDrawTorque::vertex(const float* pos, unsigned int color, const float* uv)
  106. {
  107. vertex(pos[0], pos[1], pos[2], color);
  108. }
  109. /// Submit a vertex
  110. /// @param x,y,z [in] position of the verts.
  111. /// @param color [in] color of the verts.
  112. void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v)
  113. {
  114. vertex(x, y, z, color);
  115. }
  116. /// Push a vertex onto the buffer.
  117. void duDebugDrawTorque::_vertex(const float x, const float y, const float z, unsigned int color)
  118. {
  119. GFXVertexPCT vert;
  120. vert.point.set(x, y, z);
  121. U8 r, g, b, a;
  122. // Convert color integer to components.
  123. rcCol(color, r, g, b, a);
  124. vert.color.set(r, g, b, a);
  125. mVertList.push_back(vert);
  126. }
  127. /// End drawing primitives.
  128. void duDebugDrawTorque::end()
  129. {
  130. if (mVertList.empty())
  131. return;
  132. const U32 maxVertsPerDraw = GFX_MAX_DYNAMIC_VERTS;
  133. U32 totalVerts = mVertList.size();
  134. U32 stride = 1;
  135. U32 stripStart = 0;
  136. switch (mPrimType)
  137. {
  138. case GFXLineList: stride = 2; break;
  139. case GFXTriangleList: stride = 3; break;
  140. case GFXLineStrip: stripStart = 1; stride = 1; break;
  141. case GFXTriangleStrip:stripStart = 2; stride = 1; break;
  142. default: stride = 1; break;
  143. }
  144. GFX->setPrimitiveBuffer(NULL);
  145. GFX->setStateBlockByDesc(mDesc);
  146. GFX->setupGenericShaders(GFXDevice::GSColor);
  147. for (U32 i = 0; i < totalVerts; i += maxVertsPerDraw)
  148. {
  149. U32 batchSize = getMin(maxVertsPerDraw, totalVerts - i);
  150. mVertexBuffer.set(GFX, batchSize, GFXBufferTypeDynamic);
  151. GFXVertexPCT* verts = mVertexBuffer.lock();
  152. if (verts)
  153. dMemcpy(verts, &mVertList[i], sizeof(GFXVertexPCT) * batchSize);
  154. mVertexBuffer.unlock();
  155. GFX->setVertexBuffer(mVertexBuffer);
  156. U32 numPrims = (batchSize / stride) - stripStart;
  157. GFX->drawPrimitive((GFXPrimitiveType)mPrimType, 0, numPrims);
  158. }
  159. mVertList.clear();
  160. }