From ff805d334f321ed1d414f3d4d5fbbf743d1b6766 Mon Sep 17 00:00:00 2001 From: Malin E Date: Fri, 26 May 2023 16:34:38 +0200 Subject: [PATCH] Fix issue with atmosphere rendering on top of spheres --- modules/base/rendering/renderablesphere.cpp | 12 ++++++++---- modules/base/rendering/renderablesphere.h | 4 ++-- modules/base/shaders/sphere_fs.glsl | 3 ++- modules/base/shaders/sphere_vs.glsl | 6 +++++- modules/video/include/renderablevideosphere.h | 4 ++-- modules/video/src/renderablevideosphere.cpp | 12 ++++++++---- 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/modules/base/rendering/renderablesphere.cpp b/modules/base/rendering/renderablesphere.cpp index 90896dd370..640a768048 100644 --- a/modules/base/rendering/renderablesphere.cpp +++ b/modules/base/rendering/renderablesphere.cpp @@ -41,9 +41,9 @@ #include namespace { - constexpr std::array UniformNames = { - "opacity", "modelViewProjection", "modelViewRotation", "colorTexture", - "mirrorTexture" + constexpr std::array UniformNames = { + "opacity", "modelViewProjection", "modelViewTransform", "modelViewRotation", + "colorTexture", "mirrorTexture" }; enum class Orientation : int { @@ -275,6 +275,10 @@ void RenderableSphere::render(const RenderData& data, RendererTasks&) { glm::mat4(data.camera.combinedViewMatrix() * modelTransform); _shader->setUniform(_uniformCache.modelViewProjection, modelViewProjection); + const glm::dmat4 modelViewTransform = + data.camera.combinedViewMatrix() * modelTransform; + _shader->setUniform(_uniformCache.modelViewTransform, glm::mat4(modelViewTransform)); + glm::mat3 modelViewRotation = glm::mat3( glm::dmat3(data.camera.viewRotationMatrix()) * modelRotation ); @@ -342,7 +346,7 @@ void RenderableSphere::render(const RenderData& data, RendererTasks&) { } _shader->setUniform(_uniformCache.opacity, adjustedOpacity); - _shader->setUniform(_uniformCache._mirrorTexture, _mirrorTexture.value()); + _shader->setUniform(_uniformCache.mirrorTexture, _mirrorTexture.value()); ghoul::opengl::TextureUnit unit; unit.activate(); diff --git a/modules/base/rendering/renderablesphere.h b/modules/base/rendering/renderablesphere.h index c95c7a38fe..1cf492b135 100644 --- a/modules/base/rendering/renderablesphere.h +++ b/modules/base/rendering/renderablesphere.h @@ -84,8 +84,8 @@ private: std::unique_ptr _sphere; - UniformCache(opacity, modelViewProjection, modelViewRotation, colorTexture, - _mirrorTexture) _uniformCache; + UniformCache(opacity, modelViewProjection, modelViewTransform, modelViewRotation, + colorTexture, mirrorTexture) _uniformCache; bool _sphereIsDirty = false; }; diff --git a/modules/base/shaders/sphere_fs.glsl b/modules/base/shaders/sphere_fs.glsl index b0b6bf9a67..1a02027ebd 100644 --- a/modules/base/shaders/sphere_fs.glsl +++ b/modules/base/shaders/sphere_fs.glsl @@ -27,6 +27,7 @@ in vec4 vs_position; in vec2 vs_textureCoords; in vec3 vs_normal; +in float vs_screenSpaceDepth; uniform sampler2D colorTexture; uniform float opacity; @@ -43,7 +44,7 @@ Fragment getFragment() { frag.color = texture(colorTexture, texCoord); frag.color.a *= opacity; - frag.depth = vs_position.w; + frag.depth = vs_screenSpaceDepth; // G-Buffer frag.gPosition = vs_position; diff --git a/modules/base/shaders/sphere_vs.glsl b/modules/base/shaders/sphere_vs.glsl index 827e3233aa..9180214c74 100644 --- a/modules/base/shaders/sphere_vs.glsl +++ b/modules/base/shaders/sphere_vs.glsl @@ -30,8 +30,10 @@ layout(location = 1) in vec2 in_textureCoords; out vec2 vs_textureCoords; out vec4 vs_position; out vec3 vs_normal; +out float vs_screenSpaceDepth; uniform mat4 modelViewProjection; +uniform mat4 modelViewTransform; uniform mat3 modelViewRotation; @@ -40,8 +42,10 @@ void main() { vs_textureCoords = in_textureCoords; vec4 position = modelViewProjection * vec4(in_position.xyz, 1.0); - vs_position = position; + vs_position = modelViewTransform * vec4(in_position.xyz, 1.0); // Set z to 0 to disable near/far-plane clipping gl_Position = vec4(position.xy, 0.0, position.w); + + vs_screenSpaceDepth = position.w; } diff --git a/modules/video/include/renderablevideosphere.h b/modules/video/include/renderablevideosphere.h index 99dd339be2..e1a9c8723f 100644 --- a/modules/video/include/renderablevideosphere.h +++ b/modules/video/include/renderablevideosphere.h @@ -83,8 +83,8 @@ private: std::unique_ptr _sphere; - UniformCache(opacity, modelViewProjection, modelViewRotation, colorTexture, - _mirrorTexture) _uniformCache; + UniformCache(opacity, modelViewProjection, modelViewTransform, modelViewRotation, + colorTexture, mirrorTexture) _uniformCache; bool _sphereIsDirty = false; }; diff --git a/modules/video/src/renderablevideosphere.cpp b/modules/video/src/renderablevideosphere.cpp index 9bb108ce2f..c5fc0f2291 100644 --- a/modules/video/src/renderablevideosphere.cpp +++ b/modules/video/src/renderablevideosphere.cpp @@ -41,9 +41,9 @@ #include namespace { - constexpr std::array UniformNames = { - "opacity", "modelViewProjection", "modelViewRotation", "colorTexture", - "mirrorTexture" + constexpr std::array UniformNames = { + "opacity", "modelViewProjection", "modelViewTransform", "modelViewRotation", + "colorTexture", "mirrorTexture" }; enum class Orientation : int { @@ -258,6 +258,10 @@ void RenderableVideoSphere::render(const RenderData& data, RendererTasks&) { glm::mat4(data.camera.combinedViewMatrix() * modelTransform); _shader->setUniform(_uniformCache.modelViewProjection, modelViewProjection); + const glm::dmat4 modelViewTransform = + data.camera.combinedViewMatrix() * modelTransform; + _shader->setUniform(_uniformCache.modelViewTransform, glm::mat4(modelViewTransform)); + glm::mat3 modelViewRotation = glm::mat3( glm::dmat3(data.camera.viewRotationMatrix()) * modelRotation ); @@ -325,7 +329,7 @@ void RenderableVideoSphere::render(const RenderData& data, RendererTasks&) { } _shader->setUniform(_uniformCache.opacity, adjustedOpacity); - _shader->setUniform(_uniformCache._mirrorTexture, _mirrorTexture.value()); + _shader->setUniform(_uniformCache.mirrorTexture, _mirrorTexture.value()); ghoul::opengl::TextureUnit unit; unit.activate();