| 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 <string.h> |
| 22 | #include <float.h> |
| 23 | #include "SDL.h" |
| 24 | #include "SDL_opengl.h" |
| 25 | #ifdef __APPLE__ |
| 26 | # include <OpenGL/glu.h> |
| 27 | #else |
| 28 | # include <GL/glu.h> |
| 29 | #endif |
| 30 | #include "imgui.h" |
| 31 | #include "OffMeshConnectionTool.h" |
| 32 | #include "InputGeom.h" |
| 33 | #include "Sample.h" |
| 34 | #include "Recast.h" |
| 35 | #include "RecastDebugDraw.h" |
| 36 | #include "DetourDebugDraw.h" |
| 37 | |
| 38 | #ifdef WIN32 |
| 39 | # define snprintf _snprintf |
| 40 | #endif |
| 41 | |
| 42 | OffMeshConnectionTool::OffMeshConnectionTool() : |
| 43 | m_sample(0), |
| 44 | m_hitPosSet(0), |
| 45 | m_bidir(true), |
| 46 | m_oldFlags(0) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | OffMeshConnectionTool::~OffMeshConnectionTool() |
| 51 | { |
| 52 | if (m_sample) |
| 53 | { |
| 54 | m_sample->setNavMeshDrawFlags(m_oldFlags); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void OffMeshConnectionTool::init(Sample* sample) |
| 59 | { |
| 60 | if (m_sample != sample) |
| 61 | { |
| 62 | m_sample = sample; |
| 63 | m_oldFlags = m_sample->getNavMeshDrawFlags(); |
| 64 | m_sample->setNavMeshDrawFlags(m_oldFlags & ~DU_DRAWNAVMESH_OFFMESHCONS); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void OffMeshConnectionTool::reset() |
| 69 | { |
| 70 | m_hitPosSet = false; |
| 71 | } |
| 72 | |
| 73 | void OffMeshConnectionTool::handleMenu() |
| 74 | { |
| 75 | if (imguiCheck("One Way", !m_bidir)) |
| 76 | m_bidir = false; |
| 77 | if (imguiCheck("Bidirectional", m_bidir)) |
| 78 | m_bidir = true; |
| 79 | } |
| 80 | |
| 81 | void OffMeshConnectionTool::handleClick(const float* /*s*/, const float* p, bool shift) |
| 82 | { |
| 83 | if (!m_sample) return; |
| 84 | InputGeom* geom = m_sample->getInputGeom(); |
| 85 | if (!geom) return; |
| 86 | |
| 87 | if (shift) |
| 88 | { |
| 89 | // Delete |
| 90 | // Find nearest link end-point |
| 91 | float nearestDist = FLT_MAX; |
| 92 | int nearestIndex = -1; |
| 93 | const float* verts = geom->getOffMeshConnectionVerts(); |
| 94 | for (int i = 0; i < geom->getOffMeshConnectionCount()*2; ++i) |
| 95 | { |
| 96 | const float* v = &verts[i*3]; |
| 97 | float d = rcVdistSqr(p, v); |
| 98 | if (d < nearestDist) |
| 99 | { |
| 100 | nearestDist = d; |
| 101 | nearestIndex = i/2; // Each link has two vertices. |
| 102 | } |
| 103 | } |
| 104 | // If end point close enough, delete it. |
| 105 | if (nearestIndex != -1 && |
| 106 | sqrtf(nearestDist) < m_sample->getAgentRadius()) |
| 107 | { |
| 108 | geom->deleteOffMeshConnection(nearestIndex); |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // Create |
| 114 | if (!m_hitPosSet) |
| 115 | { |
| 116 | rcVcopy(m_hitPos, p); |
| 117 | m_hitPosSet = true; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | const unsigned char area = SAMPLE_POLYAREA_JUMP; |
| 122 | const unsigned short flags = SAMPLE_POLYFLAGS_JUMP; |
| 123 | geom->addOffMeshConnection(m_hitPos, p, m_sample->getAgentRadius(), m_bidir ? 1 : 0, area, flags); |
| 124 | m_hitPosSet = false; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | void OffMeshConnectionTool::handleToggle() |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | void OffMeshConnectionTool::handleStep() |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | void OffMeshConnectionTool::handleUpdate(const float /*dt*/) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | void OffMeshConnectionTool::handleRender() |
| 143 | { |
| 144 | duDebugDraw& dd = m_sample->getDebugDraw(); |
| 145 | const float s = m_sample->getAgentRadius(); |
| 146 | |
| 147 | if (m_hitPosSet) |
| 148 | duDebugDrawCross(&dd, m_hitPos[0],m_hitPos[1]+0.1f,m_hitPos[2], s, duRGBA(0,0,0,128), 2.0f); |
| 149 | |
| 150 | InputGeom* geom = m_sample->getInputGeom(); |
| 151 | if (geom) |
| 152 | geom->drawOffMeshConnections(&dd, true); |
| 153 | } |
| 154 | |
| 155 | void OffMeshConnectionTool::handleRenderOverlay(double* proj, double* model, int* view) |
| 156 | { |
| 157 | GLdouble x, y, z; |
| 158 | |
| 159 | // Draw start and end point labels |
| 160 | if (m_hitPosSet && gluProject((GLdouble)m_hitPos[0], (GLdouble)m_hitPos[1], (GLdouble)m_hitPos[2], |
| 161 | model, proj, view, &x, &y, &z)) |
| 162 | { |
| 163 | imguiDrawText((int)x, (int)(y-25), IMGUI_ALIGN_CENTER, "Start", imguiRGBA(0,0,0,220)); |
| 164 | } |
| 165 | |
| 166 | // Tool help |
| 167 | const int h = view[3]; |
| 168 | if (!m_hitPosSet) |
| 169 | { |
| 170 | imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "LMB: Create new connection. SHIFT+LMB: Delete existing connection, click close to start or end point.", imguiRGBA(255,255,255,192)); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "LMB: Set connection end point and finish.", imguiRGBA(255,255,255,192)); |
| 175 | } |
| 176 | } |