From 2da9efc1037fba0461303327151318b8edf4dfc7 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 29 Oct 2015 16:05:01 +0100 Subject: [PATCH] render entity bounds in debug mode --- src/client/client.cpp | 3 +++ src/graphics/PrimitiveMesh.hpp | 7 +++++++ src/graphics/mesh.cpp | 24 ++++++++++++++++++++++++ src/standalone/MasterState.cpp | 3 +++ src/world/World.hpp | 1 + src/world/world.cpp | 22 +++++++++++++++++++++- 6 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/client/client.cpp b/src/client/client.cpp index 8bd0ae3..5a34976 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -193,6 +193,9 @@ void InteractiveState::Render(Viewport &viewport) { if (master.GetConfig().video.world) { chunk_renderer.Render(viewport); world.Render(viewport); + if (master.GetConfig().video.debug) { + world.RenderDebug(viewport); + } sky.Render(viewport); } hud.Render(viewport); diff --git a/src/graphics/PrimitiveMesh.hpp b/src/graphics/PrimitiveMesh.hpp index 1aa3cce..013bf37 100644 --- a/src/graphics/PrimitiveMesh.hpp +++ b/src/graphics/PrimitiveMesh.hpp @@ -10,6 +10,8 @@ namespace blank { +struct AABB; + class PrimitiveMesh { public: @@ -52,6 +54,11 @@ public: const glm::vec2 &pivot = glm::vec2(0.0f) ); + void OutlineBox( + const AABB &, + const glm::vec4 &color = glm::vec4(0.0f) + ); + }; using VAO = VertexArray; diff --git a/src/graphics/mesh.cpp b/src/graphics/mesh.cpp index 3aa631e..4a63d99 100644 --- a/src/graphics/mesh.cpp +++ b/src/graphics/mesh.cpp @@ -4,6 +4,8 @@ #include "SkyBoxMesh.hpp" #include "SpriteMesh.hpp" +#include "../model/geometry.hpp" + #include #include @@ -90,6 +92,28 @@ void PrimitiveMesh::Buffer::FillRect( indices.assign({ 0, 2, 1, 1, 2, 3 }); } +void PrimitiveMesh::Buffer::OutlineBox(const AABB &box, const glm::vec4 &color) { + Clear(); + Reserve(8, 24); + + vertices.emplace_back(box.min.x, box.min.y, box.min.z); + vertices.emplace_back(box.min.x, box.min.y, box.max.z); + vertices.emplace_back(box.min.x, box.max.y, box.min.z); + vertices.emplace_back(box.min.x, box.max.y, box.max.z); + vertices.emplace_back(box.max.x, box.min.y, box.min.z); + vertices.emplace_back(box.max.x, box.min.y, box.max.z); + vertices.emplace_back(box.max.x, box.max.y, box.min.z); + vertices.emplace_back(box.max.x, box.max.y, box.max.z); + + colors.resize(8, color); + + indices.assign({ + 0, 1, 1, 3, 3, 2, 2, 0, // left + 4, 5, 5, 7, 7, 6, 6, 4, // right + 0, 4, 1, 5, 3, 7, 2, 6, // others + }); +} + void PrimitiveMesh::Update(const Buffer &buf) noexcept { #ifndef NDEBUG diff --git a/src/standalone/MasterState.cpp b/src/standalone/MasterState.cpp index 71117e2..e802703 100644 --- a/src/standalone/MasterState.cpp +++ b/src/standalone/MasterState.cpp @@ -162,6 +162,9 @@ void MasterState::Render(Viewport &viewport) { if (config.video.world) { chunk_renderer.Render(viewport); world.Render(viewport); + if (config.video.debug) { + world.RenderDebug(viewport); + } sky.Render(viewport); } hud.Render(viewport); diff --git a/src/world/World.hpp b/src/world/World.hpp index e29474e..7ee5f43 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -101,6 +101,7 @@ public: void Update(Entity &, float dt); void Render(Viewport &); + void RenderDebug(Viewport &); private: using EntityHandle = std::list::iterator; diff --git a/src/world/world.cpp b/src/world/world.cpp index 3f5740e..f472da9 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -666,7 +666,27 @@ void World::Render(Viewport &viewport) { entity_prog.SetFogDensity(fog_density); for (Entity &entity : entities) { - entity.Render(entity.Transform(players.front().GetEntity().ChunkCoords()), entity_prog); + glm::mat4 M(entity.Transform(players.front().GetEntity().ChunkCoords())); + if (!CullTest(entity.Bounds(), entity_prog.GetVP() * M)) { + entity.Render(M, entity_prog); + } + } +} + +namespace { + +PrimitiveMesh::Buffer debug_buf; + +} + +void World::RenderDebug(Viewport &viewport) { + PrimitiveMesh debug_mesh; + PlainColor &prog = viewport.WorldColorProgram(); + for (const Entity &entity : entities) { + debug_buf.OutlineBox(entity.Bounds(), glm::vec4(1.0f, 0.0f, 0.0f, 1.0f)); + debug_mesh.Update(debug_buf); + prog.SetM(entity.Transform(players.front().GetEntity().ChunkCoords())); + debug_mesh.DrawLines(); } } -- 2.39.2