Debian Codegraph @

RecastDemo/Source/Sample.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 <math.h>
20 #include <stdio.h>
21 #include "Sample.h"
22 #include "InputGeom.h"
23 #include "Recast.h"
24 #include "RecastDebugDraw.h"
25 #include "DetourDebugDraw.h"
26 #include "DetourNavMesh.h"
27 #include "DetourNavMeshQuery.h"
28 #include "DetourCrowd.h"
29 #include "imgui.h"
30 #include "SDL.h"
31 #include "SDL_opengl.h"
32
33 #ifdef WIN32
34 # define snprintf _snprintf
35 #endif
36
37 SampleTool::~SampleTool()
38 {
39 // Defined out of line to fix the weak v-tables warning
40 }
41
42 SampleToolState::~SampleToolState()
43 {
44 // Defined out of line to fix the weak v-tables warning
45 }
46
47 unsigned int SampleDebugDraw::areaToCol(unsigned int area)
48 {
49 switch(area)
50 {
51 // Ground (0) : light blue
52 case SAMPLE_POLYAREA_GROUND: return duRGBA(0, 192, 255, 255);
53 // Water : blue
54 case SAMPLE_POLYAREA_WATER: return duRGBA(0, 0, 255, 255);
55 // Road : brown
56 case SAMPLE_POLYAREA_ROAD: return duRGBA(50, 20, 12, 255);
57 // Door : cyan
58 case SAMPLE_POLYAREA_DOOR: return duRGBA(0, 255, 255, 255);
59 // Grass : green
60 case SAMPLE_POLYAREA_GRASS: return duRGBA(0, 255, 0, 255);
61 // Jump : yellow
62 case SAMPLE_POLYAREA_JUMP: return duRGBA(255, 255, 0, 255);
63 // Unexpected : red
64 default: return duRGBA(255, 0, 0, 255);
65 }
66 }
67
68 Sample::Sample() :
69 m_geom(0),
70 m_navMesh(0),
71 m_navQuery(0),
72 m_crowd(0),
73 m_navMeshDrawFlags(DU_DRAWNAVMESH_OFFMESHCONS|DU_DRAWNAVMESH_CLOSEDLIST),
74 m_filterLowHangingObstacles(true),
75 m_filterLedgeSpans(true),
76 m_filterWalkableLowHeightSpans(true),
77 m_tool(0),
78 m_ctx(0)
79 {
80 resetCommonSettings();
81 m_navQuery = dtAllocNavMeshQuery();
82 m_crowd = dtAllocCrowd();
83
84 for (int i = 0; i < MAX_TOOLS; i++)
85 m_toolStates[i] = 0;
86 }
87
88 Sample::~Sample()
89 {
90 dtFreeNavMeshQuery(m_navQuery);
91 dtFreeNavMesh(m_navMesh);
92 dtFreeCrowd(m_crowd);
93 delete m_tool;
94 for (int i = 0; i < MAX_TOOLS; i++)
95 delete m_toolStates[i];
96 }
97
98 void Sample::setTool(SampleTool* tool)
99 {
100 delete m_tool;
101 m_tool = tool;
102 if (tool)
103 m_tool->init(this);
104 }
105
106 void Sample::handleSettings()
107 {
108 }
109
110 void Sample::handleTools()
111 {
112 }
113
114 void Sample::handleDebugMode()
115 {
116 }
117
118 void Sample::handleRender()
119 {
120 if (!m_geom)
121 return;
122
123 // Draw mesh
124 duDebugDrawTriMesh(&m_dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(),
125 m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(), 0, 1.0f);
126 // Draw bounds
127 const float* bmin = m_geom->getMeshBoundsMin();
128 const float* bmax = m_geom->getMeshBoundsMax();
129 duDebugDrawBoxWire(&m_dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], duRGBA(255,255,255,128), 1.0f);
130 }
131
132 void Sample::handleRenderOverlay(double* /*proj*/, double* /*model*/, int* /*view*/)
133 {
134 }
135
136 void Sample::handleMeshChanged(InputGeom* geom)
137 {
138 m_geom = geom;
139
140 const BuildSettings* buildSettings = geom->getBuildSettings();
141 if (buildSettings)
142 {
143 m_cellSize = buildSettings->cellSize;
144 m_cellHeight = buildSettings->cellHeight;
145 m_agentHeight = buildSettings->agentHeight;
146 m_agentRadius = buildSettings->agentRadius;
147 m_agentMaxClimb = buildSettings->agentMaxClimb;
148 m_agentMaxSlope = buildSettings->agentMaxSlope;
149 m_regionMinSize = buildSettings->regionMinSize;
150 m_regionMergeSize = buildSettings->regionMergeSize;
151 m_edgeMaxLen = buildSettings->edgeMaxLen;
152 m_edgeMaxError = buildSettings->edgeMaxError;
153 m_vertsPerPoly = buildSettings->vertsPerPoly;
154 m_detailSampleDist = buildSettings->detailSampleDist;
155 m_detailSampleMaxError = buildSettings->detailSampleMaxError;
156 m_partitionType = buildSettings->partitionType;
157 }
158 }
159
160 void Sample::collectSettings(BuildSettings& settings)
161 {
162 settings.cellSize = m_cellSize;
163 settings.cellHeight = m_cellHeight;
164 settings.agentHeight = m_agentHeight;
165 settings.agentRadius = m_agentRadius;
166 settings.agentMaxClimb = m_agentMaxClimb;
167 settings.agentMaxSlope = m_agentMaxSlope;
168 settings.regionMinSize = m_regionMinSize;
169 settings.regionMergeSize = m_regionMergeSize;
170 settings.edgeMaxLen = m_edgeMaxLen;
171 settings.edgeMaxError = m_edgeMaxError;
172 settings.vertsPerPoly = m_vertsPerPoly;
173 settings.detailSampleDist = m_detailSampleDist;
174 settings.detailSampleMaxError = m_detailSampleMaxError;
175 settings.partitionType = m_partitionType;
176 }
177
178
179 void Sample::resetCommonSettings()
180 {
181 m_cellSize = 0.3f;
182 m_cellHeight = 0.2f;
183 m_agentHeight = 2.0f;
184 m_agentRadius = 0.6f;
185 m_agentMaxClimb = 0.9f;
186 m_agentMaxSlope = 45.0f;
187 m_regionMinSize = 8;
188 m_regionMergeSize = 20;
189 m_edgeMaxLen = 12.0f;
190 m_edgeMaxError = 1.3f;
191 m_vertsPerPoly = 6.0f;
192 m_detailSampleDist = 6.0f;
193 m_detailSampleMaxError = 1.0f;
194 m_partitionType = SAMPLE_PARTITION_WATERSHED;
195 }
196
197 void Sample::handleCommonSettings()
198 {
199 imguiLabel("Rasterization");
200 imguiSlider("Cell Size", &m_cellSize, 0.1f, 1.0f, 0.01f);
201 imguiSlider("Cell Height", &m_cellHeight, 0.1f, 1.0f, 0.01f);
202
203 if (m_geom)
204 {
205 const float* bmin = m_geom->getNavMeshBoundsMin();
206 const float* bmax = m_geom->getNavMeshBoundsMax();
207 int gw = 0, gh = 0;
208 rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
209 char text[64];
210 snprintf(text, 64, "Voxels %d x %d", gw, gh);
211 imguiValue(text);
212 }
213
214 imguiSeparator();
215 imguiLabel("Agent");
216 imguiSlider("Height", &m_agentHeight, 0.1f, 5.0f, 0.1f);
217 imguiSlider("Radius", &m_agentRadius, 0.0f, 5.0f, 0.1f);
218 imguiSlider("Max Climb", &m_agentMaxClimb, 0.1f, 5.0f, 0.1f);
219 imguiSlider("Max Slope", &m_agentMaxSlope, 0.0f, 90.0f, 1.0f);
220
221 imguiSeparator();
222 imguiLabel("Region");
223 imguiSlider("Min Region Size", &m_regionMinSize, 0.0f, 150.0f, 1.0f);
224 imguiSlider("Merged Region Size", &m_regionMergeSize, 0.0f, 150.0f, 1.0f);
225
226 imguiSeparator();
227 imguiLabel("Partitioning");
228 if (imguiCheck("Watershed", m_partitionType == SAMPLE_PARTITION_WATERSHED))
229 m_partitionType = SAMPLE_PARTITION_WATERSHED;
230 if (imguiCheck("Monotone", m_partitionType == SAMPLE_PARTITION_MONOTONE))
231 m_partitionType = SAMPLE_PARTITION_MONOTONE;
232 if (imguiCheck("Layers", m_partitionType == SAMPLE_PARTITION_LAYERS))
233 m_partitionType = SAMPLE_PARTITION_LAYERS;
234
235 imguiSeparator();
236 imguiLabel("Filtering");
237 if (imguiCheck("Low Hanging Obstacles", m_filterLowHangingObstacles))
238 m_filterLowHangingObstacles = !m_filterLowHangingObstacles;
239 if (imguiCheck("Ledge Spans", m_filterLedgeSpans))
240 m_filterLedgeSpans= !m_filterLedgeSpans;
241 if (imguiCheck("Walkable Low Height Spans", m_filterWalkableLowHeightSpans))
242 m_filterWalkableLowHeightSpans = !m_filterWalkableLowHeightSpans;
243
244 imguiSeparator();
245 imguiLabel("Polygonization");
246 imguiSlider("Max Edge Length", &m_edgeMaxLen, 0.0f, 50.0f, 1.0f);
247 imguiSlider("Max Edge Error", &m_edgeMaxError, 0.1f, 3.0f, 0.1f);
248 imguiSlider("Verts Per Poly", &m_vertsPerPoly, 3.0f, 12.0f, 1.0f);
249
250 imguiSeparator();
251 imguiLabel("Detail Mesh");
252 imguiSlider("Sample Distance", &m_detailSampleDist, 0.0f, 16.0f, 1.0f);
253 imguiSlider("Max Sample Error", &m_detailSampleMaxError, 0.0f, 16.0f, 1.0f);
254
255 imguiSeparator();
256 }
257
258 void Sample::handleClick(const float* s, const float* p, bool shift)
259 {
260 if (m_tool)
261 m_tool->handleClick(s, p, shift);
262 }
263
264 void Sample::handleToggle()
265 {
266 if (m_tool)
267 m_tool->handleToggle();
268 }
269
270 void Sample::handleStep()
271 {
272 if (m_tool)
273 m_tool->handleStep();
274 }
275
276 bool Sample::handleBuild()
277 {
278 return true;
279 }
280
281 void Sample::handleUpdate(const float dt)
282 {
283 if (m_tool)
284 m_tool->handleUpdate(dt);
285 updateToolStates(dt);
286 }
287
288
289 void Sample::updateToolStates(const float dt)
290 {
291 for (int i = 0; i < MAX_TOOLS; i++)
292 {
293 if (m_toolStates[i])
294 m_toolStates[i]->handleUpdate(dt);
295 }
296 }
297
298 void Sample::initToolStates(Sample* sample)
299 {
300 for (int i = 0; i < MAX_TOOLS; i++)
301 {
302 if (m_toolStates[i])
303 m_toolStates[i]->init(sample);
304 }
305 }
306
307 void Sample::resetToolStates()
308 {
309 for (int i = 0; i < MAX_TOOLS; i++)
310 {
311 if (m_toolStates[i])
312 m_toolStates[i]->reset();
313 }
314 }
315
316 void Sample::renderToolStates()
317 {
318 for (int i = 0; i < MAX_TOOLS; i++)
319 {
320 if (m_toolStates[i])
321 m_toolStates[i]->handleRender();
322 }
323 }
324
325 void Sample::renderOverlayToolStates(double* proj, double* model, int* view)
326 {
327 for (int i = 0; i < MAX_TOOLS; i++)
328 {
329 if (m_toolStates[i])
330 m_toolStates[i]->handleRenderOverlay(proj, model, view);
331 }
332 }
333
334 static const int NAVMESHSET_MAGIC = 'M'<<24 | 'S'<<16 | 'E'<<8 | 'T'; //'MSET';
335 static const int NAVMESHSET_VERSION = 1;
336
337 struct NavMeshSetHeader
338 {
339 int magic;
340 int version;
341 int numTiles;
342 dtNavMeshParams params;
343 };
344
345 struct NavMeshTileHeader
346 {
347 dtTileRef tileRef;
348 int dataSize;
349 };
350
351 dtNavMesh* Sample::loadAll(const char* path)
352 {
353 FILE* fp = fopen(path, "rb");
354 if (!fp) return 0;
355
356 // Read header.
357 NavMeshSetHeader header;
358 size_t readLen = fread(&header, sizeof(NavMeshSetHeader), 1, fp);
359 if (readLen != 1)
360 {
361 fclose(fp);
362 return 0;
363 }
364 if (header.magic != NAVMESHSET_MAGIC)
365 {
366 fclose(fp);
367 return 0;
368 }
369 if (header.version != NAVMESHSET_VERSION)
370 {
371 fclose(fp);
372 return 0;
373 }
374
375 dtNavMesh* mesh = dtAllocNavMesh();
376 if (!mesh)
377 {
378 fclose(fp);
379 return 0;
380 }
381 dtStatus status = mesh->init(&header.params);
382 if (dtStatusFailed(status))
383 {
384 fclose(fp);
385 return 0;
386 }
387
388 // Read tiles.
389 for (int i = 0; i < header.numTiles; ++i)
390 {
391 NavMeshTileHeader tileHeader;
392 readLen = fread(&tileHeader, sizeof(tileHeader), 1, fp);
393 if (readLen != 1)
394 {
395 fclose(fp);
396 return 0;
397 }
398
399 if (!tileHeader.tileRef || !tileHeader.dataSize)
400 break;
401
402 unsigned char* data = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM);
403 if (!data) break;
404 memset(data, 0, tileHeader.dataSize);
405 readLen = fread(data, tileHeader.dataSize, 1, fp);
406 if (readLen != 1)
407 {
408 dtFree(data);
409 fclose(fp);
410 return 0;
411 }
412
413 mesh->addTile(data, tileHeader.dataSize, DT_TILE_FREE_DATA, tileHeader.tileRef, 0);
414 }
415
416 fclose(fp);
417
418 return mesh;
419 }
420
421 void Sample::saveAll(const char* path, const dtNavMesh* mesh)
422 {
423 if (!mesh) return;
424
425 FILE* fp = fopen(path, "wb");
426 if (!fp)
427 return;
428
429 // Store header.
430 NavMeshSetHeader header;
431 header.magic = NAVMESHSET_MAGIC;
432 header.version = NAVMESHSET_VERSION;
433 header.numTiles = 0;
434 for (int i = 0; i < mesh->getMaxTiles(); ++i)
435 {
436 const dtMeshTile* tile = mesh->getTile(i);
437 if (!tile || !tile->header || !tile->dataSize) continue;
438 header.numTiles++;
439 }
440 memcpy(&header.params, mesh->getParams(), sizeof(dtNavMeshParams));
441 fwrite(&header, sizeof(NavMeshSetHeader), 1, fp);
442
443 // Store tiles.
444 for (int i = 0; i < mesh->getMaxTiles(); ++i)
445 {
446 const dtMeshTile* tile = mesh->getTile(i);
447 if (!tile || !tile->header || !tile->dataSize) continue;
448
449 NavMeshTileHeader tileHeader;
450 tileHeader.tileRef = mesh->getTileRef(tile);
451 tileHeader.dataSize = tile->dataSize;
452 fwrite(&tileHeader, sizeof(tileHeader), 1, fp);
453
454 fwrite(tile->data, tile->dataSize, 1, fp);
455 }
456
457 fclose(fp);
458 }