From e4836174b414c9275075f09b4eee10455569cf36 Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Wed, 1 Oct 2025 16:03:11 +0200 Subject: [PATCH] Minor - Adds dynamic screen depth and near controls - V14.4.0 Implements dynamic adjustment of screen depth and near clipping plane distances via the ImGui interface. This allows users to modify the perspective projection in real-time, affecting the rendering of the scene. The changes involve: - Adding screen depth and near variables to the application class. - Exposing these variables in the ImGui settings window. - Adding functions to the application and d3d classes to update the projection matrix based on new screen depth and near values. - Updates the sky sphere size to use world scale. --- enginecustom/imgui.ini | 2 +- .../src/inc/system/application_class.h | 38 +++++++--- enginecustom/src/inc/system/d_3d_class.h | 10 +++ enginecustom/src/inc/system/imguiManager.h | 2 + .../src/src/system/application_class.cpp | 20 +++++- enginecustom/src/src/system/d_3d_class.cpp | 17 +++++ enginecustom/src/src/system/imguiManager.cpp | 69 ++++++++++++++++++- x64/Debug/config.txt | 2 +- x64/Release/config.txt | 2 +- 9 files changed, 147 insertions(+), 15 deletions(-) diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index 0f1daac..ee2e83c 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -27,7 +27,7 @@ Collapsed=0 DockId=0x0000000B,0 [Window][Terrain] -Pos=236,19 +Pos=0,19 Size=266,842 Collapsed=0 DockId=0x00000007,0 diff --git a/enginecustom/src/inc/system/application_class.h b/enginecustom/src/inc/system/application_class.h index bed2ce6..3ba6936 100644 --- a/enginecustom/src/inc/system/application_class.h +++ b/enginecustom/src/inc/system/application_class.h @@ -61,8 +61,8 @@ // GLOBALS // ///////////// constexpr bool full_screen = false; -constexpr float screen_depth = 1000.0f; -constexpr float screen_near = 0.3f; +// constexpr float screen_depth = 1000.0f; +// constexpr float screen_near = 0.3f; static std::map> g_model_cache; @@ -457,16 +457,32 @@ public: */ int get_sky_id() const { return sky_id_; } - /** - * Set the sky entity size. - * @return The sky entity size as a float. + /** Get the Sky entity as a shared pointer. + * @return A shared pointer to the sky entity. */ - XMVECTOR get_sky_distance() const { return sky_distance_; }; + std::shared_ptr get_sky_entity_shared_ptr() const { return sky_entity_; } + /** - * Set the sky entity size. - * @param distance The new sky entity size as a float. + * Update the screen depth. */ - void set_sky_distance(XMVECTOR distance); + bool update_screen_depth(float new_screen_depth); + + /** + * Update the screen near. + */ + bool update_screen_near(float new_screen_near); + + /** + * Get the screen depth. + * @return The screen depth as a float. + */ + float get_screen_depth() const { return screen_depth; } + /** + * Get the screen near. + * @return The screen near as a float. + */ + float get_screen_near() const { return screen_near; } + private: /** @@ -581,6 +597,9 @@ private : HWND hwnd_; bool windowed_; + float screen_depth = 1000.0f; + float screen_near = 0.3f; + // ------------------------------------- // // ------------- RENDERING ------------- // // ------------------------------------- // @@ -612,7 +631,6 @@ private : int sky_id_ = -1; std::shared_ptr sky_entity_; - XMVECTOR sky_distance_ = XMVectorSet(2.0f, 2.0f, 2.0f, 0.0f); // ----------------------------------- // // ------------- LIGHTS -------------- // diff --git a/enginecustom/src/inc/system/d_3d_class.h b/enginecustom/src/inc/system/d_3d_class.h index 76ef0ba..f10b82e 100644 --- a/enginecustom/src/inc/system/d_3d_class.h +++ b/enginecustom/src/inc/system/d_3d_class.h @@ -165,6 +165,16 @@ public: */ void disable_alpha_blending(); + /** + * Set new projection parameters. + * @param width The new width for the projection matrix. + * @param height The new height for the projection matrix. + * @param screenDepth The new screen depth. + * @param screenNear The new near clipping plane distance. + * @return True if the projection parameters were set successfully, false otherwise. + */ + bool set_projection_params(int width,int height,float screenDepth, float screenNear); + private: bool vsync_enabled_; int video_card_memory_; diff --git a/enginecustom/src/inc/system/imguiManager.h b/enginecustom/src/inc/system/imguiManager.h index 2c2224f..bf4d6ff 100644 --- a/enginecustom/src/inc/system/imguiManager.h +++ b/enginecustom/src/inc/system/imguiManager.h @@ -185,6 +185,8 @@ private: scene_manager* scene_manager_; stats* stats_; ecs::EntityManager* entity_manager_; + + std::shared_ptr sky_entity_shared_ptr_; bool showObjectWindow; bool showTerrainWindow; diff --git a/enginecustom/src/src/system/application_class.cpp b/enginecustom/src/src/system/application_class.cpp index 9345dad..fc1dfa0 100644 --- a/enginecustom/src/src/system/application_class.cpp +++ b/enginecustom/src/src/system/application_class.cpp @@ -2110,7 +2110,7 @@ bool application_class::create_skysphere() auto transform = sky_entity_->AddComponent(); transform->SetPosition(XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f)); - transform->SetScale(sky_distance_); + transform->SetScale(XMVectorSet(2.0f, 2.0f, 2.0f, 0.0f)); transform->UpdateWorldMatrix(); auto render = sky_entity_->AddComponent(); @@ -2122,5 +2122,23 @@ bool application_class::create_skysphere() auto shader = sky_entity_->AddComponent(); shader->SetActiveShader(ecs::ShaderType::SKYBOX); + return true; +} + +bool application_class::update_screen_depth(float new_screen_depth) +{ + if (!direct_3d_) return false; + if (new_screen_depth <= screen_near) return false; + screen_depth = new_screen_depth; + direct_3d_->set_projection_params(screen_width_, screen_height_, screen_depth, screen_near); + return true; +} + +bool application_class::update_screen_near(float new_screen_near) +{ + if (!direct_3d_) return false; + if (new_screen_near <= 0.0f || new_screen_near >= screen_depth) return false; + screen_near = new_screen_near; + direct_3d_->set_projection_params(screen_width_, screen_height_, screen_depth, screen_near); return true; } \ No newline at end of file diff --git a/enginecustom/src/src/system/d_3d_class.cpp b/enginecustom/src/src/system/d_3d_class.cpp index f6d05c0..55b500b 100644 --- a/enginecustom/src/src/system/d_3d_class.cpp +++ b/enginecustom/src/src/system/d_3d_class.cpp @@ -771,4 +771,21 @@ void d_3d_class::disable_alpha_blending() void d_3d_class::set_vsync(bool vsync) { vsync_enabled_ = vsync; +} + +bool d_3d_class::set_projection_params(int width, int height, float screenDepth, float screenNear) +{ + float fieldOfView, screenAspect; + + // Setup the projection matrix. + fieldOfView = 3.141592654f / 4.0f; + screenAspect = (float)width / (float)height; + + // Create the projection matrix for 3D rendering. + projection_matrix_ = XMMatrixPerspectiveFovLH(fieldOfView, screenAspect, screenNear, screenDepth); + + // Create an orthographic projection matrix for 2D rendering. + ortho_matrix_ = XMMatrixOrthographicLH((float)width, (float)height, screenNear, screenDepth); + + return true; } \ No newline at end of file diff --git a/enginecustom/src/src/system/imguiManager.cpp b/enginecustom/src/src/system/imguiManager.cpp index d201791..1907518 100644 --- a/enginecustom/src/src/system/imguiManager.cpp +++ b/enginecustom/src/src/system/imguiManager.cpp @@ -264,6 +264,12 @@ bool imguiManager::Initialize(HWND hwnd, ID3D11Device* device, ID3D11DeviceConte if (!IncrementBuildVersionInConfig(configPath)) { Logger::Get().Log("Failed to increment build version in config.txt of inverse build type", __FILE__, __LINE__, Logger::LogLevel::Warning); } + + // get the sky entity shared ptr from the application + sky_entity_shared_ptr_ = app_->get_sky_entity_shared_ptr(); + if (!sky_entity_shared_ptr_) { + Logger::Get().Log("Sky entity shared ptr is null", __FILE__, __LINE__, Logger::LogLevel::Warning); + } Logger::Get().Log("imgui initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize); @@ -857,6 +863,27 @@ void imguiManager::WidgetEngineSettingsWindow() { ImGui::SetTooltip("Enable or disable Vsync"); } + + float screen_depth = app_->get_screen_depth(); + float screen_near = app_->get_screen_near(); + // Drag float input for screen depth and near with min and max values and update the app values on change + if (ImGui::DragFloat("Screen Depth", &screen_depth, 1.f, 0.1f, 10000.0f)) + { + app_->update_screen_depth(screen_depth); + } + if (ImGui::IsItemHovered()) + { + ImGui::SetTooltip("Set the screen depth"); + } + + if (ImGui::DragFloat("Screen Near", &screen_near, 1.f, 0.1f, 10000.0f)) + { + app_->update_screen_near(screen_near); + } + if (ImGui::IsItemHovered()) + { + ImGui::SetTooltip("Set the screen near"); + } ImGui::EndTabItem(); } @@ -905,7 +932,47 @@ void imguiManager::WidgetEngineSettingsWindow() if (ImGui::BeginTabItem("Skysphere Settings")) { - //float skysphere_size = app_->get_sky()-> + if (sky_entity_shared_ptr_) + { + + // get the transform component + auto transform = sky_entity_shared_ptr_->GetComponent(); + auto render = sky_entity_shared_ptr_->GetComponent(); + auto shader = sky_entity_shared_ptr_->GetComponent(); + if (transform) + { + // get the scale and display it as a drag float3 input (need to convert from XMVECTOR to float[3]) + XMVECTOR scale = transform->GetScale(); + float scale_values[3] = { XMVectorGetX(scale), XMVectorGetY(scale), XMVectorGetZ(scale) }; + if (ImGui::DragFloat3("Skysphere Scale", scale_values, 0.1f, 1.0f, 1000.0f)) + { + transform->SetScale(XMVectorSet(scale_values[0], scale_values[1], scale_values[2], 0.0f)); + } + if (ImGui::IsItemHovered()) + { + ImGui::SetTooltip("Set the scale of the skysphere"); + } + } + + if (render) + { + // display the render component default imguirender + render->OnImGuiRender(); + } + + if (shader) + { + // display the render component default imguirender + shader->OnImGuiRender(); + } + + } + else + { + ImGui::Text("Sky entity not found."); + } + + ImGui::EndTabItem(); } diff --git a/x64/Debug/config.txt b/x64/Debug/config.txt index 2d5d9b4..d97d4a9 100644 --- a/x64/Debug/config.txt +++ b/x64/Debug/config.txt @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:87ab8fabde4621ecb618fa265ef5e7a87cae4f71dcee5bd74fae2272f955cd12 +oid sha256:9dd88c281e43de237b4e836f7cda89d8e78af72110fa9fdae759335878e53e38 size 8 diff --git a/x64/Release/config.txt b/x64/Release/config.txt index 2d5d9b4..d97d4a9 100644 --- a/x64/Release/config.txt +++ b/x64/Release/config.txt @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:87ab8fabde4621ecb618fa265ef5e7a87cae4f71dcee5bd74fae2272f955cd12 +oid sha256:9dd88c281e43de237b4e836f7cda89d8e78af72110fa9fdae759335878e53e38 size 8