Skip to content

Commit

Permalink
fog-added
Browse files Browse the repository at this point in the history
  • Loading branch information
perminder-17 committed May 31, 2024
1 parent dfbd6f6 commit fa3f1d8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
33 changes: 28 additions & 5 deletions avogadro/rendering/solid_first_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
}
}
14 changes: 12 additions & 2 deletions avogadro/rendering/solidpipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "avogadrogl.h"
#include "shader.h"
#include "camera.h"
#include "shaderprogram.h"

#include "solid_vs.h"
Expand Down Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -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);
}

Expand Down
22 changes: 22 additions & 0 deletions avogadro/rendering/solidpipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef AVOGADRO_RENDERING_SOLIDPIPELINE_H
#define AVOGADRO_RENDERING_SOLIDPIPELINE_H

#include "camera.h"

namespace Avogadro {
namespace Rendering {

Expand Down Expand Up @@ -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.
*/
Expand All @@ -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;
Expand Down

0 comments on commit fa3f1d8

Please sign in to comment.