diff --git a/RecastDemo/Include/OffMeshConnectionTool.h b/RecastDemo/Include/OffMeshConnectionTool.h index dd4930b9..6c012084 100644 --- a/RecastDemo/Include/OffMeshConnectionTool.h +++ b/RecastDemo/Include/OffMeshConnectionTool.h @@ -20,33 +20,33 @@ #include "Sample.h" -/// Tool to create off-mesh connection for InputGeom +/// Tool to create off-mesh connections for InputGeom class OffMeshConnectionTool : public SampleTool { - Sample* m_sample = nullptr; - float m_hitPos[3]; - bool m_hitPosSet = 0; - bool m_bidir = true; - unsigned char m_oldFlags = 0; + Sample* sample = nullptr; + float hitPos[3]; + bool hitPosSet = 0; + bool bidir = true; + unsigned char oldFlags = 0; public: OffMeshConnectionTool() = default; ~OffMeshConnectionTool() { - if (m_sample) + if (sample) { - m_sample->setNavMeshDrawFlags(m_oldFlags); + sample->setNavMeshDrawFlags(oldFlags); } } - virtual SampleToolType type() { return SampleToolType::OFFMESH_CONNECTION; } - virtual void init(Sample* sample); - virtual void reset(); - virtual void handleMenu(); - virtual void handleClick(const float* s, const float* p, bool shift); - virtual void handleToggle(); - virtual void handleStep(); - virtual void handleUpdate(const float dt); - virtual void handleRender(); - virtual void handleRenderOverlay(double* proj, double* model, int* view); + SampleToolType type() override { return SampleToolType::OFFMESH_CONNECTION; } + void init(Sample* sample) override; + void reset() override; + void handleMenu() override; + void handleClick(const float* rayStartTime, const float* rayHitPos, bool shift) override; + void handleToggle() override; + void handleStep() override; + void handleUpdate(const float dt) override; + void handleRender() override; + void handleRenderOverlay(double* proj, double* model, int* view) override; }; diff --git a/RecastDemo/Source/OffMeshConnectionTool.cpp b/RecastDemo/Source/OffMeshConnectionTool.cpp index af3cb10b..60681f32 100644 --- a/RecastDemo/Source/OffMeshConnectionTool.cpp +++ b/RecastDemo/Source/OffMeshConnectionTool.cpp @@ -16,13 +16,11 @@ // 3. This notice may not be removed or altered from any source distribution. // -#include "SDL.h" #include "SDL_opengl.h" #include -#include -#include -#include +#include + #ifdef __APPLE__ # include #else @@ -32,7 +30,6 @@ #include "InputGeom.h" #include "OffMeshConnectionTool.h" #include "Recast.h" -#include "RecastDebugDraw.h" #include "Sample.h" #include "imgui.h" @@ -40,38 +37,45 @@ # define snprintf _snprintf #endif -void OffMeshConnectionTool::init(Sample* sample) +void OffMeshConnectionTool::init(Sample* newSample) { - if (m_sample != sample) + if (sample != newSample) { - m_sample = sample; - m_oldFlags = m_sample->getNavMeshDrawFlags(); - m_sample->setNavMeshDrawFlags(m_oldFlags & ~DU_DRAWNAVMESH_OFFMESHCONS); + sample = newSample; + oldFlags = sample->getNavMeshDrawFlags(); + sample->setNavMeshDrawFlags(oldFlags & ~DU_DRAWNAVMESH_OFFMESHCONS); } } void OffMeshConnectionTool::reset() { - m_hitPosSet = false; + hitPosSet = false; } void OffMeshConnectionTool::handleMenu() { - if (imguiCheck("One Way", !m_bidir)) + if (imguiCheck("One Way", !bidir)) { - m_bidir = false; + bidir = false; } - if (imguiCheck("Bidirectional", m_bidir)) + if (imguiCheck("Bidirectional", bidir)) { - m_bidir = true; + bidir = true; } } -void OffMeshConnectionTool::handleClick(const float* /*s*/, const float* p, bool shift) +void OffMeshConnectionTool::handleClick(const float* /*rayStartPos*/, const float* rayHitPos, bool shift) { - if (!m_sample) { return; } - InputGeom* geom = m_sample->getInputGeom(); - if (!geom) { return; } + if (!sample) + { + return; + } + + InputGeom* geom = sample->getInputGeom(); + if (!geom) + { + return; + } if (shift) { @@ -82,16 +86,16 @@ void OffMeshConnectionTool::handleClick(const float* /*s*/, const float* p, bool const float* verts = geom->getOffMeshConnectionVerts(); for (int i = 0; i < geom->getOffMeshConnectionCount() * 2; ++i) { - const float* v = &verts[i * 3]; - float d = rcVdistSqr(p, v); - if (d < nearestDist) + float dist = rcVdistSqr(rayHitPos, &verts[i * 3]); + if (dist < nearestDist) { - nearestDist = d; + nearestDist = dist; nearestIndex = i / 2; // Each link has two vertices. } } + // If end point close enough, delete it. - if (nearestIndex != -1 && sqrtf(nearestDist) < m_sample->getAgentRadius()) + if (nearestIndex != -1 && sqrtf(nearestDist) < sample->getAgentRadius()) { geom->deleteOffMeshConnection(nearestIndex); } @@ -99,17 +103,21 @@ void OffMeshConnectionTool::handleClick(const float* /*s*/, const float* p, bool else { // Create - if (!m_hitPosSet) + if (!hitPosSet) { - rcVcopy(m_hitPos, p); - m_hitPosSet = true; + rcVcopy(hitPos, rayHitPos); + hitPosSet = true; } else { - const unsigned char area = SAMPLE_POLYAREA_JUMP; - const unsigned short flags = SAMPLE_POLYFLAGS_JUMP; - geom->addOffMeshConnection(m_hitPos, p, m_sample->getAgentRadius(), m_bidir ? 1 : 0, area, flags); - m_hitPosSet = false; + geom->addOffMeshConnection( + hitPos, + rayHitPos, + sample->getAgentRadius(), + bidir ? 1 : 0, + SAMPLE_POLYAREA_JUMP, + SAMPLE_POLYFLAGS_JUMP); + hitPosSet = false; } } } @@ -122,16 +130,14 @@ void OffMeshConnectionTool::handleUpdate(const float /*dt*/) {} void OffMeshConnectionTool::handleRender() { - duDebugDraw& dd = m_sample->getDebugDraw(); - const float s = m_sample->getAgentRadius(); + duDebugDraw& dd = sample->getDebugDraw(); - if (m_hitPosSet) + if (hitPosSet) { - duDebugDrawCross(&dd, m_hitPos[0], m_hitPos[1] + 0.1f, m_hitPos[2], s, duRGBA(0, 0, 0, 128), 2.0f); + duDebugDrawCross(&dd, hitPos[0], hitPos[1] + 0.1f, hitPos[2], sample->getAgentRadius(), duRGBA(0, 0, 0, 128), 2.0f); } - InputGeom* geom = m_sample->getInputGeom(); - if (geom) + if (InputGeom* geom = sample->getInputGeom()) { geom->drawOffMeshConnections(&dd, true); } @@ -142,16 +148,17 @@ void OffMeshConnectionTool::handleRenderOverlay(double* proj, double* model, int GLdouble x, y, z; // Draw start and end point labels - if (m_hitPosSet && gluProject((GLdouble)m_hitPos[0], (GLdouble)m_hitPos[1], (GLdouble)m_hitPos[2], model, proj, view, &x, &y, &z)) + if (hitPosSet && gluProject((GLdouble)hitPos[0], (GLdouble)hitPos[1], (GLdouble)hitPos[2], model, proj, view, &x, &y, &z)) { imguiDrawText((int)x, (int)(y - 25), IMGUI_ALIGN_CENTER, "Start", imguiRGBA(0, 0, 0, 220)); } // Tool help const int h = view[3]; - if (!m_hitPosSet) + if (!hitPosSet) { - imguiDrawText(280, + imguiDrawText( + 280, h - 40, IMGUI_ALIGN_LEFT, "LMB: Create new connection. SHIFT+LMB: Delete existing connection, click close to start or end point.", @@ -159,6 +166,11 @@ void OffMeshConnectionTool::handleRenderOverlay(double* proj, double* model, int } else { - imguiDrawText(280, h - 40, IMGUI_ALIGN_LEFT, "LMB: Set connection end point and finish.", imguiRGBA(255, 255, 255, 192)); + imguiDrawText( + 280, + h - 40, + IMGUI_ALIGN_LEFT, + "LMB: Set connection end point and finish.", + imguiRGBA(255, 255, 255, 192)); } }