duDebugDrawTorque.cpp 7.1 KB

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