DetourDebugDraw.cpp 23 KB

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