From a6a419fdff1c3d1b1b2de32dfd86458eb715f966 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sun, 17 Sep 2023 15:50:54 +0200 Subject: [PATCH 1/7] hud: crosshair with fragment shader --- shaders/shader-quad.fs | 18 ++++++++++++++++++ src/renderer.cpp | 3 +++ 2 files changed, 21 insertions(+) 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/renderer.cpp b/src/renderer.cpp index eeaae93..ddd8a28 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -217,6 +217,9 @@ namespace renderer{ glDisable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, renderTex); quadShader->use(); + quadShader->setInt("screenWidth", screenWidth); + quadShader->setInt("screenHeight", screenHeight); + quadShader->setInt("crosshairType", 1); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); } -- 2.40.1 From e7c4b2d56b3bc6602959f642df4ba09bc521f352 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Mon, 18 Sep 2023 16:03:30 +0200 Subject: [PATCH 2/7] lib: add dearimgui lib --- .gitmodules | 3 +++ lib/CMakeLists.txt | 3 ++- lib/imgui | 1 + src/CMakeLists.txt | 2 +- 4 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 160000 lib/imgui 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/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/src/CMakeLists.txt b/src/CMakeLists.txt index f05a4ea..cfbbf1e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,5 +5,5 @@ set(SOURCE_FILES main.cpp chunk.cpp chunkmanager.cpp chunkmesher.cpp chunkgenera 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}) -- 2.40.1 From f526e9b15210e93d9da3a5a1418deef9847abbae Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Mon, 18 Sep 2023 17:29:55 +0200 Subject: [PATCH 3/7] create debug window with imgui --- .gitignore | 3 ++- CMakeLists.txt | 4 ++-- include/debugwindow.hpp | 19 +++++++++++++++++ src/CMakeLists.txt | 3 ++- src/debugwindow.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 10 +++++++++ src/renderer.cpp | 10 ++++++++- 7 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 include/debugwindow.hpp create mode 100644 src/debugwindow.cpp 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/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/src/CMakeLists.txt b/src/CMakeLists.txt index cfbbf1e..6775018 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,8 @@ 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}) diff --git a/src/debugwindow.cpp b/src/debugwindow.cpp new file mode 100644 index 0000000..eed0a6f --- /dev/null +++ b/src/debugwindow.cpp @@ -0,0 +1,47 @@ +#include "debugwindow.hpp" + +#include +#include +#include + +namespace debug{ + namespace window{ + + + 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! :) + } + + 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(); + } + } +} diff --git a/src/main.cpp b/src/main.cpp index 7123cf8..f50dc62 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" @@ -22,6 +23,7 @@ int frames = 0; float lastBlockPick=0.0; bool blockpick = false; bool canChangeWireframe = true; +bool cursor = false; int main() { @@ -67,8 +69,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)) @@ -112,6 +116,7 @@ int main() // Cleanup allocated memory chunkmanager::destroy(); renderer::destroy(); + debug::window::destroy(); glfwTerminate(); return 0; @@ -157,5 +162,10 @@ void processInput(GLFWwindow *window) 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 ddd8a28..4027e5c 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 @@ -118,6 +119,10 @@ namespace renderer{ glClearColor(0.431f, 0.694f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + /* UPDATE IMGUI */ + debug::window::prerender(); + + /* RENDER THE WORLD TO TEXTURE */ int total{0}, toGpu{0}; glm::vec4 frustumPlanes[6]; theCamera.getFrustumPlanes(frustumPlanes, true); @@ -205,7 +210,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); @@ -222,6 +227,8 @@ namespace renderer{ quadShader->setInt("crosshairType", 1); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); + + debug::window::render(); } void framebuffer_size_callback(GLFWwindow *window, int width, int height){ @@ -271,6 +278,7 @@ namespace renderer{ void destroy(){ delete theShader; + delete quadShader; } -- 2.40.1 From 9d2d0c87729c21958d06246a632b4ed70b158baf Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Mon, 18 Sep 2023 20:54:57 +0200 Subject: [PATCH 4/7] debug window: populate with data --- src/chunkmanager.cpp | 27 ++++++++++++++ src/debugwindow.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++- src/main.cpp | 4 ++- src/renderer.cpp | 16 ++++++++- 4 files changed, 129 insertions(+), 3 deletions(-) diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index e82b19a..eecfa22 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" @@ -117,6 +118,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 +163,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)){ @@ -245,6 +259,12 @@ namespace chunkmanager // 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 +285,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 index eed0a6f..870d18d 100644 --- a/src/debugwindow.cpp +++ b/src/debugwindow.cpp @@ -3,10 +3,20 @@ #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 @@ -25,7 +35,8 @@ namespace debug{ ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - ImGui::ShowDemoWindow(); // Show demo window! :) + //ImGui::ShowDemoWindow(); // Show demo window! :) + show_debug_window(); } void render(){ @@ -43,5 +54,77 @@ namespace debug{ ImGui_ImplGlfw_Shutdown(); 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(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", + &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(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"))); + } + + 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 f50dc62..1ca4fad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -82,10 +82,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; } diff --git a/src/renderer.cpp b/src/renderer.cpp index 4027e5c..7f0180b 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -123,7 +123,7 @@ namespace renderer{ debug::window::prerender(); /* RENDER THE WORLD TO TEXTURE */ - int total{0}, toGpu{0}; + int total{0}, toGpu{0}, oof{0}, vertices{0}; glm::vec4 frustumPlanes[6]; theCamera.getFrustumPlanes(frustumPlanes, true); glm::vec3 cameraPos = theCamera.getPos(); @@ -143,6 +143,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); @@ -179,12 +182,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 @@ -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){ // we can get away with unsafe erase as access to the container is only done by this // thread -- 2.40.1 From 52696f2dde414fd7196eef326ec543836fcb5860 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Mon, 18 Sep 2023 21:00:08 +0200 Subject: [PATCH 5/7] debug window: use crosshair type from slider in shader --- src/debugwindow.cpp | 5 ++--- src/renderer.cpp | 5 ++++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/debugwindow.cpp b/src/debugwindow.cpp index 870d18d..b82a5c9 100644 --- a/src/debugwindow.cpp +++ b/src/debugwindow.cpp @@ -59,7 +59,6 @@ namespace debug{ parameters[key] = value; } - int ch_type{0}; int block_type{0}; void show_debug_window(){ ImGui::Begin("Debug Window"); @@ -91,9 +90,9 @@ namespace debug{ } ImGui::SliderInt("Crosshair type", - &ch_type, 0, 1); + std::any_cast(parameters.at("crosshair_type_return")), 0, 1); ImGui::SliderInt("Block to place", - &block_type, 2, 6); + block_type, 2, 6); } if(ImGui::CollapsingHeader("Mesh")){ diff --git a/src/renderer.cpp b/src/renderer.cpp index 7f0180b..c402382 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -25,6 +25,7 @@ namespace renderer{ GLuint renderTexFrameBuffer, renderTex, renderTexDepthBuffer, quadVAO, quadVBO; int screenWidth, screenHeight; + int crosshair_type{0}; void init(GLFWwindow* window){ // Setup rendering @@ -104,6 +105,8 @@ 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); } @@ -238,7 +241,7 @@ namespace renderer{ quadShader->use(); quadShader->setInt("screenWidth", screenWidth); quadShader->setInt("screenHeight", screenHeight); - quadShader->setInt("crosshairType", 1); + quadShader->setInt("crosshairType", crosshair_type); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); -- 2.40.1 From 07068d740a22748f9e376b882c572af7fe10539e Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Mon, 18 Sep 2023 21:00:55 +0200 Subject: [PATCH 6/7] debug window: use block type from slider in block picking --- src/chunkmanager.cpp | 5 ++++- src/debugwindow.cpp | 3 +-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index eecfa22..917e476 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -34,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; @@ -86,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) @@ -254,7 +257,7 @@ 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)); diff --git a/src/debugwindow.cpp b/src/debugwindow.cpp index b82a5c9..e44f5c8 100644 --- a/src/debugwindow.cpp +++ b/src/debugwindow.cpp @@ -59,7 +59,6 @@ namespace debug{ parameters[key] = value; } - int block_type{0}; void show_debug_window(){ ImGui::Begin("Debug Window"); @@ -92,7 +91,7 @@ namespace debug{ ImGui::SliderInt("Crosshair type", std::any_cast(parameters.at("crosshair_type_return")), 0, 1); ImGui::SliderInt("Block to place", - block_type, 2, 6); + std::any_cast(parameters.at("block_type_return")), 2, 6); } if(ImGui::CollapsingHeader("Mesh")){ -- 2.40.1 From 50bc52a679328f6344dffc7effb9cbe7b2cc585b Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Mon, 18 Sep 2023 21:37:21 +0200 Subject: [PATCH 7/7] debug window: use wireframe from checkbox in rendering --- src/debugwindow.cpp | 2 ++ src/main.cpp | 7 ------- src/renderer.cpp | 3 +++ 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/debugwindow.cpp b/src/debugwindow.cpp index e44f5c8..70ab802 100644 --- a/src/debugwindow.cpp +++ b/src/debugwindow.cpp @@ -108,6 +108,8 @@ namespace debug{ 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")){ diff --git a/src/main.cpp b/src/main.cpp index 1ca4fad..1f185f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,6 @@ int frames = 0; float lastBlockPick=0.0; bool blockpick = false; -bool canChangeWireframe = true; bool cursor = false; int main() @@ -153,12 +152,6 @@ 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; diff --git a/src/renderer.cpp b/src/renderer.cpp index c402382..8f1ae97 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -25,7 +25,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 @@ -107,6 +109,7 @@ namespace renderer{ 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); } -- 2.40.1