DetourDebugDraw.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <math.h>
  19. #include "DebugDraw.h"
  20. #include "DetourDebugDraw.h"
  21. #include "DetourNavMesh.h"
  22. #include "DetourCommon.h"
  23. #include "DetourNode.h"
  24. static float distancePtLine2d(const float* pt, const float* p, const float* q)
  25. {
  26. float pqx = q[0] - p[0];
  27. float pqz = q[2] - p[2];
  28. float dx = pt[0] - p[0];
  29. float dz = pt[2] - p[2];
  30. float d = pqx*pqx + pqz*pqz;
  31. float t = pqx*dx + pqz*dz;
  32. if (d != 0) t /= d;
  33. dx = p[0] + t*pqx - pt[0];
  34. dz = p[2] + t*pqz - pt[2];
  35. return dx*dx + dz*dz;
  36. }
  37. static void drawPolyBoundaries(duDebugDraw* dd, const dtMeshTile* tile,
  38. const unsigned int col, const float linew,
  39. bool inner)
  40. {
  41. static const float thr = 0.01f*0.01f;
  42. dd->begin(DU_DRAW_LINES, linew);
  43. for (int i = 0; i < tile->header->polyCount; ++i)
  44. {
  45. const dtPoly* p = &tile->polys[i];
  46. if (p->getType() == DT_POLYTYPE_OFFMESH_CONNECTION) continue;
  47. const dtPolyDetail* pd = &tile->detailMeshes[i];
  48. for (int j = 0, nj = (int)p->vertCount; j < nj; ++j)
  49. {
  50. unsigned int c = col;
  51. if (inner)
  52. {
  53. if (p->neis[j] == 0) continue;
  54. if (p->neis[j] & DT_EXT_LINK)
  55. {
  56. bool con = false;
  57. for (unsigned int k = p->firstLink; k != DT_NULL_LINK; k = tile->links[k].next)
  58. {
  59. if (tile->links[k].edge == j)
  60. {
  61. con = true;
  62. break;
  63. }
  64. }
  65. if (con)
  66. c = duRGBA(255,255,255,48);
  67. else
  68. c = duRGBA(0,0,0,48);
  69. }
  70. else
  71. c = duRGBA(0,48,64,32);
  72. }
  73. else
  74. {
  75. if (p->neis[j] != 0) continue;
  76. }
  77. const float* v0 = &tile->verts[p->verts[j]*3];
  78. const float* v1 = &tile->verts[p->verts[(j+1) % nj]*3];
  79. // Draw detail mesh edges which align with the actual poly edge.
  80. // This is really slow.
  81. for (int k = 0; k < pd->triCount; ++k)
  82. {
  83. const unsigned char* t = &tile->detailTris[(pd->triBase+k)*4];
  84. const float* tv[3];
  85. for (int m = 0; m < 3; ++m)
  86. {
  87. if (t[m] < p->vertCount)
  88. tv[m] = &tile->verts[p->verts[t[m]]*3];
  89. else
  90. tv[m] = &tile->detailVerts[(pd->vertBase+(t[m]-p->vertCount))*3];
  91. }
  92. for (int m = 0, n = 2; m < 3; n=m++)
  93. {
  94. if (((t[3] >> (n*2)) & 0x3) == 0) continue; // Skip inner detail edges.
  95. if (distancePtLine2d(tv[n],v0,v1) < thr &&
  96. distancePtLine2d(tv[m],v0,v1) < thr)
  97. {
  98. dd->vertex(tv[n], c);
  99. dd->vertex(tv[m], c);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. dd->end();
  106. }
  107. static void drawMeshTile(duDebugDraw* dd, const dtNavMesh& mesh, const dtNavMeshQuery* query,
  108. const dtMeshTile* tile, unsigned char flags)
  109. {
  110. dtPolyRef base = mesh.getPolyRefBase(tile);
  111. int tileNum = mesh.decodePolyIdTile(base);
  112. dd->depthMask(false);
  113. dd->begin(DU_DRAW_TRIS);
  114. for (int i = 0; i < tile->header->polyCount; ++i)
  115. {
  116. const dtPoly* p = &tile->polys[i];
  117. if (p->getType() == DT_POLYTYPE_OFFMESH_CONNECTION) // Skip off-mesh links.
  118. continue;
  119. const dtPolyDetail* pd = &tile->detailMeshes[i];
  120. unsigned int col;
  121. if (query && query->isInClosedList(base | (dtPolyRef)i))
  122. col = duRGBA(255,196,0,64);
  123. else
  124. {
  125. if (flags & DU_DRAWNAVMESH_COLOR_TILES)
  126. {
  127. col = duIntToCol(tileNum, 128);
  128. }
  129. else
  130. {
  131. if (p->getArea() == 0) // Treat zero area type as default.
  132. col = duRGBA(0,192,255,64);
  133. else
  134. col = duIntToCol(p->getArea(), 64);
  135. }
  136. }
  137. for (int j = 0; j < pd->triCount; ++j)
  138. {
  139. const unsigned char* t = &tile->detailTris[(pd->triBase+j)*4];
  140. for (int k = 0; k < 3; ++k)
  141. {
  142. if (t[k] < p->vertCount)
  143. dd->vertex(&tile->verts[p->verts[t[k]]*3], col);
  144. else
  145. dd->vertex(&tile->detailVerts[(pd->vertBase+t[k]-p->vertCount)*3], col);
  146. }
  147. }
  148. }
  149. dd->end();
  150. // Draw inter poly boundaries
  151. drawPolyBoundaries(dd, tile, duRGBA(0,48,64,32), 1.5f, true);
  152. // Draw outer poly boundaries
  153. drawPolyBoundaries(dd, tile, duRGBA(0,48,64,220), 2.5f, false);
  154. if (flags & DU_DRAWNAVMESH_OFFMESHCONS)
  155. {
  156. dd->begin(DU_DRAW_LINES, 2.0f);
  157. for (int i = 0; i < tile->header->polyCount; ++i)
  158. {
  159. const dtPoly* p = &tile->polys[i];
  160. if (p->getType() != DT_POLYTYPE_OFFMESH_CONNECTION) // Skip regular polys.
  161. continue;
  162. unsigned int col, col2;
  163. if (query && query->isInClosedList(base | (dtPolyRef)i))
  164. col = duRGBA(255,196,0,220);
  165. else
  166. col = duDarkenCol(duIntToCol(p->getArea(), 220));
  167. const dtOffMeshConnection* con = &tile->offMeshCons[i - tile->header->offMeshBase];
  168. const float* va = &tile->verts[p->verts[0]*3];
  169. const float* vb = &tile->verts[p->verts[1]*3];
  170. // Check to see if start and end end-points have links.
  171. bool startSet = false;
  172. bool endSet = false;
  173. for (unsigned int k = p->firstLink; k != DT_NULL_LINK; k = tile->links[k].next)
  174. {
  175. if (tile->links[k].edge == 0)
  176. startSet = true;
  177. if (tile->links[k].edge == 1)
  178. endSet = true;
  179. }
  180. // End points and their on-mesh locations.
  181. dd->vertex(va[0],va[1],va[2], col);
  182. dd->vertex(con->pos[0],con->pos[1],con->pos[2], col);
  183. col2 = startSet ? col : duRGBA(220,32,16,196);
  184. duAppendCircle(dd, con->pos[0],con->pos[1]+0.1f,con->pos[2], con->rad, col2);
  185. dd->vertex(vb[0],vb[1],vb[2], col);
  186. dd->vertex(con->pos[3],con->pos[4],con->pos[5], col);
  187. col2 = endSet ? col : duRGBA(220,32,16,196);
  188. duAppendCircle(dd, con->pos[3],con->pos[4]+0.1f,con->pos[5], con->rad, col2);
  189. // End point vertices.
  190. dd->vertex(con->pos[0],con->pos[1],con->pos[2], duRGBA(0,48,64,196));
  191. dd->vertex(con->pos[0],con->pos[1]+0.2f,con->pos[2], duRGBA(0,48,64,196));
  192. dd->vertex(con->pos[3],con->pos[4],con->pos[5], duRGBA(0,48,64,196));
  193. dd->vertex(con->pos[3],con->pos[4]+0.2f,con->pos[5], duRGBA(0,48,64,196));
  194. // Connection arc.
  195. duAppendArc(dd, con->pos[0],con->pos[1],con->pos[2], con->pos[3],con->pos[4],con->pos[5], 0.25f,
  196. (con->flags & 1) ? 0.6f : 0, 0.6f, col);
  197. }
  198. dd->end();
  199. }
  200. const unsigned int vcol = duRGBA(0,0,0,196);
  201. dd->begin(DU_DRAW_POINTS, 3.0f);
  202. for (int i = 0; i < tile->header->vertCount; ++i)
  203. {
  204. const float* v = &tile->verts[i*3];
  205. dd->vertex(v[0], v[1], v[2], vcol);
  206. }
  207. dd->end();
  208. dd->depthMask(true);
  209. }
  210. void duDebugDrawNavMesh(duDebugDraw* dd, const dtNavMesh& mesh, unsigned char flags)
  211. {
  212. if (!dd) return;
  213. for (int i = 0; i < mesh.getMaxTiles(); ++i)
  214. {
  215. const dtMeshTile* tile = mesh.getTile(i);
  216. if (!tile->header) continue;
  217. drawMeshTile(dd, mesh, 0, tile, flags);
  218. }
  219. }
  220. void duDebugDrawNavMeshWithClosedList(struct duDebugDraw* dd, const dtNavMesh& mesh, const dtNavMeshQuery& query, unsigned char flags)
  221. {
  222. if (!dd) return;
  223. const dtNavMeshQuery* q = (flags & DU_DRAWNAVMESH_CLOSEDLIST) ? &query : 0;
  224. for (int i = 0; i < mesh.getMaxTiles(); ++i)
  225. {
  226. const dtMeshTile* tile = mesh.getTile(i);
  227. if (!tile->header) continue;
  228. drawMeshTile(dd, mesh, q, tile, flags);
  229. }
  230. }
  231. void duDebugDrawNavMeshNodes(struct duDebugDraw* dd, const dtNavMeshQuery& query)
  232. {
  233. if (!dd) return;
  234. const dtNodePool* pool = query.getNodePool();
  235. if (pool)
  236. {
  237. const float off = 0.5f;
  238. dd->begin(DU_DRAW_POINTS, 4.0f);
  239. for (int i = 0; i < pool->getHashSize(); ++i)
  240. {
  241. for (dtNodeIndex j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j))
  242. {
  243. const dtNode* node = pool->getNodeAtIdx(j+1);
  244. if (!node) continue;
  245. dd->vertex(node->pos[0],node->pos[1]+off,node->pos[2], duRGBA(255,192,0,255));
  246. }
  247. }
  248. dd->end();
  249. dd->begin(DU_DRAW_LINES, 2.0f);
  250. for (int i = 0; i < pool->getHashSize(); ++i)
  251. {
  252. for (dtNodeIndex j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j))
  253. {
  254. const dtNode* node = pool->getNodeAtIdx(j+1);
  255. if (!node) continue;
  256. if (!node->pidx) continue;
  257. const dtNode* parent = pool->getNodeAtIdx(node->pidx);
  258. if (!parent) continue;
  259. dd->vertex(node->pos[0],node->pos[1]+off,node->pos[2], duRGBA(255,192,0,128));
  260. dd->vertex(parent->pos[0],parent->pos[1]+off,parent->pos[2], duRGBA(255,192,0,128));
  261. }
  262. }
  263. dd->end();
  264. }
  265. }
  266. static void drawMeshTileBVTree(duDebugDraw* dd, const dtMeshTile* tile)
  267. {
  268. // Draw BV nodes.
  269. const float cs = 1.0f / tile->header->bvQuantFactor;
  270. dd->begin(DU_DRAW_LINES, 1.0f);
  271. for (int i = 0; i < tile->header->bvNodeCount; ++i)
  272. {
  273. const dtBVNode* n = &tile->bvTree[i];
  274. if (n->i < 0) // Leaf indices are positive.
  275. continue;
  276. duAppendBoxWire(dd, tile->header->bmin[0] + n->bmin[0]*cs,
  277. tile->header->bmin[1] + n->bmin[1]*cs,
  278. tile->header->bmin[2] + n->bmin[2]*cs,
  279. tile->header->bmin[0] + n->bmax[0]*cs,
  280. tile->header->bmin[1] + n->bmax[1]*cs,
  281. tile->header->bmin[2] + n->bmax[2]*cs,
  282. duRGBA(255,255,255,128));
  283. }
  284. dd->end();
  285. }
  286. void duDebugDrawNavMeshBVTree(duDebugDraw* dd, const dtNavMesh& mesh)
  287. {
  288. if (!dd) return;
  289. for (int i = 0; i < mesh.getMaxTiles(); ++i)
  290. {
  291. const dtMeshTile* tile = mesh.getTile(i);
  292. if (!tile->header) continue;
  293. drawMeshTileBVTree(dd, tile);
  294. }
  295. }
  296. static void drawMeshTilePortal(duDebugDraw* dd, const dtMeshTile* tile)
  297. {
  298. // Draw portals
  299. const float padx = 0.04f;
  300. const float pady = tile->header->walkableClimb;
  301. dd->begin(DU_DRAW_LINES, 2.0f);
  302. for (int side = 0; side < 8; ++side)
  303. {
  304. unsigned short m = DT_EXT_LINK | (unsigned short)side;
  305. for (int i = 0; i < tile->header->polyCount; ++i)
  306. {
  307. dtPoly* poly = &tile->polys[i];
  308. // Create new links.
  309. const int nv = poly->vertCount;
  310. for (int j = 0; j < nv; ++j)
  311. {
  312. // Skip edges which do not point to the right side.
  313. if (poly->neis[j] != m)
  314. continue;
  315. // Create new links
  316. const float* va = &tile->verts[poly->verts[j]*3];
  317. const float* vb = &tile->verts[poly->verts[(j+1) % nv]*3];
  318. if (side == 0 || side == 4)
  319. {
  320. unsigned int col = side == 0 ? duRGBA(128,0,0,128) : duRGBA(128,0,128,128);
  321. const float x = va[0] + ((side == 0) ? -padx : padx);
  322. dd->vertex(x,va[1]-pady,va[2], col);
  323. dd->vertex(x,va[1]+pady,va[2], col);
  324. dd->vertex(x,va[1]+pady,va[2], col);
  325. dd->vertex(x,vb[1]+pady,vb[2], col);
  326. dd->vertex(x,vb[1]+pady,vb[2], col);
  327. dd->vertex(x,vb[1]-pady,vb[2], col);
  328. dd->vertex(x,vb[1]-pady,vb[2], col);
  329. dd->vertex(x,va[1]-pady,va[2], col);
  330. }
  331. else if (side == 2 || side == 6)
  332. {
  333. unsigned int col = side == 2 ? duRGBA(0,128,0,128) : duRGBA(0,128,128,128);
  334. const float z = va[2] + ((side == 2) ? -padx : padx);
  335. dd->vertex(va[0],va[1]-pady,z, col);
  336. dd->vertex(va[0],va[1]+pady,z, col);
  337. dd->vertex(va[0],va[1]+pady,z, col);
  338. dd->vertex(vb[0],vb[1]+pady,z, col);
  339. dd->vertex(vb[0],vb[1]+pady,z, col);
  340. dd->vertex(vb[0],vb[1]-pady,z, col);
  341. dd->vertex(vb[0],vb[1]-pady,z, col);
  342. dd->vertex(va[0],va[1]-pady,z, col);
  343. }
  344. }
  345. }
  346. }
  347. dd->end();
  348. }
  349. void duDebugDrawNavMeshPortals(duDebugDraw* dd, const dtNavMesh& mesh)
  350. {
  351. if (!dd) return;
  352. for (int i = 0; i < mesh.getMaxTiles(); ++i)
  353. {
  354. const dtMeshTile* tile = mesh.getTile(i);
  355. if (!tile->header) continue;
  356. drawMeshTilePortal(dd, tile);
  357. }
  358. }
  359. void duDebugDrawNavMeshPolysWithFlags(struct duDebugDraw* dd, const dtNavMesh& mesh,
  360. const unsigned short polyFlags, const unsigned int col)
  361. {
  362. if (!dd) return;
  363. for (int i = 0; i < mesh.getMaxTiles(); ++i)
  364. {
  365. const dtMeshTile* tile = mesh.getTile(i);
  366. if (!tile->header) continue;
  367. dtPolyRef base = mesh.getPolyRefBase(tile);
  368. for (int j = 0; j < tile->header->polyCount; ++j)
  369. {
  370. const dtPoly* p = &tile->polys[j];
  371. if ((p->flags & polyFlags) == 0) continue;
  372. duDebugDrawNavMeshPoly(dd, mesh, base|(dtPolyRef)j, col);
  373. }
  374. }
  375. }
  376. void duDebugDrawNavMeshPoly(duDebugDraw* dd, const dtNavMesh& mesh, dtPolyRef ref, const unsigned int col)
  377. {
  378. if (!dd) return;
  379. const dtMeshTile* tile = 0;
  380. const dtPoly* poly = 0;
  381. if (dtStatusFailed(mesh.getTileAndPolyByRef(ref, &tile, &poly)))
  382. return;
  383. dd->depthMask(false);
  384. const unsigned int c = (col & 0x00ffffff) | (64 << 24);
  385. const unsigned int ip = (unsigned int)(poly - tile->polys);
  386. if (poly->getType() == DT_POLYTYPE_OFFMESH_CONNECTION)
  387. {
  388. dtOffMeshConnection* con = &tile->offMeshCons[ip - tile->header->offMeshBase];
  389. dd->begin(DU_DRAW_LINES, 2.0f);
  390. // Connection arc.
  391. duAppendArc(dd, con->pos[0],con->pos[1],con->pos[2], con->pos[3],con->pos[4],con->pos[5], 0.25f,
  392. (con->flags & 1) ? 0.6f : 0, 0.6f, c);
  393. dd->end();
  394. }
  395. else
  396. {
  397. const dtPolyDetail* pd = &tile->detailMeshes[ip];
  398. dd->begin(DU_DRAW_TRIS);
  399. for (int i = 0; i < pd->triCount; ++i)
  400. {
  401. const unsigned char* t = &tile->detailTris[(pd->triBase+i)*4];
  402. for (int j = 0; j < 3; ++j)
  403. {
  404. if (t[j] < poly->vertCount)
  405. dd->vertex(&tile->verts[poly->verts[t[j]]*3], c);
  406. else
  407. dd->vertex(&tile->detailVerts[(pd->vertBase+t[j]-poly->vertCount)*3], c);
  408. }
  409. }
  410. dd->end();
  411. }
  412. dd->depthMask(true);
  413. }
  414. static void debugDrawTileCachePortals(struct duDebugDraw* dd, const dtTileCacheLayer& layer, const float cs, const float ch)
  415. {
  416. const int w = (int)layer.header->width;
  417. const int h = (int)layer.header->height;
  418. const float* bmin = layer.header->bmin;
  419. // Portals
  420. unsigned int pcol = duRGBA(255,255,255,255);
  421. const int segs[4*4] = {0,0,0,1, 0,1,1,1, 1,1,1,0, 1,0,0,0};
  422. // Layer portals
  423. dd->begin(DU_DRAW_LINES, 2.0f);
  424. for (int y = 0; y < h; ++y)
  425. {
  426. for (int x = 0; x < w; ++x)
  427. {
  428. const int idx = x+y*w;
  429. const int lh = (int)layer.heights[idx];
  430. if (lh == 0xff) continue;
  431. for (int dir = 0; dir < 4; ++dir)
  432. {
  433. if (layer.cons[idx] & (1<<(dir+4)))
  434. {
  435. const int* seg = &segs[dir*4];
  436. const float ax = bmin[0] + (x+seg[0])*cs;
  437. const float ay = bmin[1] + (lh+2)*ch;
  438. const float az = bmin[2] + (y+seg[1])*cs;
  439. const float bx = bmin[0] + (x+seg[2])*cs;
  440. const float by = bmin[1] + (lh+2)*ch;
  441. const float bz = bmin[2] + (y+seg[3])*cs;
  442. dd->vertex(ax, ay, az, pcol);
  443. dd->vertex(bx, by, bz, pcol);
  444. }
  445. }
  446. }
  447. }
  448. dd->end();
  449. }
  450. void duDebugDrawTileCacheLayerAreas(struct duDebugDraw* dd, const dtTileCacheLayer& layer, const float cs, const float ch)
  451. {
  452. const int w = (int)layer.header->width;
  453. const int h = (int)layer.header->height;
  454. const float* bmin = layer.header->bmin;
  455. const float* bmax = layer.header->bmax;
  456. const int idx = layer.header->tlayer;
  457. unsigned int color = duIntToCol(idx+1, 255);
  458. // Layer bounds
  459. float lbmin[3], lbmax[3];
  460. lbmin[0] = bmin[0] + layer.header->minx*cs;
  461. lbmin[1] = bmin[1];
  462. lbmin[2] = bmin[2] + layer.header->miny*cs;
  463. lbmax[0] = bmin[0] + (layer.header->maxx+1)*cs;
  464. lbmax[1] = bmax[1];
  465. lbmax[2] = bmin[2] + (layer.header->maxy+1)*cs;
  466. duDebugDrawBoxWire(dd, lbmin[0],lbmin[1],lbmin[2], lbmax[0],lbmax[1],lbmax[2], duTransCol(color,128), 2.0f);
  467. // Layer height
  468. dd->begin(DU_DRAW_QUADS);
  469. for (int y = 0; y < h; ++y)
  470. {
  471. for (int x = 0; x < w; ++x)
  472. {
  473. const int lidx = x+y*w;
  474. const int lh = (int)layer.heights[lidx];
  475. if (lh == 0xff) continue;
  476. const unsigned char area = layer.areas[lidx];
  477. unsigned int col;
  478. if (area == 63)
  479. col = duLerpCol(color, duRGBA(0,192,255,64), 32);
  480. else if (area == 0)
  481. col = duLerpCol(color, duRGBA(0,0,0,64), 32);
  482. else
  483. col = duLerpCol(color, duIntToCol(area, 255), 32);
  484. const float fx = bmin[0] + x*cs;
  485. const float fy = bmin[1] + (lh+1)*ch;
  486. const float fz = bmin[2] + y*cs;
  487. dd->vertex(fx, fy, fz, col);
  488. dd->vertex(fx, fy, fz+cs, col);
  489. dd->vertex(fx+cs, fy, fz+cs, col);
  490. dd->vertex(fx+cs, fy, fz, col);
  491. }
  492. }
  493. dd->end();
  494. debugDrawTileCachePortals(dd, layer, cs, ch);
  495. }
  496. void duDebugDrawTileCacheLayerRegions(struct duDebugDraw* dd, const dtTileCacheLayer& layer, const float cs, const float ch)
  497. {
  498. const int w = (int)layer.header->width;
  499. const int h = (int)layer.header->height;
  500. const float* bmin = layer.header->bmin;
  501. const float* bmax = layer.header->bmax;
  502. const int idx = layer.header->tlayer;
  503. unsigned int color = duIntToCol(idx+1, 255);
  504. // Layer bounds
  505. float lbmin[3], lbmax[3];
  506. lbmin[0] = bmin[0] + layer.header->minx*cs;
  507. lbmin[1] = bmin[1];
  508. lbmin[2] = bmin[2] + layer.header->miny*cs;
  509. lbmax[0] = bmin[0] + (layer.header->maxx+1)*cs;
  510. lbmax[1] = bmax[1];
  511. lbmax[2] = bmin[2] + (layer.header->maxy+1)*cs;
  512. duDebugDrawBoxWire(dd, lbmin[0],lbmin[1],lbmin[2], lbmax[0],lbmax[1],lbmax[2], duTransCol(color,128), 2.0f);
  513. // Layer height
  514. dd->begin(DU_DRAW_QUADS);
  515. for (int y = 0; y < h; ++y)
  516. {
  517. for (int x = 0; x < w; ++x)
  518. {
  519. const int lidx = x+y*w;
  520. const int lh = (int)layer.heights[lidx];
  521. if (lh == 0xff) continue;
  522. const unsigned char reg = layer.regs[lidx];
  523. unsigned int col = duLerpCol(color, duIntToCol(reg, 255), 192);
  524. const float fx = bmin[0] + x*cs;
  525. const float fy = bmin[1] + (lh+1)*ch;
  526. const float fz = bmin[2] + y*cs;
  527. dd->vertex(fx, fy, fz, col);
  528. dd->vertex(fx, fy, fz+cs, col);
  529. dd->vertex(fx+cs, fy, fz+cs, col);
  530. dd->vertex(fx+cs, fy, fz, col);
  531. }
  532. }
  533. dd->end();
  534. debugDrawTileCachePortals(dd, layer, cs, ch);
  535. }
  536. /*struct dtTileCacheContour
  537. {
  538. int nverts;
  539. unsigned char* verts;
  540. unsigned char reg;
  541. unsigned char area;
  542. };
  543. struct dtTileCacheContourSet
  544. {
  545. int nconts;
  546. dtTileCacheContour* conts;
  547. };*/
  548. void duDebugDrawTileCacheContours(duDebugDraw* dd, const struct dtTileCacheContourSet& lcset,
  549. const float* orig, const float cs, const float ch)
  550. {
  551. if (!dd) return;
  552. const unsigned char a = 255;// (unsigned char)(alpha*255.0f);
  553. const int offs[2*4] = {-1,0, 0,1, 1,0, 0,-1};
  554. dd->begin(DU_DRAW_LINES, 2.0f);
  555. for (int i = 0; i < lcset.nconts; ++i)
  556. {
  557. const dtTileCacheContour& c = lcset.conts[i];
  558. unsigned int color = 0;
  559. color = duIntToCol(i, a);
  560. for (int j = 0; j < c.nverts; ++j)
  561. {
  562. const int k = (j+1) % c.nverts;
  563. const unsigned char* va = &c.verts[j*4];
  564. const unsigned char* vb = &c.verts[k*4];
  565. const float ax = orig[0] + va[0]*cs;
  566. const float ay = orig[1] + (va[1]+1+(i&1))*ch;
  567. const float az = orig[2] + va[2]*cs;
  568. const float bx = orig[0] + vb[0]*cs;
  569. const float by = orig[1] + (vb[1]+1+(i&1))*ch;
  570. const float bz = orig[2] + vb[2]*cs;
  571. unsigned int col = color;
  572. if ((va[3] & 0xf) != 0xf)
  573. {
  574. // Portal segment
  575. col = duRGBA(255,255,255,128);
  576. int d = va[3] & 0xf;
  577. const float cx = (ax+bx)*0.5f;
  578. const float cy = (ay+by)*0.5f;
  579. const float cz = (az+bz)*0.5f;
  580. const float dx = cx + offs[d*2+0]*2*cs;
  581. const float dy = cy;
  582. const float dz = cz + offs[d*2+1]*2*cs;
  583. dd->vertex(cx,cy,cz,duRGBA(255,0,0,255));
  584. dd->vertex(dx,dy,dz,duRGBA(255,0,0,255));
  585. }
  586. duAppendArrow(dd, ax,ay,az, bx,by,bz, 0.0f, cs*0.5f, col);
  587. }
  588. }
  589. dd->end();
  590. dd->begin(DU_DRAW_POINTS, 4.0f);
  591. for (int i = 0; i < lcset.nconts; ++i)
  592. {
  593. const dtTileCacheContour& c = lcset.conts[i];
  594. unsigned int color = 0;
  595. for (int j = 0; j < c.nverts; ++j)
  596. {
  597. const unsigned char* va = &c.verts[j*4];
  598. color = duDarkenCol(duIntToCol(i, a));
  599. if (va[3] & 0x80)
  600. {
  601. // Border vertex
  602. color = duRGBA(255,0,0,255);
  603. }
  604. float fx = orig[0] + va[0]*cs;
  605. float fy = orig[1] + (va[1]+1+(i&1))*ch;
  606. float fz = orig[2] + va[2]*cs;
  607. dd->vertex(fx,fy,fz, color);
  608. }
  609. }
  610. dd->end();
  611. }
  612. void duDebugDrawTileCachePolyMesh(duDebugDraw* dd, const struct dtTileCachePolyMesh& lmesh,
  613. const float* orig, const float cs, const float ch)
  614. {
  615. if (!dd) return;
  616. const int nvp = lmesh.nvp;
  617. const int offs[2*4] = {-1,0, 0,1, 1,0, 0,-1};
  618. dd->begin(DU_DRAW_TRIS);
  619. for (int i = 0; i < lmesh.npolys; ++i)
  620. {
  621. const unsigned short* p = &lmesh.polys[i*nvp*2];
  622. unsigned int color;
  623. if (lmesh.areas[i] == DT_TILECACHE_WALKABLE_AREA)
  624. color = duRGBA(0,192,255,64);
  625. else if (lmesh.areas[i] == DT_TILECACHE_NULL_AREA)
  626. color = duRGBA(0,0,0,64);
  627. else
  628. color = duIntToCol(lmesh.areas[i], 255);
  629. unsigned short vi[3];
  630. for (int j = 2; j < nvp; ++j)
  631. {
  632. if (p[j] == DT_TILECACHE_NULL_IDX) break;
  633. vi[0] = p[0];
  634. vi[1] = p[j-1];
  635. vi[2] = p[j];
  636. for (int k = 0; k < 3; ++k)
  637. {
  638. const unsigned short* v = &lmesh.verts[vi[k]*3];
  639. const float x = orig[0] + v[0]*cs;
  640. const float y = orig[1] + (v[1]+1)*ch;
  641. const float z = orig[2] + v[2]*cs;
  642. dd->vertex(x,y,z, color);
  643. }
  644. }
  645. }
  646. dd->end();
  647. // Draw neighbours edges
  648. const unsigned int coln = duRGBA(0,48,64,32);
  649. dd->begin(DU_DRAW_LINES, 1.5f);
  650. for (int i = 0; i < lmesh.npolys; ++i)
  651. {
  652. const unsigned short* p = &lmesh.polys[i*nvp*2];
  653. for (int j = 0; j < nvp; ++j)
  654. {
  655. if (p[j] == DT_TILECACHE_NULL_IDX) break;
  656. if (p[nvp+j] & 0x8000) continue;
  657. const int nj = (j+1 >= nvp || p[j+1] == DT_TILECACHE_NULL_IDX) ? 0 : j+1;
  658. int vi[2] = {p[j], p[nj]};
  659. for (int k = 0; k < 2; ++k)
  660. {
  661. const unsigned short* v = &lmesh.verts[vi[k]*3];
  662. const float x = orig[0] + v[0]*cs;
  663. const float y = orig[1] + (v[1]+1)*ch + 0.1f;
  664. const float z = orig[2] + v[2]*cs;
  665. dd->vertex(x, y, z, coln);
  666. }
  667. }
  668. }
  669. dd->end();
  670. // Draw boundary edges
  671. const unsigned int colb = duRGBA(0,48,64,220);
  672. dd->begin(DU_DRAW_LINES, 2.5f);
  673. for (int i = 0; i < lmesh.npolys; ++i)
  674. {
  675. const unsigned short* p = &lmesh.polys[i*nvp*2];
  676. for (int j = 0; j < nvp; ++j)
  677. {
  678. if (p[j] == DT_TILECACHE_NULL_IDX) break;
  679. if ((p[nvp+j] & 0x8000) == 0) continue;
  680. const int nj = (j+1 >= nvp || p[j+1] == DT_TILECACHE_NULL_IDX) ? 0 : j+1;
  681. int vi[2] = {p[j], p[nj]};
  682. unsigned int col = colb;
  683. if ((p[nvp+j] & 0xf) != 0xf)
  684. {
  685. const unsigned short* va = &lmesh.verts[vi[0]*3];
  686. const unsigned short* vb = &lmesh.verts[vi[1]*3];
  687. const float ax = orig[0] + va[0]*cs;
  688. const float ay = orig[1] + (va[1]+1+(i&1))*ch;
  689. const float az = orig[2] + va[2]*cs;
  690. const float bx = orig[0] + vb[0]*cs;
  691. const float by = orig[1] + (vb[1]+1+(i&1))*ch;
  692. const float bz = orig[2] + vb[2]*cs;
  693. const float cx = (ax+bx)*0.5f;
  694. const float cy = (ay+by)*0.5f;
  695. const float cz = (az+bz)*0.5f;
  696. int d = p[nvp+j] & 0xf;
  697. const float dx = cx + offs[d*2+0]*2*cs;
  698. const float dy = cy;
  699. const float dz = cz + offs[d*2+1]*2*cs;
  700. dd->vertex(cx,cy,cz,duRGBA(255,0,0,255));
  701. dd->vertex(dx,dy,dz,duRGBA(255,0,0,255));
  702. col = duRGBA(255,255,255,128);
  703. }
  704. for (int k = 0; k < 2; ++k)
  705. {
  706. const unsigned short* v = &lmesh.verts[vi[k]*3];
  707. const float x = orig[0] + v[0]*cs;
  708. const float y = orig[1] + (v[1]+1)*ch + 0.1f;
  709. const float z = orig[2] + v[2]*cs;
  710. dd->vertex(x, y, z, col);
  711. }
  712. }
  713. }
  714. dd->end();
  715. dd->begin(DU_DRAW_POINTS, 3.0f);
  716. const unsigned int colv = duRGBA(0,0,0,220);
  717. for (int i = 0; i < lmesh.nverts; ++i)
  718. {
  719. const unsigned short* v = &lmesh.verts[i*3];
  720. const float x = orig[0] + v[0]*cs;
  721. const float y = orig[1] + (v[1]+1)*ch + 0.1f;
  722. const float z = orig[2] + v[2]*cs;
  723. dd->vertex(x,y,z, colv);
  724. }
  725. dd->end();
  726. }