diff --git a/examples/DeferredShading/main.cpp b/examples/DeferredShading/main.cpp index 6bee921fc..3175d43d9 100644 --- a/examples/DeferredShading/main.cpp +++ b/examples/DeferredShading/main.cpp @@ -239,6 +239,7 @@ int main(int argc, char* argv[]) Nz::ViewerInstance viewerInstance; viewerInstance.UpdateTargetSize(Nz::Vector2f(windowSize)); viewerInstance.UpdateProjViewMatrices(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(windowSize.x) / windowSize.y, 0.1f, 1000.f), Nz::Matrix4f::Translate(Nz::Vector3f::Up() * 1)); + viewerInstance.UpdateNearFarPlanes(0.1f, 1000.f); Nz::WorldInstance modelInstance1; modelInstance1.UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Left() + Nz::Vector3f::Up())); diff --git a/examples/PhysicallyBasedRendering/main.cpp b/examples/PhysicallyBasedRendering/main.cpp index 4a748782a..0b0f17aa0 100644 --- a/examples/PhysicallyBasedRendering/main.cpp +++ b/examples/PhysicallyBasedRendering/main.cpp @@ -68,6 +68,7 @@ int main(int argc, char* argv[]) Nz::ViewerInstance& viewerInstance = camera.GetViewerInstance(); viewerInstance.UpdateTargetSize(Nz::Vector2f(window.GetSize())); viewerInstance.UpdateProjViewMatrices(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(windowSize.x) / windowSize.y, 0.1f, 1000.f), Nz::Matrix4f::Translate(Nz::Vector3f::Backward() * 1)); + viewerInstance.UpdateNearFarPlanes(0.1f, 1000.f); Nz::WorldInstancePtr modelInstance = std::make_shared(); modelInstance->UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Left())); diff --git a/include/Nazara/Graphics/Camera.inl b/include/Nazara/Graphics/Camera.inl index 204a85343..fe747811b 100644 --- a/include/Nazara/Graphics/Camera.inl +++ b/include/Nazara/Graphics/Camera.inl @@ -248,6 +248,8 @@ namespace Nz m_viewerInstance.UpdateProjectionMatrix(Matrix4f::Perspective(m_fov, m_aspectRatio, m_zNear, m_zFar)); break; } + + m_viewerInstance.UpdateNearFarPlanes(m_zNear, m_zFar); } inline void Camera::UpdateViewport() diff --git a/include/Nazara/Graphics/ViewerInstance.hpp b/include/Nazara/Graphics/ViewerInstance.hpp index a429dfca7..5b73681b4 100644 --- a/include/Nazara/Graphics/ViewerInstance.hpp +++ b/include/Nazara/Graphics/ViewerInstance.hpp @@ -30,9 +30,11 @@ namespace Nz ~ViewerInstance() = default; inline const Vector3f& GetEyePosition() const; + inline float GetFarPlane() const; inline const Matrix4f& GetInvProjectionMatrix() const; inline const Matrix4f& GetInvViewMatrix() const; inline const Matrix4f& GetInvViewProjMatrix() const; + inline float GetNearPlane() const; inline const Matrix4f& GetProjectionMatrix() const; inline const Vector2f& GetTargetSize() const; inline const Matrix4f& GetViewMatrix() const; @@ -43,6 +45,7 @@ namespace Nz void OnTransfer(RenderFrame& renderFrame, CommandBufferBuilder& builder) override; inline void UpdateEyePosition(const Vector3f& eyePosition); + inline void UpdateNearFarPlanes(float nearPlane, float farPlane); inline void UpdateProjectionMatrix(const Matrix4f& projectionMatrix); inline void UpdateProjectionMatrix(const Matrix4f& projectionMatrix, const Matrix4f& invProjectionMatrix); inline void UpdateProjViewMatrices(const Matrix4f& projectionMatrix, const Matrix4f& viewMatrix); @@ -68,6 +71,8 @@ namespace Nz Vector2f m_targetSize; Vector3f m_eyePosition; bool m_dataInvalidated; + float m_farPlane; + float m_nearPlane; }; } diff --git a/include/Nazara/Graphics/ViewerInstance.inl b/include/Nazara/Graphics/ViewerInstance.inl index d55e0f979..620c89af6 100644 --- a/include/Nazara/Graphics/ViewerInstance.inl +++ b/include/Nazara/Graphics/ViewerInstance.inl @@ -12,6 +12,11 @@ namespace Nz return m_eyePosition; } + inline float ViewerInstance::GetFarPlane() const + { + return m_farPlane; + } + inline const Matrix4f& ViewerInstance::GetInvProjectionMatrix() const { return m_invProjectionMatrix; @@ -27,6 +32,11 @@ namespace Nz return m_invViewProjMatrix; } + inline float ViewerInstance::GetNearPlane() const + { + return m_nearPlane; + } + inline const Matrix4f& ViewerInstance::GetProjectionMatrix() const { return m_projectionMatrix; @@ -64,6 +74,14 @@ namespace Nz InvalidateData(); } + inline void ViewerInstance::UpdateNearFarPlanes(float nearPlane, float farPlane) + { + m_farPlane = farPlane; + m_nearPlane = nearPlane; + + InvalidateData(); + } + inline void ViewerInstance::UpdateProjectionMatrix(const Matrix4f& projectionMatrix) { m_projectionMatrix = projectionMatrix; diff --git a/src/Nazara/Graphics/PointLightShadowData.cpp b/src/Nazara/Graphics/PointLightShadowData.cpp index b0c5c6944..5a1f7a7d9 100644 --- a/src/Nazara/Graphics/PointLightShadowData.cpp +++ b/src/Nazara/Graphics/PointLightShadowData.cpp @@ -63,7 +63,9 @@ namespace Nz std::size_t shadowPassIndex = Graphics::Instance()->GetMaterialPassRegistry().GetPassIndex("ShadowPass"); - Matrix4f projectionMatrix = Matrix4f::Perspective(RadianAnglef(HalfPi), 1.f, 0.01f, m_light.GetRadius()); + constexpr float zNear = 0.01f; + + Matrix4f projectionMatrix = Matrix4f::Perspective(RadianAnglef(HalfPi), 1.f, zNear, m_light.GetRadius()); UInt32 shadowMapSize = light.GetShadowMapSize(); for (std::size_t i = 0; i < m_directions.size(); ++i) @@ -77,6 +79,7 @@ namespace Nz viewerInstance.UpdateEyePosition(m_light.GetPosition()); viewerInstance.UpdateProjectionMatrix(projectionMatrix); viewerInstance.UpdateViewMatrix(Matrix4f::TransformInverse(m_light.GetPosition(), s_dirRotations[i])); + viewerInstance.UpdateNearFarPlanes(zNear, m_light.GetRadius()); m_pipeline.QueueTransfer(&viewerInstance); diff --git a/src/Nazara/Graphics/Resources/Shaders/Modules/Engine/ViewerData.nzsl b/src/Nazara/Graphics/Resources/Shaders/Modules/Engine/ViewerData.nzsl index 5cbccace8..b6f93c185 100644 --- a/src/Nazara/Graphics/Resources/Shaders/Modules/Engine/ViewerData.nzsl +++ b/src/Nazara/Graphics/Resources/Shaders/Modules/Engine/ViewerData.nzsl @@ -13,5 +13,7 @@ struct ViewerData invViewProjMatrix: mat4[f32], renderTargetSize: vec2[f32], invRenderTargetSize: vec2[f32], - eyePosition: vec3[f32] + eyePosition: vec3[f32], + nearPlane: f32, + farPlane: f32 } diff --git a/src/Nazara/Graphics/SpotLightShadowData.cpp b/src/Nazara/Graphics/SpotLightShadowData.cpp index bea80c191..0332909b5 100644 --- a/src/Nazara/Graphics/SpotLightShadowData.cpp +++ b/src/Nazara/Graphics/SpotLightShadowData.cpp @@ -20,8 +20,11 @@ namespace Nz m_viewer.UpdateRenderMask(0xFFFFFFFF); m_viewer.UpdateViewport(Recti(0, 0, SafeCast(shadowMapSize), SafeCast(shadowMapSize))); + constexpr float zNear = 0.01f; + ViewerInstance& viewerInstance = m_viewer.GetViewerInstance(); - viewerInstance.UpdateProjectionMatrix(Matrix4f::Perspective(m_light.GetOuterAngle() * 2.f, 1.f, 0.01f, m_light.GetRadius())); + viewerInstance.UpdateProjectionMatrix(Matrix4f::Perspective(m_light.GetOuterAngle() * 2.f, 1.f, zNear, m_light.GetRadius())); + viewerInstance.UpdateNearFarPlanes(zNear, m_light.GetRadius()); m_onLightShadowMapSettingChange.Connect(m_light.OnLightShadowMapSettingChange, [this](Light* /*light*/, PixelFormat /*newPixelFormat*/, UInt32 newSize) { diff --git a/tests/GraphicsTest/main.cpp b/tests/GraphicsTest/main.cpp index 2fcbd1213..703a1646f 100644 --- a/tests/GraphicsTest/main.cpp +++ b/tests/GraphicsTest/main.cpp @@ -82,6 +82,7 @@ int main() Nz::ViewerInstance& viewerInstance = camera.GetViewerInstance(); viewerInstance.UpdateTargetSize(Nz::Vector2f(window.GetSize())); viewerInstance.UpdateProjViewMatrices(Nz::Matrix4f::Perspective(Nz::DegreeAnglef(70.f), float(windowSize.x) / windowSize.y, 0.1f, 1000.f), Nz::Matrix4f::Translate(Nz::Vector3f::Backward() * 1)); + viewerInstance.UpdateNearFarPlanes(0.1f, 1000.f); Nz::WorldInstancePtr modelInstance = std::make_shared(); modelInstance->UpdateWorldMatrix(Nz::Matrix4f::Translate(Nz::Vector3f::Forward() * 2 + Nz::Vector3f::Left()));