diff --git a/proj-windows/CMakeLists.txt b/proj-windows/CMakeLists.txt index c2c602f..c5c2b8f 100644 --- a/proj-windows/CMakeLists.txt +++ b/proj-windows/CMakeLists.txt @@ -111,6 +111,7 @@ target_compile_definitions(space-game001 PRIVATE WIN32_LEAN_AND_MEAN PNG_ENABLED SDL_MAIN_HANDLED + SHOW_PATH # NETWORK # SIMPLIFIED ) diff --git a/resources/shaders/defaultColor_desktop.fragment b/resources/shaders/defaultColor_desktop.fragment index 9dfd488..eb6e0fb 100644 --- a/resources/shaders/defaultColor_desktop.fragment +++ b/resources/shaders/defaultColor_desktop.fragment @@ -1,9 +1,7 @@ //precisionmediump float; varying vec3 color; -void main() +void main() { - //gl_FragColor = vec4(color, 1.0); - gl_FragColor = vec4(1.0, 1.0, 0.5, 1.0); - + gl_FragColor = vec4(color, 1.0); } \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 45717d7..2bb5f9d 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -300,6 +300,10 @@ namespace ZL navigation.build(obstacles, "resources/config2/navigation.json", CONST_ZIP_FILE); +#ifdef SHOW_PATH + buildDebugNavMeshes(); +#endif + auto planner = [this](const Eigen::Vector3f& start, const Eigen::Vector3f& end) { return navigation.findPath(start, end); }; @@ -315,6 +319,35 @@ namespace ZL } } +#ifdef SHOW_PATH + void Game::buildDebugNavMeshes() + { + debugNavMeshes.clear(); + const auto& areas = navigation.getAreas(); + float y = navigation.getFloorY() + 0.02f; + Eigen::Vector3f red(1.0f, 0.0f, 0.0f); + + for (const auto& area : areas) { + if (area.polygon.size() < 3) continue; + VertexRenderStruct mesh; + mesh.data = CreatePolygonFloor(area.polygon, y, red); + mesh.RefreshVBO(); + debugNavMeshes.push_back(std::move(mesh)); + } + } + + void Game::drawDebugNavigation() + { + renderer.shaderManager.PushShader("defaultColor"); + renderer.SetMatrix(); + for (const auto& mesh : debugNavMeshes) { + renderer.DrawVertexRenderStruct(mesh); + } + renderer.shaderManager.PopShader(); + renderer.SetMatrix(); + } +#endif + InteractiveObject* Game::raycastInteractiveObjects(const Eigen::Vector3f& rayOrigin, const Eigen::Vector3f& rayDir) { if (interactiveObjects.empty()) { std::cout << "[RAYCAST] No interactive objects to check" << std::endl; @@ -465,6 +498,9 @@ namespace ZL if (player) player->draw(renderer); for (auto& npc : npcs) npc->draw(renderer); +#ifdef SHOW_PATH + drawDebugNavigation(); +#endif renderer.PopMatrix(); diff --git a/src/Game.h b/src/Game.h index 0f588f2..5d2eb9f 100644 --- a/src/Game.h +++ b/src/Game.h @@ -88,6 +88,10 @@ namespace ZL { ScriptEngine scriptEngine; PathFinder navigation; +#ifdef SHOW_PATH + std::vector debugNavMeshes; +#endif + private: bool rightMouseDown = false; int lastMouseX = 0; @@ -108,6 +112,11 @@ namespace ZL { InteractiveObject* raycastInteractiveObjects(const Eigen::Vector3f& rayOrigin, const Eigen::Vector3f& rayDir); Character* raycastNpcs(const Eigen::Vector3f& rayOrigin, const Eigen::Vector3f& rayDir, float maxDistance = 100.0f); +#ifdef SHOW_PATH + void buildDebugNavMeshes(); + void drawDebugNavigation(); +#endif + #ifdef EMSCRIPTEN static Game* s_instance; static void onResourcesZipLoaded(const char* filename); diff --git a/src/navigation/PathFinder.h b/src/navigation/PathFinder.h index 48d2cae..1ac9b9a 100644 --- a/src/navigation/PathFinder.h +++ b/src/navigation/PathFinder.h @@ -19,6 +19,12 @@ public: int z = 0; }; + struct NavigationArea { + std::string name; + bool available = true; + std::vector polygon; + }; + void build(const std::vector& obstacleMeshes, const std::string& configPath, const std::string& zipPath = ""); @@ -29,13 +35,10 @@ public: bool setAreaAvailable(const std::string& areaName, bool available); bool isReady() const { return ready; } bool isWalkable(const Eigen::Vector3f& point) const; + const std::vector& getAreas() const { return areas; } + float getFloorY() const { return floorY; } private: - struct NavigationArea { - std::string name; - bool available = true; - std::vector polygon; - }; float cellSize = 0.4f; float agentRadius = 0.45f; diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index f50aec0..f7a80cc 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -179,6 +179,24 @@ namespace ZL { return result; } + VertexDataStruct CreatePolygonFloor(const std::vector& polygon, float yLevel, const Vector3f& color) + { + VertexDataStruct result; + if (polygon.size() < 3) return result; + + for (size_t i = 1; i + 1 < polygon.size(); ++i) { + result.PositionData.push_back({ polygon[0].x(), yLevel, polygon[0].y() }); + result.PositionData.push_back({ polygon[i].x(), yLevel, polygon[i].y() }); + result.PositionData.push_back({ polygon[i + 1].x(), yLevel, polygon[i + 1].y() }); + + result.ColorData.push_back(color); + result.ColorData.push_back(color); + result.ColorData.push_back(color); + } + + return result; + } + VertexDataStruct CreateRectHorizontalSections2D(Vector2f center, Vector2f halfWidthHeight, float zLevel, size_t sectionCount) { Vector2f posFrom = center - halfWidthHeight; diff --git a/src/render/Renderer.h b/src/render/Renderer.h index e203b34..52ac4f8 100644 --- a/src/render/Renderer.h +++ b/src/render/Renderer.h @@ -87,6 +87,7 @@ namespace ZL { }; VertexDataStruct CreateRect2D(Vector2f center, Vector2f halfWidthHeight, float zLevel); + VertexDataStruct CreatePolygonFloor(const std::vector& polygon, float yLevel, const Vector3f& color); VertexDataStruct CreateRectHorizontalSections2D(Vector2f center, Vector2f halfWidthHeight, float zLevel, size_t sectionCount); VertexDataStruct CreateCube3D(float scale); VertexDataStruct CreateCubemap(float scale = 1000.f);