Sample_TempObstacles.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  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. #define _USE_MATH_DEFINES
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <float.h>
  23. #include <new>
  24. #include "SDL.h"
  25. #include "SDL_opengl.h"
  26. #ifdef __APPLE__
  27. # include <OpenGL/glu.h>
  28. #else
  29. # include <GL/glu.h>
  30. #endif
  31. #include "imgui.h"
  32. #include "InputGeom.h"
  33. #include "Sample.h"
  34. #include "Sample_TempObstacles.h"
  35. #include "Recast.h"
  36. #include "RecastDebugDraw.h"
  37. #include "DetourAssert.h"
  38. #include "DetourNavMesh.h"
  39. #include "DetourNavMeshBuilder.h"
  40. #include "DetourDebugDraw.h"
  41. #include "DetourCommon.h"
  42. #include "DetourTileCache.h"
  43. #include "NavMeshTesterTool.h"
  44. #include "OffMeshConnectionTool.h"
  45. #include "ConvexVolumeTool.h"
  46. #include "CrowdTool.h"
  47. #include "RecastAlloc.h"
  48. #include "RecastAssert.h"
  49. #include "fastlz.h"
  50. #ifdef WIN32
  51. # define snprintf _snprintf
  52. #endif
  53. // This value specifies how many layers (or "floors") each navmesh tile is expected to have.
  54. static const int EXPECTED_LAYERS_PER_TILE = 4;
  55. static bool isectSegAABB(const float* sp, const float* sq,
  56. const float* amin, const float* amax,
  57. float& tmin, float& tmax)
  58. {
  59. static const float EPS = 1e-6f;
  60. float d[3];
  61. rcVsub(d, sq, sp);
  62. tmin = 0; // set to -FLT_MAX to get first hit on line
  63. tmax = FLT_MAX; // set to max distance ray can travel (for segment)
  64. // For all three slabs
  65. for (int i = 0; i < 3; i++)
  66. {
  67. if (fabsf(d[i]) < EPS)
  68. {
  69. // Ray is parallel to slab. No hit if origin not within slab
  70. if (sp[i] < amin[i] || sp[i] > amax[i])
  71. return false;
  72. }
  73. else
  74. {
  75. // Compute intersection t value of ray with near and far plane of slab
  76. const float ood = 1.0f / d[i];
  77. float t1 = (amin[i] - sp[i]) * ood;
  78. float t2 = (amax[i] - sp[i]) * ood;
  79. // Make t1 be intersection with near plane, t2 with far plane
  80. if (t1 > t2) rcSwap(t1, t2);
  81. // Compute the intersection of slab intersections intervals
  82. if (t1 > tmin) tmin = t1;
  83. if (t2 < tmax) tmax = t2;
  84. // Exit with no collision as soon as slab intersection becomes empty
  85. if (tmin > tmax) return false;
  86. }
  87. }
  88. return true;
  89. }
  90. static int calcLayerBufferSize(const int gridWidth, const int gridHeight)
  91. {
  92. const int headerSize = dtAlign4(sizeof(dtTileCacheLayerHeader));
  93. const int gridSize = gridWidth * gridHeight;
  94. return headerSize + gridSize*4;
  95. }
  96. struct FastLZCompressor : public dtTileCacheCompressor
  97. {
  98. virtual int maxCompressedSize(const int bufferSize)
  99. {
  100. return (int)(bufferSize* 1.05f);
  101. }
  102. virtual dtStatus compress(const unsigned char* buffer, const int bufferSize,
  103. unsigned char* compressed, const int /*maxCompressedSize*/, int* compressedSize)
  104. {
  105. *compressedSize = fastlz_compress((const void *const)buffer, bufferSize, compressed);
  106. return DT_SUCCESS;
  107. }
  108. virtual dtStatus decompress(const unsigned char* compressed, const int compressedSize,
  109. unsigned char* buffer, const int maxBufferSize, int* bufferSize)
  110. {
  111. *bufferSize = fastlz_decompress(compressed, compressedSize, buffer, maxBufferSize);
  112. return *bufferSize < 0 ? DT_FAILURE : DT_SUCCESS;
  113. }
  114. };
  115. struct LinearAllocator : public dtTileCacheAlloc
  116. {
  117. unsigned char* buffer;
  118. size_t capacity;
  119. size_t top;
  120. size_t high;
  121. LinearAllocator(const size_t cap) : buffer(0), capacity(0), top(0), high(0)
  122. {
  123. resize(cap);
  124. }
  125. ~LinearAllocator()
  126. {
  127. dtFree(buffer);
  128. }
  129. void resize(const size_t cap)
  130. {
  131. if (buffer) dtFree(buffer);
  132. buffer = (unsigned char*)dtAlloc(cap, DT_ALLOC_PERM);
  133. capacity = cap;
  134. }
  135. virtual void reset()
  136. {
  137. high = dtMax(high, top);
  138. top = 0;
  139. }
  140. virtual void* alloc(const size_t size)
  141. {
  142. if (!buffer)
  143. return 0;
  144. if (top+size > capacity)
  145. return 0;
  146. unsigned char* mem = &buffer[top];
  147. top += size;
  148. return mem;
  149. }
  150. virtual void free(void* /*ptr*/)
  151. {
  152. // Empty
  153. }
  154. };
  155. struct MeshProcess : public dtTileCacheMeshProcess
  156. {
  157. InputGeom* m_geom;
  158. inline MeshProcess() : m_geom(0)
  159. {
  160. }
  161. inline void init(InputGeom* geom)
  162. {
  163. m_geom = geom;
  164. }
  165. virtual void process(struct dtNavMeshCreateParams* params,
  166. unsigned char* polyAreas, unsigned short* polyFlags)
  167. {
  168. // Update poly flags from areas.
  169. for (int i = 0; i < params->polyCount; ++i)
  170. {
  171. if (polyAreas[i] == DT_TILECACHE_WALKABLE_AREA)
  172. polyAreas[i] = SAMPLE_POLYAREA_GROUND;
  173. if (polyAreas[i] == SAMPLE_POLYAREA_GROUND ||
  174. polyAreas[i] == SAMPLE_POLYAREA_GRASS ||
  175. polyAreas[i] == SAMPLE_POLYAREA_ROAD)
  176. {
  177. polyFlags[i] = SAMPLE_POLYFLAGS_WALK;
  178. }
  179. else if (polyAreas[i] == SAMPLE_POLYAREA_WATER)
  180. {
  181. polyFlags[i] = SAMPLE_POLYFLAGS_SWIM;
  182. }
  183. else if (polyAreas[i] == SAMPLE_POLYAREA_DOOR)
  184. {
  185. polyFlags[i] = SAMPLE_POLYFLAGS_WALK | SAMPLE_POLYFLAGS_DOOR;
  186. }
  187. }
  188. // Pass in off-mesh connections.
  189. if (m_geom)
  190. {
  191. params->offMeshConVerts = m_geom->getOffMeshConnectionVerts();
  192. params->offMeshConRad = m_geom->getOffMeshConnectionRads();
  193. params->offMeshConDir = m_geom->getOffMeshConnectionDirs();
  194. params->offMeshConAreas = m_geom->getOffMeshConnectionAreas();
  195. params->offMeshConFlags = m_geom->getOffMeshConnectionFlags();
  196. params->offMeshConUserID = m_geom->getOffMeshConnectionId();
  197. params->offMeshConCount = m_geom->getOffMeshConnectionCount();
  198. }
  199. }
  200. };
  201. static const int MAX_LAYERS = 32;
  202. struct TileCacheData
  203. {
  204. unsigned char* data;
  205. int dataSize;
  206. };
  207. struct RasterizationContext
  208. {
  209. RasterizationContext() :
  210. solid(0),
  211. triareas(0),
  212. lset(0),
  213. chf(0),
  214. ntiles(0)
  215. {
  216. memset(tiles, 0, sizeof(TileCacheData)*MAX_LAYERS);
  217. }
  218. ~RasterizationContext()
  219. {
  220. rcFreeHeightField(solid);
  221. delete [] triareas;
  222. rcFreeHeightfieldLayerSet(lset);
  223. rcFreeCompactHeightfield(chf);
  224. for (int i = 0; i < MAX_LAYERS; ++i)
  225. {
  226. dtFree(tiles[i].data);
  227. tiles[i].data = 0;
  228. }
  229. }
  230. rcHeightfield* solid;
  231. unsigned char* triareas;
  232. rcHeightfieldLayerSet* lset;
  233. rcCompactHeightfield* chf;
  234. TileCacheData tiles[MAX_LAYERS];
  235. int ntiles;
  236. };
  237. int Sample_TempObstacles::rasterizeTileLayers(
  238. const int tx, const int ty,
  239. const rcConfig& cfg,
  240. TileCacheData* tiles,
  241. const int maxTiles)
  242. {
  243. if (!m_geom || !m_geom->getMesh() || !m_geom->getChunkyMesh())
  244. {
  245. m_ctx->log(RC_LOG_ERROR, "buildTile: Input mesh is not specified.");
  246. return 0;
  247. }
  248. FastLZCompressor comp;
  249. RasterizationContext rc;
  250. const float* verts = m_geom->getMesh()->getVerts();
  251. const int nverts = m_geom->getMesh()->getVertCount();
  252. const rcChunkyTriMesh* chunkyMesh = m_geom->getChunkyMesh();
  253. // Tile bounds.
  254. const float tcs = cfg.tileSize * cfg.cs;
  255. rcConfig tcfg;
  256. memcpy(&tcfg, &cfg, sizeof(tcfg));
  257. tcfg.bmin[0] = cfg.bmin[0] + tx*tcs;
  258. tcfg.bmin[1] = cfg.bmin[1];
  259. tcfg.bmin[2] = cfg.bmin[2] + ty*tcs;
  260. tcfg.bmax[0] = cfg.bmin[0] + (tx+1)*tcs;
  261. tcfg.bmax[1] = cfg.bmax[1];
  262. tcfg.bmax[2] = cfg.bmin[2] + (ty+1)*tcs;
  263. tcfg.bmin[0] -= tcfg.borderSize*tcfg.cs;
  264. tcfg.bmin[2] -= tcfg.borderSize*tcfg.cs;
  265. tcfg.bmax[0] += tcfg.borderSize*tcfg.cs;
  266. tcfg.bmax[2] += tcfg.borderSize*tcfg.cs;
  267. // Allocate voxel heightfield where we rasterize our input data to.
  268. rc.solid = rcAllocHeightfield();
  269. if (!rc.solid)
  270. {
  271. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'solid'.");
  272. return 0;
  273. }
  274. if (!rcCreateHeightfield(m_ctx, *rc.solid, tcfg.width, tcfg.height, tcfg.bmin, tcfg.bmax, tcfg.cs, tcfg.ch))
  275. {
  276. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not create solid heightfield.");
  277. return 0;
  278. }
  279. // Allocate array that can hold triangle flags.
  280. // If you have multiple meshes you need to process, allocate
  281. // and array which can hold the max number of triangles you need to process.
  282. rc.triareas = new unsigned char[chunkyMesh->maxTrisPerChunk];
  283. if (!rc.triareas)
  284. {
  285. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'm_triareas' (%d).", chunkyMesh->maxTrisPerChunk);
  286. return 0;
  287. }
  288. float tbmin[2], tbmax[2];
  289. tbmin[0] = tcfg.bmin[0];
  290. tbmin[1] = tcfg.bmin[2];
  291. tbmax[0] = tcfg.bmax[0];
  292. tbmax[1] = tcfg.bmax[2];
  293. int cid[512];// TODO: Make grow when returning too many items.
  294. const int ncid = rcGetChunksOverlappingRect(chunkyMesh, tbmin, tbmax, cid, 512);
  295. if (!ncid)
  296. {
  297. return 0; // empty
  298. }
  299. for (int i = 0; i < ncid; ++i)
  300. {
  301. const rcChunkyTriMeshNode& node = chunkyMesh->nodes[cid[i]];
  302. const int* tris = &chunkyMesh->tris[node.i*3];
  303. const int ntris = node.n;
  304. memset(rc.triareas, 0, ntris*sizeof(unsigned char));
  305. rcMarkWalkableTriangles(m_ctx, tcfg.walkableSlopeAngle,
  306. verts, nverts, tris, ntris, rc.triareas);
  307. if (!rcRasterizeTriangles(m_ctx, verts, nverts, tris, rc.triareas, ntris, *rc.solid, tcfg.walkableClimb))
  308. return 0;
  309. }
  310. // Once all geometry is rasterized, we do initial pass of filtering to
  311. // remove unwanted overhangs caused by the conservative rasterization
  312. // as well as filter spans where the character cannot possibly stand.
  313. if (m_filterLowHangingObstacles)
  314. rcFilterLowHangingWalkableObstacles(m_ctx, tcfg.walkableClimb, *rc.solid);
  315. if (m_filterLedgeSpans)
  316. rcFilterLedgeSpans(m_ctx, tcfg.walkableHeight, tcfg.walkableClimb, *rc.solid);
  317. if (m_filterWalkableLowHeightSpans)
  318. rcFilterWalkableLowHeightSpans(m_ctx, tcfg.walkableHeight, *rc.solid);
  319. rc.chf = rcAllocCompactHeightfield();
  320. if (!rc.chf)
  321. {
  322. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'chf'.");
  323. return 0;
  324. }
  325. if (!rcBuildCompactHeightfield(m_ctx, tcfg.walkableHeight, tcfg.walkableClimb, *rc.solid, *rc.chf))
  326. {
  327. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build compact data.");
  328. return 0;
  329. }
  330. // Erode the walkable area by agent radius.
  331. if (!rcErodeWalkableArea(m_ctx, tcfg.walkableRadius, *rc.chf))
  332. {
  333. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not erode.");
  334. return 0;
  335. }
  336. // (Optional) Mark areas.
  337. const ConvexVolume* vols = m_geom->getConvexVolumes();
  338. for (int i = 0; i < m_geom->getConvexVolumeCount(); ++i)
  339. {
  340. rcMarkConvexPolyArea(m_ctx, vols[i].verts, vols[i].nverts,
  341. vols[i].hmin, vols[i].hmax,
  342. (unsigned char)vols[i].area, *rc.chf);
  343. }
  344. rc.lset = rcAllocHeightfieldLayerSet();
  345. if (!rc.lset)
  346. {
  347. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'lset'.");
  348. return 0;
  349. }
  350. if (!rcBuildHeightfieldLayers(m_ctx, *rc.chf, tcfg.borderSize, tcfg.walkableHeight, *rc.lset))
  351. {
  352. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build heighfield layers.");
  353. return 0;
  354. }
  355. rc.ntiles = 0;
  356. for (int i = 0; i < rcMin(rc.lset->nlayers, MAX_LAYERS); ++i)
  357. {
  358. TileCacheData* tile = &rc.tiles[rc.ntiles++];
  359. const rcHeightfieldLayer* layer = &rc.lset->layers[i];
  360. // Store header
  361. dtTileCacheLayerHeader header;
  362. header.magic = DT_TILECACHE_MAGIC;
  363. header.version = DT_TILECACHE_VERSION;
  364. // Tile layer location in the navmesh.
  365. header.tx = tx;
  366. header.ty = ty;
  367. header.tlayer = i;
  368. dtVcopy(header.bmin, layer->bmin);
  369. dtVcopy(header.bmax, layer->bmax);
  370. // Tile info.
  371. header.width = (unsigned char)layer->width;
  372. header.height = (unsigned char)layer->height;
  373. header.minx = (unsigned char)layer->minx;
  374. header.maxx = (unsigned char)layer->maxx;
  375. header.miny = (unsigned char)layer->miny;
  376. header.maxy = (unsigned char)layer->maxy;
  377. header.hmin = (unsigned short)layer->hmin;
  378. header.hmax = (unsigned short)layer->hmax;
  379. dtStatus status = dtBuildTileCacheLayer(&comp, &header, layer->heights, layer->areas, layer->cons,
  380. &tile->data, &tile->dataSize);
  381. if (dtStatusFailed(status))
  382. {
  383. return 0;
  384. }
  385. }
  386. // Transfer ownsership of tile data from build context to the caller.
  387. int n = 0;
  388. for (int i = 0; i < rcMin(rc.ntiles, maxTiles); ++i)
  389. {
  390. tiles[n++] = rc.tiles[i];
  391. rc.tiles[i].data = 0;
  392. rc.tiles[i].dataSize = 0;
  393. }
  394. return n;
  395. }
  396. void drawTiles(duDebugDraw* dd, dtTileCache* tc)
  397. {
  398. unsigned int fcol[6];
  399. float bmin[3], bmax[3];
  400. for (int i = 0; i < tc->getTileCount(); ++i)
  401. {
  402. const dtCompressedTile* tile = tc->getTile(i);
  403. if (!tile->header) continue;
  404. tc->calcTightTileBounds(tile->header, bmin, bmax);
  405. const unsigned int col = duIntToCol(i,64);
  406. duCalcBoxColors(fcol, col, col);
  407. duDebugDrawBox(dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], fcol);
  408. }
  409. for (int i = 0; i < tc->getTileCount(); ++i)
  410. {
  411. const dtCompressedTile* tile = tc->getTile(i);
  412. if (!tile->header) continue;
  413. tc->calcTightTileBounds(tile->header, bmin, bmax);
  414. const unsigned int col = duIntToCol(i,255);
  415. const float pad = tc->getParams()->cs * 0.1f;
  416. duDebugDrawBoxWire(dd, bmin[0]-pad,bmin[1]-pad,bmin[2]-pad,
  417. bmax[0]+pad,bmax[1]+pad,bmax[2]+pad, col, 2.0f);
  418. }
  419. }
  420. enum DrawDetailType
  421. {
  422. DRAWDETAIL_AREAS,
  423. DRAWDETAIL_REGIONS,
  424. DRAWDETAIL_CONTOURS,
  425. DRAWDETAIL_MESH,
  426. };
  427. void drawDetail(duDebugDraw* dd, dtTileCache* tc, const int tx, const int ty, int type)
  428. {
  429. struct TileCacheBuildContext
  430. {
  431. inline TileCacheBuildContext(struct dtTileCacheAlloc* a) : layer(0), lcset(0), lmesh(0), alloc(a) {}
  432. inline ~TileCacheBuildContext() { purge(); }
  433. void purge()
  434. {
  435. dtFreeTileCacheLayer(alloc, layer);
  436. layer = 0;
  437. dtFreeTileCacheContourSet(alloc, lcset);
  438. lcset = 0;
  439. dtFreeTileCachePolyMesh(alloc, lmesh);
  440. lmesh = 0;
  441. }
  442. struct dtTileCacheLayer* layer;
  443. struct dtTileCacheContourSet* lcset;
  444. struct dtTileCachePolyMesh* lmesh;
  445. struct dtTileCacheAlloc* alloc;
  446. };
  447. dtCompressedTileRef tiles[MAX_LAYERS];
  448. const int ntiles = tc->getTilesAt(tx,ty,tiles,MAX_LAYERS);
  449. dtTileCacheAlloc* talloc = tc->getAlloc();
  450. dtTileCacheCompressor* tcomp = tc->getCompressor();
  451. const dtTileCacheParams* params = tc->getParams();
  452. for (int i = 0; i < ntiles; ++i)
  453. {
  454. const dtCompressedTile* tile = tc->getTileByRef(tiles[i]);
  455. talloc->reset();
  456. TileCacheBuildContext bc(talloc);
  457. const int walkableClimbVx = (int)(params->walkableClimb / params->ch);
  458. dtStatus status;
  459. // Decompress tile layer data.
  460. status = dtDecompressTileCacheLayer(talloc, tcomp, tile->data, tile->dataSize, &bc.layer);
  461. if (dtStatusFailed(status))
  462. return;
  463. if (type == DRAWDETAIL_AREAS)
  464. {
  465. duDebugDrawTileCacheLayerAreas(dd, *bc.layer, params->cs, params->ch);
  466. continue;
  467. }
  468. // Build navmesh
  469. status = dtBuildTileCacheRegions(talloc, *bc.layer, walkableClimbVx);
  470. if (dtStatusFailed(status))
  471. return;
  472. if (type == DRAWDETAIL_REGIONS)
  473. {
  474. duDebugDrawTileCacheLayerRegions(dd, *bc.layer, params->cs, params->ch);
  475. continue;
  476. }
  477. bc.lcset = dtAllocTileCacheContourSet(talloc);
  478. if (!bc.lcset)
  479. return;
  480. status = dtBuildTileCacheContours(talloc, *bc.layer, walkableClimbVx,
  481. params->maxSimplificationError, *bc.lcset);
  482. if (dtStatusFailed(status))
  483. return;
  484. if (type == DRAWDETAIL_CONTOURS)
  485. {
  486. duDebugDrawTileCacheContours(dd, *bc.lcset, tile->header->bmin, params->cs, params->ch);
  487. continue;
  488. }
  489. bc.lmesh = dtAllocTileCachePolyMesh(talloc);
  490. if (!bc.lmesh)
  491. return;
  492. status = dtBuildTileCachePolyMesh(talloc, *bc.lcset, *bc.lmesh);
  493. if (dtStatusFailed(status))
  494. return;
  495. if (type == DRAWDETAIL_MESH)
  496. {
  497. duDebugDrawTileCachePolyMesh(dd, *bc.lmesh, tile->header->bmin, params->cs, params->ch);
  498. continue;
  499. }
  500. }
  501. }
  502. void drawDetailOverlay(const dtTileCache* tc, const int tx, const int ty, double* proj, double* model, int* view)
  503. {
  504. dtCompressedTileRef tiles[MAX_LAYERS];
  505. const int ntiles = tc->getTilesAt(tx,ty,tiles,MAX_LAYERS);
  506. if (!ntiles)
  507. return;
  508. const int rawSize = calcLayerBufferSize(tc->getParams()->width, tc->getParams()->height);
  509. char text[128];
  510. for (int i = 0; i < ntiles; ++i)
  511. {
  512. const dtCompressedTile* tile = tc->getTileByRef(tiles[i]);
  513. float pos[3];
  514. pos[0] = (tile->header->bmin[0]+tile->header->bmax[0])/2.0f;
  515. pos[1] = tile->header->bmin[1];
  516. pos[2] = (tile->header->bmin[2]+tile->header->bmax[2])/2.0f;
  517. GLdouble x, y, z;
  518. if (gluProject((GLdouble)pos[0], (GLdouble)pos[1], (GLdouble)pos[2],
  519. model, proj, view, &x, &y, &z))
  520. {
  521. snprintf(text,128,"(%d,%d)/%d", tile->header->tx,tile->header->ty,tile->header->tlayer);
  522. imguiDrawText((int)x, (int)y-25, IMGUI_ALIGN_CENTER, text, imguiRGBA(0,0,0,220));
  523. snprintf(text,128,"Compressed: %.1f kB", tile->dataSize/1024.0f);
  524. imguiDrawText((int)x, (int)y-45, IMGUI_ALIGN_CENTER, text, imguiRGBA(0,0,0,128));
  525. snprintf(text,128,"Raw:%.1fkB", rawSize/1024.0f);
  526. imguiDrawText((int)x, (int)y-65, IMGUI_ALIGN_CENTER, text, imguiRGBA(0,0,0,128));
  527. }
  528. }
  529. }
  530. dtObstacleRef hitTestObstacle(const dtTileCache* tc, const float* sp, const float* sq)
  531. {
  532. float tmin = FLT_MAX;
  533. const dtTileCacheObstacle* obmin = 0;
  534. for (int i = 0; i < tc->getObstacleCount(); ++i)
  535. {
  536. const dtTileCacheObstacle* ob = tc->getObstacle(i);
  537. if (ob->state == DT_OBSTACLE_EMPTY)
  538. continue;
  539. float bmin[3], bmax[3], t0,t1;
  540. tc->getObstacleBounds(ob, bmin,bmax);
  541. if (isectSegAABB(sp,sq, bmin,bmax, t0,t1))
  542. {
  543. if (t0 < tmin)
  544. {
  545. tmin = t0;
  546. obmin = ob;
  547. }
  548. }
  549. }
  550. return tc->getObstacleRef(obmin);
  551. }
  552. void drawObstacles(duDebugDraw* dd, const dtTileCache* tc)
  553. {
  554. // Draw obstacles
  555. for (int i = 0; i < tc->getObstacleCount(); ++i)
  556. {
  557. const dtTileCacheObstacle* ob = tc->getObstacle(i);
  558. if (ob->state == DT_OBSTACLE_EMPTY) continue;
  559. float bmin[3], bmax[3];
  560. tc->getObstacleBounds(ob, bmin,bmax);
  561. unsigned int col = 0;
  562. if (ob->state == DT_OBSTACLE_PROCESSING)
  563. col = duRGBA(255,255,0,128);
  564. else if (ob->state == DT_OBSTACLE_PROCESSED)
  565. col = duRGBA(255,192,0,192);
  566. else if (ob->state == DT_OBSTACLE_REMOVING)
  567. col = duRGBA(220,0,0,128);
  568. duDebugDrawCylinder(dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], col);
  569. duDebugDrawCylinderWire(dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], duDarkenCol(col), 2);
  570. }
  571. }
  572. class TempObstacleHilightTool : public SampleTool
  573. {
  574. Sample_TempObstacles* m_sample;
  575. float m_hitPos[3];
  576. bool m_hitPosSet;
  577. int m_drawType;
  578. public:
  579. TempObstacleHilightTool() :
  580. m_sample(0),
  581. m_hitPosSet(false),
  582. m_drawType(DRAWDETAIL_AREAS)
  583. {
  584. m_hitPos[0] = m_hitPos[1] = m_hitPos[2] = 0;
  585. }
  586. virtual ~TempObstacleHilightTool()
  587. {
  588. }
  589. virtual int type() { return TOOL_TILE_HIGHLIGHT; }
  590. virtual void init(Sample* sample)
  591. {
  592. m_sample = (Sample_TempObstacles*)sample;
  593. }
  594. virtual void reset() {}
  595. virtual void handleMenu()
  596. {
  597. imguiLabel("Highlight Tile Cache");
  598. imguiValue("Click LMB to highlight a tile.");
  599. imguiSeparator();
  600. if (imguiCheck("Draw Areas", m_drawType == DRAWDETAIL_AREAS))
  601. m_drawType = DRAWDETAIL_AREAS;
  602. if (imguiCheck("Draw Regions", m_drawType == DRAWDETAIL_REGIONS))
  603. m_drawType = DRAWDETAIL_REGIONS;
  604. if (imguiCheck("Draw Contours", m_drawType == DRAWDETAIL_CONTOURS))
  605. m_drawType = DRAWDETAIL_CONTOURS;
  606. if (imguiCheck("Draw Mesh", m_drawType == DRAWDETAIL_MESH))
  607. m_drawType = DRAWDETAIL_MESH;
  608. }
  609. virtual void handleClick(const float* /*s*/, const float* p, bool /*shift*/)
  610. {
  611. m_hitPosSet = true;
  612. rcVcopy(m_hitPos,p);
  613. }
  614. virtual void handleToggle() {}
  615. virtual void handleStep() {}
  616. virtual void handleUpdate(const float /*dt*/) {}
  617. virtual void handleRender()
  618. {
  619. if (m_hitPosSet && m_sample)
  620. {
  621. const float s = m_sample->getAgentRadius();
  622. glColor4ub(0,0,0,128);
  623. glLineWidth(2.0f);
  624. glBegin(GL_LINES);
  625. glVertex3f(m_hitPos[0]-s,m_hitPos[1]+0.1f,m_hitPos[2]);
  626. glVertex3f(m_hitPos[0]+s,m_hitPos[1]+0.1f,m_hitPos[2]);
  627. glVertex3f(m_hitPos[0],m_hitPos[1]-s+0.1f,m_hitPos[2]);
  628. glVertex3f(m_hitPos[0],m_hitPos[1]+s+0.1f,m_hitPos[2]);
  629. glVertex3f(m_hitPos[0],m_hitPos[1]+0.1f,m_hitPos[2]-s);
  630. glVertex3f(m_hitPos[0],m_hitPos[1]+0.1f,m_hitPos[2]+s);
  631. glEnd();
  632. glLineWidth(1.0f);
  633. int tx=0, ty=0;
  634. m_sample->getTilePos(m_hitPos, tx, ty);
  635. m_sample->renderCachedTile(tx,ty,m_drawType);
  636. }
  637. }
  638. virtual void handleRenderOverlay(double* proj, double* model, int* view)
  639. {
  640. if (m_hitPosSet)
  641. {
  642. if (m_sample)
  643. {
  644. int tx=0, ty=0;
  645. m_sample->getTilePos(m_hitPos, tx, ty);
  646. m_sample->renderCachedTileOverlay(tx,ty,proj,model,view);
  647. }
  648. }
  649. }
  650. };
  651. class TempObstacleCreateTool : public SampleTool
  652. {
  653. Sample_TempObstacles* m_sample;
  654. public:
  655. TempObstacleCreateTool() : m_sample(0)
  656. {
  657. }
  658. virtual ~TempObstacleCreateTool()
  659. {
  660. }
  661. virtual int type() { return TOOL_TEMP_OBSTACLE; }
  662. virtual void init(Sample* sample)
  663. {
  664. m_sample = (Sample_TempObstacles*)sample;
  665. }
  666. virtual void reset() {}
  667. virtual void handleMenu()
  668. {
  669. imguiLabel("Create Temp Obstacles");
  670. if (imguiButton("Remove All"))
  671. m_sample->clearAllTempObstacles();
  672. imguiSeparator();
  673. imguiValue("Click LMB to create an obstacle.");
  674. imguiValue("Shift+LMB to remove an obstacle.");
  675. }
  676. virtual void handleClick(const float* s, const float* p, bool shift)
  677. {
  678. if (m_sample)
  679. {
  680. if (shift)
  681. m_sample->removeTempObstacle(s,p);
  682. else
  683. m_sample->addTempObstacle(p);
  684. }
  685. }
  686. virtual void handleToggle() {}
  687. virtual void handleStep() {}
  688. virtual void handleUpdate(const float /*dt*/) {}
  689. virtual void handleRender() {}
  690. virtual void handleRenderOverlay(double* /*proj*/, double* /*model*/, int* /*view*/) { }
  691. };
  692. Sample_TempObstacles::Sample_TempObstacles() :
  693. m_keepInterResults(false),
  694. m_tileCache(0),
  695. m_cacheBuildTimeMs(0),
  696. m_cacheCompressedSize(0),
  697. m_cacheRawSize(0),
  698. m_cacheLayerCount(0),
  699. m_cacheBuildMemUsage(0),
  700. m_drawMode(DRAWMODE_NAVMESH),
  701. m_maxTiles(0),
  702. m_maxPolysPerTile(0),
  703. m_tileSize(48)
  704. {
  705. resetCommonSettings();
  706. m_talloc = new LinearAllocator(32000);
  707. m_tcomp = new FastLZCompressor;
  708. m_tmproc = new MeshProcess;
  709. setTool(new TempObstacleCreateTool);
  710. }
  711. Sample_TempObstacles::~Sample_TempObstacles()
  712. {
  713. dtFreeNavMesh(m_navMesh);
  714. m_navMesh = 0;
  715. dtFreeTileCache(m_tileCache);
  716. }
  717. void Sample_TempObstacles::handleSettings()
  718. {
  719. Sample::handleCommonSettings();
  720. if (imguiCheck("Keep Itermediate Results", m_keepInterResults))
  721. m_keepInterResults = !m_keepInterResults;
  722. imguiLabel("Tiling");
  723. imguiSlider("TileSize", &m_tileSize, 16.0f, 128.0f, 8.0f);
  724. int gridSize = 1;
  725. if (m_geom)
  726. {
  727. const float* bmin = m_geom->getNavMeshBoundsMin();
  728. const float* bmax = m_geom->getNavMeshBoundsMax();
  729. char text[64];
  730. int gw = 0, gh = 0;
  731. rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
  732. const int ts = (int)m_tileSize;
  733. const int tw = (gw + ts-1) / ts;
  734. const int th = (gh + ts-1) / ts;
  735. snprintf(text, 64, "Tiles %d x %d", tw, th);
  736. imguiValue(text);
  737. // Max tiles and max polys affect how the tile IDs are caculated.
  738. // There are 22 bits available for identifying a tile and a polygon.
  739. int tileBits = rcMin((int)dtIlog2(dtNextPow2(tw*th*EXPECTED_LAYERS_PER_TILE)), 14);
  740. if (tileBits > 14) tileBits = 14;
  741. int polyBits = 22 - tileBits;
  742. m_maxTiles = 1 << tileBits;
  743. m_maxPolysPerTile = 1 << polyBits;
  744. snprintf(text, 64, "Max Tiles %d", m_maxTiles);
  745. imguiValue(text);
  746. snprintf(text, 64, "Max Polys %d", m_maxPolysPerTile);
  747. imguiValue(text);
  748. gridSize = tw*th;
  749. }
  750. else
  751. {
  752. m_maxTiles = 0;
  753. m_maxPolysPerTile = 0;
  754. }
  755. imguiSeparator();
  756. imguiLabel("Tile Cache");
  757. char msg[64];
  758. const float compressionRatio = (float)m_cacheCompressedSize / (float)(m_cacheRawSize+1);
  759. snprintf(msg, 64, "Layers %d", m_cacheLayerCount);
  760. imguiValue(msg);
  761. snprintf(msg, 64, "Layers (per tile) %.1f", (float)m_cacheLayerCount/(float)gridSize);
  762. imguiValue(msg);
  763. snprintf(msg, 64, "Memory %.1f kB / %.1f kB (%.1f%%)", m_cacheCompressedSize/1024.0f, m_cacheRawSize/1024.0f, compressionRatio*100.0f);
  764. imguiValue(msg);
  765. snprintf(msg, 64, "Navmesh Build Time %.1f ms", m_cacheBuildTimeMs);
  766. imguiValue(msg);
  767. snprintf(msg, 64, "Build Peak Mem Usage %.1f kB", m_cacheBuildMemUsage/1024.0f);
  768. imguiValue(msg);
  769. imguiSeparator();
  770. imguiIndent();
  771. imguiIndent();
  772. if (imguiButton("Save"))
  773. {
  774. saveAll("all_tiles_tilecache.bin");
  775. }
  776. if (imguiButton("Load"))
  777. {
  778. dtFreeNavMesh(m_navMesh);
  779. dtFreeTileCache(m_tileCache);
  780. loadAll("all_tiles_tilecache.bin");
  781. m_navQuery->init(m_navMesh, 2048);
  782. }
  783. imguiUnindent();
  784. imguiUnindent();
  785. imguiSeparator();
  786. }
  787. void Sample_TempObstacles::handleTools()
  788. {
  789. int type = !m_tool ? TOOL_NONE : m_tool->type();
  790. if (imguiCheck("Test Navmesh", type == TOOL_NAVMESH_TESTER))
  791. {
  792. setTool(new NavMeshTesterTool);
  793. }
  794. if (imguiCheck("Highlight Tile Cache", type == TOOL_TILE_HIGHLIGHT))
  795. {
  796. setTool(new TempObstacleHilightTool);
  797. }
  798. if (imguiCheck("Create Temp Obstacles", type == TOOL_TEMP_OBSTACLE))
  799. {
  800. setTool(new TempObstacleCreateTool);
  801. }
  802. if (imguiCheck("Create Off-Mesh Links", type == TOOL_OFFMESH_CONNECTION))
  803. {
  804. setTool(new OffMeshConnectionTool);
  805. }
  806. if (imguiCheck("Create Convex Volumes", type == TOOL_CONVEX_VOLUME))
  807. {
  808. setTool(new ConvexVolumeTool);
  809. }
  810. if (imguiCheck("Create Crowds", type == TOOL_CROWD))
  811. {
  812. setTool(new CrowdTool);
  813. }
  814. imguiSeparatorLine();
  815. imguiIndent();
  816. if (m_tool)
  817. m_tool->handleMenu();
  818. imguiUnindent();
  819. }
  820. void Sample_TempObstacles::handleDebugMode()
  821. {
  822. // Check which modes are valid.
  823. bool valid[MAX_DRAWMODE];
  824. for (int i = 0; i < MAX_DRAWMODE; ++i)
  825. valid[i] = false;
  826. if (m_geom)
  827. {
  828. valid[DRAWMODE_NAVMESH] = m_navMesh != 0;
  829. valid[DRAWMODE_NAVMESH_TRANS] = m_navMesh != 0;
  830. valid[DRAWMODE_NAVMESH_BVTREE] = m_navMesh != 0;
  831. valid[DRAWMODE_NAVMESH_NODES] = m_navQuery != 0;
  832. valid[DRAWMODE_NAVMESH_PORTALS] = m_navMesh != 0;
  833. valid[DRAWMODE_NAVMESH_INVIS] = m_navMesh != 0;
  834. valid[DRAWMODE_MESH] = true;
  835. valid[DRAWMODE_CACHE_BOUNDS] = true;
  836. }
  837. int unavail = 0;
  838. for (int i = 0; i < MAX_DRAWMODE; ++i)
  839. if (!valid[i]) unavail++;
  840. if (unavail == MAX_DRAWMODE)
  841. return;
  842. imguiLabel("Draw");
  843. if (imguiCheck("Input Mesh", m_drawMode == DRAWMODE_MESH, valid[DRAWMODE_MESH]))
  844. m_drawMode = DRAWMODE_MESH;
  845. if (imguiCheck("Navmesh", m_drawMode == DRAWMODE_NAVMESH, valid[DRAWMODE_NAVMESH]))
  846. m_drawMode = DRAWMODE_NAVMESH;
  847. if (imguiCheck("Navmesh Invis", m_drawMode == DRAWMODE_NAVMESH_INVIS, valid[DRAWMODE_NAVMESH_INVIS]))
  848. m_drawMode = DRAWMODE_NAVMESH_INVIS;
  849. if (imguiCheck("Navmesh Trans", m_drawMode == DRAWMODE_NAVMESH_TRANS, valid[DRAWMODE_NAVMESH_TRANS]))
  850. m_drawMode = DRAWMODE_NAVMESH_TRANS;
  851. if (imguiCheck("Navmesh BVTree", m_drawMode == DRAWMODE_NAVMESH_BVTREE, valid[DRAWMODE_NAVMESH_BVTREE]))
  852. m_drawMode = DRAWMODE_NAVMESH_BVTREE;
  853. if (imguiCheck("Navmesh Nodes", m_drawMode == DRAWMODE_NAVMESH_NODES, valid[DRAWMODE_NAVMESH_NODES]))
  854. m_drawMode = DRAWMODE_NAVMESH_NODES;
  855. if (imguiCheck("Navmesh Portals", m_drawMode == DRAWMODE_NAVMESH_PORTALS, valid[DRAWMODE_NAVMESH_PORTALS]))
  856. m_drawMode = DRAWMODE_NAVMESH_PORTALS;
  857. if (imguiCheck("Cache Bounds", m_drawMode == DRAWMODE_CACHE_BOUNDS, valid[DRAWMODE_CACHE_BOUNDS]))
  858. m_drawMode = DRAWMODE_CACHE_BOUNDS;
  859. if (unavail)
  860. {
  861. imguiValue("Tick 'Keep Itermediate Results'");
  862. imguiValue("rebuild some tiles to see");
  863. imguiValue("more debug mode options.");
  864. }
  865. }
  866. void Sample_TempObstacles::handleRender()
  867. {
  868. if (!m_geom || !m_geom->getMesh())
  869. return;
  870. const float texScale = 1.0f / (m_cellSize * 10.0f);
  871. // Draw mesh
  872. if (m_drawMode != DRAWMODE_NAVMESH_TRANS)
  873. {
  874. // Draw mesh
  875. duDebugDrawTriMeshSlope(&m_dd, m_geom->getMesh()->getVerts(), m_geom->getMesh()->getVertCount(),
  876. m_geom->getMesh()->getTris(), m_geom->getMesh()->getNormals(), m_geom->getMesh()->getTriCount(),
  877. m_agentMaxSlope, texScale);
  878. m_geom->drawOffMeshConnections(&m_dd);
  879. }
  880. if (m_tileCache && m_drawMode == DRAWMODE_CACHE_BOUNDS)
  881. drawTiles(&m_dd, m_tileCache);
  882. if (m_tileCache)
  883. drawObstacles(&m_dd, m_tileCache);
  884. glDepthMask(GL_FALSE);
  885. // Draw bounds
  886. const float* bmin = m_geom->getNavMeshBoundsMin();
  887. const float* bmax = m_geom->getNavMeshBoundsMax();
  888. duDebugDrawBoxWire(&m_dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], duRGBA(255,255,255,128), 1.0f);
  889. // Tiling grid.
  890. int gw = 0, gh = 0;
  891. rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
  892. const int tw = (gw + (int)m_tileSize-1) / (int)m_tileSize;
  893. const int th = (gh + (int)m_tileSize-1) / (int)m_tileSize;
  894. const float s = m_tileSize*m_cellSize;
  895. duDebugDrawGridXZ(&m_dd, bmin[0],bmin[1],bmin[2], tw,th, s, duRGBA(0,0,0,64), 1.0f);
  896. if (m_navMesh && m_navQuery &&
  897. (m_drawMode == DRAWMODE_NAVMESH ||
  898. m_drawMode == DRAWMODE_NAVMESH_TRANS ||
  899. m_drawMode == DRAWMODE_NAVMESH_BVTREE ||
  900. m_drawMode == DRAWMODE_NAVMESH_NODES ||
  901. m_drawMode == DRAWMODE_NAVMESH_PORTALS ||
  902. m_drawMode == DRAWMODE_NAVMESH_INVIS))
  903. {
  904. if (m_drawMode != DRAWMODE_NAVMESH_INVIS)
  905. duDebugDrawNavMeshWithClosedList(&m_dd, *m_navMesh, *m_navQuery, m_navMeshDrawFlags/*|DU_DRAWNAVMESH_COLOR_TILES*/);
  906. if (m_drawMode == DRAWMODE_NAVMESH_BVTREE)
  907. duDebugDrawNavMeshBVTree(&m_dd, *m_navMesh);
  908. if (m_drawMode == DRAWMODE_NAVMESH_PORTALS)
  909. duDebugDrawNavMeshPortals(&m_dd, *m_navMesh);
  910. if (m_drawMode == DRAWMODE_NAVMESH_NODES)
  911. duDebugDrawNavMeshNodes(&m_dd, *m_navQuery);
  912. duDebugDrawNavMeshPolysWithFlags(&m_dd, *m_navMesh, SAMPLE_POLYFLAGS_DISABLED, duRGBA(0,0,0,128));
  913. }
  914. glDepthMask(GL_TRUE);
  915. m_geom->drawConvexVolumes(&m_dd);
  916. if (m_tool)
  917. m_tool->handleRender();
  918. renderToolStates();
  919. glDepthMask(GL_TRUE);
  920. }
  921. void Sample_TempObstacles::renderCachedTile(const int tx, const int ty, const int type)
  922. {
  923. if (m_tileCache)
  924. drawDetail(&m_dd,m_tileCache,tx,ty,type);
  925. }
  926. void Sample_TempObstacles::renderCachedTileOverlay(const int tx, const int ty, double* proj, double* model, int* view)
  927. {
  928. if (m_tileCache)
  929. drawDetailOverlay(m_tileCache, tx, ty, proj, model, view);
  930. }
  931. void Sample_TempObstacles::handleRenderOverlay(double* proj, double* model, int* view)
  932. {
  933. if (m_tool)
  934. m_tool->handleRenderOverlay(proj, model, view);
  935. renderOverlayToolStates(proj, model, view);
  936. // Stats
  937. /* imguiDrawRect(280,10,300,100,imguiRGBA(0,0,0,64));
  938. char text[64];
  939. int y = 110-30;
  940. snprintf(text,64,"Lean Data: %.1fkB", m_tileCache->getRawSize()/1024.0f);
  941. imguiDrawText(300, y, IMGUI_ALIGN_LEFT, text, imguiRGBA(255,255,255,255));
  942. y -= 20;
  943. snprintf(text,64,"Compressed: %.1fkB (%.1f%%)", m_tileCache->getCompressedSize()/1024.0f,
  944. m_tileCache->getRawSize() > 0 ? 100.0f*(float)m_tileCache->getCompressedSize()/(float)m_tileCache->getRawSize() : 0);
  945. imguiDrawText(300, y, IMGUI_ALIGN_LEFT, text, imguiRGBA(255,255,255,255));
  946. y -= 20;
  947. if (m_rebuildTileCount > 0 && m_rebuildTime > 0.0f)
  948. {
  949. snprintf(text,64,"Changed obstacles, rebuild %d tiles: %.3f ms", m_rebuildTileCount, m_rebuildTime);
  950. imguiDrawText(300, y, IMGUI_ALIGN_LEFT, text, imguiRGBA(255,192,0,255));
  951. y -= 20;
  952. }
  953. */
  954. }
  955. void Sample_TempObstacles::handleMeshChanged(class InputGeom* geom)
  956. {
  957. Sample::handleMeshChanged(geom);
  958. dtFreeTileCache(m_tileCache);
  959. m_tileCache = 0;
  960. dtFreeNavMesh(m_navMesh);
  961. m_navMesh = 0;
  962. if (m_tool)
  963. {
  964. m_tool->reset();
  965. m_tool->init(this);
  966. m_tmproc->init(m_geom);
  967. }
  968. resetToolStates();
  969. initToolStates(this);
  970. }
  971. void Sample_TempObstacles::addTempObstacle(const float* pos)
  972. {
  973. if (!m_tileCache)
  974. return;
  975. float p[3];
  976. dtVcopy(p, pos);
  977. p[1] -= 0.5f;
  978. m_tileCache->addObstacle(p, 1.0f, 2.0f, 0);
  979. }
  980. void Sample_TempObstacles::removeTempObstacle(const float* sp, const float* sq)
  981. {
  982. if (!m_tileCache)
  983. return;
  984. dtObstacleRef ref = hitTestObstacle(m_tileCache, sp, sq);
  985. m_tileCache->removeObstacle(ref);
  986. }
  987. void Sample_TempObstacles::clearAllTempObstacles()
  988. {
  989. if (!m_tileCache)
  990. return;
  991. for (int i = 0; i < m_tileCache->getObstacleCount(); ++i)
  992. {
  993. const dtTileCacheObstacle* ob = m_tileCache->getObstacle(i);
  994. if (ob->state == DT_OBSTACLE_EMPTY) continue;
  995. m_tileCache->removeObstacle(m_tileCache->getObstacleRef(ob));
  996. }
  997. }
  998. bool Sample_TempObstacles::handleBuild()
  999. {
  1000. dtStatus status;
  1001. if (!m_geom || !m_geom->getMesh())
  1002. {
  1003. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: No vertices and triangles.");
  1004. return false;
  1005. }
  1006. m_tmproc->init(m_geom);
  1007. // Init cache
  1008. const float* bmin = m_geom->getNavMeshBoundsMin();
  1009. const float* bmax = m_geom->getNavMeshBoundsMax();
  1010. int gw = 0, gh = 0;
  1011. rcCalcGridSize(bmin, bmax, m_cellSize, &gw, &gh);
  1012. const int ts = (int)m_tileSize;
  1013. const int tw = (gw + ts-1) / ts;
  1014. const int th = (gh + ts-1) / ts;
  1015. // Generation params.
  1016. rcConfig cfg;
  1017. memset(&cfg, 0, sizeof(cfg));
  1018. cfg.cs = m_cellSize;
  1019. cfg.ch = m_cellHeight;
  1020. cfg.walkableSlopeAngle = m_agentMaxSlope;
  1021. cfg.walkableHeight = (int)ceilf(m_agentHeight / cfg.ch);
  1022. cfg.walkableClimb = (int)floorf(m_agentMaxClimb / cfg.ch);
  1023. cfg.walkableRadius = (int)ceilf(m_agentRadius / cfg.cs);
  1024. cfg.maxEdgeLen = (int)(m_edgeMaxLen / m_cellSize);
  1025. cfg.maxSimplificationError = m_edgeMaxError;
  1026. cfg.minRegionArea = (int)rcSqr(m_regionMinSize); // Note: area = size*size
  1027. cfg.mergeRegionArea = (int)rcSqr(m_regionMergeSize); // Note: area = size*size
  1028. cfg.maxVertsPerPoly = (int)m_vertsPerPoly;
  1029. cfg.tileSize = (int)m_tileSize;
  1030. cfg.borderSize = cfg.walkableRadius + 3; // Reserve enough padding.
  1031. cfg.width = cfg.tileSize + cfg.borderSize*2;
  1032. cfg.height = cfg.tileSize + cfg.borderSize*2;
  1033. cfg.detailSampleDist = m_detailSampleDist < 0.9f ? 0 : m_cellSize * m_detailSampleDist;
  1034. cfg.detailSampleMaxError = m_cellHeight * m_detailSampleMaxError;
  1035. rcVcopy(cfg.bmin, bmin);
  1036. rcVcopy(cfg.bmax, bmax);
  1037. // Tile cache params.
  1038. dtTileCacheParams tcparams;
  1039. memset(&tcparams, 0, sizeof(tcparams));
  1040. rcVcopy(tcparams.orig, bmin);
  1041. tcparams.cs = m_cellSize;
  1042. tcparams.ch = m_cellHeight;
  1043. tcparams.width = (int)m_tileSize;
  1044. tcparams.height = (int)m_tileSize;
  1045. tcparams.walkableHeight = m_agentHeight;
  1046. tcparams.walkableRadius = m_agentRadius;
  1047. tcparams.walkableClimb = m_agentMaxClimb;
  1048. tcparams.maxSimplificationError = m_edgeMaxError;
  1049. tcparams.maxTiles = tw*th*EXPECTED_LAYERS_PER_TILE;
  1050. tcparams.maxObstacles = 128;
  1051. dtFreeTileCache(m_tileCache);
  1052. m_tileCache = dtAllocTileCache();
  1053. if (!m_tileCache)
  1054. {
  1055. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not allocate tile cache.");
  1056. return false;
  1057. }
  1058. status = m_tileCache->init(&tcparams, m_talloc, m_tcomp, m_tmproc);
  1059. if (dtStatusFailed(status))
  1060. {
  1061. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not init tile cache.");
  1062. return false;
  1063. }
  1064. dtFreeNavMesh(m_navMesh);
  1065. m_navMesh = dtAllocNavMesh();
  1066. if (!m_navMesh)
  1067. {
  1068. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not allocate navmesh.");
  1069. return false;
  1070. }
  1071. dtNavMeshParams params;
  1072. memset(&params, 0, sizeof(params));
  1073. rcVcopy(params.orig, bmin);
  1074. params.tileWidth = m_tileSize*m_cellSize;
  1075. params.tileHeight = m_tileSize*m_cellSize;
  1076. params.maxTiles = m_maxTiles;
  1077. params.maxPolys = m_maxPolysPerTile;
  1078. status = m_navMesh->init(&params);
  1079. if (dtStatusFailed(status))
  1080. {
  1081. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not init navmesh.");
  1082. return false;
  1083. }
  1084. status = m_navQuery->init(m_navMesh, 2048);
  1085. if (dtStatusFailed(status))
  1086. {
  1087. m_ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not init Detour navmesh query");
  1088. return false;
  1089. }
  1090. // Preprocess tiles.
  1091. m_ctx->resetTimers();
  1092. m_cacheLayerCount = 0;
  1093. m_cacheCompressedSize = 0;
  1094. m_cacheRawSize = 0;
  1095. for (int y = 0; y < th; ++y)
  1096. {
  1097. for (int x = 0; x < tw; ++x)
  1098. {
  1099. TileCacheData tiles[MAX_LAYERS];
  1100. memset(tiles, 0, sizeof(tiles));
  1101. int ntiles = rasterizeTileLayers(x, y, cfg, tiles, MAX_LAYERS);
  1102. for (int i = 0; i < ntiles; ++i)
  1103. {
  1104. TileCacheData* tile = &tiles[i];
  1105. status = m_tileCache->addTile(tile->data, tile->dataSize, DT_COMPRESSEDTILE_FREE_DATA, 0);
  1106. if (dtStatusFailed(status))
  1107. {
  1108. dtFree(tile->data);
  1109. tile->data = 0;
  1110. continue;
  1111. }
  1112. m_cacheLayerCount++;
  1113. m_cacheCompressedSize += tile->dataSize;
  1114. m_cacheRawSize += calcLayerBufferSize(tcparams.width, tcparams.height);
  1115. }
  1116. }
  1117. }
  1118. // Build initial meshes
  1119. m_ctx->startTimer(RC_TIMER_TOTAL);
  1120. for (int y = 0; y < th; ++y)
  1121. for (int x = 0; x < tw; ++x)
  1122. m_tileCache->buildNavMeshTilesAt(x,y, m_navMesh);
  1123. m_ctx->stopTimer(RC_TIMER_TOTAL);
  1124. m_cacheBuildTimeMs = m_ctx->getAccumulatedTime(RC_TIMER_TOTAL)/1000.0f;
  1125. m_cacheBuildMemUsage = static_cast<unsigned int>(m_talloc->high);
  1126. const dtNavMesh* nav = m_navMesh;
  1127. int navmeshMemUsage = 0;
  1128. for (int i = 0; i < nav->getMaxTiles(); ++i)
  1129. {
  1130. const dtMeshTile* tile = nav->getTile(i);
  1131. if (tile->header)
  1132. navmeshMemUsage += tile->dataSize;
  1133. }
  1134. printf("navmeshMemUsage = %.1f kB", navmeshMemUsage/1024.0f);
  1135. if (m_tool)
  1136. m_tool->init(this);
  1137. initToolStates(this);
  1138. return true;
  1139. }
  1140. void Sample_TempObstacles::handleUpdate(const float dt)
  1141. {
  1142. Sample::handleUpdate(dt);
  1143. if (!m_navMesh)
  1144. return;
  1145. if (!m_tileCache)
  1146. return;
  1147. m_tileCache->update(dt, m_navMesh);
  1148. }
  1149. void Sample_TempObstacles::getTilePos(const float* pos, int& tx, int& ty)
  1150. {
  1151. if (!m_geom) return;
  1152. const float* bmin = m_geom->getNavMeshBoundsMin();
  1153. const float ts = m_tileSize*m_cellSize;
  1154. tx = (int)((pos[0] - bmin[0]) / ts);
  1155. ty = (int)((pos[2] - bmin[2]) / ts);
  1156. }
  1157. static const int TILECACHESET_MAGIC = 'T'<<24 | 'S'<<16 | 'E'<<8 | 'T'; //'TSET';
  1158. static const int TILECACHESET_VERSION = 1;
  1159. struct TileCacheSetHeader
  1160. {
  1161. int magic;
  1162. int version;
  1163. int numTiles;
  1164. dtNavMeshParams meshParams;
  1165. dtTileCacheParams cacheParams;
  1166. };
  1167. struct TileCacheTileHeader
  1168. {
  1169. dtCompressedTileRef tileRef;
  1170. int dataSize;
  1171. };
  1172. void Sample_TempObstacles::saveAll(const char* path)
  1173. {
  1174. if (!m_tileCache) return;
  1175. FILE* fp = fopen(path, "wb");
  1176. if (!fp)
  1177. return;
  1178. // Store header.
  1179. TileCacheSetHeader header;
  1180. header.magic = TILECACHESET_MAGIC;
  1181. header.version = TILECACHESET_VERSION;
  1182. header.numTiles = 0;
  1183. for (int i = 0; i < m_tileCache->getTileCount(); ++i)
  1184. {
  1185. const dtCompressedTile* tile = m_tileCache->getTile(i);
  1186. if (!tile || !tile->header || !tile->dataSize) continue;
  1187. header.numTiles++;
  1188. }
  1189. memcpy(&header.cacheParams, m_tileCache->getParams(), sizeof(dtTileCacheParams));
  1190. memcpy(&header.meshParams, m_navMesh->getParams(), sizeof(dtNavMeshParams));
  1191. fwrite(&header, sizeof(TileCacheSetHeader), 1, fp);
  1192. // Store tiles.
  1193. for (int i = 0; i < m_tileCache->getTileCount(); ++i)
  1194. {
  1195. const dtCompressedTile* tile = m_tileCache->getTile(i);
  1196. if (!tile || !tile->header || !tile->dataSize) continue;
  1197. TileCacheTileHeader tileHeader;
  1198. tileHeader.tileRef = m_tileCache->getTileRef(tile);
  1199. tileHeader.dataSize = tile->dataSize;
  1200. fwrite(&tileHeader, sizeof(tileHeader), 1, fp);
  1201. fwrite(tile->data, tile->dataSize, 1, fp);
  1202. }
  1203. fclose(fp);
  1204. }
  1205. void Sample_TempObstacles::loadAll(const char* path)
  1206. {
  1207. FILE* fp = fopen(path, "rb");
  1208. if (!fp) return;
  1209. // Read header.
  1210. TileCacheSetHeader header;
  1211. size_t headerReadReturnCode = fread(&header, sizeof(TileCacheSetHeader), 1, fp);
  1212. if( headerReadReturnCode != 1)
  1213. {
  1214. // Error or early EOF
  1215. fclose(fp);
  1216. return;
  1217. }
  1218. if (header.magic != TILECACHESET_MAGIC)
  1219. {
  1220. fclose(fp);
  1221. return;
  1222. }
  1223. if (header.version != TILECACHESET_VERSION)
  1224. {
  1225. fclose(fp);
  1226. return;
  1227. }
  1228. m_navMesh = dtAllocNavMesh();
  1229. if (!m_navMesh)
  1230. {
  1231. fclose(fp);
  1232. return;
  1233. }
  1234. dtStatus status = m_navMesh->init(&header.meshParams);
  1235. if (dtStatusFailed(status))
  1236. {
  1237. fclose(fp);
  1238. return;
  1239. }
  1240. m_tileCache = dtAllocTileCache();
  1241. if (!m_tileCache)
  1242. {
  1243. fclose(fp);
  1244. return;
  1245. }
  1246. status = m_tileCache->init(&header.cacheParams, m_talloc, m_tcomp, m_tmproc);
  1247. if (dtStatusFailed(status))
  1248. {
  1249. fclose(fp);
  1250. return;
  1251. }
  1252. // Read tiles.
  1253. for (int i = 0; i < header.numTiles; ++i)
  1254. {
  1255. TileCacheTileHeader tileHeader;
  1256. size_t tileHeaderReadReturnCode = fread(&tileHeader, sizeof(tileHeader), 1, fp);
  1257. if( tileHeaderReadReturnCode != 1)
  1258. {
  1259. // Error or early EOF
  1260. fclose(fp);
  1261. return;
  1262. }
  1263. if (!tileHeader.tileRef || !tileHeader.dataSize)
  1264. break;
  1265. unsigned char* data = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM);
  1266. if (!data) break;
  1267. memset(data, 0, tileHeader.dataSize);
  1268. size_t tileDataReadReturnCode = fread(data, tileHeader.dataSize, 1, fp);
  1269. if( tileDataReadReturnCode != 1)
  1270. {
  1271. // Error or early EOF
  1272. dtFree(data);
  1273. fclose(fp);
  1274. return;
  1275. }
  1276. dtCompressedTileRef tile = 0;
  1277. dtStatus addTileStatus = m_tileCache->addTile(data, tileHeader.dataSize, DT_COMPRESSEDTILE_FREE_DATA, &tile);
  1278. if (dtStatusFailed(addTileStatus))
  1279. {
  1280. dtFree(data);
  1281. }
  1282. if (tile)
  1283. m_tileCache->buildNavMeshTile(tile, m_navMesh);
  1284. }
  1285. fclose(fp);
  1286. }