Debian Codegraph @

RecastDemo/Source/NavMeshTesterTool.cpp

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