From 59ab47d3173b4fd65fbc9175e3f5770992968647 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sun, 26 Apr 2026 17:29:58 +0800 Subject: [PATCH] feat: add anisotropic filtering control --- include/Cubed/dev_panel.hpp | 22 +++++---- include/Cubed/texture_manager.hpp | 4 +- src/config.cpp | 6 ++- src/dev_panel.cpp | 77 +++++++++++++++++++++++++------ src/texture_manager.cpp | 30 +++++++----- 5 files changed, 103 insertions(+), 36 deletions(-) diff --git a/include/Cubed/dev_panel.hpp b/include/Cubed/dev_panel.hpp index 2092566..3e2716f 100644 --- a/include/Cubed/dev_panel.hpp +++ b/include/Cubed/dev_panel.hpp @@ -8,13 +8,18 @@ class App; class Player; class DevPanel { struct ConfigView { - float fov; - bool fullscreen; - bool v_sync; - float mouse_sensitivity; - int width; - int height; - int rendering_distance; + float fov = 70.0f; + bool fullscreen = false; + bool v_sync = true; + float mouse_sensitivity = 0.15f; + int width = 800; + int height = 600; + int rendering_distance = 24; + int aniso = 1; + int max_aniso = 1; + bool is_enable_aniso = false; + bool is_support_aniso = true; + bool is_reload = true; }; struct PlayerProfile { int game_mode = 0; @@ -42,8 +47,9 @@ private: void show_world_tab_item(); void show_player_tab_item(); + void update_config_view(); void update_player_profile(); - + }; diff --git a/include/Cubed/texture_manager.hpp b/include/Cubed/texture_manager.hpp index 1453af4..7cf7523 100644 --- a/include/Cubed/texture_manager.hpp +++ b/include/Cubed/texture_manager.hpp @@ -12,6 +12,8 @@ private: GLuint m_block_status_array; GLuint m_texture_array; GLuint m_ui_array; + GLfloat m_max_aniso = 0.0f; + int m_aniso = 1; void load_block_status(unsigned status_id); void load_block_texture(unsigned block_id); void load_ui_texture(unsigned id); @@ -29,7 +31,7 @@ public: void hot_reload(); void need_reload(); void update(); - + int max_aniso() const; }; diff --git a/src/config.cpp b/src/config.cpp index a6d962c..feffa45 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -45,7 +45,11 @@ void Config::create_config() { rendering_distance = 24 [devpanel] - theme = 0 # 0 is Dark Theme, 1 is Light Theme + theme = 0 # 0 is Dark Theme, 1 is Light Theme + + [texture] + aniso = 1 # i is the minimun value, indicating off + )"sv; try { diff --git a/src/dev_panel.cpp b/src/dev_panel.cpp index 11fb7ab..9705575 100644 --- a/src/dev_panel.cpp +++ b/src/dev_panel.cpp @@ -50,18 +50,7 @@ DevPanel::DevPanel(App& app) : void DevPanel::init() { m_player = &m_app.world().get_player("TestPlayer"); - auto config = Config::get(); - m_config.fov = static_cast(config.val_view("player.fov").value_or(70.0)); - m_config.fullscreen = config.val_view("window.fullscreen").value_or(false); - m_config.v_sync = config.val_view("window.V-Sync").value_or(true); - m_config.mouse_sensitivity = static_cast(config.val_view("player.mouse_sensitivity").value_or(0.15)); - m_config.width = config.val_view("window.width").value_or(800); - m_config.height = config.val_view("window.height").value_or(600); - m_config.rendering_distance = config.val_view("world.rendering_distance").value_or(24); - m_theme = config.val_view("devpanel.theme").value_or(0); - if (m_theme != 1 && m_theme != 0) { - m_theme = 0; - } + update_config_view(); update_player_profile(); } @@ -186,7 +175,47 @@ void DevPanel::show_settings_tab_item() { Config::get().set("window.V-Sync", m_config.v_sync); m_app.window().hot_reload(); } - + if (ImGui::Checkbox("Aniso", &m_config.is_enable_aniso)) { + m_config.is_reload = false; + if (m_config.is_enable_aniso) { + m_config.max_aniso = m_app.texture_manager().max_aniso(); + if (m_config.max_aniso < 2) { + m_config.is_support_aniso = false; + } else { + m_config.aniso = 2; + } + } else { + m_config.aniso = 1; + } + + } + if (m_config.is_enable_aniso) { + ImGui::SameLine(); + if (!m_config.is_support_aniso) { + ImGui::Text("Not Support\n"); + } else { + if (ImGui::SliderInt("##aniso", &m_config.aniso, 2, m_config.max_aniso)) { + m_config.is_reload = false; + int log = static_cast(std::log2(m_config.aniso) + 0.5f); + m_config.aniso = static_cast(std::pow(2, log)); + if (m_config.aniso < 2) { + m_config.aniso = 2; + } + if (m_config.aniso > m_config.max_aniso) { + m_config.aniso = m_config.max_aniso; + } + } + } + } + if (ImGui::Button("ReloadTexture")) { + Config::get().set("texture.aniso", m_config.aniso); + m_app.texture_manager().hot_reload(); + m_config.is_reload = true; + } + if (!m_config.is_reload) { + ImGui::SameLine(); + ImGui::Text("Your need to click this button to apply config\n"); + } if (ImGui::Combo("Theme", &m_theme, THEMES, IM_ARRAYSIZE(THEMES))) { if (m_theme == 0) { ImGui::StyleColorsDark(); @@ -311,7 +340,27 @@ void DevPanel::show_player_tab_item() { ImGui::EndTabItem(); } } - +void DevPanel::update_config_view() { + auto config = Config::get(); + m_config.fov = static_cast(config.val_view("player.fov").value_or(70.0)); + m_config.fullscreen = config.val_view("window.fullscreen").value_or(false); + m_config.v_sync = config.val_view("window.V-Sync").value_or(true); + m_config.mouse_sensitivity = static_cast(config.val_view("player.mouse_sensitivity").value_or(0.15)); + m_config.width = config.val_view("window.width").value_or(800); + m_config.height = config.val_view("window.height").value_or(600); + m_config.rendering_distance = config.val_view("world.rendering_distance").value_or(24); + m_theme = config.val_view("devpanel.theme").value_or(0); + if (m_theme != 1 && m_theme != 0) { + m_theme = 0; + } + m_config.aniso = config.val_view("texture.aniso").value_or(1); + m_config.max_aniso = m_app.texture_manager().max_aniso(); + if (m_config.aniso <= 1) { + m_config.is_enable_aniso = false; + } else { + m_config.is_enable_aniso = true; + } +} void DevPanel::update_player_profile() { if (!m_player) { Logger::error("Player is Nullptr"); diff --git a/src/texture_manager.cpp b/src/texture_manager.cpp index 16f7cfb..75667fd 100644 --- a/src/texture_manager.cpp +++ b/src/texture_manager.cpp @@ -99,7 +99,13 @@ void TextureManager::load_ui_texture(unsigned id) { } void TextureManager::init_texture() { - + glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &m_max_aniso); + if (m_max_aniso > 0.0f) { + Logger::info("Support anisotropic filtering max_aniso is {}", m_max_aniso); + } + m_aniso = Config::get().get("texture.aniso"); + m_aniso = std::min(static_cast(m_max_aniso), m_aniso); + Logger::info("Setting Texture Aniso is {}", m_aniso); MapTable::init_map(); Logger::info("Map Init Success"); glGenTextures(1, &m_texture_array); @@ -125,14 +131,10 @@ void TextureManager::init_texture() { Tools::check_opengl_error(); glGenerateMipmap(GL_TEXTURE_2D_ARRAY); Tools::check_opengl_error(); - - GLfloat max_aniso = 0.0f; - glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY, &max_aniso); - if (max_aniso > 0.0f) { - Logger::info("Support anisotropic filtering max_aniso is {}", max_aniso); - glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY, max_aniso); - } - + if (m_aniso >= 1) { + glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY, static_cast(m_aniso)); + } + glGenTextures(1, &m_block_status_array); Tools::check_opengl_error(); glBindTexture(GL_TEXTURE_2D_ARRAY, m_block_status_array); @@ -157,9 +159,8 @@ void TextureManager::init_texture() { glGenerateMipmap(GL_TEXTURE_2D_ARRAY); Tools::check_opengl_error(); - if (max_aniso > 0.0f) { - Logger::info("Support anisotropic filtering max_aniso is {}", max_aniso); - glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY, max_aniso); + if (m_aniso >= 1) { + glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY, static_cast(m_aniso)); } glGenTextures(1, &m_ui_array); @@ -196,8 +197,13 @@ void TextureManager::need_reload() { void TextureManager::hot_reload() { delet_texture(); + init_texture(); m_need_reload = false; } +int TextureManager::max_aniso() const { + return static_cast(m_max_aniso); +} + } \ No newline at end of file