duDebugDrawTorque.cpp 14 KB

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