diff --git a/.gitignore b/.gitignore index b3dedbe..864e63f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ gmon.out* cscope* test.cpp a.out -*screenshot* \ No newline at end of file +*screenshot* +imgui.ini diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cda35d8 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/imgui"] + path = lib/imgui + url = https://github.com/ocornut/imgui/ diff --git a/CMakeLists.txt b/CMakeLists.txt index b4183c6..de73763 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.2) project(cmake-project-template) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3") +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -O3") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g") set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}) diff --git a/include/debugwindow.hpp b/include/debugwindow.hpp new file mode 100644 index 0000000..5707917 --- /dev/null +++ b/include/debugwindow.hpp @@ -0,0 +1,19 @@ +#ifndef DEBUG_WINDOW_H +#define DEBUG_WINDOW_H + +#include +#include +#include + +namespace debug{ + namespace window { + void init(GLFWwindow* window); + void prerender(); + void render(); + void destroy(); + + void set_parameter(std::string key, std::any value); + } +} + +#endif diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index bc8d57b..8e1e4fd 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(glad) -add_subdirectory(glm) \ No newline at end of file +add_subdirectory(glm) +add_subdirectory(imgui) diff --git a/lib/imgui b/lib/imgui new file mode 160000 index 0000000..6addf28 --- /dev/null +++ b/lib/imgui @@ -0,0 +1 @@ +Subproject commit 6addf28c4b5d8fd109a6db73bed6436952b230b2 diff --git a/shaders/shader-quad.fs b/shaders/shader-quad.fs index c4dc9a6..de35cc8 100644 --- a/shaders/shader-quad.fs +++ b/shaders/shader-quad.fs @@ -4,7 +4,25 @@ in vec2 TexCoord; out vec4 FragColor; uniform sampler2D renderTex; +uniform int screenWidth; +uniform int screenHeight; +uniform int crosshairType; void main(){ + float crosshair_alpha = 0.8; + + float dist = length(gl_FragCoord.xy-vec2(screenWidth/2, screenHeight/2)); + FragColor = texture(renderTex, TexCoord); + /*float crosshair_color = (FragColor.x + FragColor.y + FragColor.z) / 3; + /*if(crosshair_color <= 0.5) crosshair_color = 1.0; + /*else crosshair_color = 0.0;*/ + float crosshair_color = 1.0; + + if(dist <= 7){ + if( (crosshairType == 0 && dist >= 5) || + (crosshairType == 1 && ( int(gl_FragCoord.x) == int(screenWidth / 2) || + int(gl_FragCoord.y) == int(screenHeight / 2)) ) + ) FragColor = vec4(vec3(crosshair_color), crosshair_alpha); + } } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f05a4ea..6775018 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,9 +1,10 @@ cmake_minimum_required(VERSION 3.2) project(OpenGLTest) -set(SOURCE_FILES main.cpp chunk.cpp chunkmanager.cpp chunkmesher.cpp chunkgenerator.cpp renderer.cpp spacefilling.cpp stb_image.cpp utils.cpp OpenSimplexNoise.cpp) +set(SOURCE_FILES main.cpp chunk.cpp chunkmanager.cpp chunkmesher.cpp chunkgenerator.cpp + debugwindow.cpp renderer.cpp spacefilling.cpp stb_image.cpp utils.cpp OpenSimplexNoise.cpp) add_executable(OpenGLTest ${SOURCE_FILES}) -target_link_libraries(OpenGLTest glfw tbb glad glm) +target_link_libraries(OpenGLTest glfw tbb glad glm imgui) install(TARGETS OpenGLTest DESTINATION ${DIVISIBLE_INSTALL_BIN_DIR}) diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index e82b19a..917e476 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -12,6 +12,7 @@ #include "chunk.hpp" #include "chunkgenerator.hpp" #include "chunkmesher.hpp" +#include "debugwindow.hpp" #include "globals.hpp" #include "renderer.hpp" @@ -33,6 +34,7 @@ namespace chunkmanager // Queue of chunks to be meshed ChunkPriorityQueue chunks_to_mesh_queue; + int block_to_place{2}; // Init chunkmanager. Chunk indices and start threads int chunks_volume_real; @@ -85,6 +87,8 @@ namespace chunkmanager update_thread = std::thread(update); gen_thread = std::thread(generate); mesh_thread = std::thread(mesh); + + debug::window::set_parameter("block_type_return", &block_to_place); } // Method for world generation thread(s) @@ -117,6 +121,16 @@ namespace chunkmanager int chunkY=static_cast(theCamera.getAtomicPosY() / CHUNK_SIZE); int chunkZ=static_cast(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 for(int i = 0; i < chunks_volume_real; i++) { const uint16_t x = chunks_indices[i][0] + chunkX; @@ -152,6 +166,9 @@ namespace chunkmanager 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; nUnloaded = 0; while(chunks_todelete.try_pop(n)){ @@ -240,11 +257,17 @@ namespace chunkmanager if(!chunks.find(a1, calculateIndex(px1, py1, pz1))) return; Chunk::Chunk* c1 = a1->second; // place the new block (only stone for now) - c1->setBlock( Block::STONE, bx1, by1, bz1); + c1->setBlock((Block)block_to_place, bx1, by1, bz1); // 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(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{ // replace the current block with air to remove it c->setBlock( Block::AIR, bx, by, bz); @@ -265,6 +288,13 @@ namespace chunkmanager chunkmesher::mesh(b2->second); if(bz == CHUNK_SIZE - 1 && pz +1 < 1024 && chunks.find(c2, calculateIndex(px, py, pz +1))) 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; } diff --git a/src/debugwindow.cpp b/src/debugwindow.cpp new file mode 100644 index 0000000..70ab802 --- /dev/null +++ b/src/debugwindow.cpp @@ -0,0 +1,130 @@ +#include "debugwindow.hpp" + +#include +#include +#include +#include + +#include +#include +#include + +namespace debug{ + namespace window{ + + void show_debug_window(); + constexpr int frametimes_array_size = 20; + float frametimes_array[frametimes_array_size]{}; + + std::unordered_map parameters; + + void init(GLFWwindow* window){ + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + + // Setup Platform/Renderer backends + ImGui_ImplGlfw_InitForOpenGL(window, true); // Second param install_callback=true will install GLFW callbacks and chain to existing ones. + ImGui_ImplOpenGL3_Init(); + } + + void prerender(){ + // Start the Dear ImGui frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + //ImGui::ShowDemoWindow(); // Show demo window! :) + show_debug_window(); + } + + void render(){ + // (Your code clears your framebuffer, renders your other stuff etc.) + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + // (Your code calls glfwSwapBuffers() etc.) + } + + void destroy(){ + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + } + + void set_parameter(std::string key, std::any value){ + parameters[key] = value; + } + + 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(parameters.at("fps"))); + ImGui::Text("Frametime (ms): %f", + std::any_cast(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(parameters.at("px")),std::any_cast(parameters.at("py")),std::any_cast(parameters.at("pz")) ); + ImGui::Text("X: %d, Y: %d, Z: %d (chunk)", std::any_cast(parameters.at("cx")),std::any_cast(parameters.at("cy")),std::any_cast(parameters.at("cz")) ); + ImGui::Text("Pointing in direction: %f, %f, %f", + std::any_cast(parameters.at("lx")),std::any_cast(parameters.at("ly")),std::any_cast(parameters.at("lz")) ); + + if(parameters.find("block_last_action") != parameters.end()){ + ImGui::Text("Last Block action: %s", + std::any_cast(parameters.at("block_last_action")) ? "place" : "destroy"); + ImGui::Text("Last Block action block type: %d", + std::any_cast(parameters.at("block_last_action_block_type"))); + ImGui::Text("Last Block action position: X: %d, Y: %d, Z: %d", + std::any_cast(parameters.at("block_last_action_x")),std::any_cast(parameters.at("block_last_action_y")),std::any_cast(parameters.at("block_last_action_z")) ); + } + + ImGui::SliderInt("Crosshair type", + std::any_cast(parameters.at("crosshair_type_return")), 0, 1); + ImGui::SliderInt("Block to place", + std::any_cast(parameters.at("block_type_return")), 2, 6); + } + + if(ImGui::CollapsingHeader("Mesh")){ + ImGui::Text("Total chunks updated: %d", + std::any_cast(parameters.at("render_chunks_total"))); + ImGui::Text("Chunks rendered: %d", + std::any_cast(parameters.at("render_chunks_rendered"))); + ImGui::Text("Frustum culled: %d", + std::any_cast(parameters.at("render_chunks_culled"))); + ImGui::Text("Chunks out of view: %d", + std::any_cast(parameters.at("render_chunks_oof"))); + if(parameters.find("render_chunks_deleted") != parameters.end()) + ImGui::Text("Chunks deleted: %d", + std::any_cast(parameters.at("render_chunks_deleted"))); + ImGui::Text("Vertices in the scene: %d", + std::any_cast(parameters.at("render_chunks_vertices"))); + ImGui::Checkbox("Wireframe", + std::any_cast(parameters.at("wireframe_return"))); + } + + if(ImGui::CollapsingHeader("Chunks")){ + ImGui::Text("Total chunks present: %d", + std::any_cast(parameters.at("update_chunks_total"))); + /*ImGui::Text("Chunks freed from memory: %d", + std::any_cast(parameters.at("update_chunks_delete")));*/ + ImGui::Text("Bucket size: %d", + std::any_cast(parameters.at("update_chunks_bucket"))); + } + }catch(const std::bad_any_cast& e){ + std::cout << e.what(); + } + + ImGui::End(); + } + } +} diff --git a/src/main.cpp b/src/main.cpp index 7123cf8..1f185f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "chunkmanager.hpp" #include "main.hpp" +#include "debugwindow.hpp" #include "renderer.hpp" #include "spacefilling.hpp" #include "shader.hpp" @@ -21,7 +22,7 @@ int frames = 0; float lastBlockPick=0.0; bool blockpick = false; -bool canChangeWireframe = true; +bool cursor = false; int main() { @@ -67,8 +68,10 @@ int main() sines[i] = sin(3.14 / 180 * i); cosines[i] = cos(3.14 / 180 * i); } + SpaceFilling::initLUT(); chunkmanager::init(); + debug::window::init(window); renderer::init(window); while (!glfwWindowShouldClose(window)) @@ -78,10 +81,12 @@ int main() deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; + debug::window::set_parameter("frametime", deltaTime); // FPS Counter frames++; 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; lastFPSFrame = currentFrame; } @@ -112,6 +117,7 @@ int main() // Cleanup allocated memory chunkmanager::destroy(); renderer::destroy(); + debug::window::destroy(); glfwTerminate(); return 0; @@ -146,16 +152,15 @@ void processInput(GLFWwindow *window) lastBlockPick=glfwGetTime(); } - if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS && canChangeWireframe){ - wireframe = !wireframe; - canChangeWireframe = false; - } - if (glfwGetKey(window, GLFW_KEY_F) == GLFW_RELEASE) canChangeWireframe = true; - // Reset blockpicking if enough time has passed if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_RELEASE) blockpick = false; if(glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS) renderer::saveScreenshot(); if(glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS) renderer::saveScreenshot(true); + if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) { + cursor = !cursor; + glfwSetInputMode(window, GLFW_CURSOR, cursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED); + } + } diff --git a/src/renderer.cpp b/src/renderer.cpp index eeaae93..8f1ae97 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -5,6 +5,7 @@ #include "chunkmanager.hpp" #include "chunkmesher.hpp" +#include "debugwindow.hpp" #include "globals.hpp" #include "stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION @@ -25,6 +26,9 @@ namespace renderer{ GLuint renderTexFrameBuffer, renderTex, renderTexDepthBuffer, quadVAO, quadVBO; int screenWidth, screenHeight; + int crosshair_type{0}; + bool wireframe{false}; + void init(GLFWwindow* window){ // Setup rendering // We will render the image to a texture, then display the texture on a quad that fills the @@ -103,6 +107,9 @@ namespace renderer{ glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_T,GL_REPEAT); + + debug::window::set_parameter("crosshair_type_return", &crosshair_type); + debug::window::set_parameter("wireframe_return", &wireframe); } @@ -118,7 +125,11 @@ namespace renderer{ glClearColor(0.431f, 0.694f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - int total{0}, toGpu{0}; + /* UPDATE IMGUI */ + debug::window::prerender(); + + /* RENDER THE WORLD TO TEXTURE */ + int total{0}, toGpu{0}, oof{0}, vertices{0}; glm::vec4 frustumPlanes[6]; theCamera.getFrustumPlanes(frustumPlanes, true); glm::vec3 cameraPos = theCamera.getPos(); @@ -138,6 +149,9 @@ namespace renderer{ if(dist <= static_cast(RENDER_DISTANCE)){ if(!c->getState(Chunk::CHUNK_STATE_MESH_LOADED)) continue; + // Increase total vertex count + vertices += c->numVertices; + // reset out-of-vision and unload flags c->setState(Chunk::CHUNK_STATE_OUTOFVISION, false); c->setState(Chunk::CHUNK_STATE_UNLOADED, false); @@ -174,12 +188,15 @@ namespace renderer{ glBindVertexArray(c->VAO); glDrawArrays(GL_POINTS, 0, c->numVertices); glBindVertexArray(0); + + toGpu++; } } }else{ // When the chunk is outside render distance if(c->getState(Chunk::CHUNK_STATE_OUTOFVISION)){ + oof++; if(glfwGetTime() - c->unload_timer > UNLOAD_TIMEOUT){ // If chunk was already out and enough time has passed // Mark the chunk to be unloaded @@ -196,6 +213,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){ // we can get away with unsafe erase as access to the container is only done by this // thread @@ -205,7 +230,7 @@ namespace renderer{ } render_todelete.clear(); - + /* DISPLAY TEXTURE ON A QUAD THAT FILLS THE SCREEN */ // Now to render the quad, with the texture on top // Switch to the default frame buffer glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -217,8 +242,13 @@ namespace renderer{ glDisable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, renderTex); quadShader->use(); + quadShader->setInt("screenWidth", screenWidth); + quadShader->setInt("screenHeight", screenHeight); + quadShader->setInt("crosshairType", crosshair_type); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); + + debug::window::render(); } void framebuffer_size_callback(GLFWwindow *window, int width, int height){ @@ -268,6 +298,7 @@ namespace renderer{ void destroy(){ delete theShader; + delete quadShader; }