| 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 <cstdio> |
| 20 | #include <cmath> |
| 21 | |
| 22 | #include "SDL.h" |
| 23 | #include "SDL_opengl.h" |
| 24 | #ifdef __APPLE__ |
| 25 | # include <OpenGL/glu.h> |
| 26 | #else |
| 27 | # include <GL/glu.h> |
| 28 | #endif |
| 29 | |
| 30 | #include <vector> |
| 31 | #include <string> |
| 32 | |
| 33 | #include "imgui.h" |
| 34 | #include "imguiRenderGL.h" |
| 35 | |
| 36 | #include "Recast.h" |
| 37 | #include "RecastDebugDraw.h" |
| 38 | #include "InputGeom.h" |
| 39 | #include "TestCase.h" |
| 40 | #include "Filelist.h" |
| 41 | #include "Sample_SoloMesh.h" |
| 42 | #include "Sample_TileMesh.h" |
| 43 | #include "Sample_TempObstacles.h" |
| 44 | #include "Sample_Debug.h" |
| 45 | |
| 46 | #ifdef WIN32 |
| 47 | # define snprintf _snprintf |
| 48 | # define putenv _putenv |
| 49 | #endif |
| 50 | |
| 51 | using std::string; |
| 52 | using std::vector; |
| 53 | |
| 54 | struct SampleItem |
| 55 | { |
| 56 | Sample* (*create)(); |
| 57 | const string name; |
| 58 | }; |
| 59 | Sample* createSolo() { return new Sample_SoloMesh(); } |
| 60 | Sample* createTile() { return new Sample_TileMesh(); } |
| 61 | Sample* createTempObstacle() { return new Sample_TempObstacles(); } |
| 62 | Sample* createDebug() { return new Sample_Debug(); } |
| 63 | static SampleItem g_samples[] = |
| 64 | { |
| 65 | { createSolo, "Solo Mesh" }, |
| 66 | { createTile, "Tile Mesh" }, |
| 67 | { createTempObstacle, "Temp Obstacles" }, |
| 68 | }; |
| 69 | static const int g_nsamples = sizeof(g_samples) / sizeof(SampleItem); |
| 70 | |
| 71 | int main(int /*argc*/, char** /*argv*/) |
| 72 | { |
| 73 | // Init SDL |
| 74 | if (SDL_Init(SDL_INIT_EVERYTHING) != 0) |
| 75 | { |
| 76 | printf("Could not initialise SDL.\nError: %s\n", SDL_GetError()); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | // Use OpenGL render driver. |
| 81 | SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); |
| 82 | |
| 83 | // Enable depth buffer. |
| 84 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 85 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
| 86 | |
| 87 | // Set color channel depth. |
| 88 | SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); |
| 89 | SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); |
| 90 | SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); |
| 91 | SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); |
| 92 | |
| 93 | // 4x MSAA. |
| 94 | SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); |
| 95 | SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); |
| 96 | |
| 97 | SDL_DisplayMode displayMode; |
| 98 | SDL_GetCurrentDisplayMode(0, &displayMode); |
| 99 | |
| 100 | bool presentationMode = false; |
| 101 | Uint32 flags = SDL_WINDOW_OPENGL; |
| 102 | int width; |
| 103 | int height; |
| 104 | if (presentationMode) |
| 105 | { |
| 106 | // Create a fullscreen window at the native resolution. |
| 107 | width = displayMode.w; |
| 108 | height = displayMode.h; |
| 109 | flags |= SDL_WINDOW_FULLSCREEN; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | float aspect = 16.0f / 9.0f; |
| 114 | width = rcMin(displayMode.w, (int)(displayMode.h * aspect)) - 80; |
| 115 | height = displayMode.h - 80; |
| 116 | } |
| 117 | |
| 118 | SDL_Window* window; |
| 119 | SDL_Renderer* renderer; |
| 120 | int errorCode = SDL_CreateWindowAndRenderer(width, height, flags, &window, &renderer); |
| 121 | |
| 122 | if (errorCode != 0 || !window || !renderer) |
| 123 | { |
| 124 | printf("Could not initialise SDL opengl\nError: %s\n", SDL_GetError()); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); |
| 129 | |
| 130 | if (!imguiRenderGLInit("DroidSans.ttf")) |
| 131 | { |
| 132 | printf("Could not init GUI renderer.\n"); |
| 133 | SDL_Quit(); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | float timeAcc = 0.0f; |
| 138 | Uint32 prevFrameTime = SDL_GetTicks(); |
| 139 | int mousePos[2] = {0, 0}; |
| 140 | int origMousePos[2] = {0, 0}; // Used to compute mouse movement totals across frames. |
| 141 | |
| 142 | float cameraEulers[] = {45, -45}; |
| 143 | float cameraPos[] = {0, 0, 0}; |
| 144 | float camr = 1000; |
| 145 | float origCameraEulers[] = {0, 0}; // Used to compute rotational changes across frames. |
| 146 | |
| 147 | float moveFront = 0.0f, moveBack = 0.0f, moveLeft = 0.0f, moveRight = 0.0f, moveUp = 0.0f, moveDown = 0.0f; |
| 148 | |
| 149 | float scrollZoom = 0; |
| 150 | bool rotate = false; |
| 151 | bool movedDuringRotate = false; |
| 152 | float rayStart[3]; |
| 153 | float rayEnd[3]; |
| 154 | bool mouseOverMenu = false; |
| 155 | |
| 156 | bool showMenu = !presentationMode; |
| 157 | bool showLog = false; |
| 158 | bool showTools = true; |
| 159 | bool showLevels = false; |
| 160 | bool showSample = false; |
| 161 | bool showTestCases = false; |
| 162 | |
| 163 | // Window scroll positions. |
| 164 | int propScroll = 0; |
| 165 | int logScroll = 0; |
| 166 | int toolsScroll = 0; |
| 167 | |
| 168 | string sampleName = "Choose Sample..."; |
| 169 | |
| 170 | vector<string> files; |
| 171 | const string meshesFolder = "Meshes"; |
| 172 | string meshName = "Choose Mesh..."; |
| 173 | |
| 174 | float markerPosition[3] = {0, 0, 0}; |
| 175 | bool markerPositionSet = false; |
| 176 | |
| 177 | InputGeom* geom = 0; |
| 178 | Sample* sample = 0; |
| 179 | |
| 180 | const string testCasesFolder = "TestCases"; |
| 181 | TestCase* test = 0; |
| 182 | |
| 183 | BuildContext ctx; |
| 184 | |
| 185 | // Fog. |
| 186 | float fogColor[4] = { 0.32f, 0.31f, 0.30f, 1.0f }; |
| 187 | glEnable(GL_FOG); |
| 188 | glFogi(GL_FOG_MODE, GL_LINEAR); |
| 189 | glFogf(GL_FOG_START, camr * 0.1f); |
| 190 | glFogf(GL_FOG_END, camr * 1.25f); |
| 191 | glFogfv(GL_FOG_COLOR, fogColor); |
| 192 | |
| 193 | glEnable(GL_CULL_FACE); |
| 194 | glDepthFunc(GL_LEQUAL); |
| 195 | |
| 196 | bool done = false; |
| 197 | while(!done) |
| 198 | { |
| 199 | // Handle input events. |
| 200 | int mouseScroll = 0; |
| 201 | bool processHitTest = false; |
| 202 | bool processHitTestShift = false; |
| 203 | SDL_Event event; |
| 204 | |
| 205 | while (SDL_PollEvent(&event)) |
| 206 | { |
| 207 | switch (event.type) |
| 208 | { |
| 209 | case SDL_KEYDOWN: |
| 210 | // Handle any key presses here. |
| 211 | if (event.key.keysym.sym == SDLK_ESCAPE) |
| 212 | { |
| 213 | done = true; |
| 214 | } |
| 215 | else if (event.key.keysym.sym == SDLK_t) |
| 216 | { |
| 217 | showLevels = false; |
| 218 | showSample = false; |
| 219 | showTestCases = true; |
| 220 | scanDirectory(testCasesFolder, ".txt", files); |
| 221 | } |
| 222 | else if (event.key.keysym.sym == SDLK_TAB) |
| 223 | { |
| 224 | showMenu = !showMenu; |
| 225 | } |
| 226 | else if (event.key.keysym.sym == SDLK_SPACE) |
| 227 | { |
| 228 | if (sample) |
| 229 | sample->handleToggle(); |
| 230 | } |
| 231 | else if (event.key.keysym.sym == SDLK_1) |
| 232 | { |
| 233 | if (sample) |
| 234 | sample->handleStep(); |
| 235 | } |
| 236 | else if (event.key.keysym.sym == SDLK_9) |
| 237 | { |
| 238 | if (sample && geom) |
| 239 | { |
| 240 | string savePath = meshesFolder + "/"; |
| 241 | BuildSettings settings; |
| 242 | memset(&settings, 0, sizeof(settings)); |
| 243 | |
| 244 | rcVcopy(settings.navMeshBMin, geom->getNavMeshBoundsMin()); |
| 245 | rcVcopy(settings.navMeshBMax, geom->getNavMeshBoundsMax()); |
| 246 | |
| 247 | sample->collectSettings(settings); |
| 248 | |
| 249 | geom->saveGeomSet(&settings); |
| 250 | } |
| 251 | } |
| 252 | break; |
| 253 | |
| 254 | case SDL_MOUSEWHEEL: |
| 255 | if (event.wheel.y < 0) |
| 256 | { |
| 257 | // wheel down |
| 258 | if (mouseOverMenu) |
| 259 | { |
| 260 | mouseScroll++; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | scrollZoom += 1.0f; |
| 265 | } |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | if (mouseOverMenu) |
| 270 | { |
| 271 | mouseScroll--; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | scrollZoom -= 1.0f; |
| 276 | } |
| 277 | } |
| 278 | break; |
| 279 | case SDL_MOUSEBUTTONDOWN: |
| 280 | if (event.button.button == SDL_BUTTON_RIGHT) |
| 281 | { |
| 282 | if (!mouseOverMenu) |
| 283 | { |
| 284 | // Rotate view |
| 285 | rotate = true; |
| 286 | movedDuringRotate = false; |
| 287 | origMousePos[0] = mousePos[0]; |
| 288 | origMousePos[1] = mousePos[1]; |
| 289 | origCameraEulers[0] = cameraEulers[0]; |
| 290 | origCameraEulers[1] = cameraEulers[1]; |
| 291 | } |
| 292 | } |
| 293 | break; |
| 294 | |
| 295 | case SDL_MOUSEBUTTONUP: |
| 296 | // Handle mouse clicks here. |
| 297 | if (event.button.button == SDL_BUTTON_RIGHT) |
| 298 | { |
| 299 | rotate = false; |
| 300 | if (!mouseOverMenu) |
| 301 | { |
| 302 | if (!movedDuringRotate) |
| 303 | { |
| 304 | processHitTest = true; |
| 305 | processHitTestShift = true; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | else if (event.button.button == SDL_BUTTON_LEFT) |
| 310 | { |
| 311 | if (!mouseOverMenu) |
| 312 | { |
| 313 | processHitTest = true; |
| 314 | processHitTestShift = (SDL_GetModState() & KMOD_SHIFT) ? true : false; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | break; |
| 319 | |
| 320 | case SDL_MOUSEMOTION: |
| 321 | mousePos[0] = event.motion.x; |
| 322 | mousePos[1] = height-1 - event.motion.y; |
| 323 | |
| 324 | if (rotate) |
| 325 | { |
| 326 | int dx = mousePos[0] - origMousePos[0]; |
| 327 | int dy = mousePos[1] - origMousePos[1]; |
| 328 | cameraEulers[0] = origCameraEulers[0] - dy * 0.25f; |
| 329 | cameraEulers[1] = origCameraEulers[1] + dx * 0.25f; |
| 330 | if (dx * dx + dy * dy > 3 * 3) |
| 331 | { |
| 332 | movedDuringRotate = true; |
| 333 | } |
| 334 | } |
| 335 | break; |
| 336 | |
| 337 | case SDL_QUIT: |
| 338 | done = true; |
| 339 | break; |
| 340 | |
| 341 | default: |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | unsigned char mouseButtonMask = 0; |
| 347 | if (SDL_GetMouseState(0, 0) & SDL_BUTTON_LMASK) |
| 348 | mouseButtonMask |= IMGUI_MBUT_LEFT; |
| 349 | if (SDL_GetMouseState(0, 0) & SDL_BUTTON_RMASK) |
| 350 | mouseButtonMask |= IMGUI_MBUT_RIGHT; |
| 351 | |
| 352 | Uint32 time = SDL_GetTicks(); |
| 353 | float dt = (time - prevFrameTime) / 1000.0f; |
| 354 | prevFrameTime = time; |
| 355 | |
| 356 | // Hit test mesh. |
| 357 | if (processHitTest && geom && sample) |
| 358 | { |
| 359 | float hitTime; |
| 360 | bool hit = geom->raycastMesh(rayStart, rayEnd, hitTime); |
| 361 | |
| 362 | if (hit) |
| 363 | { |
| 364 | if (SDL_GetModState() & KMOD_CTRL) |
| 365 | { |
| 366 | // Marker |
| 367 | markerPositionSet = true; |
| 368 | markerPosition[0] = rayStart[0] + (rayEnd[0] - rayStart[0]) * hitTime; |
| 369 | markerPosition[1] = rayStart[1] + (rayEnd[1] - rayStart[1]) * hitTime; |
| 370 | markerPosition[2] = rayStart[2] + (rayEnd[2] - rayStart[2]) * hitTime; |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | float pos[3]; |
| 375 | pos[0] = rayStart[0] + (rayEnd[0] - rayStart[0]) * hitTime; |
| 376 | pos[1] = rayStart[1] + (rayEnd[1] - rayStart[1]) * hitTime; |
| 377 | pos[2] = rayStart[2] + (rayEnd[2] - rayStart[2]) * hitTime; |
| 378 | sample->handleClick(rayStart, pos, processHitTestShift); |
| 379 | } |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | if (SDL_GetModState() & KMOD_CTRL) |
| 384 | { |
| 385 | // Marker |
| 386 | markerPositionSet = false; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | // Update sample simulation. |
| 392 | const float SIM_RATE = 20; |
| 393 | const float DELTA_TIME = 1.0f / SIM_RATE; |
| 394 | timeAcc = rcClamp(timeAcc + dt, -1.0f, 1.0f); |
| 395 | int simIter = 0; |
| 396 | while (timeAcc > DELTA_TIME) |
| 397 | { |
| 398 | timeAcc -= DELTA_TIME; |
| 399 | if (simIter < 5 && sample) |
| 400 | { |
| 401 | sample->handleUpdate(DELTA_TIME); |
| 402 | } |
| 403 | simIter++; |
| 404 | } |
| 405 | |
| 406 | // Clamp the framerate so that we do not hog all the CPU. |
| 407 | const float MIN_FRAME_TIME = 1.0f / 40.0f; |
| 408 | if (dt < MIN_FRAME_TIME) |
| 409 | { |
| 410 | int ms = (int)((MIN_FRAME_TIME - dt) * 1000.0f); |
| 411 | if (ms > 10) ms = 10; |
| 412 | if (ms >= 0) SDL_Delay(ms); |
| 413 | } |
| 414 | |
| 415 | // Set the viewport. |
| 416 | glViewport(0, 0, width, height); |
| 417 | GLint viewport[4]; |
| 418 | glGetIntegerv(GL_VIEWPORT, viewport); |
| 419 | |
| 420 | // Clear the screen |
| 421 | glClearColor(0.3f, 0.3f, 0.32f, 1.0f); |
| 422 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 423 | glEnable(GL_BLEND); |
| 424 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 425 | glDisable(GL_TEXTURE_2D); |
| 426 | glEnable(GL_DEPTH_TEST); |
| 427 | |
| 428 | // Compute the projection matrix. |
| 429 | glMatrixMode(GL_PROJECTION); |
| 430 | glLoadIdentity(); |
| 431 | gluPerspective(50.0f, (float)width/(float)height, 1.0f, camr); |
| 432 | GLdouble projectionMatrix[16]; |
| 433 | glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix); |
| 434 | |
| 435 | // Compute the modelview matrix. |
| 436 | glMatrixMode(GL_MODELVIEW); |
| 437 | glLoadIdentity(); |
| 438 | glRotatef(cameraEulers[0], 1, 0, 0); |
| 439 | glRotatef(cameraEulers[1], 0, 1, 0); |
| 440 | glTranslatef(-cameraPos[0], -cameraPos[1], -cameraPos[2]); |
| 441 | GLdouble modelviewMatrix[16]; |
| 442 | glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix); |
| 443 | |
| 444 | // Get hit ray position and direction. |
| 445 | GLdouble x, y, z; |
| 446 | gluUnProject(mousePos[0], mousePos[1], 0.0f, modelviewMatrix, projectionMatrix, viewport, &x, &y, &z); |
| 447 | rayStart[0] = (float)x; |
| 448 | rayStart[1] = (float)y; |
| 449 | rayStart[2] = (float)z; |
| 450 | gluUnProject(mousePos[0], mousePos[1], 1.0f, modelviewMatrix, projectionMatrix, viewport, &x, &y, &z); |
| 451 | rayEnd[0] = (float)x; |
| 452 | rayEnd[1] = (float)y; |
| 453 | rayEnd[2] = (float)z; |
| 454 | |
| 455 | // Handle keyboard movement. |
| 456 | const Uint8* keystate = SDL_GetKeyboardState(NULL); |
| 457 | moveFront = rcClamp(moveFront + dt * 4 * ((keystate[SDL_SCANCODE_W] || keystate[SDL_SCANCODE_UP ]) ? 1 : -1), 0.0f, 1.0f); |
| 458 | moveLeft = rcClamp(moveLeft + dt * 4 * ((keystate[SDL_SCANCODE_A] || keystate[SDL_SCANCODE_LEFT ]) ? 1 : -1), 0.0f, 1.0f); |
| 459 | moveBack = rcClamp(moveBack + dt * 4 * ((keystate[SDL_SCANCODE_S] || keystate[SDL_SCANCODE_DOWN ]) ? 1 : -1), 0.0f, 1.0f); |
| 460 | moveRight = rcClamp(moveRight + dt * 4 * ((keystate[SDL_SCANCODE_D] || keystate[SDL_SCANCODE_RIGHT ]) ? 1 : -1), 0.0f, 1.0f); |
| 461 | moveUp = rcClamp(moveUp + dt * 4 * ((keystate[SDL_SCANCODE_Q] || keystate[SDL_SCANCODE_PAGEUP ]) ? 1 : -1), 0.0f, 1.0f); |
| 462 | moveDown = rcClamp(moveDown + dt * 4 * ((keystate[SDL_SCANCODE_E] || keystate[SDL_SCANCODE_PAGEDOWN ]) ? 1 : -1), 0.0f, 1.0f); |
| 463 | |
| 464 | float keybSpeed = 22.0f; |
| 465 | if (SDL_GetModState() & KMOD_SHIFT) |
| 466 | { |
| 467 | keybSpeed *= 4.0f; |
| 468 | } |
| 469 | |
| 470 | float movex = (moveRight - moveLeft) * keybSpeed * dt; |
| 471 | float movey = (moveBack - moveFront) * keybSpeed * dt + scrollZoom * 2.0f; |
| 472 | scrollZoom = 0; |
| 473 | |
| 474 | cameraPos[0] += movex * (float)modelviewMatrix[0]; |
| 475 | cameraPos[1] += movex * (float)modelviewMatrix[4]; |
| 476 | cameraPos[2] += movex * (float)modelviewMatrix[8]; |
| 477 | |
| 478 | cameraPos[0] += movey * (float)modelviewMatrix[2]; |
| 479 | cameraPos[1] += movey * (float)modelviewMatrix[6]; |
| 480 | cameraPos[2] += movey * (float)modelviewMatrix[10]; |
| 481 | |
| 482 | cameraPos[1] += (moveUp - moveDown) * keybSpeed * dt; |
| 483 | |
| 484 | glEnable(GL_FOG); |
| 485 | |
| 486 | if (sample) |
| 487 | sample->handleRender(); |
| 488 | if (test) |
| 489 | test->handleRender(); |
| 490 | |
| 491 | glDisable(GL_FOG); |
| 492 | |
| 493 | // Render GUI |
| 494 | glDisable(GL_DEPTH_TEST); |
| 495 | glMatrixMode(GL_PROJECTION); |
| 496 | glLoadIdentity(); |
| 497 | gluOrtho2D(0, width, 0, height); |
| 498 | glMatrixMode(GL_MODELVIEW); |
| 499 | glLoadIdentity(); |
| 500 | |
| 501 | mouseOverMenu = false; |
| 502 | |
| 503 | imguiBeginFrame(mousePos[0], mousePos[1], mouseButtonMask, mouseScroll); |
| 504 | |
| 505 | if (sample) |
| 506 | { |
| 507 | sample->handleRenderOverlay((double*)projectionMatrix, (double*)modelviewMatrix, (int*)viewport); |
| 508 | } |
| 509 | if (test) |
| 510 | { |
| 511 | if (test->handleRenderOverlay((double*)projectionMatrix, (double*)modelviewMatrix, (int*)viewport)) |
| 512 | mouseOverMenu = true; |
| 513 | } |
| 514 | |
| 515 | // Help text. |
| 516 | if (showMenu) |
| 517 | { |
| 518 | const char msg[] = "W/S/A/D: Move RMB: Rotate"; |
| 519 | imguiDrawText(280, height-20, IMGUI_ALIGN_LEFT, msg, imguiRGBA(255,255,255,128)); |
| 520 | } |
| 521 | |
| 522 | if (showMenu) |
| 523 | { |
| 524 | if (imguiBeginScrollArea("Properties", width-250-10, 10, 250, height-20, &propScroll)) |
| 525 | mouseOverMenu = true; |
| 526 | |
| 527 | if (imguiCheck("Show Log", showLog)) |
| 528 | showLog = !showLog; |
| 529 | if (imguiCheck("Show Tools", showTools)) |
| 530 | showTools = !showTools; |
| 531 | |
| 532 | imguiSeparator(); |
| 533 | imguiLabel("Sample"); |
| 534 | if (imguiButton(sampleName.c_str())) |
| 535 | { |
| 536 | if (showSample) |
| 537 | { |
| 538 | showSample = false; |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | showSample = true; |
| 543 | showLevels = false; |
| 544 | showTestCases = false; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | imguiSeparator(); |
| 549 | imguiLabel("Input Mesh"); |
| 550 | if (imguiButton(meshName.c_str())) |
| 551 | { |
| 552 | if (showLevels) |
| 553 | { |
| 554 | showLevels = false; |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | showSample = false; |
| 559 | showTestCases = false; |
| 560 | showLevels = true; |
| 561 | scanDirectory(meshesFolder, ".obj", files); |
| 562 | scanDirectoryAppend(meshesFolder, ".gset", files); |
| 563 | } |
| 564 | } |
| 565 | if (geom) |
| 566 | { |
| 567 | char text[64]; |
| 568 | snprintf(text, 64, "Verts: %.1fk Tris: %.1fk", |
| 569 | geom->getMesh()->getVertCount()/1000.0f, |
| 570 | geom->getMesh()->getTriCount()/1000.0f); |
| 571 | imguiValue(text); |
| 572 | } |
| 573 | imguiSeparator(); |
| 574 | |
| 575 | if (geom && sample) |
| 576 | { |
| 577 | imguiSeparatorLine(); |
| 578 | |
| 579 | sample->handleSettings(); |
| 580 | |
| 581 | if (imguiButton("Build")) |
| 582 | { |
| 583 | ctx.resetLog(); |
| 584 | if (!sample->handleBuild()) |
| 585 | { |
| 586 | showLog = true; |
| 587 | logScroll = 0; |
| 588 | } |
| 589 | ctx.dumpLog("Build log %s:", meshName.c_str()); |
| 590 | |
| 591 | // Clear test. |
| 592 | delete test; |
| 593 | test = 0; |
| 594 | } |
| 595 | |
| 596 | imguiSeparator(); |
| 597 | } |
| 598 | |
| 599 | if (sample) |
| 600 | { |
| 601 | imguiSeparatorLine(); |
| 602 | sample->handleDebugMode(); |
| 603 | } |
| 604 | |
| 605 | imguiEndScrollArea(); |
| 606 | } |
| 607 | |
| 608 | // Sample selection dialog. |
| 609 | if (showSample) |
| 610 | { |
| 611 | static int levelScroll = 0; |
| 612 | if (imguiBeginScrollArea("Choose Sample", width-10-250-10-200, height-10-250, 200, 250, &levelScroll)) |
| 613 | mouseOverMenu = true; |
| 614 | |
| 615 | Sample* newSample = 0; |
| 616 | for (int i = 0; i < g_nsamples; ++i) |
| 617 | { |
| 618 | if (imguiItem(g_samples[i].name.c_str())) |
| 619 | { |
| 620 | newSample = g_samples[i].create(); |
| 621 | if (newSample) |
| 622 | sampleName = g_samples[i].name; |
| 623 | } |
| 624 | } |
| 625 | if (newSample) |
| 626 | { |
| 627 | delete sample; |
| 628 | sample = newSample; |
| 629 | sample->setContext(&ctx); |
| 630 | if (geom) |
| 631 | { |
| 632 | sample->handleMeshChanged(geom); |
| 633 | } |
| 634 | showSample = false; |
| 635 | } |
| 636 | |
| 637 | if (geom || sample) |
| 638 | { |
| 639 | const float* bmin = 0; |
| 640 | const float* bmax = 0; |
| 641 | if (geom) |
| 642 | { |
| 643 | bmin = geom->getNavMeshBoundsMin(); |
| 644 | bmax = geom->getNavMeshBoundsMax(); |
| 645 | } |
| 646 | // Reset camera and fog to match the mesh bounds. |
| 647 | if (bmin && bmax) |
| 648 | { |
| 649 | camr = sqrtf(rcSqr(bmax[0]-bmin[0]) + |
| 650 | rcSqr(bmax[1]-bmin[1]) + |
| 651 | rcSqr(bmax[2]-bmin[2])) / 2; |
| 652 | cameraPos[0] = (bmax[0] + bmin[0]) / 2 + camr; |
| 653 | cameraPos[1] = (bmax[1] + bmin[1]) / 2 + camr; |
| 654 | cameraPos[2] = (bmax[2] + bmin[2]) / 2 + camr; |
| 655 | camr *= 3; |
| 656 | } |
| 657 | cameraEulers[0] = 45; |
| 658 | cameraEulers[1] = -45; |
| 659 | glFogf(GL_FOG_START, camr*0.1f); |
| 660 | glFogf(GL_FOG_END, camr*1.25f); |
| 661 | } |
| 662 | |
| 663 | imguiEndScrollArea(); |
| 664 | } |
| 665 | |
| 666 | // Level selection dialog. |
| 667 | if (showLevels) |
| 668 | { |
| 669 | static int levelScroll = 0; |
| 670 | if (imguiBeginScrollArea("Choose Level", width - 10 - 250 - 10 - 200, height - 10 - 450, 200, 450, &levelScroll)) |
| 671 | mouseOverMenu = true; |
| 672 | |
| 673 | vector<string>::const_iterator fileIter = files.begin(); |
| 674 | vector<string>::const_iterator filesEnd = files.end(); |
| 675 | vector<string>::const_iterator levelToLoad = filesEnd; |
| 676 | for (; fileIter != filesEnd; ++fileIter) |
| 677 | { |
| 678 | if (imguiItem(fileIter->c_str())) |
| 679 | { |
| 680 | levelToLoad = fileIter; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | if (levelToLoad != filesEnd) |
| 685 | { |
| 686 | meshName = *levelToLoad; |
| 687 | showLevels = false; |
| 688 | |
| 689 | delete geom; |
| 690 | geom = 0; |
| 691 | |
| 692 | string path = meshesFolder + "/" + meshName; |
| 693 | |
| 694 | geom = new InputGeom; |
| 695 | if (!geom->load(&ctx, path)) |
| 696 | { |
| 697 | delete geom; |
| 698 | geom = 0; |
| 699 | |
| 700 | // Destroy the sample if it already had geometry loaded, as we've just deleted it! |
| 701 | if (sample && sample->getInputGeom()) |
| 702 | { |
| 703 | delete sample; |
| 704 | sample = 0; |
| 705 | } |
| 706 | |
| 707 | showLog = true; |
| 708 | logScroll = 0; |
| 709 | ctx.dumpLog("Geom load log %s:", meshName.c_str()); |
| 710 | } |
| 711 | if (sample && geom) |
| 712 | { |
| 713 | sample->handleMeshChanged(geom); |
| 714 | } |
| 715 | |
| 716 | if (geom || sample) |
| 717 | { |
| 718 | const float* bmin = 0; |
| 719 | const float* bmax = 0; |
| 720 | if (geom) |
| 721 | { |
| 722 | bmin = geom->getNavMeshBoundsMin(); |
| 723 | bmax = geom->getNavMeshBoundsMax(); |
| 724 | } |
| 725 | // Reset camera and fog to match the mesh bounds. |
| 726 | if (bmin && bmax) |
| 727 | { |
| 728 | camr = sqrtf(rcSqr(bmax[0]-bmin[0]) + |
| 729 | rcSqr(bmax[1]-bmin[1]) + |
| 730 | rcSqr(bmax[2]-bmin[2])) / 2; |
| 731 | cameraPos[0] = (bmax[0] + bmin[0]) / 2 + camr; |
| 732 | cameraPos[1] = (bmax[1] + bmin[1]) / 2 + camr; |
| 733 | cameraPos[2] = (bmax[2] + bmin[2]) / 2 + camr; |
| 734 | camr *= 3; |
| 735 | } |
| 736 | cameraEulers[0] = 45; |
| 737 | cameraEulers[1] = -45; |
| 738 | glFogf(GL_FOG_START, camr * 0.1f); |
| 739 | glFogf(GL_FOG_END, camr * 1.25f); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | imguiEndScrollArea(); |
| 744 | |
| 745 | } |
| 746 | |
| 747 | // Test cases |
| 748 | if (showTestCases) |
| 749 | { |
| 750 | static int testScroll = 0; |
| 751 | if (imguiBeginScrollArea("Choose Test To Run", width-10-250-10-200, height-10-450, 200, 450, &testScroll)) |
| 752 | mouseOverMenu = true; |
| 753 | |
| 754 | vector<string>::const_iterator fileIter = files.begin(); |
| 755 | vector<string>::const_iterator filesEnd = files.end(); |
| 756 | vector<string>::const_iterator testToLoad = filesEnd; |
| 757 | for (; fileIter != filesEnd; ++fileIter) |
| 758 | { |
| 759 | if (imguiItem(fileIter->c_str())) |
| 760 | { |
| 761 | testToLoad = fileIter; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | if (testToLoad != filesEnd) |
| 766 | { |
| 767 | string path = testCasesFolder + "/" + *testToLoad; |
| 768 | test = new TestCase; |
| 769 | if (test) |
| 770 | { |
| 771 | // Load the test. |
| 772 | if (!test->load(path)) |
| 773 | { |
| 774 | delete test; |
| 775 | test = 0; |
| 776 | } |
| 777 | |
| 778 | // Create sample |
| 779 | Sample* newSample = 0; |
| 780 | for (int i = 0; i < g_nsamples; ++i) |
| 781 | { |
| 782 | if (g_samples[i].name == test->getSampleName()) |
| 783 | { |
| 784 | newSample = g_samples[i].create(); |
| 785 | if (newSample) |
| 786 | sampleName = g_samples[i].name; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | delete sample; |
| 791 | sample = newSample; |
| 792 | |
| 793 | if (sample) |
| 794 | { |
| 795 | sample->setContext(&ctx); |
| 796 | showSample = false; |
| 797 | } |
| 798 | |
| 799 | // Load geom. |
| 800 | meshName = test->getGeomFileName(); |
| 801 | |
| 802 | |
| 803 | path = meshesFolder + "/" + meshName; |
| 804 | |
| 805 | delete geom; |
| 806 | geom = new InputGeom; |
| 807 | if (!geom || !geom->load(&ctx, path)) |
| 808 | { |
| 809 | delete geom; |
| 810 | geom = 0; |
| 811 | delete sample; |
| 812 | sample = 0; |
| 813 | showLog = true; |
| 814 | logScroll = 0; |
| 815 | ctx.dumpLog("Geom load log %s:", meshName.c_str()); |
| 816 | } |
| 817 | if (sample && geom) |
| 818 | { |
| 819 | sample->handleMeshChanged(geom); |
| 820 | } |
| 821 | |
| 822 | // This will ensure that tile & poly bits are updated in tiled sample. |
| 823 | if (sample) |
| 824 | sample->handleSettings(); |
| 825 | |
| 826 | ctx.resetLog(); |
| 827 | if (sample && !sample->handleBuild()) |
| 828 | { |
| 829 | ctx.dumpLog("Build log %s:", meshName.c_str()); |
| 830 | } |
| 831 | |
| 832 | if (geom || sample) |
| 833 | { |
| 834 | const float* bmin = 0; |
| 835 | const float* bmax = 0; |
| 836 | if (geom) |
| 837 | { |
| 838 | bmin = geom->getNavMeshBoundsMin(); |
| 839 | bmax = geom->getNavMeshBoundsMax(); |
| 840 | } |
| 841 | // Reset camera and fog to match the mesh bounds. |
| 842 | if (bmin && bmax) |
| 843 | { |
| 844 | camr = sqrtf(rcSqr(bmax[0] - bmin[0]) + |
| 845 | rcSqr(bmax[1] - bmin[1]) + |
| 846 | rcSqr(bmax[2] - bmin[2])) / 2; |
| 847 | cameraPos[0] = (bmax[0] + bmin[0]) / 2 + camr; |
| 848 | cameraPos[1] = (bmax[1] + bmin[1]) / 2 + camr; |
| 849 | cameraPos[2] = (bmax[2] + bmin[2]) / 2 + camr; |
| 850 | camr *= 3; |
| 851 | } |
| 852 | cameraEulers[0] = 45; |
| 853 | cameraEulers[1] = -45; |
| 854 | glFogf(GL_FOG_START, camr * 0.2f); |
| 855 | glFogf(GL_FOG_END, camr * 1.25f); |
| 856 | } |
| 857 | |
| 858 | // Do the tests. |
| 859 | if (sample) |
| 860 | test->doTests(sample->getNavMesh(), sample->getNavMeshQuery()); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | imguiEndScrollArea(); |
| 865 | } |
| 866 | |
| 867 | |
| 868 | // Log |
| 869 | if (showLog && showMenu) |
| 870 | { |
| 871 | if (imguiBeginScrollArea("Log", 250 + 20, 10, width - 300 - 250, 200, &logScroll)) |
| 872 | mouseOverMenu = true; |
| 873 | for (int i = 0; i < ctx.getLogCount(); ++i) |
| 874 | imguiLabel(ctx.getLogText(i)); |
| 875 | imguiEndScrollArea(); |
| 876 | } |
| 877 | |
| 878 | // Left column tools menu |
| 879 | if (!showTestCases && showTools && showMenu) // && geom && sample) |
| 880 | { |
| 881 | if (imguiBeginScrollArea("Tools", 10, 10, 250, height - 20, &toolsScroll)) |
| 882 | mouseOverMenu = true; |
| 883 | |
| 884 | if (sample) |
| 885 | sample->handleTools(); |
| 886 | |
| 887 | imguiEndScrollArea(); |
| 888 | } |
| 889 | |
| 890 | // Marker |
| 891 | if (markerPositionSet && gluProject((GLdouble)markerPosition[0], (GLdouble)markerPosition[1], (GLdouble)markerPosition[2], |
| 892 | modelviewMatrix, projectionMatrix, viewport, &x, &y, &z)) |
| 893 | { |
| 894 | // Draw marker circle |
| 895 | glLineWidth(5.0f); |
| 896 | glColor4ub(240,220,0,196); |
| 897 | glBegin(GL_LINE_LOOP); |
| 898 | const float r = 25.0f; |
| 899 | for (int i = 0; i < 20; ++i) |
| 900 | { |
| 901 | const float a = (float)i / 20.0f * RC_PI*2; |
| 902 | const float fx = (float)x + cosf(a)*r; |
| 903 | const float fy = (float)y + sinf(a)*r; |
| 904 | glVertex2f(fx,fy); |
| 905 | } |
| 906 | glEnd(); |
| 907 | glLineWidth(1.0f); |
| 908 | } |
| 909 | |
| 910 | imguiEndFrame(); |
| 911 | imguiRenderGLDraw(); |
| 912 | |
| 913 | glEnable(GL_DEPTH_TEST); |
| 914 | SDL_GL_SwapWindow(window); |
| 915 | } |
| 916 | |
| 917 | imguiRenderGLDestroy(); |
| 918 | |
| 919 | SDL_Quit(); |
| 920 | |
| 921 | delete sample; |
| 922 | delete geom; |
| 923 | |
| 924 | return 0; |
| 925 | } |