From c1c7597f5a0e8ec6c50937725bf29670737449cd Mon Sep 17 00:00:00 2001 From: Perminder Date: Sat, 1 Jun 2024 05:02:53 +0530 Subject: [PATCH] fog-added Signed-off-by: Perminder --- avogadro/rendering/solid_first_fs.glsl | 33 ++++++++++++++++++++++---- avogadro/rendering/solidpipeline.cpp | 14 +++++++++-- avogadro/rendering/solidpipeline.h | 22 +++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/avogadro/rendering/solid_first_fs.glsl b/avogadro/rendering/solid_first_fs.glsl index b95274fdc6..76570075b5 100644 --- a/avogadro/rendering/solid_first_fs.glsl +++ b/avogadro/rendering/solid_first_fs.glsl @@ -21,10 +21,18 @@ varying vec2 UV; // RGB rendered texture uniform sampler2D inRGBTex; + +// RGB color for applying fog +uniform float fogR; +uniform float fogG; +uniform float fogB; + // Depth rendered texture uniform sampler2D inDepthTex; // 1.0 if enabled, 0.0 if disabled uniform float inAoEnabled; +// 1.0 if enabled, 0.0 if disabled +uniform float inFogEnabled; // Shadow strength for SSAO uniform float inAoStrength; // 1.0 if enabled, 0.0 if disabled @@ -66,6 +74,15 @@ float lerp(float a, float b, float f) return a + f * (b - a); } +vec4 applyFog(vec2 texCoord) { + vec4 finalColor = mix( + texture2D(inRGBTex, texCoord), + vec4(vec3(fogR, fogG, fogB), 1.), + pow(texture2D(inDepthTex, texCoord.xy).r, 300.) + ); +return finalColor; +} + const vec2 SSAOkernel[16] = vec2[16]( vec2(0.072170, 0.081556), vec2(-0.035126, 0.056701), @@ -113,13 +130,19 @@ float computeEdgeLuminosity(vec3 normal) return max(0.0, pow(normal.z - 0.1, 1.0 / 3.0)); } -void main() -{ +void main() { float luminosity = 1.0; luminosity *= max(1.2 * (1.0 - inAoEnabled), computeSSAOLuminosity(getNormalNear(UV))); luminosity *= max(1.0 - inEdStrength, computeEdgeLuminosity(getNormalAt(UV))); - vec4 color = texture2D(inRGBTex, UV); - gl_FragColor = vec4(color.xyz * luminosity, color.w); + if(inFogEnabled == 0.0){ + gl_FragColor = vec4(color.xyz * luminosity, color.w); + } + else { + // Apply fog to the color texture + vec4 foggedColor = applyFog(UV); + vec4 fogColor = vec4(luminosity * foggedColor.xyz, foggedColor.w); + gl_FragColor = fogColor; + } gl_FragDepth = texture2D(inDepthTex, UV).x; -} +} \ No newline at end of file diff --git a/avogadro/rendering/solidpipeline.cpp b/avogadro/rendering/solidpipeline.cpp index a313d99b82..1fcf18e2e0 100644 --- a/avogadro/rendering/solidpipeline.cpp +++ b/avogadro/rendering/solidpipeline.cpp @@ -7,6 +7,7 @@ #include "avogadrogl.h" #include "shader.h" +#include "camera.h" #include "shaderprogram.h" #include "solid_vs.h" @@ -87,7 +88,7 @@ void initializeFramebuffer(GLuint* outFBO, GLuint* texRGB, GLuint* texDepth) SolidPipeline::SolidPipeline() : m_pixelRatio(1.0f), m_aoEnabled(true), m_aoStrength(1.0f), m_edEnabled(true), m_edStrength(1.0f), m_width(0), m_height(0), - d(new Private) + d(new Private), m_backgroundColor(0,0,0,0) { } @@ -158,8 +159,17 @@ void SolidPipeline::end() d->firstStageShaders.setUniformValue("inAoEnabled", m_aoEnabled ? 1.0f : 0.0f); d->firstStageShaders.setUniformValue("inAoStrength", m_aoStrength); d->firstStageShaders.setUniformValue("inEdStrength", m_edStrength); + d->firstStageShaders.setUniformValue("inFogEnabled", m_fogEnabled ? 1.0f : 0.0f); + d->firstStageShaders.setUniformValue("fogR", (m_backgroundColor[0])/255.0f); + d->firstStageShaders.setUniformValue("fogG", (m_backgroundColor[1])/255.0f); + d->firstStageShaders.setUniformValue("fogB", (m_backgroundColor[2])/255.0f); glDrawArrays(GL_TRIANGLES, 0, 6); - + + // TODO: setting modelView uniform. WIP + Camera camera; + camera.setModelView(modelView); + modelView = camera.modelView(); + // std::cout << modelView.matrix(); glDisableVertexAttribArray(0); } diff --git a/avogadro/rendering/solidpipeline.h b/avogadro/rendering/solidpipeline.h index 8c982bdc2d..bc8de62c43 100644 --- a/avogadro/rendering/solidpipeline.h +++ b/avogadro/rendering/solidpipeline.h @@ -6,6 +6,8 @@ #ifndef AVOGADRO_RENDERING_SOLIDPIPELINE_H #define AVOGADRO_RENDERING_SOLIDPIPELINE_H +#include "camera.h" + namespace Avogadro { namespace Rendering { @@ -52,6 +54,23 @@ class SolidPipeline bool getAoEnabled() { return m_aoEnabled; } void setAoEnabled(bool enabled) { m_aoEnabled = enabled; } + /** + * @brief Get or set whether Fog is enabled. + */ + bool getFogEnabled() { return m_fogEnabled; } + void setFogEnabled(bool enabled) { m_fogEnabled = enabled; } + + /** + * @brief Set the modelViewMatrix to its updated one. + */ + void setModelViewMatrix(const Eigen::Affine3f value) { modelView = value; } + + /** + * @brief Set Background Color to it's current value. + */ + Vector4ub backgroundColor() const { return m_backgroundColor; } + void setBackgroundColor(const Vector4ub& c) { m_backgroundColor = c; } + /** * @brief Get or set shadow strength for Ambient Occlusion. */ @@ -77,6 +96,9 @@ class SolidPipeline private: float m_pixelRatio; bool m_aoEnabled; + Vector4ub m_backgroundColor; + Eigen::Affine3f modelView; + bool m_fogEnabled; float m_aoStrength; bool m_edEnabled; float m_edStrength;