RecastDebugDraw.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. #define _USE_MATH_DEFINES
  19. #include <math.h>
  20. #include "DebugDraw.h"
  21. #include "RecastDebugDraw.h"
  22. #include "Recast.h"
  23. void duDebugDrawTriMesh(duDebugDraw* dd, const float* verts, int /*nverts*/,
  24. const int* tris, const float* normals, int ntris,
  25. const unsigned char* flags, const float texScale)
  26. {
  27. if (!dd) return;
  28. if (!verts) return;
  29. if (!tris) return;
  30. if (!normals) return;
  31. float uva[2];
  32. float uvb[2];
  33. float uvc[2];
  34. const unsigned int unwalkable = duRGBA(192,128,0,255);
  35. dd->texture(true);
  36. dd->begin(DU_DRAW_TRIS);
  37. for (int i = 0; i < ntris*3; i += 3)
  38. {
  39. const float* norm = &normals[i];
  40. unsigned int color;
  41. unsigned char a = (unsigned char)(220*(2+norm[0]+norm[1])/4);
  42. if (flags && !flags[i/3])
  43. color = duLerpCol(duRGBA(a,a,a,255), unwalkable, 64);
  44. else
  45. color = duRGBA(a,a,a,255);
  46. const float* va = &verts[tris[i+0]*3];
  47. const float* vb = &verts[tris[i+1]*3];
  48. const float* vc = &verts[tris[i+2]*3];
  49. int ax = 0, ay = 0;
  50. if (rcAbs(norm[1]) > rcAbs(norm[ax]))
  51. ax = 1;
  52. if (rcAbs(norm[2]) > rcAbs(norm[ax]))
  53. ax = 2;
  54. ax = (1<<ax)&3; // +1 mod 3
  55. ay = (1<<ax)&3; // +1 mod 3
  56. uva[0] = va[ax]*texScale;
  57. uva[1] = va[ay]*texScale;
  58. uvb[0] = vb[ax]*texScale;
  59. uvb[1] = vb[ay]*texScale;
  60. uvc[0] = vc[ax]*texScale;
  61. uvc[1] = vc[ay]*texScale;
  62. dd->vertex(va, color, uva);
  63. dd->vertex(vb, color, uvb);
  64. dd->vertex(vc, color, uvc);
  65. }
  66. dd->end();
  67. dd->texture(false);
  68. }
  69. void duDebugDrawTriMeshSlope(duDebugDraw* dd, const float* verts, int /*nverts*/,
  70. const int* tris, const float* normals, int ntris,
  71. const float walkableSlopeAngle, const float texScale)
  72. {
  73. if (!dd) return;
  74. if (!verts) return;
  75. if (!tris) return;
  76. if (!normals) return;
  77. const float walkableThr = cosf(walkableSlopeAngle/180.0f*DU_PI);
  78. float uva[2];
  79. float uvb[2];
  80. float uvc[2];
  81. dd->texture(true);
  82. const unsigned int unwalkable = duRGBA(192,128,0,255);
  83. dd->begin(DU_DRAW_TRIS);
  84. for (int i = 0; i < ntris*3; i += 3)
  85. {
  86. const float* norm = &normals[i];
  87. unsigned int color;
  88. unsigned char a = (unsigned char)(220*(2+norm[0]+norm[1])/4);
  89. if (norm[1] < walkableThr)
  90. color = duLerpCol(duRGBA(a,a,a,255), unwalkable, 64);
  91. else
  92. color = duRGBA(a,a,a,255);
  93. const float* va = &verts[tris[i+0]*3];
  94. const float* vb = &verts[tris[i+1]*3];
  95. const float* vc = &verts[tris[i+2]*3];
  96. int ax = 0, ay = 0;
  97. if (rcAbs(norm[1]) > rcAbs(norm[ax]))
  98. ax = 1;
  99. if (rcAbs(norm[2]) > rcAbs(norm[ax]))
  100. ax = 2;
  101. ax = (1<<ax)&3; // +1 mod 3
  102. ay = (1<<ax)&3; // +1 mod 3
  103. uva[0] = va[ax]*texScale;
  104. uva[1] = va[ay]*texScale;
  105. uvb[0] = vb[ax]*texScale;
  106. uvb[1] = vb[ay]*texScale;
  107. uvc[0] = vc[ax]*texScale;
  108. uvc[1] = vc[ay]*texScale;
  109. dd->vertex(va, color, uva);
  110. dd->vertex(vb, color, uvb);
  111. dd->vertex(vc, color, uvc);
  112. }
  113. dd->end();
  114. dd->texture(false);
  115. }
  116. void duDebugDrawHeightfieldSolid(duDebugDraw* dd, const rcHeightfield& hf)
  117. {
  118. if (!dd) return;
  119. const float* orig = hf.bmin;
  120. const float cs = hf.cs;
  121. const float ch = hf.ch;
  122. const int w = hf.width;
  123. const int h = hf.height;
  124. unsigned int fcol[6];
  125. duCalcBoxColors(fcol, duRGBA(255,255,255,255), duRGBA(255,255,255,255));
  126. dd->begin(DU_DRAW_QUADS);
  127. for (int y = 0; y < h; ++y)
  128. {
  129. for (int x = 0; x < w; ++x)
  130. {
  131. float fx = orig[0] + x*cs;
  132. float fz = orig[2] + y*cs;
  133. const rcSpan* s = hf.spans[x + y*w];
  134. while (s)
  135. {
  136. duAppendBox(dd, fx, orig[1]+s->smin*ch, fz, fx+cs, orig[1] + s->smax*ch, fz+cs, fcol);
  137. s = s->next;
  138. }
  139. }
  140. }
  141. dd->end();
  142. }
  143. void duDebugDrawHeightfieldWalkable(duDebugDraw* dd, const rcHeightfield& hf)
  144. {
  145. if (!dd) return;
  146. const float* orig = hf.bmin;
  147. const float cs = hf.cs;
  148. const float ch = hf.ch;
  149. const int w = hf.width;
  150. const int h = hf.height;
  151. unsigned int fcol[6];
  152. duCalcBoxColors(fcol, duRGBA(255,255,255,255), duRGBA(217,217,217,255));
  153. dd->begin(DU_DRAW_QUADS);
  154. for (int y = 0; y < h; ++y)
  155. {
  156. for (int x = 0; x < w; ++x)
  157. {
  158. float fx = orig[0] + x*cs;
  159. float fz = orig[2] + y*cs;
  160. const rcSpan* s = hf.spans[x + y*w];
  161. while (s)
  162. {
  163. if (s->area == RC_WALKABLE_AREA)
  164. fcol[0] = duRGBA(64,128,160,255);
  165. else if (s->area == RC_NULL_AREA)
  166. fcol[0] = duRGBA(64,64,64,255);
  167. else
  168. fcol[0] = duMultCol(dd->areaToCol(s->area), 200);
  169. duAppendBox(dd, fx, orig[1]+s->smin*ch, fz, fx+cs, orig[1] + s->smax*ch, fz+cs, fcol);
  170. s = s->next;
  171. }
  172. }
  173. }
  174. dd->end();
  175. }
  176. void duDebugDrawCompactHeightfieldSolid(duDebugDraw* dd, const rcCompactHeightfield& chf)
  177. {
  178. if (!dd) return;
  179. const float cs = chf.cs;
  180. const float ch = chf.ch;
  181. dd->begin(DU_DRAW_QUADS);
  182. for (int y = 0; y < chf.height; ++y)
  183. {
  184. for (int x = 0; x < chf.width; ++x)
  185. {
  186. const float fx = chf.bmin[0] + x*cs;
  187. const float fz = chf.bmin[2] + y*cs;
  188. const rcCompactCell& c = chf.cells[x+y*chf.width];
  189. for (unsigned i = c.index, ni = c.index+c.count; i < ni; ++i)
  190. {
  191. const rcCompactSpan& s = chf.spans[i];
  192. const unsigned char area = chf.areas[i];
  193. unsigned int color;
  194. if (area == RC_WALKABLE_AREA)
  195. color = duRGBA(0,192,255,64);
  196. else if (area == RC_NULL_AREA)
  197. color = duRGBA(0,0,0,64);
  198. else
  199. color = dd->areaToCol(area);
  200. const float fy = chf.bmin[1] + (s.y+1)*ch;
  201. dd->vertex(fx, fy, fz, color);
  202. dd->vertex(fx, fy, fz+cs, color);
  203. dd->vertex(fx+cs, fy, fz+cs, color);
  204. dd->vertex(fx+cs, fy, fz, color);
  205. }
  206. }
  207. }
  208. dd->end();
  209. }
  210. void duDebugDrawCompactHeightfieldRegions(duDebugDraw* dd, const rcCompactHeightfield& chf)
  211. {
  212. if (!dd) return;
  213. const float cs = chf.cs;
  214. const float ch = chf.ch;
  215. dd->begin(DU_DRAW_QUADS);
  216. for (int y = 0; y < chf.height; ++y)
  217. {
  218. for (int x = 0; x < chf.width; ++x)
  219. {
  220. const float fx = chf.bmin[0] + x*cs;
  221. const float fz = chf.bmin[2] + y*cs;
  222. const rcCompactCell& c = chf.cells[x+y*chf.width];
  223. for (unsigned i = c.index, ni = c.index+c.count; i < ni; ++i)
  224. {
  225. const rcCompactSpan& s = chf.spans[i];
  226. const float fy = chf.bmin[1] + (s.y)*ch;
  227. unsigned int color;
  228. if (s.reg)
  229. color = duIntToCol(s.reg, 192);
  230. else
  231. color = duRGBA(0,0,0,64);
  232. dd->vertex(fx, fy, fz, color);
  233. dd->vertex(fx, fy, fz+cs, color);
  234. dd->vertex(fx+cs, fy, fz+cs, color);
  235. dd->vertex(fx+cs, fy, fz, color);
  236. }
  237. }
  238. }
  239. dd->end();
  240. }
  241. void duDebugDrawCompactHeightfieldDistance(duDebugDraw* dd, const rcCompactHeightfield& chf)
  242. {
  243. if (!dd) return;
  244. if (!chf.dist) return;
  245. const float cs = chf.cs;
  246. const float ch = chf.ch;
  247. float maxd = chf.maxDistance;
  248. if (maxd < 1.0f) maxd = 1;
  249. const float dscale = 255.0f / maxd;
  250. dd->begin(DU_DRAW_QUADS);
  251. for (int y = 0; y < chf.height; ++y)
  252. {
  253. for (int x = 0; x < chf.width; ++x)
  254. {
  255. const float fx = chf.bmin[0] + x*cs;
  256. const float fz = chf.bmin[2] + y*cs;
  257. const rcCompactCell& c = chf.cells[x+y*chf.width];
  258. for (unsigned i = c.index, ni = c.index+c.count; i < ni; ++i)
  259. {
  260. const rcCompactSpan& s = chf.spans[i];
  261. const float fy = chf.bmin[1] + (s.y+1)*ch;
  262. const unsigned char cd = (unsigned char)(chf.dist[i] * dscale);
  263. const unsigned int color = duRGBA(cd,cd,cd,255);
  264. dd->vertex(fx, fy, fz, color);
  265. dd->vertex(fx, fy, fz+cs, color);
  266. dd->vertex(fx+cs, fy, fz+cs, color);
  267. dd->vertex(fx+cs, fy, fz, color);
  268. }
  269. }
  270. }
  271. dd->end();
  272. }
  273. static void drawLayerPortals(duDebugDraw* dd, const rcHeightfieldLayer* layer)
  274. {
  275. const float cs = layer->cs;
  276. const float ch = layer->ch;
  277. const int w = layer->width;
  278. const int h = layer->height;
  279. unsigned int pcol = duRGBA(255,255,255,255);
  280. const int segs[4*4] = {0,0,0,1, 0,1,1,1, 1,1,1,0, 1,0,0,0};
  281. // Layer portals
  282. dd->begin(DU_DRAW_LINES, 2.0f);
  283. for (int y = 0; y < h; ++y)
  284. {
  285. for (int x = 0; x < w; ++x)
  286. {
  287. const int idx = x+y*w;
  288. const int lh = (int)layer->heights[idx];
  289. if (lh == 255) continue;
  290. for (int dir = 0; dir < 4; ++dir)
  291. {
  292. if (layer->cons[idx] & (1<<(dir+4)))
  293. {
  294. const int* seg = &segs[dir*4];
  295. const float ax = layer->bmin[0] + (x+seg[0])*cs;
  296. const float ay = layer->bmin[1] + (lh+2)*ch;
  297. const float az = layer->bmin[2] + (y+seg[1])*cs;
  298. const float bx = layer->bmin[0] + (x+seg[2])*cs;
  299. const float by = layer->bmin[1] + (lh+2)*ch;
  300. const float bz = layer->bmin[2] + (y+seg[3])*cs;
  301. dd->vertex(ax, ay, az, pcol);
  302. dd->vertex(bx, by, bz, pcol);
  303. }
  304. }
  305. }
  306. }
  307. dd->end();
  308. }
  309. void duDebugDrawHeightfieldLayer(duDebugDraw* dd, const struct rcHeightfieldLayer& layer, const int idx)
  310. {
  311. const float cs = layer.cs;
  312. const float ch = layer.ch;
  313. const int w = layer.width;
  314. const int h = layer.height;
  315. unsigned int color = duIntToCol(idx+1, 255);
  316. // Layer bounds
  317. float bmin[3], bmax[3];
  318. bmin[0] = layer.bmin[0] + layer.minx*cs;
  319. bmin[1] = layer.bmin[1];
  320. bmin[2] = layer.bmin[2] + layer.miny*cs;
  321. bmax[0] = layer.bmin[0] + (layer.maxx+1)*cs;
  322. bmax[1] = layer.bmax[1];
  323. bmax[2] = layer.bmin[2] + (layer.maxy+1)*cs;
  324. duDebugDrawBoxWire(dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], duTransCol(color,128), 2.0f);
  325. // Layer height
  326. dd->begin(DU_DRAW_QUADS);
  327. for (int y = 0; y < h; ++y)
  328. {
  329. for (int x = 0; x < w; ++x)
  330. {
  331. const int lidx = x+y*w;
  332. const int lh = (int)layer.heights[lidx];
  333. if (h == 0xff) continue;
  334. const unsigned char area = layer.areas[lidx];
  335. unsigned int col;
  336. if (area == RC_WALKABLE_AREA)
  337. col = duLerpCol(color, duRGBA(0,192,255,64), 32);
  338. else if (area == RC_NULL_AREA)
  339. col = duLerpCol(color, duRGBA(0,0,0,64), 32);
  340. else
  341. col = duLerpCol(color, dd->areaToCol(area), 32);
  342. const float fx = layer.bmin[0] + x*cs;
  343. const float fy = layer.bmin[1] + (lh+1)*ch;
  344. const float fz = layer.bmin[2] + y*cs;
  345. dd->vertex(fx, fy, fz, col);
  346. dd->vertex(fx, fy, fz+cs, col);
  347. dd->vertex(fx+cs, fy, fz+cs, col);
  348. dd->vertex(fx+cs, fy, fz, col);
  349. }
  350. }
  351. dd->end();
  352. // Portals
  353. drawLayerPortals(dd, &layer);
  354. }
  355. void duDebugDrawHeightfieldLayers(duDebugDraw* dd, const struct rcHeightfieldLayerSet& lset)
  356. {
  357. if (!dd) return;
  358. for (int i = 0; i < lset.nlayers; ++i)
  359. duDebugDrawHeightfieldLayer(dd, lset.layers[i], i);
  360. }
  361. /*
  362. void duDebugDrawLayerContours(duDebugDraw* dd, const struct rcLayerContourSet& lcset)
  363. {
  364. if (!dd) return;
  365. const float* orig = lcset.bmin;
  366. const float cs = lcset.cs;
  367. const float ch = lcset.ch;
  368. const unsigned char a = 255;// (unsigned char)(alpha*255.0f);
  369. const int offs[2*4] = {-1,0, 0,1, 1,0, 0,-1};
  370. dd->begin(DU_DRAW_LINES, 2.0f);
  371. for (int i = 0; i < lcset.nconts; ++i)
  372. {
  373. const rcLayerContour& c = lcset.conts[i];
  374. unsigned int color = 0;
  375. color = duIntToCol(i, a);
  376. for (int j = 0; j < c.nverts; ++j)
  377. {
  378. const int k = (j+1) % c.nverts;
  379. const unsigned char* va = &c.verts[j*4];
  380. const unsigned char* vb = &c.verts[k*4];
  381. const float ax = orig[0] + va[0]*cs;
  382. const float ay = orig[1] + (va[1]+1+(i&1))*ch;
  383. const float az = orig[2] + va[2]*cs;
  384. const float bx = orig[0] + vb[0]*cs;
  385. const float by = orig[1] + (vb[1]+1+(i&1))*ch;
  386. const float bz = orig[2] + vb[2]*cs;
  387. unsigned int col = color;
  388. if ((va[3] & 0xf) != 0xf)
  389. {
  390. col = duRGBA(255,255,255,128);
  391. int d = va[3] & 0xf;
  392. const float cx = (ax+bx)*0.5f;
  393. const float cy = (ay+by)*0.5f;
  394. const float cz = (az+bz)*0.5f;
  395. const float dx = cx + offs[d*2+0]*2*cs;
  396. const float dy = cy;
  397. const float dz = cz + offs[d*2+1]*2*cs;
  398. dd->vertex(cx,cy,cz,duRGBA(255,0,0,255));
  399. dd->vertex(dx,dy,dz,duRGBA(255,0,0,255));
  400. }
  401. duAppendArrow(dd, ax,ay,az, bx,by,bz, 0.0f, cs*0.5f, col);
  402. }
  403. }
  404. dd->end();
  405. dd->begin(DU_DRAW_POINTS, 4.0f);
  406. for (int i = 0; i < lcset.nconts; ++i)
  407. {
  408. const rcLayerContour& c = lcset.conts[i];
  409. unsigned int color = 0;
  410. for (int j = 0; j < c.nverts; ++j)
  411. {
  412. const unsigned char* va = &c.verts[j*4];
  413. color = duDarkenCol(duIntToCol(i, a));
  414. if (va[3] & 0x80)
  415. color = duRGBA(255,0,0,255);
  416. float fx = orig[0] + va[0]*cs;
  417. float fy = orig[1] + (va[1]+1+(i&1))*ch;
  418. float fz = orig[2] + va[2]*cs;
  419. dd->vertex(fx,fy,fz, color);
  420. }
  421. }
  422. dd->end();
  423. }
  424. void duDebugDrawLayerPolyMesh(duDebugDraw* dd, const struct rcLayerPolyMesh& lmesh)
  425. {
  426. if (!dd) return;
  427. const int nvp = lmesh.nvp;
  428. const float cs = lmesh.cs;
  429. const float ch = lmesh.ch;
  430. const float* orig = lmesh.bmin;
  431. const int offs[2*4] = {-1,0, 0,1, 1,0, 0,-1};
  432. dd->begin(DU_DRAW_TRIS);
  433. for (int i = 0; i < lmesh.npolys; ++i)
  434. {
  435. const unsigned short* p = &lmesh.polys[i*nvp*2];
  436. unsigned int color;
  437. if (lmesh.areas[i] == RC_WALKABLE_AREA)
  438. color = duRGBA(0,192,255,64);
  439. else if (lmesh.areas[i] == RC_NULL_AREA)
  440. color = duRGBA(0,0,0,64);
  441. else
  442. color = duIntToCol(lmesh.areas[i], 255);
  443. unsigned short vi[3];
  444. for (int j = 2; j < nvp; ++j)
  445. {
  446. if (p[j] == RC_MESH_NULL_IDX) break;
  447. vi[0] = p[0];
  448. vi[1] = p[j-1];
  449. vi[2] = p[j];
  450. for (int k = 0; k < 3; ++k)
  451. {
  452. const unsigned short* v = &lmesh.verts[vi[k]*3];
  453. const float x = orig[0] + v[0]*cs;
  454. const float y = orig[1] + (v[1]+1)*ch;
  455. const float z = orig[2] + v[2]*cs;
  456. dd->vertex(x,y,z, color);
  457. }
  458. }
  459. }
  460. dd->end();
  461. // Draw neighbours edges
  462. const unsigned int coln = duRGBA(0,48,64,32);
  463. dd->begin(DU_DRAW_LINES, 1.5f);
  464. for (int i = 0; i < lmesh.npolys; ++i)
  465. {
  466. const unsigned short* p = &lmesh.polys[i*nvp*2];
  467. for (int j = 0; j < nvp; ++j)
  468. {
  469. if (p[j] == RC_MESH_NULL_IDX) break;
  470. if (p[nvp+j] & 0x8000) continue;
  471. const int nj = (j+1 >= nvp || p[j+1] == RC_MESH_NULL_IDX) ? 0 : j+1;
  472. int vi[2] = {p[j], p[nj]};
  473. for (int k = 0; k < 2; ++k)
  474. {
  475. const unsigned short* v = &lmesh.verts[vi[k]*3];
  476. const float x = orig[0] + v[0]*cs;
  477. const float y = orig[1] + (v[1]+1)*ch + 0.1f;
  478. const float z = orig[2] + v[2]*cs;
  479. dd->vertex(x, y, z, coln);
  480. }
  481. }
  482. }
  483. dd->end();
  484. // Draw boundary edges
  485. const unsigned int colb = duRGBA(0,48,64,220);
  486. dd->begin(DU_DRAW_LINES, 2.5f);
  487. for (int i = 0; i < lmesh.npolys; ++i)
  488. {
  489. const unsigned short* p = &lmesh.polys[i*nvp*2];
  490. for (int j = 0; j < nvp; ++j)
  491. {
  492. if (p[j] == RC_MESH_NULL_IDX) break;
  493. if ((p[nvp+j] & 0x8000) == 0) continue;
  494. const int nj = (j+1 >= nvp || p[j+1] == RC_MESH_NULL_IDX) ? 0 : j+1;
  495. int vi[2] = {p[j], p[nj]};
  496. unsigned int col = colb;
  497. if ((p[nvp+j] & 0xf) != 0xf)
  498. {
  499. const unsigned short* va = &lmesh.verts[vi[0]*3];
  500. const unsigned short* vb = &lmesh.verts[vi[1]*3];
  501. const float ax = orig[0] + va[0]*cs;
  502. const float ay = orig[1] + (va[1]+1+(i&1))*ch;
  503. const float az = orig[2] + va[2]*cs;
  504. const float bx = orig[0] + vb[0]*cs;
  505. const float by = orig[1] + (vb[1]+1+(i&1))*ch;
  506. const float bz = orig[2] + vb[2]*cs;
  507. const float cx = (ax+bx)*0.5f;
  508. const float cy = (ay+by)*0.5f;
  509. const float cz = (az+bz)*0.5f;
  510. int d = p[nvp+j] & 0xf;
  511. const float dx = cx + offs[d*2+0]*2*cs;
  512. const float dy = cy;
  513. const float dz = cz + offs[d*2+1]*2*cs;
  514. dd->vertex(cx,cy,cz,duRGBA(255,0,0,255));
  515. dd->vertex(dx,dy,dz,duRGBA(255,0,0,255));
  516. col = duRGBA(255,255,255,128);
  517. }
  518. for (int k = 0; k < 2; ++k)
  519. {
  520. const unsigned short* v = &lmesh.verts[vi[k]*3];
  521. const float x = orig[0] + v[0]*cs;
  522. const float y = orig[1] + (v[1]+1)*ch + 0.1f;
  523. const float z = orig[2] + v[2]*cs;
  524. dd->vertex(x, y, z, col);
  525. }
  526. }
  527. }
  528. dd->end();
  529. dd->begin(DU_DRAW_POINTS, 3.0f);
  530. const unsigned int colv = duRGBA(0,0,0,220);
  531. for (int i = 0; i < lmesh.nverts; ++i)
  532. {
  533. const unsigned short* v = &lmesh.verts[i*3];
  534. const float x = orig[0] + v[0]*cs;
  535. const float y = orig[1] + (v[1]+1)*ch + 0.1f;
  536. const float z = orig[2] + v[2]*cs;
  537. dd->vertex(x,y,z, colv);
  538. }
  539. dd->end();
  540. }
  541. */
  542. static void getContourCenter(const rcContour* cont, const float* orig, float cs, float ch, float* center)
  543. {
  544. center[0] = 0;
  545. center[1] = 0;
  546. center[2] = 0;
  547. if (!cont->nverts)
  548. return;
  549. for (int i = 0; i < cont->nverts; ++i)
  550. {
  551. const int* v = &cont->verts[i*4];
  552. center[0] += (float)v[0];
  553. center[1] += (float)v[1];
  554. center[2] += (float)v[2];
  555. }
  556. const float s = 1.0f / cont->nverts;
  557. center[0] *= s * cs;
  558. center[1] *= s * ch;
  559. center[2] *= s * cs;
  560. center[0] += orig[0];
  561. center[1] += orig[1] + 4*ch;
  562. center[2] += orig[2];
  563. }
  564. static const rcContour* findContourFromSet(const rcContourSet& cset, unsigned short reg)
  565. {
  566. for (int i = 0; i < cset.nconts; ++i)
  567. {
  568. if (cset.conts[i].reg == reg)
  569. return &cset.conts[i];
  570. }
  571. return 0;
  572. }
  573. void duDebugDrawRegionConnections(duDebugDraw* dd, const rcContourSet& cset, const float alpha)
  574. {
  575. if (!dd) return;
  576. const float* orig = cset.bmin;
  577. const float cs = cset.cs;
  578. const float ch = cset.ch;
  579. // Draw centers
  580. float pos[3], pos2[3];
  581. unsigned int color = duRGBA(0,0,0,196);
  582. dd->begin(DU_DRAW_LINES, 2.0f);
  583. for (int i = 0; i < cset.nconts; ++i)
  584. {
  585. const rcContour* cont = &cset.conts[i];
  586. getContourCenter(cont, orig, cs, ch, pos);
  587. for (int j = 0; j < cont->nverts; ++j)
  588. {
  589. const int* v = &cont->verts[j*4];
  590. if (v[3] == 0 || (unsigned short)v[3] < cont->reg) continue;
  591. const rcContour* cont2 = findContourFromSet(cset, (unsigned short)v[3]);
  592. if (cont2)
  593. {
  594. getContourCenter(cont2, orig, cs, ch, pos2);
  595. duAppendArc(dd, pos[0],pos[1],pos[2], pos2[0],pos2[1],pos2[2], 0.25f, 0.6f, 0.6f, color);
  596. }
  597. }
  598. }
  599. dd->end();
  600. unsigned char a = (unsigned char)(alpha * 255.0f);
  601. dd->begin(DU_DRAW_POINTS, 7.0f);
  602. for (int i = 0; i < cset.nconts; ++i)
  603. {
  604. const rcContour* cont = &cset.conts[i];
  605. unsigned int col = duDarkenCol(duIntToCol(cont->reg,a));
  606. getContourCenter(cont, orig, cs, ch, pos);
  607. dd->vertex(pos, col);
  608. }
  609. dd->end();
  610. }
  611. void duDebugDrawRawContours(duDebugDraw* dd, const rcContourSet& cset, const float alpha)
  612. {
  613. if (!dd) return;
  614. const float* orig = cset.bmin;
  615. const float cs = cset.cs;
  616. const float ch = cset.ch;
  617. const unsigned char a = (unsigned char)(alpha*255.0f);
  618. dd->begin(DU_DRAW_LINES, 2.0f);
  619. for (int i = 0; i < cset.nconts; ++i)
  620. {
  621. const rcContour& c = cset.conts[i];
  622. unsigned int color = duIntToCol(c.reg, a);
  623. for (int j = 0; j < c.nrverts; ++j)
  624. {
  625. const int* v = &c.rverts[j*4];
  626. float fx = orig[0] + v[0]*cs;
  627. float fy = orig[1] + (v[1]+1+(i&1))*ch;
  628. float fz = orig[2] + v[2]*cs;
  629. dd->vertex(fx,fy,fz,color);
  630. if (j > 0)
  631. dd->vertex(fx,fy,fz,color);
  632. }
  633. // Loop last segment.
  634. const int* v = &c.rverts[0];
  635. float fx = orig[0] + v[0]*cs;
  636. float fy = orig[1] + (v[1]+1+(i&1))*ch;
  637. float fz = orig[2] + v[2]*cs;
  638. dd->vertex(fx,fy,fz,color);
  639. }
  640. dd->end();
  641. dd->begin(DU_DRAW_POINTS, 2.0f);
  642. for (int i = 0; i < cset.nconts; ++i)
  643. {
  644. const rcContour& c = cset.conts[i];
  645. unsigned int color = duDarkenCol(duIntToCol(c.reg, a));
  646. for (int j = 0; j < c.nrverts; ++j)
  647. {
  648. const int* v = &c.rverts[j*4];
  649. float off = 0;
  650. unsigned int colv = color;
  651. if (v[3] & RC_BORDER_VERTEX)
  652. {
  653. colv = duRGBA(255,255,255,a);
  654. off = ch*2;
  655. }
  656. float fx = orig[0] + v[0]*cs;
  657. float fy = orig[1] + (v[1]+1+(i&1))*ch + off;
  658. float fz = orig[2] + v[2]*cs;
  659. dd->vertex(fx,fy,fz, colv);
  660. }
  661. }
  662. dd->end();
  663. }
  664. void duDebugDrawContours(duDebugDraw* dd, const rcContourSet& cset, const float alpha)
  665. {
  666. if (!dd) return;
  667. const float* orig = cset.bmin;
  668. const float cs = cset.cs;
  669. const float ch = cset.ch;
  670. const unsigned char a = (unsigned char)(alpha*255.0f);
  671. dd->begin(DU_DRAW_LINES, 2.5f);
  672. for (int i = 0; i < cset.nconts; ++i)
  673. {
  674. const rcContour& c = cset.conts[i];
  675. if (!c.nverts)
  676. continue;
  677. const unsigned int color = duIntToCol(c.reg, a);
  678. const unsigned int bcolor = duLerpCol(color,duRGBA(255,255,255,a),128);
  679. for (int j = 0, k = c.nverts-1; j < c.nverts; k=j++)
  680. {
  681. const int* va = &c.verts[k*4];
  682. const int* vb = &c.verts[j*4];
  683. unsigned int col = (va[3] & RC_AREA_BORDER) ? bcolor : color;
  684. float fx,fy,fz;
  685. fx = orig[0] + va[0]*cs;
  686. fy = orig[1] + (va[1]+1+(i&1))*ch;
  687. fz = orig[2] + va[2]*cs;
  688. dd->vertex(fx,fy,fz, col);
  689. fx = orig[0] + vb[0]*cs;
  690. fy = orig[1] + (vb[1]+1+(i&1))*ch;
  691. fz = orig[2] + vb[2]*cs;
  692. dd->vertex(fx,fy,fz, col);
  693. }
  694. }
  695. dd->end();
  696. dd->begin(DU_DRAW_POINTS, 3.0f);
  697. for (int i = 0; i < cset.nconts; ++i)
  698. {
  699. const rcContour& c = cset.conts[i];
  700. unsigned int color = duDarkenCol(duIntToCol(c.reg, a));
  701. for (int j = 0; j < c.nverts; ++j)
  702. {
  703. const int* v = &c.verts[j*4];
  704. float off = 0;
  705. unsigned int colv = color;
  706. if (v[3] & RC_BORDER_VERTEX)
  707. {
  708. colv = duRGBA(255,255,255,a);
  709. off = ch*2;
  710. }
  711. float fx = orig[0] + v[0]*cs;
  712. float fy = orig[1] + (v[1]+1+(i&1))*ch + off;
  713. float fz = orig[2] + v[2]*cs;
  714. dd->vertex(fx,fy,fz, colv);
  715. }
  716. }
  717. dd->end();
  718. }
  719. void duDebugDrawPolyMesh(duDebugDraw* dd, const struct rcPolyMesh& mesh)
  720. {
  721. if (!dd) return;
  722. const int nvp = mesh.nvp;
  723. const float cs = mesh.cs;
  724. const float ch = mesh.ch;
  725. const float* orig = mesh.bmin;
  726. dd->begin(DU_DRAW_TRIS);
  727. for (int i = 0; i < mesh.npolys; ++i)
  728. {
  729. const unsigned short* p = &mesh.polys[i*nvp*2];
  730. const unsigned char area = mesh.areas[i];
  731. unsigned int color;
  732. if (area == RC_WALKABLE_AREA)
  733. color = duRGBA(0,192,255,64);
  734. else if (area == RC_NULL_AREA)
  735. color = duRGBA(0,0,0,64);
  736. else
  737. color = dd->areaToCol(area);
  738. unsigned short vi[3];
  739. for (int j = 2; j < nvp; ++j)
  740. {
  741. if (p[j] == RC_MESH_NULL_IDX) break;
  742. vi[0] = p[0];
  743. vi[1] = p[j-1];
  744. vi[2] = p[j];
  745. for (int k = 0; k < 3; ++k)
  746. {
  747. const unsigned short* v = &mesh.verts[vi[k]*3];
  748. const float x = orig[0] + v[0]*cs;
  749. const float y = orig[1] + (v[1]+1)*ch;
  750. const float z = orig[2] + v[2]*cs;
  751. dd->vertex(x,y,z, color);
  752. }
  753. }
  754. }
  755. dd->end();
  756. // Draw neighbours edges
  757. const unsigned int coln = duRGBA(0,48,64,32);
  758. dd->begin(DU_DRAW_LINES, 1.5f);
  759. for (int i = 0; i < mesh.npolys; ++i)
  760. {
  761. const unsigned short* p = &mesh.polys[i*nvp*2];
  762. for (int j = 0; j < nvp; ++j)
  763. {
  764. if (p[j] == RC_MESH_NULL_IDX) break;
  765. if (p[nvp+j] & 0x8000) continue;
  766. const int nj = (j+1 >= nvp || p[j+1] == RC_MESH_NULL_IDX) ? 0 : j+1;
  767. const int vi[2] = {p[j], p[nj]};
  768. for (int k = 0; k < 2; ++k)
  769. {
  770. const unsigned short* v = &mesh.verts[vi[k]*3];
  771. const float x = orig[0] + v[0]*cs;
  772. const float y = orig[1] + (v[1]+1)*ch + 0.1f;
  773. const float z = orig[2] + v[2]*cs;
  774. dd->vertex(x, y, z, coln);
  775. }
  776. }
  777. }
  778. dd->end();
  779. // Draw boundary edges
  780. const unsigned int colb = duRGBA(0,48,64,220);
  781. dd->begin(DU_DRAW_LINES, 2.5f);
  782. for (int i = 0; i < mesh.npolys; ++i)
  783. {
  784. const unsigned short* p = &mesh.polys[i*nvp*2];
  785. for (int j = 0; j < nvp; ++j)
  786. {
  787. if (p[j] == RC_MESH_NULL_IDX) break;
  788. if ((p[nvp+j] & 0x8000) == 0) continue;
  789. const int nj = (j+1 >= nvp || p[j+1] == RC_MESH_NULL_IDX) ? 0 : j+1;
  790. const int vi[2] = {p[j], p[nj]};
  791. unsigned int col = colb;
  792. if ((p[nvp+j] & 0xf) != 0xf)
  793. col = duRGBA(255,255,255,128);
  794. for (int k = 0; k < 2; ++k)
  795. {
  796. const unsigned short* v = &mesh.verts[vi[k]*3];
  797. const float x = orig[0] + v[0]*cs;
  798. const float y = orig[1] + (v[1]+1)*ch + 0.1f;
  799. const float z = orig[2] + v[2]*cs;
  800. dd->vertex(x, y, z, col);
  801. }
  802. }
  803. }
  804. dd->end();
  805. dd->begin(DU_DRAW_POINTS, 3.0f);
  806. const unsigned int colv = duRGBA(0,0,0,220);
  807. for (int i = 0; i < mesh.nverts; ++i)
  808. {
  809. const unsigned short* v = &mesh.verts[i*3];
  810. const float x = orig[0] + v[0]*cs;
  811. const float y = orig[1] + (v[1]+1)*ch + 0.1f;
  812. const float z = orig[2] + v[2]*cs;
  813. dd->vertex(x,y,z, colv);
  814. }
  815. dd->end();
  816. }
  817. void duDebugDrawPolyMeshDetail(duDebugDraw* dd, const struct rcPolyMeshDetail& dmesh)
  818. {
  819. if (!dd) return;
  820. dd->begin(DU_DRAW_TRIS);
  821. for (int i = 0; i < dmesh.nmeshes; ++i)
  822. {
  823. const unsigned int* m = &dmesh.meshes[i*4];
  824. const unsigned int bverts = m[0];
  825. const unsigned int btris = m[2];
  826. const int ntris = (int)m[3];
  827. const float* verts = &dmesh.verts[bverts*3];
  828. const unsigned char* tris = &dmesh.tris[btris*4];
  829. unsigned int color = duIntToCol(i, 192);
  830. for (int j = 0; j < ntris; ++j)
  831. {
  832. dd->vertex(&verts[tris[j*4+0]*3], color);
  833. dd->vertex(&verts[tris[j*4+1]*3], color);
  834. dd->vertex(&verts[tris[j*4+2]*3], color);
  835. }
  836. }
  837. dd->end();
  838. // Internal edges.
  839. dd->begin(DU_DRAW_LINES, 1.0f);
  840. const unsigned int coli = duRGBA(0,0,0,64);
  841. for (int i = 0; i < dmesh.nmeshes; ++i)
  842. {
  843. const unsigned int* m = &dmesh.meshes[i*4];
  844. const unsigned int bverts = m[0];
  845. const unsigned int btris = m[2];
  846. const int ntris = (int)m[3];
  847. const float* verts = &dmesh.verts[bverts*3];
  848. const unsigned char* tris = &dmesh.tris[btris*4];
  849. for (int j = 0; j < ntris; ++j)
  850. {
  851. const unsigned char* t = &tris[j*4];
  852. for (int k = 0, kp = 2; k < 3; kp=k++)
  853. {
  854. unsigned char ef = (t[3] >> (kp*2)) & 0x3;
  855. if (ef == 0)
  856. {
  857. // Internal edge
  858. if (t[kp] < t[k])
  859. {
  860. dd->vertex(&verts[t[kp]*3], coli);
  861. dd->vertex(&verts[t[k]*3], coli);
  862. }
  863. }
  864. }
  865. }
  866. }
  867. dd->end();
  868. // External edges.
  869. dd->begin(DU_DRAW_LINES, 2.0f);
  870. const unsigned int cole = duRGBA(0,0,0,64);
  871. for (int i = 0; i < dmesh.nmeshes; ++i)
  872. {
  873. const unsigned int* m = &dmesh.meshes[i*4];
  874. const unsigned int bverts = m[0];
  875. const unsigned int btris = m[2];
  876. const int ntris = (int)m[3];
  877. const float* verts = &dmesh.verts[bverts*3];
  878. const unsigned char* tris = &dmesh.tris[btris*4];
  879. for (int j = 0; j < ntris; ++j)
  880. {
  881. const unsigned char* t = &tris[j*4];
  882. for (int k = 0, kp = 2; k < 3; kp=k++)
  883. {
  884. unsigned char ef = (t[3] >> (kp*2)) & 0x3;
  885. if (ef != 0)
  886. {
  887. // Ext edge
  888. dd->vertex(&verts[t[kp]*3], cole);
  889. dd->vertex(&verts[t[k]*3], cole);
  890. }
  891. }
  892. }
  893. }
  894. dd->end();
  895. dd->begin(DU_DRAW_POINTS, 3.0f);
  896. const unsigned int colv = duRGBA(0,0,0,64);
  897. for (int i = 0; i < dmesh.nmeshes; ++i)
  898. {
  899. const unsigned int* m = &dmesh.meshes[i*4];
  900. const unsigned int bverts = m[0];
  901. const int nverts = (int)m[1];
  902. const float* verts = &dmesh.verts[bverts*3];
  903. for (int j = 0; j < nverts; ++j)
  904. dd->vertex(&verts[j*3], colv);
  905. }
  906. dd->end();
  907. }