Debian Codegraph @

DebugUtils/Source/DebugDraw.cpp

raw symbols SCIP
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 <string.h>
20 #include "DebugDraw.h"
21 #include "DetourMath.h"
22 #include "DetourNavMesh.h"
23
24
25 duDebugDraw::~duDebugDraw()
26 {
27 // Empty
28 }
29
30 unsigned int duDebugDraw::areaToCol(unsigned int area)
31 {
32 if (area == 0)
33 {
34 // Treat zero area type as default.
35 return duRGBA(0, 192, 255, 255);
36 }
37 else
38 {
39 return duIntToCol(area, 255);
40 }
41 }
42
43 inline int bit(int a, int b)
44 {
45 return (a & (1 << b)) >> b;
46 }
47
48 unsigned int duIntToCol(int i, int a)
49 {
50 int r = bit(i, 1) + bit(i, 3) * 2 + 1;
51 int g = bit(i, 2) + bit(i, 4) * 2 + 1;
52 int b = bit(i, 0) + bit(i, 5) * 2 + 1;
53 return duRGBA(r*63,g*63,b*63,a);
54 }
55
56 void duIntToCol(int i, float* col)
57 {
58 int r = bit(i, 0) + bit(i, 3) * 2 + 1;
59 int g = bit(i, 1) + bit(i, 4) * 2 + 1;
60 int b = bit(i, 2) + bit(i, 5) * 2 + 1;
61 col[0] = 1 - r*63.0f/255.0f;
62 col[1] = 1 - g*63.0f/255.0f;
63 col[2] = 1 - b*63.0f/255.0f;
64 }
65
66 void duCalcBoxColors(unsigned int* colors, unsigned int colTop, unsigned int colSide)
67 {
68 if (!colors) return;
69
70 colors[0] = duMultCol(colTop, 250);
71 colors[1] = duMultCol(colSide, 140);
72 colors[2] = duMultCol(colSide, 165);
73 colors[3] = duMultCol(colSide, 217);
74 colors[4] = duMultCol(colSide, 165);
75 colors[5] = duMultCol(colSide, 217);
76 }
77
78 void duDebugDrawCylinderWire(struct duDebugDraw* dd, float minx, float miny, float minz,
79 float maxx, float maxy, float maxz, unsigned int col, const float lineWidth)
80 {
81 if (!dd) return;
82
83 dd->begin(DU_DRAW_LINES, lineWidth);
84 duAppendCylinderWire(dd, minx,miny,minz, maxx,maxy,maxz, col);
85 dd->end();
86 }
87
88 void duDebugDrawBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
89 float maxx, float maxy, float maxz, unsigned int col, const float lineWidth)
90 {
91 if (!dd) return;
92
93 dd->begin(DU_DRAW_LINES, lineWidth);
94 duAppendBoxWire(dd, minx,miny,minz, maxx,maxy,maxz, col);
95 dd->end();
96 }
97
98 void duDebugDrawArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
99 const float x1, const float y1, const float z1, const float h,
100 const float as0, const float as1, unsigned int col, const float lineWidth)
101 {
102 if (!dd) return;
103
104 dd->begin(DU_DRAW_LINES, lineWidth);
105 duAppendArc(dd, x0,y0,z0, x1,y1,z1, h, as0, as1, col);
106 dd->end();
107 }
108
109 void duDebugDrawArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
110 const float x1, const float y1, const float z1,
111 const float as0, const float as1, unsigned int col, const float lineWidth)
112 {
113 if (!dd) return;
114
115 dd->begin(DU_DRAW_LINES, lineWidth);
116 duAppendArrow(dd, x0,y0,z0, x1,y1,z1, as0, as1, col);
117 dd->end();
118 }
119
120 void duDebugDrawCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
121 const float r, unsigned int col, const float lineWidth)
122 {
123 if (!dd) return;
124
125 dd->begin(DU_DRAW_LINES, lineWidth);
126 duAppendCircle(dd, x,y,z, r, col);
127 dd->end();
128 }
129
130 void duDebugDrawCross(struct duDebugDraw* dd, const float x, const float y, const float z,
131 const float size, unsigned int col, const float lineWidth)
132 {
133 if (!dd) return;
134
135 dd->begin(DU_DRAW_LINES, lineWidth);
136 duAppendCross(dd, x,y,z, size, col);
137 dd->end();
138 }
139
140 void duDebugDrawBox(struct duDebugDraw* dd, float minx, float miny, float minz,
141 float maxx, float maxy, float maxz, const unsigned int* fcol)
142 {
143 if (!dd) return;
144
145 dd->begin(DU_DRAW_QUADS);
146 duAppendBox(dd, minx,miny,minz, maxx,maxy,maxz, fcol);
147 dd->end();
148 }
149
150 void duDebugDrawCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
151 float maxx, float maxy, float maxz, unsigned int col)
152 {
153 if (!dd) return;
154
155 dd->begin(DU_DRAW_TRIS);
156 duAppendCylinder(dd, minx,miny,minz, maxx,maxy,maxz, col);
157 dd->end();
158 }
159
160 void duDebugDrawGridXZ(struct duDebugDraw* dd, const float ox, const float oy, const float oz,
161 const int w, const int h, const float size,
162 const unsigned int col, const float lineWidth)
163 {
164 if (!dd) return;
165
166 dd->begin(DU_DRAW_LINES, lineWidth);
167 for (int i = 0; i <= h; ++i)
168 {
169 dd->vertex(ox,oy,oz+i*size, col);
170 dd->vertex(ox+w*size,oy,oz+i*size, col);
171 }
172 for (int i = 0; i <= w; ++i)
173 {
174 dd->vertex(ox+i*size,oy,oz, col);
175 dd->vertex(ox+i*size,oy,oz+h*size, col);
176 }
177 dd->end();
178 }
179
180
181 void duAppendCylinderWire(struct duDebugDraw* dd, float minx, float miny, float minz,
182 float maxx, float maxy, float maxz, unsigned int col)
183 {
184 if (!dd) return;
185
186 static const int NUM_SEG = 16;
187 static float dir[NUM_SEG*2];
188 static bool init = false;
189 if (!init)
190 {
191 init = true;
192 for (int i = 0; i < NUM_SEG; ++i)
193 {
194 const float a = (float)i/(float)NUM_SEG*DU_PI*2;
195 dir[i*2] = dtMathCosf(a);
196 dir[i*2+1] = dtMathSinf(a);
197 }
198 }
199
200 const float cx = (maxx + minx)/2;
201 const float cz = (maxz + minz)/2;
202 const float rx = (maxx - minx)/2;
203 const float rz = (maxz - minz)/2;
204
205 for (int i = 0, j = NUM_SEG-1; i < NUM_SEG; j = i++)
206 {
207 dd->vertex(cx+dir[j*2+0]*rx, miny, cz+dir[j*2+1]*rz, col);
208 dd->vertex(cx+dir[i*2+0]*rx, miny, cz+dir[i*2+1]*rz, col);
209 dd->vertex(cx+dir[j*2+0]*rx, maxy, cz+dir[j*2+1]*rz, col);
210 dd->vertex(cx+dir[i*2+0]*rx, maxy, cz+dir[i*2+1]*rz, col);
211 }
212 for (int i = 0; i < NUM_SEG; i += NUM_SEG/4)
213 {
214 dd->vertex(cx+dir[i*2+0]*rx, miny, cz+dir[i*2+1]*rz, col);
215 dd->vertex(cx+dir[i*2+0]*rx, maxy, cz+dir[i*2+1]*rz, col);
216 }
217 }
218
219 void duAppendBoxWire(struct duDebugDraw* dd, float minx, float miny, float minz,
220 float maxx, float maxy, float maxz, unsigned int col)
221 {
222 if (!dd) return;
223 // Top
224 dd->vertex(minx, miny, minz, col);
225 dd->vertex(maxx, miny, minz, col);
226 dd->vertex(maxx, miny, minz, col);
227 dd->vertex(maxx, miny, maxz, col);
228 dd->vertex(maxx, miny, maxz, col);
229 dd->vertex(minx, miny, maxz, col);
230 dd->vertex(minx, miny, maxz, col);
231 dd->vertex(minx, miny, minz, col);
232
233 // bottom
234 dd->vertex(minx, maxy, minz, col);
235 dd->vertex(maxx, maxy, minz, col);
236 dd->vertex(maxx, maxy, minz, col);
237 dd->vertex(maxx, maxy, maxz, col);
238 dd->vertex(maxx, maxy, maxz, col);
239 dd->vertex(minx, maxy, maxz, col);
240 dd->vertex(minx, maxy, maxz, col);
241 dd->vertex(minx, maxy, minz, col);
242
243 // Sides
244 dd->vertex(minx, miny, minz, col);
245 dd->vertex(minx, maxy, minz, col);
246 dd->vertex(maxx, miny, minz, col);
247 dd->vertex(maxx, maxy, minz, col);
248 dd->vertex(maxx, miny, maxz, col);
249 dd->vertex(maxx, maxy, maxz, col);
250 dd->vertex(minx, miny, maxz, col);
251 dd->vertex(minx, maxy, maxz, col);
252 }
253
254 void duAppendBoxPoints(struct duDebugDraw* dd, float minx, float miny, float minz,
255 float maxx, float maxy, float maxz, unsigned int col)
256 {
257 if (!dd) return;
258 // Top
259 dd->vertex(minx, miny, minz, col);
260 dd->vertex(maxx, miny, minz, col);
261 dd->vertex(maxx, miny, minz, col);
262 dd->vertex(maxx, miny, maxz, col);
263 dd->vertex(maxx, miny, maxz, col);
264 dd->vertex(minx, miny, maxz, col);
265 dd->vertex(minx, miny, maxz, col);
266 dd->vertex(minx, miny, minz, col);
267
268 // bottom
269 dd->vertex(minx, maxy, minz, col);
270 dd->vertex(maxx, maxy, minz, col);
271 dd->vertex(maxx, maxy, minz, col);
272 dd->vertex(maxx, maxy, maxz, col);
273 dd->vertex(maxx, maxy, maxz, col);
274 dd->vertex(minx, maxy, maxz, col);
275 dd->vertex(minx, maxy, maxz, col);
276 dd->vertex(minx, maxy, minz, col);
277 }
278
279 void duAppendBox(struct duDebugDraw* dd, float minx, float miny, float minz,
280 float maxx, float maxy, float maxz, const unsigned int* fcol)
281 {
282 if (!dd) return;
283 const float verts[8*3] =
284 {
285 minx, miny, minz,
286 maxx, miny, minz,
287 maxx, miny, maxz,
288 minx, miny, maxz,
289 minx, maxy, minz,
290 maxx, maxy, minz,
291 maxx, maxy, maxz,
292 minx, maxy, maxz,
293 };
294 static const unsigned char inds[6*4] =
295 {
296 7, 6, 5, 4,
297 0, 1, 2, 3,
298 1, 5, 6, 2,
299 3, 7, 4, 0,
300 2, 6, 7, 3,
301 0, 4, 5, 1,
302 };
303
304 const unsigned char* in = inds;
305 for (int i = 0; i < 6; ++i)
306 {
307 dd->vertex(&verts[*in*3], fcol[i]); in++;
308 dd->vertex(&verts[*in*3], fcol[i]); in++;
309 dd->vertex(&verts[*in*3], fcol[i]); in++;
310 dd->vertex(&verts[*in*3], fcol[i]); in++;
311 }
312 }
313
314 void duAppendCylinder(struct duDebugDraw* dd, float minx, float miny, float minz,
315 float maxx, float maxy, float maxz, unsigned int col)
316 {
317 if (!dd) return;
318
319 static const int NUM_SEG = 16;
320 static float dir[NUM_SEG*2];
321 static bool init = false;
322 if (!init)
323 {
324 init = true;
325 for (int i = 0; i < NUM_SEG; ++i)
326 {
327 const float a = (float)i/(float)NUM_SEG*DU_PI*2;
328 dir[i*2] = cosf(a);
329 dir[i*2+1] = sinf(a);
330 }
331 }
332
333 unsigned int col2 = duMultCol(col, 160);
334
335 const float cx = (maxx + minx)/2;
336 const float cz = (maxz + minz)/2;
337 const float rx = (maxx - minx)/2;
338 const float rz = (maxz - minz)/2;
339
340 for (int i = 2; i < NUM_SEG; ++i)
341 {
342 const int a = 0, b = i-1, c = i;
343 dd->vertex(cx+dir[a*2+0]*rx, miny, cz+dir[a*2+1]*rz, col2);
344 dd->vertex(cx+dir[b*2+0]*rx, miny, cz+dir[b*2+1]*rz, col2);
345 dd->vertex(cx+dir[c*2+0]*rx, miny, cz+dir[c*2+1]*rz, col2);
346 }
347 for (int i = 2; i < NUM_SEG; ++i)
348 {
349 const int a = 0, b = i, c = i-1;
350 dd->vertex(cx+dir[a*2+0]*rx, maxy, cz+dir[a*2+1]*rz, col);
351 dd->vertex(cx+dir[b*2+0]*rx, maxy, cz+dir[b*2+1]*rz, col);
352 dd->vertex(cx+dir[c*2+0]*rx, maxy, cz+dir[c*2+1]*rz, col);
353 }
354 for (int i = 0, j = NUM_SEG-1; i < NUM_SEG; j = i++)
355 {
356 dd->vertex(cx+dir[i*2+0]*rx, miny, cz+dir[i*2+1]*rz, col2);
357 dd->vertex(cx+dir[j*2+0]*rx, miny, cz+dir[j*2+1]*rz, col2);
358 dd->vertex(cx+dir[j*2+0]*rx, maxy, cz+dir[j*2+1]*rz, col);
359
360 dd->vertex(cx+dir[i*2+0]*rx, miny, cz+dir[i*2+1]*rz, col2);
361 dd->vertex(cx+dir[j*2+0]*rx, maxy, cz+dir[j*2+1]*rz, col);
362 dd->vertex(cx+dir[i*2+0]*rx, maxy, cz+dir[i*2+1]*rz, col);
363 }
364 }
365
366
367 inline void evalArc(const float x0, const float y0, const float z0,
368 const float dx, const float dy, const float dz,
369 const float h, const float u, float* res)
370 {
371 res[0] = x0 + dx * u;
372 res[1] = y0 + dy * u + h * (1-(u*2-1)*(u*2-1));
373 res[2] = z0 + dz * u;
374 }
375
376
377 inline void vcross(float* dest, const float* v1, const float* v2)
378 {
379 dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
380 dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
381 dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
382 }
383
384 inline void vnormalize(float* v)
385 {
386 float d = 1.0f / sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
387 v[0] *= d;
388 v[1] *= d;
389 v[2] *= d;
390 }
391
392 inline void vsub(float* dest, const float* v1, const float* v2)
393 {
394 dest[0] = v1[0]-v2[0];
395 dest[1] = v1[1]-v2[1];
396 dest[2] = v1[2]-v2[2];
397 }
398
399 inline float vdistSqr(const float* v1, const float* v2)
400 {
401 const float x = v1[0]-v2[0];
402 const float y = v1[1]-v2[1];
403 const float z = v1[2]-v2[2];
404 return x*x + y*y + z*z;
405 }
406
407
408 void appendArrowHead(struct duDebugDraw* dd, const float* p, const float* q,
409 const float s, unsigned int col)
410 {
411 const float eps = 0.001f;
412 if (!dd) return;
413 if (vdistSqr(p,q) < eps*eps) return;
414 float ax[3], ay[3] = {0,1,0}, az[3];
415 vsub(az, q, p);
416 vnormalize(az);
417 vcross(ax, ay, az);
418 vcross(ay, az, ax);
419 vnormalize(ay);
420
421 dd->vertex(p, col);
422 // dd->vertex(p[0]+az[0]*s+ay[0]*s/2, p[1]+az[1]*s+ay[1]*s/2, p[2]+az[2]*s+ay[2]*s/2, col);
423 dd->vertex(p[0]+az[0]*s+ax[0]*s/3, p[1]+az[1]*s+ax[1]*s/3, p[2]+az[2]*s+ax[2]*s/3, col);
424
425 dd->vertex(p, col);
426 // dd->vertex(p[0]+az[0]*s-ay[0]*s/2, p[1]+az[1]*s-ay[1]*s/2, p[2]+az[2]*s-ay[2]*s/2, col);
427 dd->vertex(p[0]+az[0]*s-ax[0]*s/3, p[1]+az[1]*s-ax[1]*s/3, p[2]+az[2]*s-ax[2]*s/3, col);
428
429 }
430
431 void duAppendArc(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
432 const float x1, const float y1, const float z1, const float h,
433 const float as0, const float as1, unsigned int col)
434 {
435 if (!dd) return;
436 static const int NUM_ARC_PTS = 8;
437 static const float PAD = 0.05f;
438 static const float ARC_PTS_SCALE = (1.0f-PAD*2) / (float)NUM_ARC_PTS;
439 const float dx = x1 - x0;
440 const float dy = y1 - y0;
441 const float dz = z1 - z0;
442 const float len = sqrtf(dx*dx + dy*dy + dz*dz);
443 float prev[3];
444 evalArc(x0,y0,z0, dx,dy,dz, len*h, PAD, prev);
445 for (int i = 1; i <= NUM_ARC_PTS; ++i)
446 {
447 const float u = PAD + i * ARC_PTS_SCALE;
448 float pt[3];
449 evalArc(x0,y0,z0, dx,dy,dz, len*h, u, pt);
450 dd->vertex(prev[0],prev[1],prev[2], col);
451 dd->vertex(pt[0],pt[1],pt[2], col);
452 prev[0] = pt[0]; prev[1] = pt[1]; prev[2] = pt[2];
453 }
454
455 // End arrows
456 if (as0 > 0.001f)
457 {
458 float p[3], q[3];
459 evalArc(x0,y0,z0, dx,dy,dz, len*h, PAD, p);
460 evalArc(x0,y0,z0, dx,dy,dz, len*h, PAD+0.05f, q);
461 appendArrowHead(dd, p, q, as0, col);
462 }
463
464 if (as1 > 0.001f)
465 {
466 float p[3], q[3];
467 evalArc(x0,y0,z0, dx,dy,dz, len*h, 1-PAD, p);
468 evalArc(x0,y0,z0, dx,dy,dz, len*h, 1-(PAD+0.05f), q);
469 appendArrowHead(dd, p, q, as1, col);
470 }
471 }
472
473 void duAppendArrow(struct duDebugDraw* dd, const float x0, const float y0, const float z0,
474 const float x1, const float y1, const float z1,
475 const float as0, const float as1, unsigned int col)
476 {
477 if (!dd) return;
478
479 dd->vertex(x0,y0,z0, col);
480 dd->vertex(x1,y1,z1, col);
481
482 // End arrows
483 const float p[3] = {x0,y0,z0}, q[3] = {x1,y1,z1};
484 if (as0 > 0.001f)
485 appendArrowHead(dd, p, q, as0, col);
486 if (as1 > 0.001f)
487 appendArrowHead(dd, q, p, as1, col);
488 }
489
490 void duAppendCircle(struct duDebugDraw* dd, const float x, const float y, const float z,
491 const float r, unsigned int col)
492 {
493 if (!dd) return;
494 static const int NUM_SEG = 40;
495 static float dir[40*2];
496 static bool init = false;
497 if (!init)
498 {
499 init = true;
500 for (int i = 0; i < NUM_SEG; ++i)
501 {
502 const float a = (float)i/(float)NUM_SEG*DU_PI*2;
503 dir[i*2] = cosf(a);
504 dir[i*2+1] = sinf(a);
505 }
506 }
507
508 for (int i = 0, j = NUM_SEG-1; i < NUM_SEG; j = i++)
509 {
510 dd->vertex(x+dir[j*2+0]*r, y, z+dir[j*2+1]*r, col);
511 dd->vertex(x+dir[i*2+0]*r, y, z+dir[i*2+1]*r, col);
512 }
513 }
514
515 void duAppendCross(struct duDebugDraw* dd, const float x, const float y, const float z,
516 const float s, unsigned int col)
517 {
518 if (!dd) return;
519 dd->vertex(x-s,y,z, col);
520 dd->vertex(x+s,y,z, col);
521 dd->vertex(x,y-s,z, col);
522 dd->vertex(x,y+s,z, col);
523 dd->vertex(x,y,z-s, col);
524 dd->vertex(x,y,z+s, col);
525 }
526
527 duDisplayList::duDisplayList(int cap) :
528 m_pos(0),
529 m_color(0),
530 m_size(0),
531 m_cap(0),
532 m_prim(DU_DRAW_LINES),
533 m_primSize(1.0f),
534 m_depthMask(true)
535 {
536 if (cap < 8)
537 cap = 8;
538 resize(cap);
539 }
540
541 duDisplayList::~duDisplayList()
542 {
543 delete [] m_pos;
544 delete [] m_color;
545 }
546
547 void duDisplayList::resize(int cap)
548 {
549 float* newPos = new float[cap*3];
550 if (m_size)
551 memcpy(newPos, m_pos, sizeof(float)*3*m_size);
552 delete [] m_pos;
553 m_pos = newPos;
554
555 unsigned int* newColor = new unsigned int[cap];
556 if (m_size)
557 memcpy(newColor, m_color, sizeof(unsigned int)*m_size);
558 delete [] m_color;
559 m_color = newColor;
560
561 m_cap = cap;
562 }
563
564 void duDisplayList::clear()
565 {
566 m_size = 0;
567 }
568
569 void duDisplayList::depthMask(bool state)
570 {
571 m_depthMask = state;
572 }
573
574 void duDisplayList::begin(duDebugDrawPrimitives prim, float size)
575 {
576 clear();
577 m_prim = prim;
578 m_primSize = size;
579 }
580
581 void duDisplayList::vertex(const float x, const float y, const float z, unsigned int color)
582 {
583 if (m_size+1 >= m_cap)
584 resize(m_cap*2);
585 float* p = &m_pos[m_size*3];
586 p[0] = x;
587 p[1] = y;
588 p[2] = z;
589 m_color[m_size] = color;
590 m_size++;
591 }
592
593 void duDisplayList::vertex(const float* pos, unsigned int color)
594 {
595 vertex(pos[0],pos[1],pos[2],color);
596 }
597
598 void duDisplayList::end()
599 {
600 }
601
602 void duDisplayList::draw(struct duDebugDraw* dd)
603 {
604 if (!dd) return;
605 if (!m_size) return;
606 dd->depthMask(m_depthMask);
607 dd->begin(m_prim, m_primSize);
608 for (int i = 0; i < m_size; ++i)
609 dd->vertex(&m_pos[i*3], m_color[i]);
610 dd->end();
611 }