debug window: populate with data

hud
EmaMaker 2023-09-18 20:54:57 +02:00
parent f526e9b152
commit 9d2d0c8772
4 changed files with 129 additions and 3 deletions

View File

@ -12,6 +12,7 @@
#include "chunk.hpp" #include "chunk.hpp"
#include "chunkgenerator.hpp" #include "chunkgenerator.hpp"
#include "chunkmesher.hpp" #include "chunkmesher.hpp"
#include "debugwindow.hpp"
#include "globals.hpp" #include "globals.hpp"
#include "renderer.hpp" #include "renderer.hpp"
@ -117,6 +118,16 @@ namespace chunkmanager
int chunkY=static_cast<int>(theCamera.getAtomicPosY() / CHUNK_SIZE); int chunkY=static_cast<int>(theCamera.getAtomicPosY() / CHUNK_SIZE);
int chunkZ=static_cast<int>(theCamera.getAtomicPosZ() / CHUNK_SIZE); int chunkZ=static_cast<int>(theCamera.getAtomicPosZ() / CHUNK_SIZE);
debug::window::set_parameter("px", theCamera.getAtomicPosX());
debug::window::set_parameter("py", theCamera.getAtomicPosY());
debug::window::set_parameter("pz", theCamera.getAtomicPosZ());
debug::window::set_parameter("cx", chunkX);
debug::window::set_parameter("cy", chunkY);
debug::window::set_parameter("cz", chunkZ);
debug::window::set_parameter("lx", theCamera.getFront().x);
debug::window::set_parameter("ly", theCamera.getFront().y);
debug::window::set_parameter("lz", theCamera.getFront().z);
// Update other chunks // Update other chunks
for(int i = 0; i < chunks_volume_real; i++) { for(int i = 0; i < chunks_volume_real; i++) {
const uint16_t x = chunks_indices[i][0] + chunkX; const uint16_t x = chunks_indices[i][0] + chunkX;
@ -152,6 +163,9 @@ namespace chunkmanager
a.release(); a.release();
} }
debug::window::set_parameter("update_chunks_total", (int) (chunks.size()));
debug::window::set_parameter("update_chunks_bucket", (int) (chunks.max_size()));
Chunk::Chunk* n; Chunk::Chunk* n;
nUnloaded = 0; nUnloaded = 0;
while(chunks_todelete.try_pop(n)){ while(chunks_todelete.try_pop(n)){
@ -245,6 +259,12 @@ namespace chunkmanager
// mark the mesh of the chunk the be updated // mark the mesh of the chunk the be updated
chunks_to_mesh_queue.push(std::make_pair(c1, MESHING_PRIORITY_PLAYER_EDIT)); chunks_to_mesh_queue.push(std::make_pair(c1, MESHING_PRIORITY_PLAYER_EDIT));
chunks_to_mesh_queue.push(std::make_pair(c, MESHING_PRIORITY_PLAYER_EDIT)); chunks_to_mesh_queue.push(std::make_pair(c, MESHING_PRIORITY_PLAYER_EDIT));
debug::window::set_parameter("block_last_action", place);
debug::window::set_parameter("block_last_action_block_type", (int)(Block::STONE));
debug::window::set_parameter("block_last_action_x", px1*CHUNK_SIZE + bx1);
debug::window::set_parameter("block_last_action_y", px1*CHUNK_SIZE + by1);
debug::window::set_parameter("block_last_action_z", px1*CHUNK_SIZE + bz1);
}else{ }else{
// replace the current block with air to remove it // replace the current block with air to remove it
c->setBlock( Block::AIR, bx, by, bz); c->setBlock( Block::AIR, bx, by, bz);
@ -265,6 +285,13 @@ namespace chunkmanager
chunkmesher::mesh(b2->second); chunkmesher::mesh(b2->second);
if(bz == CHUNK_SIZE - 1 && pz +1 < 1024 && chunks.find(c2, calculateIndex(px, py, pz +1))) if(bz == CHUNK_SIZE - 1 && pz +1 < 1024 && chunks.find(c2, calculateIndex(px, py, pz +1)))
chunkmesher::mesh(c2->second); chunkmesher::mesh(c2->second);
debug::window::set_parameter("block_last_action", place);
debug::window::set_parameter("block_last_action_block_type", (int) (Block::AIR));
debug::window::set_parameter("block_last_action_x", px*CHUNK_SIZE + bx);
debug::window::set_parameter("block_last_action_y", py*CHUNK_SIZE + by);
debug::window::set_parameter("block_last_action_z", pz*CHUNK_SIZE + bz);
} }
break; break;
} }

