duDebugDrawTorque.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 (1) : 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. mDesc.setZReadWrite(true);
  86. }
  87. /// Submit a vertex
  88. /// @param pos [in] position of the verts.
  89. /// @param color [in] color of the verts.
  90. void duDebugDrawTorque::vertex(const float* pos, unsigned int color)
  91. {
  92. vertex(pos[0], pos[1], pos[2], color);
  93. }
  94. /// Submit a vertex
  95. /// @param x,y,z [in] position of the verts.
  96. /// @param color [in] color of the verts.
  97. void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color)
  98. {
  99. _vertex(x, -z, y, color);
  100. }
  101. /// Submit a vertex
  102. /// @param pos [in] position of the verts.
  103. /// @param color [in] color of the verts.
  104. void duDebugDrawTorque::vertex(const float* pos, unsigned int color, const float* uv)
  105. {
  106. vertex(pos[0], pos[1], pos[2], color);
  107. }
  108. /// Submit a vertex
  109. /// @param x,y,z [in] position of the verts.
  110. /// @param color [in] color of the verts.
  111. void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v)
  112. {
  113. vertex(x, y, z, color);
  114. }
  115. /// Push a vertex onto the buffer.
  116. void duDebugDrawTorque::_vertex(const float x, const float y, const float z, unsigned int color)
  117. {
  118. GFXVertexPCT vert;
  119. vert.point.set(x, y, z);
  120. U8 r, g, b, a;
  121. // Convert color integer to components.
  122. rcCol(color, r, g, b, a);
  123. vert.color.set(r, g, b, a);
  124. mVertList.push_back(vert);
  125. }
  126. /// End drawing primitives.
  127. void duDebugDrawTorque::end()
  128. {
  129. if (mVertList.empty())
  130. return;
  131. const U32 maxVertsPerDraw = GFX_MAX_DYNAMIC_VERTS;
  132. Box3F box;
  133. box.minExtents.set(F32_MAX, F32_MAX, F32_MAX);
  134. box.maxExtents.set(-F32_MAX, -F32_MAX, -F32_MAX);
  135. switch (mPrimType)
  136. {
  137. case DU_DRAW_POINTS:
  138. {
  139. const U32 totalPoints = mVertList.size();
  140. for (U32 p = 0; p < totalPoints;)
  141. {
  142. const U32 pointsThisBatch = getMin(maxVertsPerDraw, totalPoints - p);
  143. const U32 batchVerts = pointsThisBatch;
  144. GFXVertexBufferHandle<GFXVertexPCT> buffer;
  145. buffer.set(GFX, batchVerts, GFXBufferTypeStatic);
  146. GFXVertexPCT* verts = buffer.lock();
  147. for (U32 i = 0; i < pointsThisBatch; ++i)
  148. {
  149. verts[i] = mVertList[p + i];
  150. box.minExtents.setMin(verts[i].point);
  151. box.maxExtents.setMax(verts[i].point);
  152. }
  153. buffer.unlock();
  154. // --- Build index buffer
  155. GFXPrimitiveBufferHandle pb;
  156. pb.set(GFX, pointsThisBatch, pointsThisBatch, GFXBufferTypeStatic);
  157. U16* indices = nullptr;
  158. pb.lock(&indices);
  159. for (U32 i = 0; i < pointsThisBatch; ++i)
  160. {
  161. indices[i] = i;
  162. }
  163. pb.unlock();
  164. CachedDraw batch;
  165. batch.primType = GFXPointList;
  166. batch.buffer = buffer;
  167. batch.vertexCount = batchVerts;
  168. batch.primitiveBuffer = pb;
  169. batch.primitiveCount = pointsThisBatch;
  170. batch.state = mDesc;
  171. batch.bounds = box;
  172. mDrawCache.push_back(batch);
  173. p += pointsThisBatch;
  174. }
  175. break;
  176. }
  177. case DU_DRAW_LINES:
  178. {
  179. AssertFatal(mVertList.size() % 2 == 0, "DU_DRAW_LINES given invalid vertex count.");
  180. const U32 vertsPerLine = 2;
  181. const U32 totalLines = mVertList.size() / vertsPerLine;
  182. for (U32 l = 0; l < totalLines;)
  183. {
  184. const U32 linesThisBatch = getMin(maxVertsPerDraw / vertsPerLine, totalLines - l);
  185. const U32 batchVerts = linesThisBatch * vertsPerLine;
  186. GFXVertexBufferHandle<GFXVertexPCT> buffer;
  187. buffer.set(GFX, batchVerts, GFXBufferTypeStatic);
  188. GFXVertexPCT* verts = buffer.lock();
  189. for (U32 i = 0; i < linesThisBatch * vertsPerLine; ++i)
  190. {
  191. verts[i] = mVertList[l * vertsPerLine + i];
  192. box.minExtents.setMin(verts[i].point);
  193. box.maxExtents.setMax(verts[i].point);
  194. }
  195. buffer.unlock();
  196. // --- Build index buffer
  197. GFXPrimitiveBufferHandle pb;
  198. pb.set(GFX, linesThisBatch * 2, linesThisBatch, GFXBufferTypeStatic);
  199. U16* indices = nullptr;
  200. pb.lock(&indices);
  201. for (U32 i = 0; i < linesThisBatch; ++i)
  202. {
  203. indices[i * 2 + 0] = i * 2;
  204. indices[i * 2 + 1] = i * 2 + 1;
  205. }
  206. pb.unlock();
  207. CachedDraw batch;
  208. batch.primType = GFXLineList;
  209. batch.buffer = buffer;
  210. batch.vertexCount = batchVerts;
  211. batch.primitiveBuffer = pb;
  212. batch.primitiveCount = linesThisBatch;
  213. batch.state = mDesc;
  214. batch.bounds = box;
  215. mDrawCache.push_back(batch);
  216. l += linesThisBatch;
  217. }
  218. break;
  219. }
  220. case DU_DRAW_TRIS:
  221. {
  222. AssertFatal(mVertList.size() % 3 == 0, "DU_DRAW_TRIS given invalid vertex count.");
  223. const U32 vertsPerTri = 3;
  224. const U32 totalTris = mVertList.size() / vertsPerTri;
  225. for (U32 t = 0; t < totalTris;)
  226. {
  227. const U32 trisThisBatch = getMin(maxVertsPerDraw / vertsPerTri, totalTris - t);
  228. const U32 batchVerts = trisThisBatch * vertsPerTri;
  229. GFXVertexBufferHandle<GFXVertexPCT> buffer;
  230. buffer.set(GFX, batchVerts, GFXBufferTypeStatic);
  231. GFXVertexPCT* verts = buffer.lock();
  232. for (U32 i = 0; i < trisThisBatch * vertsPerTri; ++i)
  233. {
  234. verts[i] = mVertList[t * vertsPerTri + i];
  235. box.minExtents.setMin(verts[i].point);
  236. box.maxExtents.setMax(verts[i].point);
  237. }
  238. buffer.unlock();
  239. // --- Build index buffer
  240. GFXPrimitiveBufferHandle pb;
  241. pb.set(GFX, trisThisBatch*3, trisThisBatch, GFXBufferTypeStatic);
  242. U16* indices = nullptr;
  243. pb.lock(&indices);
  244. for (U32 i = 0; i < trisThisBatch; ++i)
  245. {
  246. indices[i * 3 + 0] = i * 3 + 0;
  247. indices[i * 3 + 1] = i * 3 + 1;
  248. indices[i * 3 + 2] = i * 3 + 2;
  249. }
  250. pb.unlock();
  251. CachedDraw batch;
  252. batch.primType = GFXTriangleList;
  253. batch.buffer = buffer;
  254. batch.vertexCount = batchVerts;
  255. batch.primitiveBuffer = pb;
  256. batch.primitiveCount = trisThisBatch;
  257. batch.state = mDesc;
  258. batch.bounds = box;
  259. mDrawCache.push_back(batch);
  260. t += trisThisBatch;
  261. }
  262. break;
  263. }
  264. case DU_DRAW_QUADS:
  265. {
  266. AssertFatal(mVertList.size() % 4 == 0, "DU_DRAW_QUADS given wrong number of vertices.");
  267. const U32 vertsPerQuad = 4;
  268. const U32 totalQuads = mVertList.size() / 4;
  269. for (U32 q = 0; q < totalQuads;)
  270. {
  271. const U32 quadsThisBatch = getMin(maxVertsPerDraw / vertsPerQuad, totalQuads - q);
  272. const U32 batchVerts = quadsThisBatch * vertsPerQuad;
  273. const U32 batchIndices = quadsThisBatch * 6;
  274. GFXVertexBufferHandle<GFXVertexPCT> buffer;
  275. buffer.set(GFX, batchVerts, GFXBufferTypeStatic);
  276. GFXVertexPCT* verts = buffer.lock();
  277. U32 outIdx = 0;
  278. for (U32 i = 0; i < quadsThisBatch; ++i)
  279. {
  280. const GFXVertexPCT& v0 = mVertList[(q + i) * 4 + 0];
  281. const GFXVertexPCT& v1 = mVertList[(q + i) * 4 + 1];
  282. const GFXVertexPCT& v2 = mVertList[(q + i) * 4 + 2];
  283. const GFXVertexPCT& v3 = mVertList[(q + i) * 4 + 3];
  284. verts[outIdx++] = v0;
  285. verts[outIdx++] = v1;
  286. verts[outIdx++] = v2;
  287. verts[outIdx++] = v3;
  288. }
  289. buffer.unlock();
  290. GFXPrimitiveBufferHandle pb;
  291. pb.set(GFX, batchIndices, quadsThisBatch*2, GFXBufferTypeStatic);
  292. U16* indices = nullptr;
  293. pb.lock(&indices);
  294. for (U32 i = 0; i < quadsThisBatch; ++i)
  295. {
  296. const U16 base = i * 4;
  297. indices[i * 6 + 0] = base + 0;
  298. indices[i * 6 + 1] = base + 1;
  299. indices[i * 6 + 2] = base + 2;
  300. indices[i * 6 + 3] = base + 0;
  301. indices[i * 6 + 4] = base + 2;
  302. indices[i * 6 + 5] = base + 3;
  303. }
  304. pb.unlock();
  305. CachedDraw batch;
  306. batch.primType = GFXTriangleList;
  307. batch.buffer = buffer;
  308. batch.vertexCount = batchVerts;
  309. batch.primitiveBuffer = pb;
  310. batch.primitiveCount = quadsThisBatch*2;
  311. batch.state = mDesc;
  312. mDrawCache.push_back(batch);
  313. q += quadsThisBatch;
  314. }
  315. break;
  316. }
  317. }
  318. mVertList.clear();
  319. }
  320. void duDebugDrawTorque::clearCache()
  321. {
  322. mDrawCache.clear();
  323. }
  324. void duDebugDrawTorque::render(SceneRenderState* state)
  325. {
  326. const Frustum& frustum = state->getCameraFrustum();
  327. for (U32 i = 0; i < mDrawCache.size(); ++i)
  328. {
  329. const CachedDraw& draw = mDrawCache[i];
  330. if (!frustum.getBounds().isOverlapped(draw.bounds))
  331. continue;
  332. GFX->setPrimitiveBuffer(draw.primitiveBuffer);
  333. GFX->setStateBlockByDesc(draw.state);
  334. GFX->setupGenericShaders(GFXDevice::GSColor);
  335. GFX->setVertexBuffer(draw.buffer);
  336. GFX->drawIndexedPrimitive(
  337. draw.primType,
  338. 0, // start vertex
  339. 0, // min vertex index
  340. draw.vertexCount, // vertex count
  341. 0, // start index
  342. draw.primitiveCount // primitive count
  343. );
  344. }
  345. }
  346. void duDebugDrawTorque::immediateRender()
  347. {
  348. for (U32 i = 0; i < mDrawCache.size(); ++i)
  349. {
  350. const CachedDraw& draw = mDrawCache[i];
  351. GFX->setPrimitiveBuffer(draw.primitiveBuffer);
  352. GFX->setStateBlockByDesc(draw.state);
  353. GFX->setupGenericShaders(GFXDevice::GSColor);
  354. GFX->setVertexBuffer(draw.buffer);
  355. GFX->drawIndexedPrimitive(
  356. draw.primType,
  357. 0, // start vertex
  358. 0, // min vertex index
  359. draw.vertexCount, // vertex count
  360. 0, // start index
  361. draw.primitiveCount // primitive count
  362. );
  363. }
  364. }