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