View File

@ -3,10 +3,20 @@
#include <imgui/imgui.h> #include <imgui/imgui.h>
#include <imgui/imgui_impl_opengl3.h> #include <imgui/imgui_impl_opengl3.h>
#include <imgui/imgui_impl_glfw.h> #include <imgui/imgui_impl_glfw.h>
#include <imgui_stdlib.h>
#include <iostream>
#include <string>
#include <unordered_map>
namespace debug{ namespace debug{
namespace window{ namespace window{
void show_debug_window();
constexpr int frametimes_array_size = 20;
float frametimes_array[frametimes_array_size]{};
std::unordered_map<std::string, std::any> parameters;
void init(GLFWwindow* window){ void init(GLFWwindow* window){
// Setup Dear ImGui context // Setup Dear ImGui context
@ -25,7 +35,8 @@ namespace debug{
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
ImGui::ShowDemoWindow(); // Show demo window! :) //ImGui::ShowDemoWindow(); // Show demo window! :)
show_debug_window();
} }
void render(){ void render(){
@ -43,5 +54,77 @@ namespace debug{
ImGui_ImplGlfw_Shutdown(); ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext(); ImGui::DestroyContext();
} }
void set_parameter(std::string key, std::any value){
parameters[key] = value;
}
int ch_type{0};
int block_type{0};
void show_debug_window(){
ImGui::Begin("Debug Window");
ImGui::PushItemWidth(ImGui::GetFontSize() * -12);
try{
if (ImGui::CollapsingHeader("Frametimes")){
ImGui::Text("FPS: %d", std::any_cast<int>(parameters.at("fps")));
ImGui::Text("Frametime (ms): %f",
std::any_cast<float>(parameters.at("frametime"))*1000);
//ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr);
}
if(ImGui::CollapsingHeader("Player")){
ImGui::Text("X: %f, Y: %f, Z: %f",
std::any_cast<float>(parameters.at("px")),std::any_cast<float>(parameters.at("py")),std::any_cast<float>(parameters.at("pz")) );
ImGui::Text("X: %d, Y: %d, Z: %d (chunk)", std::any_cast<int>(parameters.at("cx")),std::any_cast<int>(parameters.at("cy")),std::any_cast<int>(parameters.at("cz")) );
ImGui::Text("Pointing in direction: %f, %f, %f",
std::any_cast<float>(parameters.at("lx")),std::any_cast<float>(parameters.at("ly")),std::any_cast<float>(parameters.at("lz")) );
if(parameters.find("block_last_action") != parameters.end()){
ImGui::Text("Last Block action: %s",
std::any_cast<bool>(parameters.at("block_last_action")) ? "place" : "destroy");
ImGui::Text("Last Block action block type: %d",
std::any_cast<int>(parameters.at("block_last_action_block_type")));
ImGui::Text("Last Block action position: X: %d, Y: %d, Z: %d",
std::any_cast<int>(parameters.at("block_last_action_x")),std::any_cast<int>(parameters.at("block_last_action_y")),std::any_cast<int>(parameters.at("block_last_action_z")) );
}
ImGui::SliderInt("Crosshair type",
&ch_type, 0, 1);
ImGui::SliderInt("Block to place",
&block_type, 2, 6);
}
if(ImGui::CollapsingHeader("Mesh")){
ImGui::Text("Total chunks updated: %d",
std::any_cast<int>(parameters.at("render_chunks_total")));
ImGui::Text("Chunks rendered: %d",
std::any_cast<int>(parameters.at("render_chunks_rendered")));
ImGui::Text("Frustum culled: %d",
std::any_cast<int>(parameters.at("render_chunks_culled")));
ImGui::Text("Chunks out of view: %d",
std::any_cast<int>(parameters.at("render_chunks_oof")));
if(parameters.find("render_chunks_deleted") != parameters.end())
ImGui::Text("Chunks deleted: %d",
std::any_cast<int>(parameters.at("render_chunks_deleted")));
ImGui::Text("Vertices in the scene: %d",
std::any_cast<int>(parameters.at("render_chunks_vertices")));
}
if(ImGui::CollapsingHeader("Chunks")){
ImGui::Text("Total chunks present: %d",
std::any_cast<int>(parameters.at("update_chunks_total")));
/*ImGui::Text("Chunks freed from memory: %d",
std::any_cast<int>(parameters.at("update_chunks_delete")));*/
ImGui::Text("Bucket size: %d",
std::any_cast<int>(parameters.at("update_chunks_bucket")));
}
}catch(const std::bad_any_cast& e){
std::cout << e.what();
}
ImGui::End();
}
} }
} }

View File

@ -82,10 +82,12 @@ int main()
deltaTime = currentFrame - lastFrame; deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame; lastFrame = currentFrame;
debug::window::set_parameter("frametime", deltaTime);
// FPS Counter // FPS Counter
frames++; frames++;
if(currentFrame - lastFPSFrame >= 1.0f){ if(currentFrame - lastFPSFrame >= 1.0f){
std::cout << "FPS: " << frames << " Frametime: " << deltaTime << std::endl; //std::cout << "FPS: " << frames << " Frametime: " << deltaTime << std::endl;
debug::window::set_parameter("fps", frames);
frames = 0; frames = 0;
lastFPSFrame = currentFrame; lastFPSFrame = currentFrame;
} }

View File

@ -123,7 +123,7 @@ namespace renderer{
debug::window::prerender(); debug::window::prerender();
/* RENDER THE WORLD TO TEXTURE */ /* RENDER THE WORLD TO TEXTURE */
int total{0}, toGpu{0}; int total{0}, toGpu{0}, oof{0}, vertices{0};
glm::vec4 frustumPlanes[6]; glm::vec4 frustumPlanes[6];
theCamera.getFrustumPlanes(frustumPlanes, true); theCamera.getFrustumPlanes(frustumPlanes, true);
glm::vec3 cameraPos = theCamera.getPos(); glm::vec3 cameraPos = theCamera.getPos();
@ -143,6 +143,9 @@ namespace renderer{
if(dist <= static_cast<float>(RENDER_DISTANCE)){ if(dist <= static_cast<float>(RENDER_DISTANCE)){
if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue; if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue;
// Increase total vertex count
vertices += c->numVertices;
// reset out-of-vision and unload flags // reset out-of-vision and unload flags
c->setState(Chunk::CHUNK_STATE_OUTOFVISION, false); c->setState(Chunk::CHUNK_STATE_OUTOFVISION, false);
c->setState(Chunk::CHUNK_STATE_UNLOADED, false); c->setState(Chunk::CHUNK_STATE_UNLOADED, false);
@ -179,12 +182,15 @@ namespace renderer{
glBindVertexArray(c->VAO); glBindVertexArray(c->VAO);
glDrawArrays(GL_POINTS, 0, c->numVertices); glDrawArrays(GL_POINTS, 0, c->numVertices);
glBindVertexArray(0); glBindVertexArray(0);
toGpu++;
} }
} }
}else{ }else{
// When the chunk is outside render distance // When the chunk is outside render distance
if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){ if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){
oof++;
if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){ if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){
// If chunk was already out and enough time has passed // If chunk was already out and enough time has passed
// Mark the chunk to be unloaded // Mark the chunk to be unloaded
@ -201,6 +207,14 @@ namespace renderer{
} }
} }
total = chunks_torender.size();
debug::window::set_parameter("render_chunks_total", total);
debug::window::set_parameter("render_chunks_rendered", toGpu);
debug::window::set_parameter("render_chunks_culled", total-toGpu);
debug::window::set_parameter("render_chunks_oof", oof);
debug::window::set_parameter("render_chunks_deleted", (int) (render_todelete.size()));
debug::window::set_parameter("render_chunks_vertices", vertices);
for(auto& c : render_todelete){ for(auto& c : render_todelete){
// we can get away with unsafe erase as access to the container is only done by this // we can get away with unsafe erase as access to the container is only done by this
// thread // thread