duDebugDrawTorque.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. mOverrideColor = 0;
  38. mOverride = false;
  39. mGroup = 0;
  40. }
  41. duDebugDrawTorque::~duDebugDrawTorque()
  42. {
  43. clear();
  44. }
  45. void duDebugDrawTorque::depthMask(bool state)
  46. {
  47. mDesc.setZReadWrite(state, state);
  48. }
  49. void duDebugDrawTorque::texture(bool state)
  50. {
  51. }
  52. /// Begin drawing primitives.
  53. /// @param prim [in] primitive type to draw, one of rcDebugDrawPrimitives.
  54. /// @param size [in] size of a primitive, applies to point size and line width only.
  55. void duDebugDrawTorque::begin(duDebugDrawPrimitives prim, float size)
  56. {
  57. mCurrColor = -1;
  58. mQuadsMode = false;
  59. mVertCount = 0;
  60. mPrimType = 0;
  61. switch(prim)
  62. {
  63. case DU_DRAW_POINTS: mPrimType = GFXPointList; break;
  64. case DU_DRAW_LINES: mPrimType = GFXLineList; break;
  65. case DU_DRAW_TRIS: mPrimType = GFXTriangleList; break;
  66. case DU_DRAW_QUADS: mPrimType = GFXTriangleList;
  67. mQuadsMode = true; break;
  68. }
  69. mBuffers.push_back(Buffer(mPrimType));
  70. mBuffers.last().group = mGroup;
  71. mDesc.setCullMode(GFXCullNone);
  72. mDesc.setBlend(true);
  73. }
  74. void duDebugDrawTorque::beginGroup(U32 group)
  75. {
  76. mGroup = group;
  77. }
  78. /// Submit a vertex
  79. /// @param pos [in] position of the verts.
  80. /// @param color [in] color of the verts.
  81. void duDebugDrawTorque::vertex(const float* pos, unsigned int color)
  82. {
  83. vertex(pos[0], pos[1], pos[2], color);
  84. }
  85. /// Submit a vertex
  86. /// @param x,y,z [in] position of the verts.
  87. /// @param color [in] color of the verts.
  88. void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color)
  89. {
  90. if(mQuadsMode)
  91. {
  92. if(mVertCount == 3)
  93. {
  94. _vertex(x, -z, y, color);
  95. _vertex(mStore[0][0], mStore[0][1], mStore[0][2], color);
  96. _vertex(mStore[1][0], mStore[1][1], mStore[1][2], color);
  97. _vertex(mStore[1][0], mStore[1][1], mStore[1][2], color);
  98. _vertex(mStore[2][0], mStore[2][1], mStore[2][2], color);
  99. _vertex(x, -z, y, color);
  100. mVertCount = 0;
  101. }
  102. else
  103. {
  104. mStore[mVertCount][0] = x;
  105. mStore[mVertCount][1] = -z;
  106. mStore[mVertCount][2] = y;
  107. mVertCount++;
  108. }
  109. }
  110. else
  111. {
  112. _vertex(x, -z, y, color);
  113. }
  114. }
  115. /// Submit a vertex
  116. /// @param pos [in] position of the verts.
  117. /// @param color [in] color of the verts.
  118. void duDebugDrawTorque::vertex(const float* pos, unsigned int color, const float* uv)
  119. {
  120. vertex(pos[0], pos[1], pos[2], color);
  121. }
  122. /// Submit a vertex
  123. /// @param x,y,z [in] position of the verts.
  124. /// @param color [in] color of the verts.
  125. void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v)
  126. {
  127. vertex(x, y, z, color);
  128. }
  129. /// Push a vertex onto the buffer.
  130. void duDebugDrawTorque::_vertex(const float x, const float y, const float z, unsigned int color)
  131. {
  132. // Use override color if we must.
  133. //if(mOverride)
  134. //color = mOverrideColor;
  135. if(mCurrColor != color || !mBuffers.last().buffer.size())
  136. {
  137. U8 r, g, b, a;
  138. // Convert color integer to components.
  139. rcCol(color, r, g, b, a);
  140. mBuffers.last().buffer.push_back(Instruction(r, g, b, a));
  141. mCurrColor = color;
  142. }
  143. // Construct vertex data.
  144. mBuffers.last().buffer.push_back(Instruction(x, y, z));
  145. }
  146. /// End drawing primitives.
  147. void duDebugDrawTorque::end()
  148. {
  149. }
  150. void duDebugDrawTorque::overrideColor(unsigned int col)
  151. {
  152. mOverride = true;
  153. mOverrideColor = col;
  154. }
  155. void duDebugDrawTorque::cancelOverride()
  156. {
  157. mOverride = false;
  158. }
  159. void duDebugDrawTorque::renderBuffer(Buffer &b)
  160. {
  161. PrimBuild::begin(b.primType, b.buffer.size());
  162. Vector<Instruction> &buf = b.buffer;
  163. for(U32 i = 0; i < buf.size(); i++)
  164. {
  165. switch(buf[i].type)
  166. {
  167. case Instruction::POINT:
  168. PrimBuild::vertex3f(buf[i].data.point.x,
  169. buf[i].data.point.y,
  170. buf[i].data.point.z);
  171. break;
  172. case Instruction::COLOR:
  173. if(mOverride)
  174. break;
  175. PrimBuild::color4i(buf[i].data.color.r,
  176. buf[i].data.color.g,
  177. buf[i].data.color.b,
  178. buf[i].data.color.a);
  179. break;
  180. }
  181. }
  182. PrimBuild::end();
  183. }
  184. void duDebugDrawTorque::render()
  185. {
  186. GFXStateBlockRef sb = GFX->createStateBlock(mDesc);
  187. GFX->setStateBlock(sb);
  188. // Use override color for all rendering.
  189. if(mOverride)
  190. {
  191. U8 r, g, b, a;
  192. rcCol(mOverrideColor, r, g, b, a);
  193. PrimBuild::color4i(r, g, b, a);
  194. }
  195. for(U32 b = 0; b < mBuffers.size(); b++)
  196. {
  197. renderBuffer(mBuffers[b]);
  198. }
  199. }
  200. void duDebugDrawTorque::renderGroup(U32 group)
  201. {
  202. GFXStateBlockRef sb = GFX->createStateBlock(mDesc);
  203. GFX->setStateBlock(sb);
  204. // Use override color for all rendering.
  205. if(mOverride)
  206. {
  207. U8 r, g, b, a;
  208. rcCol(mOverrideColor, r, g, b, a);
  209. PrimBuild::color4i(r, g, b, a);
  210. }
  211. for(U32 b = 0; b < mBuffers.size(); b++)
  212. {
  213. if(mBuffers[b].group == group)
  214. renderBuffer(mBuffers[b]);
  215. }
  216. }
  217. void duDebugDrawTorque::clear()
  218. {
  219. for(U32 b = 0; b < mBuffers.size(); b++)
  220. mBuffers[b].buffer.clear();
  221. mBuffers.clear();
  222. }