duDebugDrawTorque.cpp 14 KB

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