Debian Codegraph @

DebugUtils/Source/RecastDebugDraw.cpp

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