diff --git a/include/Nazara/Graphics/SkyboxBackground.hpp b/include/Nazara/Graphics/SkyboxBackground.hpp index b861dbbb8..427548dc0 100644 --- a/include/Nazara/Graphics/SkyboxBackground.hpp +++ b/include/Nazara/Graphics/SkyboxBackground.hpp @@ -32,11 +32,15 @@ namespace Nz void Draw(const AbstractViewer* viewer) const; - BackgroundType GetBackgroundType() const; + BackgroundType GetBackgroundType() const override; + inline const Vector3f& GetMovementOffset() const; + inline float GetMovementScale() const; inline const TextureRef& GetTexture() const; inline TextureSampler& GetTextureSampler(); inline const TextureSampler& GetTextureSampler() const; + inline void SetMovementOffset(const Vector3f& offset); + inline void SetMovementScale(float scale); inline void SetTexture(TextureRef cubemapTexture); inline void SetTextureSampler(const TextureSampler& sampler); @@ -48,6 +52,8 @@ namespace Nz TextureRef m_texture; TextureSampler m_sampler; + Vector3f m_movementOffset; + float m_movementScale; }; } diff --git a/include/Nazara/Graphics/SkyboxBackground.inl b/include/Nazara/Graphics/SkyboxBackground.inl index 51b3bf123..8edb694c9 100644 --- a/include/Nazara/Graphics/SkyboxBackground.inl +++ b/include/Nazara/Graphics/SkyboxBackground.inl @@ -7,6 +7,16 @@ namespace Nz { + inline const Vector3f& Nz::SkyboxBackground::GetMovementOffset() const + { + return m_movementOffset; + } + + inline float SkyboxBackground::GetMovementScale() const + { + return m_movementScale; + } + inline const TextureRef& SkyboxBackground::GetTexture() const { return m_texture; @@ -22,6 +32,20 @@ namespace Nz return m_sampler; } + inline void SkyboxBackground::SetMovementOffset(const Vector3f& offset) + { + NazaraAssert(std::isfinite(offset.x) && std::isfinite(offset.y) && std::isfinite(offset.z), "Offset must be a finite vector"); + + m_movementOffset = offset; + } + + inline void SkyboxBackground::SetMovementScale(float scale) + { + NazaraAssert(std::isfinite(scale), "Scale must be a finite value"); + + m_movementScale = scale; + } + inline void SkyboxBackground::SetTexture(TextureRef cubemapTexture) { NazaraAssert(!cubemapTexture || cubemapTexture->IsValid(), "Invalid texture"); diff --git a/src/Nazara/Graphics/SkyboxBackground.cpp b/src/Nazara/Graphics/SkyboxBackground.cpp index 1e8678c20..a70a9dd2d 100644 --- a/src/Nazara/Graphics/SkyboxBackground.cpp +++ b/src/Nazara/Graphics/SkyboxBackground.cpp @@ -22,9 +22,11 @@ namespace Nz static VertexBufferRef s_vertexBuffer; } - SkyboxBackground::SkyboxBackground(TextureRef cubemapTexture) + SkyboxBackground::SkyboxBackground(TextureRef cubemapTexture) : + m_movementOffset(Vector3f::Zero()), + m_movementScale(0.f) { - m_sampler.SetWrapMode(SamplerWrap_Clamp); // Nécessaire pour ne pas voir les côtés + m_sampler.SetWrapMode(SamplerWrap_Clamp); // We don't want to see any beam SetTexture(std::move(cubemapTexture)); } @@ -34,9 +36,24 @@ namespace Nz Matrix4f skyboxMatrix(viewer->GetViewMatrix()); skyboxMatrix.SetTranslation(Vector3f::Zero()); + float zNear = viewer->GetZNear(); + + constexpr float movementLimit = 0.05f; + + Vector3f offset = (viewer->GetEyePosition() - m_movementOffset) * -m_movementScale; + offset.x = Clamp(offset.x, -movementLimit, movementLimit); + offset.y = Clamp(offset.y, -movementLimit, movementLimit); + offset.z = Clamp(offset.z, -movementLimit, movementLimit); + offset *= zNear; + + Matrix4f world; + world.MakeIdentity(); + world.SetScale(Vector3f(zNear)); + world.SetTranslation(offset); + Renderer::SetIndexBuffer(s_indexBuffer); Renderer::SetMatrix(MatrixType_View, skyboxMatrix); - Renderer::SetMatrix(MatrixType_World, Matrix4f::Scale(Vector3f(viewer->GetZNear()))); + Renderer::SetMatrix(MatrixType_World, world); Renderer::SetRenderStates(s_renderStates); Renderer::SetShader(s_shader); Renderer::SetTexture(0, m_texture);