Debian Codegraph @

RecastDemo/Source/NavMeshTesterTool.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "SDL.h"
24 #include "SDL_opengl.h"
25 #ifdef __APPLE__
26 # include <OpenGL/glu.h>
27 #else
28 # include <GL/glu.h>
29 #endif
30 #include "imgui.h"
31 #include "NavMeshTesterTool.h"
32 #include "Sample.h"
33 #include "Recast.h"
34 #include "RecastDebugDraw.h"
35 #include "DetourNavMesh.h"
36 #include "DetourNavMeshBuilder.h"
37 #include "DetourDebugDraw.h"
38 #include "DetourCommon.h"
39
40 #ifdef WIN32
41 # define snprintf _snprintf
42 #endif
43
44 // Uncomment this to dump all the requests in stdout.
45 #define DUMP_REQS
46
47 // Returns a random number [0..1]
48 static float frand()
49 {
50 // return ((float)(rand() & 0xffff)/(float)0xffff);
51 return (float)rand()/(float)RAND_MAX;
52 }
53
54 inline bool inRange(const float* v1, const float* v2, const float r, const float h)
55 {
56 const float dx = v2[0] - v1[0];
57 const float dy = v2[1] - v1[1];
58 const float dz = v2[2] - v1[2];
59 return (dx*dx + dz*dz) < r*r && fabsf(dy) < h;
60 }
61
62
63 static int fixupCorridor(dtPolyRef* path, const int npath, const int maxPath,
64 const dtPolyRef* visited, const int nvisited)
65 {
66 int furthestPath = -1;
67 int furthestVisited = -1;
68
69 // Find furthest common polygon.
70 for (int i = npath-1; i >= 0; --i)
71 {
72 bool found = false;
73 for (int j = nvisited-1; j >= 0; --j)
74 {
75 if (path[i] == visited[j])
76 {
77 furthestPath = i;
78 furthestVisited = j;
79 found = true;
80 }
81 }
82 if (found)
83 break;
84 }
85
86 // If no intersection found just return current path.
87 if (furthestPath == -1 || furthestVisited == -1)
88 return npath;
89
90 // Concatenate paths.
91
92 // Adjust beginning of the buffer to include the visited.
93 const int req = nvisited - furthestVisited;
94 const int orig = rcMin(furthestPath+1, npath);
95 int size = rcMax(0, npath-orig);
96 if (req+size > maxPath)
97 size = maxPath-req;
98 if (size)
99 memmove(path+req, path+orig, size*sizeof(dtPolyRef));
100
101 // Store visited
102 for (int i = 0; i < req; ++i)
103 path[i] = visited[(nvisited-1)-i];
104
105 return req+size;
106 }
107
108 // This function checks if the path has a small U-turn, that is,
109 // a polygon further in the path is adjacent to the first polygon
110 // in the path. If that happens, a shortcut is taken.
111 // This can happen if the target (T) location is at tile boundary,
112 // and we're (S) approaching it parallel to the tile edge.
113 // The choice at the vertex can be arbitrary,
114 // +---+---+
115 // |:::|:::|
116 // +-S-+-T-+
117 // |:::| | <-- the step can end up in here, resulting U-turn path.
118 // +---+---+
119 static int fixupShortcuts(dtPolyRef* path, int npath, dtNavMeshQuery* navQuery)
120 {
121 if (npath < 3)
122 return npath;
123
124 // Get connected polygons
125 static const int maxNeis = 16;
126 dtPolyRef neis[maxNeis];
127 int nneis = 0;
128
129 const dtMeshTile* tile = 0;
130 const dtPoly* poly = 0;
131 if (dtStatusFailed(navQuery->getAttachedNavMesh()->getTileAndPolyByRef(path[0], &tile, &poly)))
132 return npath;
133
134 for (unsigned int k = poly->firstLink; k != DT_NULL_LINK; k = tile->links[k].next)
135 {
136 const dtLink* link = &tile->links[k];
137 if (link->ref != 0)
138 {
139 if (nneis < maxNeis)
140 neis[nneis++] = link->ref;
141 }
142 }
143
144 // If any of the neighbour polygons is within the next few polygons
145 // in the path, short cut to that polygon directly.
146 static const int maxLookAhead = 6;
147 int cut = 0;
148 for (int i = dtMin(maxLookAhead, npath) - 1; i > 1 && cut == 0; i--) {
149 for (int j = 0; j < nneis; j++)
150 {
151 if (path[i] == neis[j]) {
152 cut = i;
153 break;
154 }
155 }
156 }
157 if (cut > 1)
158 {
159 int offset = cut-1;
160 npath -= offset;
161 for (int i = 1; i < npath; i++)
162 path[i] = path[i+offset];
163 }
164
165 return npath;
166 }
167
168 static bool getSteerTarget(dtNavMeshQuery* navQuery, const float* startPos, const float* endPos,
169 const float minTargetDist,
170 const dtPolyRef* path, const int pathSize,
171 float* steerPos, unsigned char& steerPosFlag, dtPolyRef& steerPosRef,
172 float* outPoints = 0, int* outPointCount = 0)
173 {
174 // Find steer target.
175 static const int MAX_STEER_POINTS = 3;
176 float steerPath[MAX_STEER_POINTS*3];
177 unsigned char steerPathFlags[MAX_STEER_POINTS];
178 dtPolyRef steerPathPolys[MAX_STEER_POINTS];
179 int nsteerPath = 0;
180 navQuery->findStraightPath(startPos, endPos, path, pathSize,
181 steerPath, steerPathFlags, steerPathPolys, &nsteerPath, MAX_STEER_POINTS);
182 if (!nsteerPath)
183 return false;
184
185 if (outPoints && outPointCount)
186 {
187 *outPointCount = nsteerPath;
188 for (int i = 0; i < nsteerPath; ++i)
189 dtVcopy(&outPoints[i*3], &steerPath[i*3]);
190 }
191
192
193 // Find vertex far enough to steer to.
194 int ns = 0;
195 while (ns < nsteerPath)
196 {
197 // Stop at Off-Mesh link or when point is further than slop away.
198 if ((steerPathFlags[ns] & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ||
199 !inRange(&steerPath[ns*3], startPos, minTargetDist, 1000.0f))
200 break;
201 ns++;
202 }
203 // Failed to find good point to steer to.
204 if (ns >= nsteerPath)
205 return false;
206
207 dtVcopy(steerPos, &steerPath[ns*3]);
208 steerPos[1] = startPos[1];
209 steerPosFlag = steerPathFlags[ns];
210 steerPosRef = steerPathPolys[ns];
211
212 return true;
213 }
214
215
216 NavMeshTesterTool::NavMeshTesterTool() :
217 m_sample(0),
218 m_navMesh(0),
219 m_navQuery(0),
220 m_pathFindStatus(DT_FAILURE),
221 m_toolMode(TOOLMODE_PATHFIND_FOLLOW),
222 m_straightPathOptions(0),
223 m_startRef(0),
224 m_endRef(0),
225 m_npolys(0),
226 m_nstraightPath(0),
227 m_nsmoothPath(0),
228 m_nrandPoints(0),
229 m_randPointsInCircle(false),
230 m_hitResult(false),
231 m_distanceToWall(0),
232 m_sposSet(false),
233 m_eposSet(false),
234 m_pathIterNum(0),
235 m_pathIterPolyCount(0),
236 m_steerPointCount(0)
237 {
238 m_filter.setIncludeFlags(SAMPLE_POLYFLAGS_ALL ^ SAMPLE_POLYFLAGS_DISABLED);
239 m_filter.setExcludeFlags(0);
240
241 m_polyPickExt[0] = 2;
242 m_polyPickExt[1] = 4;
243 m_polyPickExt[2] = 2;
244
245 m_neighbourhoodRadius = 2.5f;
246 m_randomRadius = 5.0f;
247 }
248
249 void NavMeshTesterTool::init(Sample* sample)
250 {
251 m_sample = sample;
252 m_navMesh = sample->getNavMesh();
253 m_navQuery = sample->getNavMeshQuery();
254 recalc();
255
256 if (m_navQuery)
257 {
258 // Change costs.
259 m_filter.setAreaCost(SAMPLE_POLYAREA_GROUND, 1.0f);
260 m_filter.setAreaCost(SAMPLE_POLYAREA_WATER, 10.0f);
261 m_filter.setAreaCost(SAMPLE_POLYAREA_ROAD, 1.0f);
262 m_filter.setAreaCost(SAMPLE_POLYAREA_DOOR, 1.0f);
263 m_filter.setAreaCost(SAMPLE_POLYAREA_GRASS, 2.0f);
264 m_filter.setAreaCost(SAMPLE_POLYAREA_JUMP, 1.5f);
265 }
266
267 m_neighbourhoodRadius = sample->getAgentRadius() * 20.0f;
268 m_randomRadius = sample->getAgentRadius() * 30.0f;
269 }
270
271 void NavMeshTesterTool::handleMenu()
272 {
273 if (imguiCheck("Pathfind Follow", m_toolMode == TOOLMODE_PATHFIND_FOLLOW))
274 {
275 m_toolMode = TOOLMODE_PATHFIND_FOLLOW;
276 recalc();
277 }
278 if (imguiCheck("Pathfind Straight", m_toolMode == TOOLMODE_PATHFIND_STRAIGHT))
279 {
280 m_toolMode = TOOLMODE_PATHFIND_STRAIGHT;
281 recalc();
282 }
283 if (m_toolMode == TOOLMODE_PATHFIND_STRAIGHT)
284 {
285 imguiIndent();
286 imguiLabel("Vertices at crossings");
287 if (imguiCheck("None", m_straightPathOptions == 0))
288 {
289 m_straightPathOptions = 0;
290 recalc();
291 }
292 if (imguiCheck("Area", m_straightPathOptions == DT_STRAIGHTPATH_AREA_CROSSINGS))
293 {
294 m_straightPathOptions = DT_STRAIGHTPATH_AREA_CROSSINGS;
295 recalc();
296 }
297 if (imguiCheck("All", m_straightPathOptions == DT_STRAIGHTPATH_ALL_CROSSINGS))
298 {
299 m_straightPathOptions = DT_STRAIGHTPATH_ALL_CROSSINGS;
300 recalc();
301 }
302
303 imguiUnindent();
304 }
305 if (imguiCheck("Pathfind Sliced", m_toolMode == TOOLMODE_PATHFIND_SLICED))
306 {
307 m_toolMode = TOOLMODE_PATHFIND_SLICED;
308 recalc();
309 }
310
311 imguiSeparator();
312
313 if (imguiCheck("Distance to Wall", m_toolMode == TOOLMODE_DISTANCE_TO_WALL))
314 {
315 m_toolMode = TOOLMODE_DISTANCE_TO_WALL;
316 recalc();
317 }
318
319 imguiSeparator();
320
321 if (imguiCheck("Raycast", m_toolMode == TOOLMODE_RAYCAST))
322 {
323 m_toolMode = TOOLMODE_RAYCAST;
324 recalc();
325 }
326
327 imguiSeparator();
328
329 if (imguiCheck("Find Polys in Circle", m_toolMode == TOOLMODE_FIND_POLYS_IN_CIRCLE))
330 {
331 m_toolMode = TOOLMODE_FIND_POLYS_IN_CIRCLE;
332 recalc();
333 }
334 if (imguiCheck("Find Polys in Shape", m_toolMode == TOOLMODE_FIND_POLYS_IN_SHAPE))
335 {
336 m_toolMode = TOOLMODE_FIND_POLYS_IN_SHAPE;
337 recalc();
338 }
339
340 imguiSeparator();
341
342 if (imguiCheck("Find Local Neighbourhood", m_toolMode == TOOLMODE_FIND_LOCAL_NEIGHBOURHOOD))
343 {
344 m_toolMode = TOOLMODE_FIND_LOCAL_NEIGHBOURHOOD;
345 recalc();
346 }
347
348 imguiSeparator();
349
350 if (imguiButton("Set Random Start"))
351 {
352 dtStatus status = m_navQuery->findRandomPoint(&m_filter, frand, &m_startRef, m_spos);
353 if (dtStatusSucceed(status))
354 {
355 m_sposSet = true;
356 recalc();
357 }
358 }
359 if (imguiButton("Set Random End", m_sposSet))
360 {
361 if (m_sposSet)
362 {
363 dtStatus status = m_navQuery->findRandomPointAroundCircle(m_startRef, m_spos, m_randomRadius, &m_filter, frand, &m_endRef, m_epos);
364 if (dtStatusSucceed(status))
365 {
366 m_eposSet = true;
367 recalc();
368 }
369 }
370 }
371
372 imguiSeparator();
373
374 if (imguiButton("Make Random Points"))
375 {
376 m_randPointsInCircle = false;
377 m_nrandPoints = 0;
378 for (int i = 0; i < MAX_RAND_POINTS; i++)
379 {
380 float pt[3];
381 dtPolyRef ref;
382 dtStatus status = m_navQuery->findRandomPoint(&m_filter, frand, &ref, pt);
383 if (dtStatusSucceed(status))
384 {
385 dtVcopy(&m_randPoints[m_nrandPoints*3], pt);
386 m_nrandPoints++;
387 }
388 }
389 }
390 if (imguiButton("Make Random Points Around", m_sposSet))
391 {
392 if (m_sposSet)
393 {
394 m_nrandPoints = 0;
395 m_randPointsInCircle = true;
396 for (int i = 0; i < MAX_RAND_POINTS; i++)
397 {
398 float pt[3];
399 dtPolyRef ref;
400 dtStatus status = m_navQuery->findRandomPointAroundCircle(m_startRef, m_spos, m_randomRadius, &m_filter, frand, &ref, pt);
401 if (dtStatusSucceed(status))
402 {
403 dtVcopy(&m_randPoints[m_nrandPoints*3], pt);
404 m_nrandPoints++;
405 }
406 }
407 }
408 }
409
410
411 imguiSeparator();
412
413 imguiLabel("Include Flags");
414
415 imguiIndent();
416 if (imguiCheck("Walk", (m_filter.getIncludeFlags() & SAMPLE_POLYFLAGS_WALK) != 0))
417 {
418 m_filter.setIncludeFlags(m_filter.getIncludeFlags() ^ SAMPLE_POLYFLAGS_WALK);
419 recalc();
420 }
421 if (imguiCheck("Swim", (m_filter.getIncludeFlags() & SAMPLE_POLYFLAGS_SWIM) != 0))
422 {
423 m_filter.setIncludeFlags(m_filter.getIncludeFlags() ^ SAMPLE_POLYFLAGS_SWIM);
424 recalc();
425 }
426 if (imguiCheck("Door", (m_filter.getIncludeFlags() & SAMPLE_POLYFLAGS_DOOR) != 0))
427 {
428 m_filter.setIncludeFlags(m_filter.getIncludeFlags() ^ SAMPLE_POLYFLAGS_DOOR);
429 recalc();
430 }
431 if (imguiCheck("Jump", (m_filter.getIncludeFlags() & SAMPLE_POLYFLAGS_JUMP) != 0))
432 {
433 m_filter.setIncludeFlags(m_filter.getIncludeFlags() ^ SAMPLE_POLYFLAGS_JUMP);
434 recalc();
435 }
436 imguiUnindent();
437
438 imguiSeparator();
439 imguiLabel("Exclude Flags");
440
441 imguiIndent();
442 if (imguiCheck("Walk", (m_filter.getExcludeFlags() & SAMPLE_POLYFLAGS_WALK) != 0))
443 {
444 m_filter.setExcludeFlags(m_filter.getExcludeFlags() ^ SAMPLE_POLYFLAGS_WALK);
445 recalc();
446 }
447 if (imguiCheck("Swim", (m_filter.getExcludeFlags() & SAMPLE_POLYFLAGS_SWIM) != 0))
448 {
449 m_filter.setExcludeFlags(m_filter.getExcludeFlags() ^ SAMPLE_POLYFLAGS_SWIM);
450 recalc();
451 }
452 if (imguiCheck("Door", (m_filter.getExcludeFlags() & SAMPLE_POLYFLAGS_DOOR) != 0))
453 {
454 m_filter.setExcludeFlags(m_filter.getExcludeFlags() ^ SAMPLE_POLYFLAGS_DOOR);
455 recalc();
456 }
457 if (imguiCheck("Jump", (m_filter.getExcludeFlags() & SAMPLE_POLYFLAGS_JUMP) != 0))
458 {
459 m_filter.setExcludeFlags(m_filter.getExcludeFlags() ^ SAMPLE_POLYFLAGS_JUMP);
460 recalc();
461 }
462 imguiUnindent();
463
464 imguiSeparator();
465 }
466
467 void NavMeshTesterTool::handleClick(const float* /*s*/, const float* p, bool shift)
468 {
469 if (shift)
470 {
471 m_sposSet = true;
472 dtVcopy(m_spos, p);
473 }
474 else
475 {
476 m_eposSet = true;
477 dtVcopy(m_epos, p);
478 }
479 recalc();
480 }
481
482 void NavMeshTesterTool::handleStep()
483 {
484 }
485
486 void NavMeshTesterTool::handleToggle()
487 {
488 // TODO: merge separate to a path iterator. Use same code in recalc() too.
489 if (m_toolMode != TOOLMODE_PATHFIND_FOLLOW)
490 return;
491
492 if (!m_sposSet || !m_eposSet || !m_startRef || !m_endRef)
493 return;
494
495 static const float STEP_SIZE = 0.5f;
496 static const float SLOP = 0.01f;
497
498 if (m_pathIterNum == 0)
499 {
500 m_navQuery->findPath(m_startRef, m_endRef, m_spos, m_epos, &m_filter, m_polys, &m_npolys, MAX_POLYS);
501 m_nsmoothPath = 0;
502
503 m_pathIterPolyCount = m_npolys;
504 if (m_pathIterPolyCount)
505 memcpy(m_pathIterPolys, m_polys, sizeof(dtPolyRef)*m_pathIterPolyCount);
506
507 if (m_pathIterPolyCount)
508 {
509 // Iterate over the path to find smooth path on the detail mesh surface.
510 m_navQuery->closestPointOnPoly(m_startRef, m_spos, m_iterPos, 0);
511 m_navQuery->closestPointOnPoly(m_pathIterPolys[m_pathIterPolyCount-1], m_epos, m_targetPos, 0);
512
513 m_nsmoothPath = 0;
514
515 dtVcopy(&m_smoothPath[m_nsmoothPath*3], m_iterPos);
516 m_nsmoothPath++;
517 }
518 }
519
520 dtVcopy(m_prevIterPos, m_iterPos);
521
522 m_pathIterNum++;
523
524 if (!m_pathIterPolyCount)
525 return;
526
527 if (m_nsmoothPath >= MAX_SMOOTH)
528 return;
529
530 // Move towards target a small advancement at a time until target reached or
531 // when ran out of memory to store the path.
532
533 // Find location to steer towards.
534 float steerPos[3];
535 unsigned char steerPosFlag;
536 dtPolyRef steerPosRef;
537
538 if (!getSteerTarget(m_navQuery, m_iterPos, m_targetPos, SLOP,
539 m_pathIterPolys, m_pathIterPolyCount, steerPos, steerPosFlag, steerPosRef,
540 m_steerPoints, &m_steerPointCount))
541 return;
542
543 dtVcopy(m_steerPos, steerPos);
544
545 bool endOfPath = (steerPosFlag & DT_STRAIGHTPATH_END) ? true : false;
546 bool offMeshConnection = (steerPosFlag & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ? true : false;
547
548 // Find movement delta.
549 float delta[3], len;
550 dtVsub(delta, steerPos, m_iterPos);
551 len = sqrtf(dtVdot(delta,delta));
552 // If the steer target is end of path or off-mesh link, do not move past the location.
553 if ((endOfPath || offMeshConnection) && len < STEP_SIZE)
554 len = 1;
555 else
556 len = STEP_SIZE / len;
557 float moveTgt[3];
558 dtVmad(moveTgt, m_iterPos, delta, len);
559
560 // Move
561 float result[3];
562 dtPolyRef visited[16];
563 int nvisited = 0;
564 m_navQuery->moveAlongSurface(m_pathIterPolys[0], m_iterPos, moveTgt, &m_filter,
565 result, visited, &nvisited, 16);
566 m_pathIterPolyCount = fixupCorridor(m_pathIterPolys, m_pathIterPolyCount, MAX_POLYS, visited, nvisited);
567 m_pathIterPolyCount = fixupShortcuts(m_pathIterPolys, m_pathIterPolyCount, m_navQuery);
568
569 float h = 0;
570 m_navQuery->getPolyHeight(m_pathIterPolys[0], result, &h);
571 result[1] = h;
572 dtVcopy(m_iterPos, result);
573
574 // Handle end of path and off-mesh links when close enough.
575 if (endOfPath && inRange(m_iterPos, steerPos, SLOP, 1.0f))
576 {
577 // Reached end of path.
578 dtVcopy(m_iterPos, m_targetPos);
579 if (m_nsmoothPath < MAX_SMOOTH)
580 {
581 dtVcopy(&m_smoothPath[m_nsmoothPath*3], m_iterPos);
582 m_nsmoothPath++;
583 }
584 return;
585 }
586 else if (offMeshConnection && inRange(m_iterPos, steerPos, SLOP, 1.0f))
587 {
588 // Reached off-mesh connection.
589 float startPos[3], endPos[3];
590
591 // Advance the path up to and over the off-mesh connection.
592 dtPolyRef prevRef = 0, polyRef = m_pathIterPolys[0];
593 int npos = 0;
594 while (npos < m_pathIterPolyCount && polyRef != steerPosRef)
595 {
596 prevRef = polyRef;
597 polyRef = m_pathIterPolys[npos];
598 npos++;
599 }
600 for (int i = npos; i < m_pathIterPolyCount; ++i)
601 m_pathIterPolys[i-npos] = m_pathIterPolys[i];
602 m_pathIterPolyCount -= npos;
603
604 // Handle the connection.
605 dtStatus status = m_navMesh->getOffMeshConnectionPolyEndPoints(prevRef, polyRef, startPos, endPos);
606 if (dtStatusSucceed(status))
607 {
608 if (m_nsmoothPath < MAX_SMOOTH)
609 {
610 dtVcopy(&m_smoothPath[m_nsmoothPath*3], startPos);
611 m_nsmoothPath++;
612 // Hack to make the dotted path not visible during off-mesh connection.
613 if (m_nsmoothPath & 1)
614 {
615 dtVcopy(&m_smoothPath[m_nsmoothPath*3], startPos);
616 m_nsmoothPath++;
617 }
618 }
619 // Move position at the other side of the off-mesh link.
620 dtVcopy(m_iterPos, endPos);
621 float eh = 0.0f;
622 m_navQuery->getPolyHeight(m_pathIterPolys[0], m_iterPos, &eh);
623 m_iterPos[1] = eh;
624 }
625 }
626
627 // Store results.
628 if (m_nsmoothPath < MAX_SMOOTH)
629 {
630 dtVcopy(&m_smoothPath[m_nsmoothPath*3], m_iterPos);
631 m_nsmoothPath++;
632 }
633
634 }
635
636 void NavMeshTesterTool::handleUpdate(const float /*dt*/)
637 {
638 if (m_toolMode == TOOLMODE_PATHFIND_SLICED)
639 {
640 if (dtStatusInProgress(m_pathFindStatus))
641 {
642 m_pathFindStatus = m_navQuery->updateSlicedFindPath(1,0);
643 }
644 if (dtStatusSucceed(m_pathFindStatus))
645 {
646 m_navQuery->finalizeSlicedFindPath(m_polys, &m_npolys, MAX_POLYS);
647 m_nstraightPath = 0;
648 if (m_npolys)
649 {
650 // In case of partial path, make sure the end point is clamped to the last polygon.
651 float epos[3];
652 dtVcopy(epos, m_epos);
653 if (m_polys[m_npolys-1] != m_endRef)
654 m_navQuery->closestPointOnPoly(m_polys[m_npolys-1], m_epos, epos, 0);
655
656 m_navQuery->findStraightPath(m_spos, epos, m_polys, m_npolys,
657 m_straightPath, m_straightPathFlags,
658 m_straightPathPolys, &m_nstraightPath, MAX_POLYS, DT_STRAIGHTPATH_ALL_CROSSINGS);
659 }
660
661 m_pathFindStatus = DT_FAILURE;
662 }
663 }
664 }
665
666 void NavMeshTesterTool::reset()
667 {
668 m_startRef = 0;
669 m_endRef = 0;
670 m_npolys = 0;
671 m_nstraightPath = 0;
672 m_nsmoothPath = 0;
673 memset(m_hitPos, 0, sizeof(m_hitPos));
674 memset(m_hitNormal, 0, sizeof(m_hitNormal));
675 m_distanceToWall = 0;
676 }
677
678
679 void NavMeshTesterTool::recalc()
680 {
681 if (!m_navMesh)
682 return;
683
684 if (m_sposSet)
685 m_navQuery->findNearestPoly(m_spos, m_polyPickExt, &m_filter, &m_startRef, 0);
686 else
687 m_startRef = 0;
688
689 if (m_eposSet)
690 m_navQuery->findNearestPoly(m_epos, m_polyPickExt, &m_filter, &m_endRef, 0);
691 else
692 m_endRef = 0;
693
694 m_pathFindStatus = DT_FAILURE;
695
696 if (m_toolMode == TOOLMODE_PATHFIND_FOLLOW)
697 {
698 m_pathIterNum = 0;
699 if (m_sposSet && m_eposSet && m_startRef && m_endRef)
700 {
701 #ifdef DUMP_REQS
702 printf("pi %f %f %f %f %f %f 0x%x 0x%x\n",
703 m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
704 m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
705 #endif
706
707 m_navQuery->findPath(m_startRef, m_endRef, m_spos, m_epos, &m_filter, m_polys, &m_npolys, MAX_POLYS);
708
709 m_nsmoothPath = 0;
710
711 if (m_npolys)
712 {
713 // Iterate over the path to find smooth path on the detail mesh surface.
714 dtPolyRef polys[MAX_POLYS];
715 memcpy(polys, m_polys, sizeof(dtPolyRef)*m_npolys);
716 int npolys = m_npolys;
717
718 float iterPos[3], targetPos[3];
719 m_navQuery->closestPointOnPoly(m_startRef, m_spos, iterPos, 0);
720 m_navQuery->closestPointOnPoly(polys[npolys-1], m_epos, targetPos, 0);
721
722 static const float STEP_SIZE = 0.5f;
723 static const float SLOP = 0.01f;
724
725 m_nsmoothPath = 0;
726
727 dtVcopy(&m_smoothPath[m_nsmoothPath*3], iterPos);
728 m_nsmoothPath++;
729
730 // Move towards target a small advancement at a time until target reached or
731 // when ran out of memory to store the path.
732 while (npolys && m_nsmoothPath < MAX_SMOOTH)
733 {
734 // Find location to steer towards.
735 float steerPos[3];
736 unsigned char steerPosFlag;
737 dtPolyRef steerPosRef;
738
739 if (!getSteerTarget(m_navQuery, iterPos, targetPos, SLOP,
740 polys, npolys, steerPos, steerPosFlag, steerPosRef))
741 break;
742
743 bool endOfPath = (steerPosFlag & DT_STRAIGHTPATH_END) ? true : false;
744 bool offMeshConnection = (steerPosFlag & DT_STRAIGHTPATH_OFFMESH_CONNECTION) ? true : false;
745
746 // Find movement delta.
747 float delta[3], len;
748 dtVsub(delta, steerPos, iterPos);
749 len = dtMathSqrtf(dtVdot(delta, delta));
750 // If the steer target is end of path or off-mesh link, do not move past the location.
751 if ((endOfPath || offMeshConnection) && len < STEP_SIZE)
752 len = 1;
753 else
754 len = STEP_SIZE / len;
755 float moveTgt[3];
756 dtVmad(moveTgt, iterPos, delta, len);
757
758 // Move
759 float result[3];
760 dtPolyRef visited[16];
761 int nvisited = 0;
762 m_navQuery->moveAlongSurface(polys[0], iterPos, moveTgt, &m_filter,
763 result, visited, &nvisited, 16);
764
765 npolys = fixupCorridor(polys, npolys, MAX_POLYS, visited, nvisited);
766 npolys = fixupShortcuts(polys, npolys, m_navQuery);
767
768 float h = 0;
769 m_navQuery->getPolyHeight(polys[0], result, &h);
770 result[1] = h;
771 dtVcopy(iterPos, result);
772
773 // Handle end of path and off-mesh links when close enough.
774 if (endOfPath && inRange(iterPos, steerPos, SLOP, 1.0f))
775 {
776 // Reached end of path.
777 dtVcopy(iterPos, targetPos);
778 if (m_nsmoothPath < MAX_SMOOTH)
779 {
780 dtVcopy(&m_smoothPath[m_nsmoothPath*3], iterPos);
781 m_nsmoothPath++;
782 }
783 break;
784 }
785 else if (offMeshConnection && inRange(iterPos, steerPos, SLOP, 1.0f))
786 {
787 // Reached off-mesh connection.
788 float startPos[3], endPos[3];
789
790 // Advance the path up to and over the off-mesh connection.
791 dtPolyRef prevRef = 0, polyRef = polys[0];
792 int npos = 0;
793 while (npos < npolys && polyRef != steerPosRef)
794 {
795 prevRef = polyRef;
796 polyRef = polys[npos];
797 npos++;
798 }
799 for (int i = npos; i < npolys; ++i)
800 polys[i-npos] = polys[i];
801 npolys -= npos;
802
803 // Handle the connection.
804 dtStatus status = m_navMesh->getOffMeshConnectionPolyEndPoints(prevRef, polyRef, startPos, endPos);
805 if (dtStatusSucceed(status))
806 {
807 if (m_nsmoothPath < MAX_SMOOTH)
808 {
809 dtVcopy(&m_smoothPath[m_nsmoothPath*3], startPos);
810 m_nsmoothPath++;
811 // Hack to make the dotted path not visible during off-mesh connection.
812 if (m_nsmoothPath & 1)
813 {
814 dtVcopy(&m_smoothPath[m_nsmoothPath*3], startPos);
815 m_nsmoothPath++;
816 }
817 }
818 // Move position at the other side of the off-mesh link.
819 dtVcopy(iterPos, endPos);
820 float eh = 0.0f;
821 m_navQuery->getPolyHeight(polys[0], iterPos, &eh);
822 iterPos[1] = eh;
823 }
824 }
825
826 // Store results.
827 if (m_nsmoothPath < MAX_SMOOTH)
828 {
829 dtVcopy(&m_smoothPath[m_nsmoothPath*3], iterPos);
830 m_nsmoothPath++;
831 }
832 }
833 }
834
835 }
836 else
837 {
838 m_npolys = 0;
839 m_nsmoothPath = 0;
840 }
841 }
842 else if (m_toolMode == TOOLMODE_PATHFIND_STRAIGHT)
843 {
844 if (m_sposSet && m_eposSet && m_startRef && m_endRef)
845 {
846 #ifdef DUMP_REQS
847 printf("ps %f %f %f %f %f %f 0x%x 0x%x\n",
848 m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
849 m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
850 #endif
851 m_navQuery->findPath(m_startRef, m_endRef, m_spos, m_epos, &m_filter, m_polys, &m_npolys, MAX_POLYS);
852 m_nstraightPath = 0;
853 if (m_npolys)
854 {
855 // In case of partial path, make sure the end point is clamped to the last polygon.
856 float epos[3];
857 dtVcopy(epos, m_epos);
858 if (m_polys[m_npolys-1] != m_endRef)
859 m_navQuery->closestPointOnPoly(m_polys[m_npolys-1], m_epos, epos, 0);
860
861 m_navQuery->findStraightPath(m_spos, epos, m_polys, m_npolys,
862 m_straightPath, m_straightPathFlags,
863 m_straightPathPolys, &m_nstraightPath, MAX_POLYS, m_straightPathOptions);
864 }
865 }
866 else
867 {
868 m_npolys = 0;
869 m_nstraightPath = 0;
870 }
871 }
872 else if (m_toolMode == TOOLMODE_PATHFIND_SLICED)
873 {
874 if (m_sposSet && m_eposSet && m_startRef && m_endRef)
875 {
876 #ifdef DUMP_REQS
877 printf("ps %f %f %f %f %f %f 0x%x 0x%x\n",
878 m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
879 m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
880 #endif
881 m_npolys = 0;
882 m_nstraightPath = 0;
883
884 m_pathFindStatus = m_navQuery->initSlicedFindPath(m_startRef, m_endRef, m_spos, m_epos, &m_filter, DT_FINDPATH_ANY_ANGLE);
885 }
886 else
887 {
888 m_npolys = 0;
889 m_nstraightPath = 0;
890 }
891 }
892 else if (m_toolMode == TOOLMODE_RAYCAST)
893 {
894 m_nstraightPath = 0;
895 if (m_sposSet && m_eposSet && m_startRef)
896 {
897 #ifdef DUMP_REQS
898 printf("rc %f %f %f %f %f %f 0x%x 0x%x\n",
899 m_spos[0],m_spos[1],m_spos[2], m_epos[0],m_epos[1],m_epos[2],
900 m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
901 #endif
902 float t = 0;
903 m_npolys = 0;
904 m_nstraightPath = 2;
905 m_straightPath[0] = m_spos[0];
906 m_straightPath[1] = m_spos[1];
907 m_straightPath[2] = m_spos[2];
908 m_navQuery->raycast(m_startRef, m_spos, m_epos, &m_filter, &t, m_hitNormal, m_polys, &m_npolys, MAX_POLYS);
909 if (t > 1)
910 {
911 // No hit
912 dtVcopy(m_hitPos, m_epos);
913 m_hitResult = false;
914 }
915 else
916 {
917 // Hit
918 dtVlerp(m_hitPos, m_spos, m_epos, t);
919 m_hitResult = true;
920 }
921 // Adjust height.
922 if (m_npolys > 0)
923 {
924 float h = 0;
925 m_navQuery->getPolyHeight(m_polys[m_npolys-1], m_hitPos, &h);
926 m_hitPos[1] = h;
927 }
928 dtVcopy(&m_straightPath[3], m_hitPos);
929 }
930 }
931 else if (m_toolMode == TOOLMODE_DISTANCE_TO_WALL)
932 {
933 m_distanceToWall = 0;
934 if (m_sposSet && m_startRef)
935 {
936 #ifdef DUMP_REQS
937 printf("dw %f %f %f %f 0x%x 0x%x\n",
938 m_spos[0],m_spos[1],m_spos[2], 100.0f,
939 m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
940 #endif
941 m_distanceToWall = 0.0f;
942 m_navQuery->findDistanceToWall(m_startRef, m_spos, 100.0f, &m_filter, &m_distanceToWall, m_hitPos, m_hitNormal);
943 }
944 }
945 else if (m_toolMode == TOOLMODE_FIND_POLYS_IN_CIRCLE)
946 {
947 if (m_sposSet && m_startRef && m_eposSet)
948 {
949 const float dx = m_epos[0] - m_spos[0];
950 const float dz = m_epos[2] - m_spos[2];
951 float dist = sqrtf(dx*dx + dz*dz);
952 #ifdef DUMP_REQS
953 printf("fpc %f %f %f %f 0x%x 0x%x\n",
954 m_spos[0],m_spos[1],m_spos[2], dist,
955 m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
956 #endif
957 m_navQuery->findPolysAroundCircle(m_startRef, m_spos, dist, &m_filter,
958 m_polys, m_parent, 0, &m_npolys, MAX_POLYS);
959
960 }
961 }
962 else if (m_toolMode == TOOLMODE_FIND_POLYS_IN_SHAPE)
963 {
964 if (m_sposSet && m_startRef && m_eposSet)
965 {
966 const float nx = (m_epos[2] - m_spos[2])*0.25f;
967 const float nz = -(m_epos[0] - m_spos[0])*0.25f;
968 const float agentHeight = m_sample ? m_sample->getAgentHeight() : 0;
969
970 m_queryPoly[0] = m_spos[0] + nx*1.2f;
971 m_queryPoly[1] = m_spos[1] + agentHeight/2;
972 m_queryPoly[2] = m_spos[2] + nz*1.2f;
973
974 m_queryPoly[3] = m_spos[0] - nx*1.3f;
975 m_queryPoly[4] = m_spos[1] + agentHeight/2;
976 m_queryPoly[5] = m_spos[2] - nz*1.3f;
977
978 m_queryPoly[6] = m_epos[0] - nx*0.8f;
979 m_queryPoly[7] = m_epos[1] + agentHeight/2;
980 m_queryPoly[8] = m_epos[2] - nz*0.8f;
981
982 m_queryPoly[9] = m_epos[0] + nx;
983 m_queryPoly[10] = m_epos[1] + agentHeight/2;
984 m_queryPoly[11] = m_epos[2] + nz;
985
986 #ifdef DUMP_REQS
987 printf("fpp %f %f %f %f %f %f %f %f %f %f %f %f 0x%x 0x%x\n",
988 m_queryPoly[0],m_queryPoly[1],m_queryPoly[2],
989 m_queryPoly[3],m_queryPoly[4],m_queryPoly[5],
990 m_queryPoly[6],m_queryPoly[7],m_queryPoly[8],
991 m_queryPoly[9],m_queryPoly[10],m_queryPoly[11],
992 m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
993 #endif
994 m_navQuery->findPolysAroundShape(m_startRef, m_queryPoly, 4, &m_filter,
995 m_polys, m_parent, 0, &m_npolys, MAX_POLYS);
996 }
997 }
998 else if (m_toolMode == TOOLMODE_FIND_LOCAL_NEIGHBOURHOOD)
999 {
1000 if (m_sposSet && m_startRef)
1001 {
1002 #ifdef DUMP_REQS
1003 printf("fln %f %f %f %f 0x%x 0x%x\n",
1004 m_spos[0],m_spos[1],m_spos[2], m_neighbourhoodRadius,
1005 m_filter.getIncludeFlags(), m_filter.getExcludeFlags());
1006 #endif
1007 m_navQuery->findLocalNeighbourhood(m_startRef, m_spos, m_neighbourhoodRadius, &m_filter,
1008 m_polys, m_parent, &m_npolys, MAX_POLYS);
1009 }
1010 }
1011 }
1012
1013 static void getPolyCenter(dtNavMesh* navMesh, dtPolyRef ref, float* center)
1014 {
1015 center[0] = 0;
1016 center[1] = 0;
1017 center[2] = 0;
1018
1019 const dtMeshTile* tile = 0;
1020 const dtPoly* poly = 0;
1021 dtStatus status = navMesh->getTileAndPolyByRef(ref, &tile, &poly);
1022 if (dtStatusFailed(status))
1023 return;
1024
1025 for (int i = 0; i < (int)poly->vertCount; ++i)
1026 {
1027 const float* v = &tile->verts[poly->verts[i]*3];
1028 center[0] += v[0];
1029 center[1] += v[1];
1030 center[2] += v[2];
1031 }
1032 const float s = 1.0f / poly->vertCount;
1033 center[0] *= s;
1034 center[1] *= s;
1035 center[2] *= s;
1036 }
1037
1038
1039
1040 void NavMeshTesterTool::handleRender()
1041 {
1042 duDebugDraw& dd = m_sample->getDebugDraw();
1043
1044 static const unsigned int startCol = duRGBA(128,25,0,192);
1045 static const unsigned int endCol = duRGBA(51,102,0,129);
1046 static const unsigned int pathCol = duRGBA(0,0,0,64);
1047
1048 const float agentRadius = m_sample->getAgentRadius();
1049 const float agentHeight = m_sample->getAgentHeight();
1050 const float agentClimb = m_sample->getAgentClimb();
1051
1052 dd.depthMask(false);
1053 if (m_sposSet)
1054 drawAgent(m_spos, agentRadius, agentHeight, agentClimb, startCol);
1055 if (m_eposSet)
1056 drawAgent(m_epos, agentRadius, agentHeight, agentClimb, endCol);
1057 dd.depthMask(true);
1058
1059 if (!m_navMesh)
1060 {
1061 return;
1062 }
1063
1064 if (m_toolMode == TOOLMODE_PATHFIND_FOLLOW)
1065 {
1066 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_startRef, startCol);
1067 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_endRef, endCol);
1068
1069 if (m_npolys)
1070 {
1071 for (int i = 0; i < m_npolys; ++i)
1072 {
1073 if (m_polys[i] == m_startRef || m_polys[i] == m_endRef)
1074 continue;
1075 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
1076 }
1077 }
1078
1079 if (m_nsmoothPath)
1080 {
1081 dd.depthMask(false);
1082 const unsigned int spathCol = duRGBA(0,0,0,220);
1083 dd.begin(DU_DRAW_LINES, 3.0f);
1084 for (int i = 0; i < m_nsmoothPath; ++i)
1085 dd.vertex(m_smoothPath[i*3], m_smoothPath[i*3+1]+0.1f, m_smoothPath[i*3+2], spathCol);
1086 dd.end();
1087 dd.depthMask(true);
1088 }
1089
1090 if (m_pathIterNum)
1091 {
1092 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_pathIterPolys[0], duRGBA(255,255,255,128));
1093
1094 dd.depthMask(false);
1095 dd.begin(DU_DRAW_LINES, 1.0f);
1096
1097 const unsigned int prevCol = duRGBA(255,192,0,220);
1098 const unsigned int curCol = duRGBA(255,255,255,220);
1099 const unsigned int steerCol = duRGBA(0,192,255,220);
1100
1101 dd.vertex(m_prevIterPos[0],m_prevIterPos[1]-0.3f,m_prevIterPos[2], prevCol);
1102 dd.vertex(m_prevIterPos[0],m_prevIterPos[1]+0.3f,m_prevIterPos[2], prevCol);
1103
1104 dd.vertex(m_iterPos[0],m_iterPos[1]-0.3f,m_iterPos[2], curCol);
1105 dd.vertex(m_iterPos[0],m_iterPos[1]+0.3f,m_iterPos[2], curCol);
1106
1107 dd.vertex(m_prevIterPos[0],m_prevIterPos[1]+0.3f,m_prevIterPos[2], prevCol);
1108 dd.vertex(m_iterPos[0],m_iterPos[1]+0.3f,m_iterPos[2], prevCol);
1109
1110 dd.vertex(m_prevIterPos[0],m_prevIterPos[1]+0.3f,m_prevIterPos[2], steerCol);
1111 dd.vertex(m_steerPos[0],m_steerPos[1]+0.3f,m_steerPos[2], steerCol);
1112
1113 for (int i = 0; i < m_steerPointCount-1; ++i)
1114 {
1115 dd.vertex(m_steerPoints[i*3+0],m_steerPoints[i*3+1]+0.2f,m_steerPoints[i*3+2], duDarkenCol(steerCol));
1116 dd.vertex(m_steerPoints[(i+1)*3+0],m_steerPoints[(i+1)*3+1]+0.2f,m_steerPoints[(i+1)*3+2], duDarkenCol(steerCol));
1117 }
1118
1119 dd.end();
1120 dd.depthMask(true);
1121 }
1122 }
1123 else if (m_toolMode == TOOLMODE_PATHFIND_STRAIGHT ||
1124 m_toolMode == TOOLMODE_PATHFIND_SLICED)
1125 {
1126 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_startRef, startCol);
1127 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_endRef, endCol);
1128
1129 if (m_npolys)
1130 {
1131 for (int i = 0; i < m_npolys; ++i)
1132 {
1133 if (m_polys[i] == m_startRef || m_polys[i] == m_endRef)
1134 continue;
1135 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
1136 }
1137 }
1138
1139 if (m_nstraightPath)
1140 {
1141 dd.depthMask(false);
1142 const unsigned int spathCol = duRGBA(64,16,0,220);
1143 const unsigned int offMeshCol = duRGBA(128,96,0,220);
1144 dd.begin(DU_DRAW_LINES, 2.0f);
1145 for (int i = 0; i < m_nstraightPath-1; ++i)
1146 {
1147 unsigned int col;
1148 if (m_straightPathFlags[i] & DT_STRAIGHTPATH_OFFMESH_CONNECTION)
1149 col = offMeshCol;
1150 else
1151 col = spathCol;
1152
1153 dd.vertex(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2], col);
1154 dd.vertex(m_straightPath[(i+1)*3], m_straightPath[(i+1)*3+1]+0.4f, m_straightPath[(i+1)*3+2], col);
1155 }
1156 dd.end();
1157 dd.begin(DU_DRAW_POINTS, 6.0f);
1158 for (int i = 0; i < m_nstraightPath; ++i)
1159 {
1160 unsigned int col;
1161 if (m_straightPathFlags[i] & DT_STRAIGHTPATH_START)
1162 col = startCol;
1163 else if (m_straightPathFlags[i] & DT_STRAIGHTPATH_END)
1164 col = endCol;
1165 else if (m_straightPathFlags[i] & DT_STRAIGHTPATH_OFFMESH_CONNECTION)
1166 col = offMeshCol;
1167 else
1168 col = spathCol;
1169 dd.vertex(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2], col);
1170 }
1171 dd.end();
1172 dd.depthMask(true);
1173 }
1174 }
1175 else if (m_toolMode == TOOLMODE_RAYCAST)
1176 {
1177 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_startRef, startCol);
1178
1179 if (m_nstraightPath)
1180 {
1181 for (int i = 1; i < m_npolys; ++i)
1182 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
1183
1184 dd.depthMask(false);
1185 const unsigned int spathCol = m_hitResult ? duRGBA(64,16,0,220) : duRGBA(240,240,240,220);
1186 dd.begin(DU_DRAW_LINES, 2.0f);
1187 for (int i = 0; i < m_nstraightPath-1; ++i)
1188 {
1189 dd.vertex(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2], spathCol);
1190 dd.vertex(m_straightPath[(i+1)*3], m_straightPath[(i+1)*3+1]+0.4f, m_straightPath[(i+1)*3+2], spathCol);
1191 }
1192 dd.end();
1193 dd.begin(DU_DRAW_POINTS, 4.0f);
1194 for (int i = 0; i < m_nstraightPath; ++i)
1195 dd.vertex(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2], spathCol);
1196 dd.end();
1197
1198 if (m_hitResult)
1199 {
1200 const unsigned int hitCol = duRGBA(0,0,0,128);
1201 dd.begin(DU_DRAW_LINES, 2.0f);
1202 dd.vertex(m_hitPos[0], m_hitPos[1] + 0.4f, m_hitPos[2], hitCol);
1203 dd.vertex(m_hitPos[0] + m_hitNormal[0]*agentRadius,
1204 m_hitPos[1] + 0.4f + m_hitNormal[1]*agentRadius,
1205 m_hitPos[2] + m_hitNormal[2]*agentRadius, hitCol);
1206 dd.end();
1207 }
1208 dd.depthMask(true);
1209 }
1210 }
1211 else if (m_toolMode == TOOLMODE_DISTANCE_TO_WALL)
1212 {
1213 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_startRef, startCol);
1214 dd.depthMask(false);
1215 duDebugDrawCircle(&dd, m_spos[0], m_spos[1]+agentHeight/2, m_spos[2], m_distanceToWall, duRGBA(64,16,0,220), 2.0f);
1216 dd.begin(DU_DRAW_LINES, 3.0f);
1217 dd.vertex(m_hitPos[0], m_hitPos[1] + 0.02f, m_hitPos[2], duRGBA(0,0,0,192));
1218 dd.vertex(m_hitPos[0], m_hitPos[1] + agentHeight, m_hitPos[2], duRGBA(0,0,0,192));
1219 dd.end();
1220 dd.depthMask(true);
1221 }
1222 else if (m_toolMode == TOOLMODE_FIND_POLYS_IN_CIRCLE)
1223 {
1224 for (int i = 0; i < m_npolys; ++i)
1225 {
1226 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
1227 dd.depthMask(false);
1228 if (m_parent[i])
1229 {
1230 float p0[3], p1[3];
1231 dd.depthMask(false);
1232 getPolyCenter(m_navMesh, m_parent[i], p0);
1233 getPolyCenter(m_navMesh, m_polys[i], p1);
1234 duDebugDrawArc(&dd, p0[0],p0[1],p0[2], p1[0],p1[1],p1[2], 0.25f, 0.0f, 0.4f, duRGBA(0,0,0,128), 2.0f);
1235 dd.depthMask(true);
1236 }
1237 dd.depthMask(true);
1238 }
1239
1240 if (m_sposSet && m_eposSet)
1241 {
1242 dd.depthMask(false);
1243 const float dx = m_epos[0] - m_spos[0];
1244 const float dz = m_epos[2] - m_spos[2];
1245 const float dist = sqrtf(dx*dx + dz*dz);
1246 duDebugDrawCircle(&dd, m_spos[0], m_spos[1]+agentHeight/2, m_spos[2], dist, duRGBA(64,16,0,220), 2.0f);
1247 dd.depthMask(true);
1248 }
1249 }
1250 else if (m_toolMode == TOOLMODE_FIND_POLYS_IN_SHAPE)
1251 {
1252 for (int i = 0; i < m_npolys; ++i)
1253 {
1254 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
1255 dd.depthMask(false);
1256 if (m_parent[i])
1257 {
1258 float p0[3], p1[3];
1259 dd.depthMask(false);
1260 getPolyCenter(m_navMesh, m_parent[i], p0);
1261 getPolyCenter(m_navMesh, m_polys[i], p1);
1262 duDebugDrawArc(&dd, p0[0],p0[1],p0[2], p1[0],p1[1],p1[2], 0.25f, 0.0f, 0.4f, duRGBA(0,0,0,128), 2.0f);
1263 dd.depthMask(true);
1264 }
1265 dd.depthMask(true);
1266 }
1267
1268 if (m_sposSet && m_eposSet)
1269 {
1270 dd.depthMask(false);
1271 const unsigned int col = duRGBA(64,16,0,220);
1272 dd.begin(DU_DRAW_LINES, 2.0f);
1273 for (int i = 0, j = 3; i < 4; j=i++)
1274 {
1275 const float* p0 = &m_queryPoly[j*3];
1276 const float* p1 = &m_queryPoly[i*3];
1277 dd.vertex(p0, col);
1278 dd.vertex(p1, col);
1279 }
1280 dd.end();
1281 dd.depthMask(true);
1282 }
1283 }
1284 else if (m_toolMode == TOOLMODE_FIND_LOCAL_NEIGHBOURHOOD)
1285 {
1286 for (int i = 0; i < m_npolys; ++i)
1287 {
1288 duDebugDrawNavMeshPoly(&dd, *m_navMesh, m_polys[i], pathCol);
1289 dd.depthMask(false);
1290 if (m_parent[i])
1291 {
1292 float p0[3], p1[3];
1293 dd.depthMask(false);
1294 getPolyCenter(m_navMesh, m_parent[i], p0);
1295 getPolyCenter(m_navMesh, m_polys[i], p1);
1296 duDebugDrawArc(&dd, p0[0],p0[1],p0[2], p1[0],p1[1],p1[2], 0.25f, 0.0f, 0.4f, duRGBA(0,0,0,128), 2.0f);
1297 dd.depthMask(true);
1298 }
1299
1300 static const int MAX_SEGS = DT_VERTS_PER_POLYGON*4;
1301 float segs[MAX_SEGS*6];
1302 dtPolyRef refs[MAX_SEGS];
1303 memset(refs, 0, sizeof(dtPolyRef)*MAX_SEGS);
1304 int nsegs = 0;
1305 m_navQuery->getPolyWallSegments(m_polys[i], &m_filter, segs, refs, &nsegs, MAX_SEGS);
1306 dd.begin(DU_DRAW_LINES, 2.0f);
1307 for (int j = 0; j < nsegs; ++j)
1308 {
1309 const float* s = &segs[j*6];
1310
1311 // Skip too distant segments.
1312 float tseg;
1313 float distSqr = dtDistancePtSegSqr2D(m_spos, s, s+3, tseg);
1314 if (distSqr > dtSqr(m_neighbourhoodRadius))
1315 continue;
1316
1317 float delta[3], norm[3], p0[3], p1[3];
1318 dtVsub(delta, s+3,s);
1319 dtVmad(p0, s, delta, 0.5f);
1320 norm[0] = delta[2];
1321 norm[1] = 0;
1322 norm[2] = -delta[0];
1323 dtVnormalize(norm);
1324 dtVmad(p1, p0, norm, agentRadius*0.5f);
1325
1326 // Skip backfacing segments.
1327 if (refs[j])
1328 {
1329 unsigned int col = duRGBA(255,255,255,32);
1330 dd.vertex(s[0],s[1]+agentClimb,s[2],col);
1331 dd.vertex(s[3],s[4]+agentClimb,s[5],col);
1332 }
1333 else
1334 {
1335 unsigned int col = duRGBA(192,32,16,192);
1336 if (dtTriArea2D(m_spos, s, s+3) < 0.0f)
1337 col = duRGBA(96,32,16,192);
1338
1339 dd.vertex(p0[0],p0[1]+agentClimb,p0[2],col);
1340 dd.vertex(p1[0],p1[1]+agentClimb,p1[2],col);
1341
1342 dd.vertex(s[0],s[1]+agentClimb,s[2],col);
1343 dd.vertex(s[3],s[4]+agentClimb,s[5],col);
1344 }
1345 }
1346 dd.end();
1347
1348 dd.depthMask(true);
1349 }
1350
1351 if (m_sposSet)
1352 {
1353 dd.depthMask(false);
1354 duDebugDrawCircle(&dd, m_spos[0], m_spos[1]+agentHeight/2, m_spos[2], m_neighbourhoodRadius, duRGBA(64,16,0,220), 2.0f);
1355 dd.depthMask(true);
1356 }
1357 }
1358
1359 if (m_nrandPoints > 0)
1360 {
1361 dd.begin(DU_DRAW_POINTS, 6.0f);
1362 for (int i = 0; i < m_nrandPoints; i++)
1363 {
1364 const float* p = &m_randPoints[i*3];
1365 dd.vertex(p[0],p[1]+0.1f,p[2], duRGBA(220,32,16,192));
1366 }
1367 dd.end();
1368
1369 if (m_randPointsInCircle && m_sposSet)
1370 {
1371 duDebugDrawCircle(&dd, m_spos[0], m_spos[1]+agentHeight/2, m_spos[2], m_randomRadius, duRGBA(64,16,0,220), 2.0f);
1372 }
1373 }
1374 }
1375
1376 void NavMeshTesterTool::handleRenderOverlay(double* proj, double* model, int* view)
1377 {
1378 GLdouble x, y, z;
1379
1380 // Draw start and end point labels
1381 if (m_sposSet && gluProject((GLdouble)m_spos[0], (GLdouble)m_spos[1], (GLdouble)m_spos[2],
1382 model, proj, view, &x, &y, &z))
1383 {
1384 imguiDrawText((int)x, (int)(y-25), IMGUI_ALIGN_CENTER, "Start", imguiRGBA(0,0,0,220));
1385 }
1386 if (m_eposSet && gluProject((GLdouble)m_epos[0], (GLdouble)m_epos[1], (GLdouble)m_epos[2],
1387 model, proj, view, &x, &y, &z))
1388 {
1389 imguiDrawText((int)x, (int)(y-25), IMGUI_ALIGN_CENTER, "End", imguiRGBA(0,0,0,220));
1390 }
1391
1392 // Tool help
1393 const int h = view[3];
1394 imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "LMB+SHIFT: Set start location LMB: Set end location", imguiRGBA(255,255,255,192));
1395 }
1396
1397 void NavMeshTesterTool::drawAgent(const float* pos, float r, float h, float c, const unsigned int col)
1398 {
1399 duDebugDraw& dd = m_sample->getDebugDraw();
1400
1401 dd.depthMask(false);
1402
1403 // Agent dimensions.
1404 duDebugDrawCylinderWire(&dd, pos[0]-r, pos[1]+0.02f, pos[2]-r, pos[0]+r, pos[1]+h, pos[2]+r, col, 2.0f);
1405
1406 duDebugDrawCircle(&dd, pos[0],pos[1]+c,pos[2],r,duRGBA(0,0,0,64),1.0f);
1407
1408 unsigned int colb = duRGBA(0,0,0,196);
1409 dd.begin(DU_DRAW_LINES);
1410 dd.vertex(pos[0], pos[1]-c, pos[2], colb);
1411 dd.vertex(pos[0], pos[1]+c, pos[2], colb);
1412 dd.vertex(pos[0]-r/2, pos[1]+0.02f, pos[2], colb);
1413 dd.vertex(pos[0]+r/2, pos[1]+0.02f, pos[2], colb);
1414 dd.vertex(pos[0], pos[1]+0.02f, pos[2]-r/2, colb);
1415 dd.vertex(pos[0], pos[1]+0.02f, pos[2]+r/2, colb);
1416 dd.end();
1417
1418 dd.depthMask(true);
1419 }