Sample_Debug.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "Sample_Debug.h"
  22. #include "InputGeom.h"
  23. #include "Recast.h"
  24. #include "DetourNavMesh.h"
  25. #include "RecastDebugDraw.h"
  26. #include "DetourDebugDraw.h"
  27. #include "RecastDump.h"
  28. #include "imgui.h"
  29. #include "SDL.h"
  30. #include "SDL_opengl.h"
  31. #ifdef WIN32
  32. # define snprintf _snprintf
  33. #endif
  34. /*
  35. static int loadBin(const char* path, unsigned char** data)
  36. {
  37. FILE* fp = fopen(path, "rb");
  38. if (!fp) return 0;
  39. fseek(fp, 0, SEEK_END);
  40. int size = ftell(fp);
  41. fseek(fp, 0, SEEK_SET);
  42. *data = new unsigned char[size];
  43. fread(*data, size, 1, fp);
  44. fclose(fp);
  45. return size;
  46. }
  47. */
  48. Sample_Debug::Sample_Debug() :
  49. m_chf(0),
  50. m_cset(0),
  51. m_pmesh(0)
  52. {
  53. resetCommonSettings();
  54. // Test
  55. /* m_chf = rcAllocCompactHeightfield();
  56. FileIO io;
  57. if (!io.openForRead("test.chf"))
  58. {
  59. delete m_chf;
  60. m_chf = 0;
  61. }
  62. else
  63. {
  64. if (!duReadCompactHeightfield(*m_chf, &io))
  65. {
  66. delete m_chf;
  67. m_chf = 0;
  68. }
  69. }*/
  70. /* if (m_chf)
  71. {
  72. unsigned short ymin = 0xffff;
  73. unsigned short ymax = 0;
  74. for (int i = 0; i < m_chf->spanCount; ++i)
  75. {
  76. const rcCompactSpan& s = m_chf->spans[i];
  77. if (s.y < ymin) ymin = s.y;
  78. if (s.y > ymax) ymax = s.y;
  79. }
  80. printf("ymin=%d ymax=%d\n", (int)ymin, (int)ymax);
  81. int maxSpans = 0;
  82. for (int i = 0; i < m_chf->width*m_chf->height; ++i)
  83. {
  84. maxSpans = rcMax(maxSpans, (int)m_chf->cells[i].count);
  85. }
  86. printf("maxSpans = %d\n", maxSpans);
  87. }*/
  88. /* const float orig[3] = {0,0,0};
  89. m_navMesh = new dtNavMesh;
  90. m_navMesh->init(orig, 133.333f,133.333f, 2048, 4096, 4096);
  91. unsigned char* data = 0;
  92. int dataSize = 0;
  93. // Tile_-13_-14.bin is basically just the bytes that was output by Detour. It should be loaded at X: -13 and Y: -14.
  94. dataSize = loadBin("Tile_-13_-13.bin", &data);
  95. if (dataSize > 0)
  96. {
  97. m_navMesh->addTileAt(-13,-13, data, dataSize, true);
  98. dtMeshHeader* header = (dtMeshHeader*)data;
  99. vcopy(m_bmin, header->bmin);
  100. vcopy(m_bmax, header->bmax);
  101. }
  102. dataSize = loadBin("Tile_-13_-14.bin", &data);
  103. if (dataSize > 0)
  104. {
  105. m_navMesh->addTileAt(-13,-14, data, dataSize, true);
  106. }
  107. dataSize = loadBin("Tile_-14_-14.bin", &data);
  108. if (dataSize > 0)
  109. {
  110. m_navMesh->addTileAt(-14,-14, data, dataSize, true);
  111. }
  112. const float halfExtents[3] = {40,100,40};
  113. const float center[3] = { -1667.9491f, 135.52649f, -1680.6149f };
  114. dtQueryFilter filter;
  115. m_ref = m_navMesh->findNearestPoly(center, halfExtents, &filter, 0);
  116. vcopy(m_halfExtents, halfExtents);
  117. vcopy(m_center, center);*/
  118. {
  119. m_cset = rcAllocContourSet();
  120. if (m_cset)
  121. {
  122. FileIO io;
  123. if (io.openForRead("PathSet_TMP_NA_PathingTestAReg1_1_2_CS.rc"))
  124. {
  125. duReadContourSet(*m_cset, &io);
  126. printf("bmin=(%f,%f,%f) bmax=(%f,%f,%f)\n",
  127. m_cset->bmin[0], m_cset->bmin[1], m_cset->bmin[2],
  128. m_cset->bmax[0], m_cset->bmax[1], m_cset->bmax[2]);
  129. printf("cs=%f ch=%f\n", m_cset->cs, m_cset->ch);
  130. }
  131. else
  132. {
  133. printf("could not open test.cset\n");
  134. }
  135. }
  136. else
  137. {
  138. printf("Could not alloc cset\n");
  139. }
  140. /* if (m_cset)
  141. {
  142. m_pmesh = rcAllocPolyMesh();
  143. if (m_pmesh)
  144. {
  145. rcBuildPolyMesh(m_ctx, *m_cset, 6, *m_pmesh);
  146. }
  147. }*/
  148. }
  149. }
  150. Sample_Debug::~Sample_Debug()
  151. {
  152. rcFreeCompactHeightfield(m_chf);
  153. rcFreeContourSet(m_cset);
  154. rcFreePolyMesh(m_pmesh);
  155. }
  156. void Sample_Debug::handleSettings()
  157. {
  158. }
  159. void Sample_Debug::handleTools()
  160. {
  161. }
  162. void Sample_Debug::handleDebugMode()
  163. {
  164. }
  165. void Sample_Debug::handleRender()
  166. {
  167. if (m_chf)
  168. {
  169. duDebugDrawCompactHeightfieldRegions(&m_dd, *m_chf);
  170. // duDebugDrawCompactHeightfieldSolid(&dd, *m_chf);
  171. }
  172. if (m_navMesh)
  173. duDebugDrawNavMesh(&m_dd, *m_navMesh, DU_DRAWNAVMESH_OFFMESHCONS);
  174. if (m_ref && m_navMesh)
  175. duDebugDrawNavMeshPoly(&m_dd, *m_navMesh, m_ref, duRGBA(255,0,0,128));
  176. /* float bmin[3], bmax[3];
  177. rcVsub(bmin, m_center, m_halfExtents);
  178. rcVadd(bmax, m_center, m_halfExtents);
  179. duDebugDrawBoxWire(&dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], duRGBA(255,255,255,128), 1.0f);
  180. duDebugDrawCross(&dd, m_center[0], m_center[1], m_center[2], 1.0f, duRGBA(255,255,255,128), 2.0f);*/
  181. if (m_cset)
  182. {
  183. duDebugDrawRawContours(&m_dd, *m_cset, 0.25f);
  184. duDebugDrawContours(&m_dd, *m_cset);
  185. }
  186. if (m_pmesh)
  187. {
  188. duDebugDrawPolyMesh(&m_dd, *m_pmesh);
  189. }
  190. /*
  191. dd.depthMask(false);
  192. {
  193. const float bmin[3] = {-32.000004f,-11.488281f,-115.343544f};
  194. const float cs = 0.300000f;
  195. const float ch = 0.200000f;
  196. const int verts[] = {
  197. 158,46,336,0,
  198. 157,47,331,0,
  199. 161,53,330,0,
  200. 162,52,335,0,
  201. 158,46,336,0,
  202. 154,46,339,5,
  203. 161,46,365,5,
  204. 171,46,385,5,
  205. 174,46,400,5,
  206. 177,46,404,5,
  207. 177,46,410,5,
  208. 183,46,416,5,
  209. 188,49,416,5,
  210. 193,52,411,6,
  211. 194,53,382,6,
  212. 188,52,376,6,
  213. 188,57,363,6,
  214. 174,57,349,6,
  215. 174,60,342,6,
  216. 168,58,336,6,
  217. 167,59,328,6,
  218. 162,55,324,6,
  219. 159,53,324,5,
  220. 152,46,328,5,
  221. 151,46,336,5,
  222. 154,46,339,5,
  223. 158,46,336,0,
  224. 160,46,340,0,
  225. 164,52,339,0,
  226. 168,55,343,0,
  227. 168,50,351,0,
  228. 182,54,364,0,
  229. 182,47,378,0,
  230. 188,50,383,0,
  231. 188,49,409,0,
  232. 183,46,409,0,
  233. 183,46,403,0,
  234. 180,46,399,0,
  235. 177,46,384,0,
  236. 165,46,359,0,
  237. 160,46,340,0,
  238. };
  239. const int nverts = sizeof(verts)/(sizeof(int)*4);
  240. const unsigned int colln = duRGBA(255,255,255,128);
  241. dd.begin(DU_DRAW_LINES, 1.0f);
  242. for (int i = 0, j = nverts-1; i < nverts; j=i++)
  243. {
  244. const int* va = &verts[j*4];
  245. const int* vb = &verts[i*4];
  246. dd.vertex(bmin[0]+va[0]*cs, bmin[1]+va[1]*ch+j*0.01f, bmin[2]+va[2]*cs, colln);
  247. dd.vertex(bmin[0]+vb[0]*cs, bmin[1]+vb[1]*ch+i*0.01f, bmin[2]+vb[2]*cs, colln);
  248. }
  249. dd.end();
  250. const unsigned int colpt = duRGBA(255,255,255,255);
  251. dd.begin(DU_DRAW_POINTS, 3.0f);
  252. for (int i = 0, j = nverts-1; i < nverts; j=i++)
  253. {
  254. const int* va = &verts[j*4];
  255. dd.vertex(bmin[0]+va[0]*cs, bmin[1]+va[1]*ch+j*0.01f, bmin[2]+va[2]*cs, colpt);
  256. }
  257. dd.end();
  258. extern int triangulate(int n, const int* verts, int* indices, int* tris);
  259. static int indices[nverts];
  260. static int tris[nverts*3];
  261. for (int j = 0; j < nverts; ++j)
  262. indices[j] = j;
  263. static int ntris = 0;
  264. if (!ntris)
  265. {
  266. ntris = triangulate(nverts, verts, &indices[0], &tris[0]);
  267. if (ntris < 0) ntris = -ntris;
  268. }
  269. const unsigned int coltri = duRGBA(255,255,255,64);
  270. dd.begin(DU_DRAW_TRIS);
  271. for (int i = 0; i < ntris*3; ++i)
  272. {
  273. const int* va = &verts[indices[tris[i]]*4];
  274. dd.vertex(bmin[0]+va[0]*cs, bmin[1]+va[1]*ch, bmin[2]+va[2]*cs, coltri);
  275. }
  276. dd.end();
  277. }
  278. dd.depthMask(true);*/
  279. }
  280. void Sample_Debug::handleRenderOverlay(double* /*proj*/, double* /*model*/, int* /*view*/)
  281. {
  282. }
  283. void Sample_Debug::handleMeshChanged(InputGeom* geom)
  284. {
  285. m_geom = geom;
  286. }
  287. const float* Sample_Debug::getBoundsMin()
  288. {
  289. if (m_cset)
  290. return m_cset->bmin;
  291. if (m_chf)
  292. return m_chf->bmin;
  293. if (m_navMesh)
  294. return m_bmin;
  295. return 0;
  296. }
  297. const float* Sample_Debug::getBoundsMax()
  298. {
  299. if (m_cset)
  300. return m_cset->bmax;
  301. if (m_chf)
  302. return m_chf->bmax;
  303. if (m_navMesh)
  304. return m_bmax;
  305. return 0;
  306. }
  307. void Sample_Debug::handleClick(const float* s, const float* p, bool shift)
  308. {
  309. if (m_tool)
  310. m_tool->handleClick(s, p, shift);
  311. }
  312. void Sample_Debug::handleToggle()
  313. {
  314. if (m_tool)
  315. m_tool->handleToggle();
  316. }
  317. bool Sample_Debug::handleBuild()
  318. {
  319. if (m_chf)
  320. {
  321. rcFreeContourSet(m_cset);
  322. m_cset = 0;
  323. // Create contours.
  324. m_cset = rcAllocContourSet();
  325. if (!m_cset)
  326. {
  327. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'cset'.");
  328. return false;
  329. }
  330. if (!rcBuildContours(m_ctx, *m_chf, /*m_cfg.maxSimplificationError*/1.3f, /*m_cfg.maxEdgeLen*/12, *m_cset))
  331. {
  332. m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not create contours.");
  333. return false;
  334. }
  335. }
  336. return true;
  337. }