duDebugDrawTorque.cpp 7.2 KB

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