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.
This commit is contained in:
2025-10-01 16:03:11 +02:00
parent 284e282679
commit e4836174b4
9 changed files with 147 additions and 15 deletions

View File

@@ -27,7 +27,7 @@ Collapsed=0
DockId=0x0000000B,0 DockId=0x0000000B,0
[Window][Terrain] [Window][Terrain]
Pos=236,19 Pos=0,19
Size=266,842 Size=266,842
Collapsed=0 Collapsed=0
DockId=0x00000007,0 DockId=0x00000007,0

View File

@@ -61,8 +61,8 @@
// GLOBALS // // GLOBALS //
///////////// /////////////
constexpr bool full_screen = false; constexpr bool full_screen = false;
constexpr float screen_depth = 1000.0f; // constexpr float screen_depth = 1000.0f;
constexpr float screen_near = 0.3f; // constexpr float screen_near = 0.3f;
static std::map<std::string, std::shared_ptr<model_class>> g_model_cache; static std::map<std::string, std::shared_ptr<model_class>> g_model_cache;
@@ -457,16 +457,32 @@ public:
*/ */
int get_sky_id() const { return sky_id_; } int get_sky_id() const { return sky_id_; }
/** /** Get the Sky entity as a shared pointer.
* Set the sky entity size. * @return A shared pointer to the sky entity.
* @return The sky entity size as a float.
*/ */
XMVECTOR get_sky_distance() const { return sky_distance_; }; std::shared_ptr<ecs::Entity> get_sky_entity_shared_ptr() const { return sky_entity_; }
/** /**
* Set the sky entity size. * Update the screen depth.
* @param distance The new sky entity size as a float.
*/ */
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: private:
/** /**
@@ -581,6 +597,9 @@ private :
HWND hwnd_; HWND hwnd_;
bool windowed_; bool windowed_;
float screen_depth = 1000.0f;
float screen_near = 0.3f;
// ------------------------------------- // // ------------------------------------- //
// ------------- RENDERING ------------- // // ------------- RENDERING ------------- //
// ------------------------------------- // // ------------------------------------- //
@@ -612,7 +631,6 @@ private :
int sky_id_ = -1; int sky_id_ = -1;
std::shared_ptr<ecs::Entity> sky_entity_; std::shared_ptr<ecs::Entity> sky_entity_;
XMVECTOR sky_distance_ = XMVectorSet(2.0f, 2.0f, 2.0f, 0.0f);
// ----------------------------------- // // ----------------------------------- //
// ------------- LIGHTS -------------- // // ------------- LIGHTS -------------- //

View File

@@ -165,6 +165,16 @@ public:
*/ */
void disable_alpha_blending(); 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: private:
bool vsync_enabled_; bool vsync_enabled_;
int video_card_memory_; int video_card_memory_;

View File

@@ -185,6 +185,8 @@ private:
scene_manager* scene_manager_; scene_manager* scene_manager_;
stats* stats_; stats* stats_;
ecs::EntityManager* entity_manager_; ecs::EntityManager* entity_manager_;
std::shared_ptr<ecs::Entity> sky_entity_shared_ptr_;
bool showObjectWindow; bool showObjectWindow;
bool showTerrainWindow; bool showTerrainWindow;

View File

@@ -2110,7 +2110,7 @@ bool application_class::create_skysphere()
auto transform = sky_entity_->AddComponent<ecs::TransformComponent>(); auto transform = sky_entity_->AddComponent<ecs::TransformComponent>();
transform->SetPosition(XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f)); 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(); transform->UpdateWorldMatrix();
auto render = sky_entity_->AddComponent<ecs::RenderComponent>(); auto render = sky_entity_->AddComponent<ecs::RenderComponent>();
@@ -2122,5 +2122,23 @@ bool application_class::create_skysphere()
auto shader = sky_entity_->AddComponent<ecs::ShaderComponent>(); auto shader = sky_entity_->AddComponent<ecs::ShaderComponent>();
shader->SetActiveShader(ecs::ShaderType::SKYBOX); 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; return true;
} }

View File

@@ -771,4 +771,21 @@ void d_3d_class::disable_alpha_blending()
void d_3d_class::set_vsync(bool vsync) void d_3d_class::set_vsync(bool vsync)
{ {
vsync_enabled_ = 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;
} }

View File

@@ -264,6 +264,12 @@ bool imguiManager::Initialize(HWND hwnd, ID3D11Device* device, ID3D11DeviceConte
if (!IncrementBuildVersionInConfig(configPath)) { if (!IncrementBuildVersionInConfig(configPath)) {
Logger::Get().Log("Failed to increment build version in config.txt of inverse build type", __FILE__, __LINE__, Logger::LogLevel::Warning); 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); Logger::Get().Log("imgui initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
@@ -857,6 +863,27 @@ void imguiManager::WidgetEngineSettingsWindow()
{ {
ImGui::SetTooltip("Enable or disable Vsync"); 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(); ImGui::EndTabItem();
} }
@@ -905,7 +932,47 @@ void imguiManager::WidgetEngineSettingsWindow()
if (ImGui::BeginTabItem("Skysphere Settings")) 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<ecs::TransformComponent>();
auto render = sky_entity_shared_ptr_->GetComponent<ecs::RenderComponent>();
auto shader = sky_entity_shared_ptr_->GetComponent<ecs::ShaderComponent>();
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();
} }

BIN
x64/Debug/config.txt (Stored with Git LFS)

Binary file not shown.

BIN
x64/Release/config.txt (Stored with Git LFS)

Binary file not shown.