From f6eac4f0c7fe1ede7dafdc96a370ca0dbe3dfe4d Mon Sep 17 00:00:00 2001 From: Malin E Date: Tue, 21 Feb 2023 18:08:38 +0100 Subject: [PATCH] Take back manual depth test for the two pass model rendering --- .../openspace/rendering/framebufferrenderer.h | 44 ++++++++++++++++++- modules/base/rendering/renderablemodel.cpp | 30 ++++++++++++- modules/base/rendering/renderablemodel.h | 2 +- modules/base/shaders/model_fs.glsl | 13 ++++++ src/rendering/framebufferrenderer.cpp | 29 ++++++++++-- 5 files changed, 111 insertions(+), 7 deletions(-) diff --git a/include/openspace/rendering/framebufferrenderer.h b/include/openspace/rendering/framebufferrenderer.h index d9bbba53d8..32dd262d94 100644 --- a/include/openspace/rendering/framebufferrenderer.h +++ b/include/openspace/rendering/framebufferrenderer.h @@ -59,7 +59,9 @@ class FramebufferRenderer final : public RaycasterListener, public Deferredcaste public: virtual ~FramebufferRenderer() override = default; - // Functions to access and reuse some of the existing textures + //============================// + //===== Reuse textures =====// + //============================// /** * Gives access to the currently NOT used pingPongTexture. This texture is available * for all RenderBins. However, it cannot be used at the same time as the Deferred @@ -100,6 +102,46 @@ public: */ GLuint additionalDepthTexture(); + //=============================// + //===== Access G-buffer =====// + //=============================// + // Functions to access the G-buffer textures + /** + * Gives access to the color texture of the G-buffer. NOTE: This texture is used for + * the majority of rendering the scene and might be already in use. Use CAUTION when + * using this function. The size of the texture is the resolution of the viewport. + * + * \return GLuint identifier of the color texture of the G-buffer + */ + GLuint gBufferColorTexture(); + + /** + * Gives access to the position texture of the G-buffer. NOTE: This texture is used for + * the majority of rendering the scene and might be already in use. Use CAUTION when + * using this function. The size of the texture is the resolution of the viewport. + * + * \return GLuint identifier of the position texture of the G-buffer + */ + GLuint gBufferPositionTexture(); + + /** + * Gives access to the normal texture of the G-buffer. NOTE: This texture is used for + * the majority of rendering the scene and might be already in use. Use CAUTION when + * using this function. The size of the texture is the resolution of the viewport. + * + * \return GLuint identifier of the normal texture of the G-buffer + */ + GLuint gBufferNormalTexture(); + + /** + * Gives access to the depth texture of the G-buffer. NOTE: This texture is used for + * the majority of rendering the scene and might be already in use. Use CAUTION when + * using this function. The size of the texture is the resolution of the viewport. + * + * \return GLuint identifier of the depth texture of the G-buffer + */ + GLuint gBufferDepthTexture(); + void initialize(); void deinitialize(); diff --git a/modules/base/rendering/renderablemodel.cpp b/modules/base/rendering/renderablemodel.cpp index 5a2c9f2ba0..e6ce87a0eb 100644 --- a/modules/base/rendering/renderablemodel.cpp +++ b/modules/base/rendering/renderablemodel.cpp @@ -71,11 +71,11 @@ namespace { GL_COLOR_ATTACHMENT2, }; - constexpr std::array UniformNames = { + constexpr std::array UniformNames = { "nLightSources", "lightDirectionsViewSpace", "lightIntensities", "modelViewTransform", "normalTransform", "projectionTransform", "performShading", "ambientIntensity", "diffuseIntensity", - "specularIntensity" + "specularIntensity", "performManualDepthTest", "gBufferDepthTexture" }; constexpr std::array UniformOpacityNames = { @@ -781,6 +781,12 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) { } if (!shouldRenderTwise) { + // Reset manual depth test + _program->setUniform( + _uniformCache.performManualDepthTest, + false + ); + _geometry->render(*_program); } else { @@ -806,6 +812,26 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) { glClearBufferfv(GL_COLOR, 1, glm::value_ptr(glm::vec4(0.f, 0.f, 0.f, 0.f))); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + // Use a manuel depth test to make the models aware of the rest of the envierment + _program->setUniform( + _uniformCache.performManualDepthTest, + true + ); + + // Bind the G-buffer depth texture for a manual depth test towards the rest + // of the scene + ghoul::opengl::TextureUnit gBufferDepthTextureUnit; + gBufferDepthTextureUnit.activate(); + glBindTexture( + GL_TEXTURE_2D, + global::renderEngine->renderer()->gBufferDepthTexture() + ); + _program->setUniform( + _uniformCache.gBufferDepthTexture, + gBufferDepthTextureUnit + ); + + // Render Pass 1 // Render all parts of the model into the new framebuffer without opacity _geometry->render(*_program); diff --git a/modules/base/rendering/renderablemodel.h b/modules/base/rendering/renderablemodel.h index d61d3b91e9..fc715201b0 100644 --- a/modules/base/rendering/renderablemodel.h +++ b/modules/base/rendering/renderablemodel.h @@ -106,7 +106,7 @@ private: UniformCache(nLightSources, lightDirectionsViewSpace, lightIntensities, modelViewTransform, normalTransform, projectionTransform, performShading, ambientIntensity, diffuseIntensity, - specularIntensity) _uniformCache; + specularIntensity, performManualDepthTest, gBufferDepthTexture) _uniformCache; std::vector> _lightSources; diff --git a/modules/base/shaders/model_fs.glsl b/modules/base/shaders/model_fs.glsl index 62655cbd5d..bd0ef01bd5 100644 --- a/modules/base/shaders/model_fs.glsl +++ b/modules/base/shaders/model_fs.glsl @@ -48,6 +48,8 @@ uniform vec4 color_specular; uniform int nLightSources; uniform vec3 lightDirectionsViewSpace[8]; uniform float lightIntensities[8]; +uniform bool performManualDepthTest = false; +uniform sampler2D gBufferDepthTexture; Fragment getFragment() { Fragment frag; @@ -56,6 +58,17 @@ Fragment getFragment() { frag.gNormal = vec4(vs_normalViewSpace, 0.0); frag.disableLDR2HDR = true; + if (performManualDepthTest) { + // Manual depth test + float gBufferDepth = + denormalizeFloat(texture(gBufferDepthTexture, viewportPixelCoord).x); + if (vs_screenSpaceDepth > gBufferDepth) { + frag.color = vec4(0.f); + frag.depth = gBufferDepth; + return frag; + } + } + // Render invisible mesh with flashy procedural material if (use_forced_color) { vec3 adjustedPos = floor(vs_positionCameraSpace.xyz * 3.0); diff --git a/src/rendering/framebufferrenderer.cpp b/src/rendering/framebufferrenderer.cpp index e12e7a55b8..6228ab603c 100644 --- a/src/rendering/framebufferrenderer.cpp +++ b/src/rendering/framebufferrenderer.cpp @@ -87,9 +87,9 @@ namespace { namespace openspace { -//=====================================// -//===== Reuse NOT used textures =====// -//=====================================// +//============================// +//===== Reuse textures =====// +//============================// // Gives access to the currently NOT used pingPongTexture GLuint FramebufferRenderer::additionalColorTexture1() { int unusedPingPongIndex = _pingPongIndex == 0 ? 1 : 0; @@ -111,6 +111,29 @@ GLuint FramebufferRenderer::additionalDepthTexture() { return _exitDepthTexture; } +//=============================// +//===== Access G-buffer =====// +//=============================// +// / Gives access to the color texture of the G-buffer +GLuint FramebufferRenderer::gBufferColorTexture() { + return _gBuffers.colorTexture; +} + +// Gives access to the position texture of the G-buffer +GLuint FramebufferRenderer::gBufferPositionTexture() { + return _gBuffers.positionTexture; +} + +// Gives access to the normal texture of the G-buffer +GLuint FramebufferRenderer::gBufferNormalTexture() { + return _gBuffers.normalTexture; +} + +// Gives access to the depth texture of the G-buffer +GLuint FramebufferRenderer::gBufferDepthTexture() { + return _gBuffers.depthTexture; +} + void FramebufferRenderer::initialize() { ZoneScoped TracyGpuZone("Rendering initialize");