DetourDebugDraw.cpp 23 KB

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