From 15c9cbd2280f223a093b9d2b092a306233d19a1e Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 17 Mar 2016 16:31:16 +1300 Subject: [PATCH] Debug mode for displaying crash spheres, closes #400 --- src/graphics/engine/engine.cpp | 70 ++++++++++++++++++++++++++++++++++ src/graphics/engine/engine.h | 7 ++++ src/level/robotmain.cpp | 32 ++++++++++++++++ src/level/robotmain.h | 2 + 4 files changed, 111 insertions(+) diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp index 04f86c79..309a0f9d 100644 --- a/src/graphics/engine/engine.cpp +++ b/src/graphics/engine/engine.cpp @@ -3065,6 +3065,18 @@ void CEngine::ApplyChange() SetFocus(m_focus); } +void CEngine::ClearDisplayCrashSpheres() +{ + m_displayCrashSpheres.clear(); +} + +void CEngine::AddDisplayCrashSpheres(const std::vector& crashSpheres) +{ + for (const auto& crashSphere : crashSpheres) + { + m_displayCrashSpheres.push_back(crashSphere); + } +} /******************************************************* @@ -3359,6 +3371,8 @@ void CEngine::Draw3DScene() m_device->SetRenderState(RENDER_STATE_LIGHTING, false); + DrawCrashSpheres(); + m_app->StartPerformanceCounter(PCNT_RENDER_PARTICLE); m_particle->DrawParticle(SH_WORLD); // draws the particles of the 3D world m_app->StopPerformanceCounter(PCNT_RENDER_PARTICLE); @@ -3372,6 +3386,60 @@ void CEngine::Draw3DScene() if (! m_overFront) DrawOverColor(); // draws the foreground color } +void CEngine::DrawCrashSpheres() +{ + Math::Matrix worldMatrix; + worldMatrix.LoadIdentity(); + m_device->SetTransform(TRANSFORM_WORLD, worldMatrix); + + SetState(ENG_RSTATE_OPAQUE_COLOR); + + const int LINE_SEGMENTS = 32; + const int LONGITUDE_DIVISIONS = 16; + const int LATITUDE_DIVISIONS = 8; + + VertexCol line[LINE_SEGMENTS]; + Color color(0.0f, 0.0f, 1.0f); + + auto SpherePoint = [&](float sphereRadius, float latitude, float longitude) + { + float latitudeAngle = (latitude - 0.5f) * 2.0f * Math::PI; + float longitudeAngle = longitude * 2.0f * Math::PI; + return Math::Vector(sphereRadius * sinf(latitudeAngle) * cosf(longitudeAngle), + sphereRadius * cosf(latitudeAngle), + sphereRadius * sinf(latitudeAngle) * sinf(longitudeAngle)); + }; + + for (const auto& crashSphere : m_displayCrashSpheres) + { + for (int longitudeDivision = 0; longitudeDivision <= LONGITUDE_DIVISIONS; ++longitudeDivision) + { + for (int segment = 0; segment < LINE_SEGMENTS; ++segment) + { + Math::Vector pos = crashSphere.pos; + float latitude = static_cast(segment) / LINE_SEGMENTS; + float longitude = static_cast(longitudeDivision) / (LONGITUDE_DIVISIONS); + pos += SpherePoint(crashSphere.radius, latitude, longitude); + line[segment] = VertexCol(pos, color); + } + m_device->DrawPrimitive(PRIMITIVE_LINE_STRIP, line, LINE_SEGMENTS); + } + + for (int latitudeDivision = 0; latitudeDivision <= LATITUDE_DIVISIONS; ++latitudeDivision) + { + for (int segment = 0; segment < LINE_SEGMENTS; ++segment) + { + Math::Vector pos = crashSphere.pos; + float latitude = static_cast(latitudeDivision + 1) / (LATITUDE_DIVISIONS + 2); + float longitude = static_cast(segment) / LINE_SEGMENTS; + pos += SpherePoint(crashSphere.radius, latitude, longitude); + line[segment] = VertexCol(pos, color); + } + m_device->DrawPrimitive(PRIMITIVE_LINE_STRIP, line, LINE_SEGMENTS); + } + } +} + void CEngine::RenderShadowMap() { m_shadowMapping = m_shadowMapping && m_device->IsShadowMappingSupported(); @@ -3798,8 +3866,10 @@ void CEngine::DrawInterface() // At the end to not overlap if (m_renderInterface) DrawHighlight(); + DrawTimer(); DrawStats(); + if (m_renderInterface) DrawMouse(); diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h index 73ea73ee..9e679cdb 100644 --- a/src/graphics/engine/engine.h +++ b/src/graphics/engine/engine.h @@ -34,6 +34,7 @@ #include "math/intpoint.h" #include "math/matrix.h" #include "math/point.h" +#include "math/sphere.h" #include "math/vector.h" @@ -1180,6 +1181,9 @@ public: //! Updates the scene after a change of parameters void ApplyChange(); + void ClearDisplayCrashSpheres(); + void AddDisplayCrashSpheres(const std::vector& crashSpheres); + protected: //! Prepares the interface for 3D scene void Draw3DScene(); @@ -1221,6 +1225,7 @@ protected: void DrawStats(); //! Draw mission timer void DrawTimer(); + void DrawCrashSpheres(); //! Creates a new tier 2 object (texture) EngineBaseObjTexTier& AddLevel2(EngineBaseObject& p1, const std::string& tex1Name, const std::string& tex2Name); @@ -1468,6 +1473,8 @@ protected: std::unordered_map m_staticMeshBaseObjects; + std::vector m_displayCrashSpheres; + //! Pause the animation updates bool m_pause = false; }; diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 7c9f50a8..cbcd5f89 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -250,6 +250,8 @@ CRobotMain::CRobotMain() m_showLimit[i].link = nullptr; } + m_debugCrashSpheres = false; + m_engine->SetTerrain(m_terrain.get()); m_app->SetMouseMode(MOUSE_ENGINE); @@ -1394,6 +1396,16 @@ void CRobotMain::ExecuteCmd(char *cmd) } return; } + if (strcmp(cmd, "debugcrashon") == 0) + { + m_debugCrashSpheres = true; + return; + } + if (strcmp(cmd, "debugcrashoff") == 0) + { + m_debugCrashSpheres = false; + return; + } } if (strcmp(cmd, "debugmode") == 0) @@ -2457,6 +2469,8 @@ bool CRobotMain::EventFrame(const Event &event) m_lightning->EventProcess(event); m_planet->EventProcess(event); + UpdateDebugCrashSpheres(); + Ui::CMap* pm = nullptr; Ui::CWindow* pw = static_cast(m_interface->SearchControl(EVENT_WINDOW1)); if (pw == nullptr) @@ -6003,3 +6017,21 @@ void CRobotMain::SetCodeBattleSpectatorMode(bool mode) if (mode) m_camera->SetFixDirectionV(-0.25f*Math::PI); } + +void CRobotMain::UpdateDebugCrashSpheres() +{ + m_engine->ClearDisplayCrashSpheres(); + if (m_debugCrashSpheres) + { + for (CObject* obj : m_objMan->GetAllObjects()) + { + auto crashSpheres = obj->GetAllCrashSpheres(); + std::vector displaySpheres; + for (const auto& crashSphere : crashSpheres) + { + displaySpheres.push_back(crashSphere.sphere); + } + m_engine->AddDisplayCrashSpheres(displaySpheres); + } + } +} diff --git a/src/level/robotmain.h b/src/level/robotmain.h index ee870287..991c97ce 100644 --- a/src/level/robotmain.h +++ b/src/level/robotmain.h @@ -407,6 +407,7 @@ protected: void CreateCodeBattleInterface(); void DestroyCodeBattleInterface(); void SetCodeBattleSpectatorMode(bool mode); + void UpdateDebugCrashSpheres(); protected: @@ -583,4 +584,5 @@ protected: int m_shotSaving = 0; std::deque m_selectionHistory; + bool m_debugCrashSpheres; };