Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fog as rendering options. #1672

Merged
merged 7 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions avogadro/rendering/glrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void GLRenderer::render()
glDisable(GL_BLEND);
m_scene.rootNode().accept(visitor);
m_solidPipeline.end();
m_solidPipeline.adjustOffset(m_camera);

// Setup for opaque geometry
visitor.setRenderPass(OpaquePass);
Expand Down
40 changes: 35 additions & 5 deletions avogadro/rendering/solid_first_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,29 @@ varying vec2 UV;

// RGB rendered texture
uniform sampler2D inRGBTex;

// RGB color for applying fog
uniform float fogR;
uniform float fogG;
uniform float fogB;
uniform float offset;

// 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;
ghutchis marked this conversation as resolved.
Show resolved Hide resolved
// Shadow strength for SSAO
uniform float inAoStrength;
// Fog strength
uniform float inFogStrength;
// 1.0 if enabled, 0.0 if disabled
uniform float inEdStrength;
// offset for zoom-in and zoom-out
uniform float uoffset;
// position for other molecules.
uniform float inFogPosition;
// Rendering surface dimensions, in pixels
uniform float width, height;

Expand Down Expand Up @@ -66,6 +81,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, uoffset * inFogPosition/10.0)
) + inFogStrength / 100.0;
return finalColor;
}

const vec2 SSAOkernel[16] = vec2[16](
vec2(0.072170, 0.081556),
vec2(-0.035126, 0.056701),
Expand Down Expand Up @@ -113,13 +137,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){
ghutchis marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
36 changes: 32 additions & 4 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 All @@ -15,6 +16,8 @@

#include <iostream>

#include<cmath>
ghutchis marked this conversation as resolved.
Show resolved Hide resolved

namespace Avogadro::Rendering {

class SolidPipeline::Private
Expand Down Expand Up @@ -85,9 +88,9 @@ 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)
: m_pixelRatio(1.0f), m_aoEnabled(true), m_aoStrength(1.0f),
m_fogStrength(1.0f), m_fogPosition(1.0), m_fogEnabled(true), m_edEnabled(true), m_edStrength(1.0f),
m_width(0), m_height(0), d(new Private), m_backgroundColor(0,0,0,0)
{
}

Expand Down Expand Up @@ -157,12 +160,37 @@ void SolidPipeline::end()
d->depthTexture, m_width, m_height);
d->firstStageShaders.setUniformValue("inAoEnabled", m_aoEnabled ? 1.0f : 0.0f);
d->firstStageShaders.setUniformValue("inAoStrength", m_aoStrength);
d->firstStageShaders.setUniformValue("inFogStrength", (m_fogStrength));
d->firstStageShaders.setUniformValue("inEdStrength", m_edStrength);
d->firstStageShaders.setUniformValue("inFogEnabled", m_fogEnabled ? 1.0f : 0.0f);
ghutchis marked this conversation as resolved.
Show resolved Hide resolved
d->firstStageShaders.setUniformValue("inFogPosition", m_fogPosition);
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);

// std::cout<<m_fogStrength<<std::endl;
glDisableVertexAttribArray(0);
}

// TODO: More precise calculations needed
void SolidPipeline::adjustOffset(const Camera& cam) {

//since it's a workaround, I feel like I can optimize it more.
ghutchis marked this conversation as resolved.
Show resolved Hide resolved
Eigen::Matrix4f projectView = cam.projection().matrix();

float project = ((((5000 + projectView(2,3) * 1000)/6) + 55) * 100);

float offSet = 0.000102337 * pow(project,2) - 3.84689 * project + 36182.2;
if(project >= 21018.106 && project<21595.588){
offSet = 2.63129 * project - 54768.4;
}
else if(project >= 21595.588 ){
offSet = 9.952 * project - 212865;
}
d->firstStageShaders.setUniformValue("uoffset", offSet);

}

void SolidPipeline::resize(int width, int height)
{
m_width = width * m_pixelRatio;
Expand Down
32 changes: 32 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 All @@ -31,6 +33,7 @@ class SolidPipeline
*/
void begin();

void adjustOffset(const Camera& camera);
/**
* @brief End solid geometry rendering and apply screen-space shaders.
*/
Expand All @@ -52,12 +55,36 @@ 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 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.
*/
float getAoStrength() { return m_aoStrength; }
void setAoStrength(float strength) { m_aoStrength = strength; }

/**
* @brief Get or set fog strength.
*/
float getFogStrength() { return m_fogStrength; }
void setFogStrength(float strength) { m_fogStrength = strength; }

/**
* @brief Get or set fog position
*/
float getFogPosition(){ return m_fogPosition;}
void setFogPosition(float position) { m_fogPosition = position; }

/**
* @brief Get or set whether Edge Detection is enabled.
*/
Expand All @@ -77,7 +104,12 @@ class SolidPipeline
private:
float m_pixelRatio;
bool m_aoEnabled;
float m_fogPosition;
Vector4ub m_backgroundColor;
Eigen::Affine3f modelView;
bool m_fogEnabled;
float m_aoStrength;
float m_fogStrength;
bool m_edEnabled;
float m_edStrength;
int m_width;
Expand Down
Loading