2
0

duDebugDrawTorque.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. VECTOR_SET_ASSOCIATION(mDrawCache);
  39. mPrimType = 0;
  40. mVertCount = 0;
  41. }
  42. duDebugDrawTorque::~duDebugDrawTorque()
  43. {
  44. }
  45. void duDebugDrawTorque::depthMask(bool state)
  46. {
  47. mDesc.setZReadWrite(state);
  48. }
  49. void duDebugDrawTorque::texture(bool state)
  50. {
  51. // need a checker texture?...... if(state is true) then set first slot to that texture.
  52. }
  53. unsigned int duDebugDrawTorque::areaToCol(unsigned int area)
  54. {
  55. switch (area)
  56. {
  57. // Ground (0) : light blue
  58. case GroundArea: return duRGBA(0, 192, 255, 255);
  59. // Water : blue
  60. case WaterArea: return duRGBA(0, 0, 255, 255);
  61. // Road : brown
  62. case OffMeshArea: return duRGBA(50, 20, 12, 255);
  63. // Unexpected : red
  64. default: return duRGBA(255, 0, 0, 255);
  65. }
  66. }
  67. /// Begin drawing primitives.
  68. /// @param prim [in] primitive type to draw, one of rcDebugDrawPrimitives.
  69. /// @param size [in] size of a primitive, applies to point size and line width only.
  70. void duDebugDrawTorque::begin(duDebugDrawPrimitives prim, float size)
  71. {
  72. if (!mVertList.empty())
  73. mVertList.clear();
  74. mVertCount = 0;
  75. mPrimType = 0;
  76. switch (prim)
  77. {
  78. case DU_DRAW_POINTS: mPrimType = DU_DRAW_POINTS; break;
  79. case DU_DRAW_LINES: mPrimType = DU_DRAW_LINES; break;
  80. case DU_DRAW_TRIS: mPrimType = DU_DRAW_TRIS; break;
  81. case DU_DRAW_QUADS: mPrimType = DU_DRAW_QUADS; break;
  82. }
  83. mDesc.setCullMode(GFXCullCW);
  84. mDesc.setBlend(false);
  85. }
  86. /// Submit a vertex
  87. /// @param pos [in] position of the verts.
  88. /// @param color [in] color of the verts.
  89. void duDebugDrawTorque::vertex(const float* pos, unsigned int color)
  90. {
  91. vertex(pos[0], pos[1], pos[2], color);
  92. }
  93. /// Submit a vertex
  94. /// @param x,y,z [in] position of the verts.
  95. /// @param color [in] color of the verts.
  96. void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color)
  97. {
  98. _vertex(x, -z, y, color);
  99. }
  100. /// Submit a vertex
  101. /// @param pos [in] position of the verts.
  102. /// @param color [in] color of the verts.
  103. void duDebugDrawTorque::vertex(const float* pos, unsigned int color, const float* uv)
  104. {
  105. vertex(pos[0], pos[1], pos[2], color);
  106. }
  107. /// Submit a vertex
  108. /// @param x,y,z [in] position of the verts.
  109. /// @param color [in] color of the verts.
  110. void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v)
  111. {
  112. vertex(x, y, z, color);
  113. }
  114. /// Push a vertex onto the buffer.
  115. void duDebugDrawTorque::_vertex(const float x, const float y, const float z, unsigned int color)
  116. {
  117. GFXVertexPCT vert;
  118. vert.point.set(x, y, z);
  119. U8 r, g, b, a;
  120. // Convert color integer to components.
  121. rcCol(color, r, g, b, a);
  122. vert.color.set(r, g, b, a);
  123. mVertList.push_back(vert);
  124. }
  125. /// End drawing primitives.
  126. void duDebugDrawTorque::end()
  127. {
  128. if (mVertList.empty())
  129. return;
  130. const U32 maxVertsPerDraw = GFX_MAX_DYNAMIC_VERTS;
  131. Box3F box;
  132. box.minExtents.set(F32_MAX, F32_MAX, F32_MAX);
  133. box.maxExtents.set(-F32_MAX, -F32_MAX, -F32_MAX);
  134. switch (mPrimType)
  135. {
  136. case DU_DRAW_POINTS:
  137. {
  138. const U32 totalPoints = mVertList.size();
  139. for (U32 p = 0; p < totalPoints;)
  140. {
  141. const U32 pointsThisBatch = getMin(maxVertsPerDraw, totalPoints - p);
  142. const U32 batchVerts = pointsThisBatch;
  143. GFXVertexBufferHandle<GFXVertexPCT> buffer;
  144. buffer.set(GFX, batchVerts, GFXBufferTypeStatic);
  145. GFXVertexPCT* verts = buffer.lock();
  146. for (U32 i = 0; i < pointsThisBatch; ++i)
  147. {
  148. verts[i] = mVertList[p + i];
  149. box.minExtents.setMin(verts[i].point);
  150. box.maxExtents.setMax(verts[i].point);
  151. }
  152. buffer.unlock();
  153. // --- Build index buffer
  154. GFXPrimitiveBufferHandle pb;
  155. pb.set(GFX, pointsThisBatch, pointsThisBatch, GFXBufferTypeStatic);
  156. U16* indices = nullptr;
  157. pb.lock(&indices);
  158. for (U32 i = 0; i < pointsThisBatch; ++i)
  159. {
  160. indices[i] = i;
  161. }
  162. pb.unlock();
  163. CachedDraw batch;
  164. batch.primType = GFXPointList;
  165. batch.buffer = buffer;
  166. batch.vertexCount = batchVerts;
  167. batch.primitiveBuffer = pb;
  168. batch.primitiveCount = pointsThisBatch;
  169. batch.state = mDesc;
  170. batch.bounds = box;
  171. mDrawCache.push_back(batch);
  172. p += pointsThisBatch;
  173. }
  174. break;
  175. }
  176. case DU_DRAW_LINES:
  177. {
  178. AssertFatal(mVertList.size() % 2 == 0, "DU_DRAW_LINES given invalid vertex count.");
  179. const U32 vertsPerLine = 2;
  180. const U32 totalLines = mVertList.size() / vertsPerLine;
  181. for (U32 l = 0; l < totalLines;)
  182. {
  183. const U32 linesThisBatch = getMin(maxVertsPerDraw / vertsPerLine, totalLines - l);
  184. const U32 batchVerts = linesThisBatch * vertsPerLine;
  185. GFXVertexBufferHandle<GFXVertexPCT> buffer;
  186. buffer.set(GFX, batchVerts, GFXBufferTypeStatic);
  187. GFXVertexPCT* verts = buffer.lock();
  188. for (U32 i = 0; i < linesThisBatch * vertsPerLine; ++i)
  189. {
  190. verts[i] = mVertList[l * vertsPerLine + i];
  191. box.minExtents.setMin(verts[i].point);
  192. box.maxExtents.setMax(verts[i].point);
  193. }
  194. buffer.unlock();
  195. // --- Build index buffer
  196. GFXPrimitiveBufferHandle pb;
  197. pb.set(GFX, linesThisBatch * 2, linesThisBatch, GFXBufferTypeStatic);
  198. U16* indices = nullptr;
  199. pb.lock(&indices);
  200. for (U32 i = 0; i < linesThisBatch; ++i)
  201. {
  202. indices[i * 2 + 0] = i * 2;
  203. indices[i * 2 + 1] = i * 2 + 1;
  204. }
  205. pb.unlock();
  206. CachedDraw batch;
  207. batch.primType = GFXLineList;
  208. batch.buffer = buffer;
  209. batch.vertexCount = batchVerts;
  210. batch.primitiveBuffer = pb;
  211. batch.primitiveCount = linesThisBatch;
  212. batch.state = mDesc;
  213. batch.bounds = box;
  214. mDrawCache.push_back(batch);
  215. l += linesThisBatch;
  216. }
  217. break;
  218. }
  219. case DU_DRAW_TRIS:
  220. {
  221. AssertFatal(mVertList.size() % 3 == 0, "DU_DRAW_TRIS given invalid vertex count.");
  222. const U32 vertsPerTri = 3;
  223. const U32 totalTris = mVertList.size() / vertsPerTri;
  224. for (U32 t = 0; t < totalTris;)
  225. {
  226. const U32 trisThisBatch = getMin(maxVertsPerDraw / vertsPerTri, totalTris - t);
  227. const U32 batchVerts = trisThisBatch * vertsPerTri;
  228. GFXVertexBufferHandle<GFXVertexPCT> buffer;
  229. buffer.set(GFX, batchVerts, GFXBufferTypeStatic);
  230. GFXVertexPCT* verts = buffer.lock();
  231. for (U32 i = 0; i < trisThisBatch * vertsPerTri; ++i)
  232. {
  233. verts[i] = mVertList[t * vertsPerTri + i];
  234. box.minExtents.setMin(verts[i].point);
  235. box.maxExtents.setMax(verts[i].point);
  236. }
  237. buffer.unlock();
  238. // --- Build index buffer
  239. GFXPrimitiveBufferHandle pb;
  240. pb.set(GFX, trisThisBatch*3, trisThisBatch, GFXBufferTypeStatic);
  241. U16* indices = nullptr;
  242. pb.lock(&indices);
  243. for (U32 i = 0; i < trisThisBatch; ++i)
  244. {
  245. indices[i * 3 + 0] = i * 3 + 0;
  246. indices[i * 3 + 1] = i * 3 + 1;
  247. indices[i * 3 + 2] = i * 3 + 2;
  248. }
  249. pb.unlock();
  250. CachedDraw batch;
  251. batch.primType = GFXTriangleList;
  252. batch.buffer = buffer;
  253. batch.vertexCount = batchVerts;
  254. batch.primitiveBuffer = pb;
  255. batch.primitiveCount = trisThisBatch;
  256. batch.state = mDesc;
  257. batch.bounds = box;
  258. mDrawCache.push_back(batch);
  259. t += trisThisBatch;
  260. }
  261. break;
  262. }
  263. case DU_DRAW_QUADS:
  264. {
  265. AssertFatal(mVertList.size() % 4 == 0, "DU_DRAW_QUADS given wrong number of vertices.");
  266. const U32 vertsPerQuad = 4;
  267. const U32 totalQuads = mVertList.size() / 4;
  268. for (U32 q = 0; q < totalQuads;)
  269. {
  270. const U32 quadsThisBatch = getMin(maxVertsPerDraw / vertsPerQuad, totalQuads - q);
  271. const U32 batchVerts = quadsThisBatch * vertsPerQuad;
  272. const U32 batchIndices = quadsThisBatch * 6;
  273. GFXVertexBufferHandle<GFXVertexPCT> buffer;
  274. buffer.set(GFX, batchVerts, GFXBufferTypeStatic);
  275. GFXVertexPCT* verts = buffer.lock();
  276. U32 outIdx = 0;
  277. for (U32 i = 0; i < quadsThisBatch; ++i)
  278. {
  279. const GFXVertexPCT& v0 = mVertList[(q + i) * 4 + 0];
  280. const GFXVertexPCT& v1 = mVertList[(q + i) * 4 + 1];
  281. const GFXVertexPCT& v2 = mVertList[(q + i) * 4 + 2];
  282. const GFXVertexPCT& v3 = mVertList[(q + i) * 4 + 3];
  283. verts[outIdx++] = v0;
  284. verts[outIdx++] = v1;
  285. verts[outIdx++] = v2;
  286. verts[outIdx++] = v3;
  287. }
  288. buffer.unlock();
  289. GFXPrimitiveBufferHandle pb;
  290. pb.set(GFX, batchIndices, quadsThisBatch*2, GFXBufferTypeStatic);
  291. U16* indices = nullptr;
  292. pb.lock(&indices);
  293. for (U32 i = 0; i < quadsThisBatch; ++i)
  294. {
  295. const U16 base = i * 4;
  296. indices[i * 6 + 0] = base + 0;
  297. indices[i * 6 + 1] = base + 1;
  298. indices[i * 6 + 2] = base + 2;
  299. indices[i * 6 + 3] = base + 0;
  300. indices[i * 6 + 4] = base + 2;
  301. indices[i * 6 + 5] = base + 3;
  302. }
  303. pb.unlock();
  304. CachedDraw batch;
  305. batch.primType = GFXTriangleList;
  306. batch.buffer = buffer;
  307. batch.vertexCount = batchVerts;
  308. batch.primitiveBuffer = pb;
  309. batch.primitiveCount = quadsThisBatch*2;
  310. batch.state = mDesc;
  311. mDrawCache.push_back(batch);
  312. q += quadsThisBatch;
  313. }
  314. break;
  315. }
  316. }
  317. mVertList.clear();
  318. }
  319. void duDebugDrawTorque::clearCache()
  320. {
  321. mDrawCache.clear();
  322. }
  323. void duDebugDrawTorque::render(SceneRenderState* state)
  324. {
  325. const Frustum& frustum = state->getCameraFrustum();
  326. for (U32 i = 0; i < mDrawCache.size(); ++i)
  327. {
  328. const CachedDraw& draw = mDrawCache[i];
  329. if (!frustum.getBounds().isOverlapped(draw.bounds))
  330. continue;
  331. GFX->setPrimitiveBuffer(draw.primitiveBuffer);
  332. GFX->setStateBlockByDesc(draw.state);
  333. GFX->setupGenericShaders(GFXDevice::GSColor);
  334. GFX->setVertexBuffer(draw.buffer);
  335. GFX->drawIndexedPrimitive(
  336. draw.primType,
  337. 0, // start vertex
  338. 0, // min vertex index
  339. draw.vertexCount, // vertex count
  340. 0, // start index
  341. draw.primitiveCount // primitive count
  342. );
  343. }
  344. }