Skip to content

Commit

Permalink
SSAO (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
fszewczyk authored Jan 30, 2025
1 parent 40f3858 commit f0dc569
Show file tree
Hide file tree
Showing 22 changed files with 394 additions and 67 deletions.
4 changes: 2 additions & 2 deletions resources/shaders/fragment/depth.glsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#version 330 core

in float FragDepth;
in float VertexDepth;

out float FragValue;

void main() {
FragValue = FragDepth;
FragValue = VertexDepth;
}
2 changes: 1 addition & 1 deletion resources/shaders/fragment/distance.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ in vec3 FragPos;
out float FragValue;

void main() {
FragValue = length(FragPos);// / 30.0;
FragValue = length(FragPos);
}
9 changes: 9 additions & 0 deletions resources/shaders/fragment/normal.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 330 core

in vec3 Normal;

out vec3 NormalOut;

void main() {
NormalOut = normalize(Normal);
}
47 changes: 47 additions & 0 deletions resources/shaders/fragment/ssao.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#version 330 core
in vec2 TexCoords;

uniform sampler2D depthTexture; // View-space depth texture
uniform sampler2D normalTexture; // View-space normal texture

uniform mat4 projection;
uniform vec3 samples[128]; // SSAO Kernel

out float SSAO;

void main() {
vec3 fragViewPos = texture(depthTexture, TexCoords).xyz; // View-space position
float fragViewDepth = -fragViewPos.z;
vec3 normal = normalize(texture(normalTexture, TexCoords).xyz);

float occlusion = 0.0;
float radius = 0.5; // Sample radius
float bias = 0.01;

for (int i = 0; i < 128; ++i) {
vec3 randomVec = vec3(samples[i].y, samples[i].z, samples[i].x);
vec3 tangent = normalize(normalize(randomVec) - normal * dot(randomVec, normal));
vec3 bitangent = cross(normal, tangent);
mat3 TBN = mat3(tangent, bitangent, normal); // Create tangent-space matrix

vec3 samplePos = TBN * samples[i]; // Transform to view space
samplePos = fragViewPos + samplePos * radius; // Move sample position

vec4 offset = vec4(samplePos, 1.0);
offset = projection * offset;
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5 + 0.5;

float offsetDepth = -samplePos.z;
float newlySampledDepth = -texture(depthTexture, offset.xy).z;

if(newlySampledDepth + bias < offsetDepth)
{
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(newlySampledDepth - fragViewDepth));
occlusion += rangeCheck;
}
}

occlusion = 1.0 - (occlusion / 128.0); // Normalize occlusion value
SSAO = occlusion;
}
11 changes: 10 additions & 1 deletion resources/shaders/fragment/uber.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ out vec4 FragColor;
in vec3 FragPos;
in vec3 Normal;

uniform vec2 viewportSize;
uniform vec3 viewPos; // Camera position

// ******** LIGHT DATA ********
Expand Down Expand Up @@ -47,6 +48,7 @@ uniform SpotLight spotLights[SPOT_LIGHT_MAX_NUM];
uniform sampler2D spotLightsShadowMap[SPOT_LIGHT_MAX_NUM];

uniform vec3 ambientLight;
uniform sampler2D ssao;

// ******** MATERIAL DATA ********
struct Material {
Expand Down Expand Up @@ -106,6 +108,12 @@ vec3 calculateAmbient() {
return ambientLight * material.color;
}

float getAmbientOcclusion()
{
vec2 texCoords = gl_FragCoord.xy / viewportSize;
return texture(ssao, texCoords).r;
}

float calculateSpotFalloff(vec3 lightDir, vec3 spotlightDir, float innerCutoffCosine, float outerCutoffCosine) {
float theta = dot(lightDir, normalize(-spotlightDir));
float epsilon = innerCutoffCosine - outerCutoffCosine;
Expand All @@ -121,6 +129,7 @@ vec3 calculateSpotLights() {
float distanceToLight = length(lightToFrag);

float attenuation = 1.0 - (distanceToLight / spotLights[i].range);
attenuation = attenuation * attenuation;

if (attenuation > 0.0) {
vec3 lightDir = normalize(-lightToFrag);
Expand Down Expand Up @@ -305,7 +314,7 @@ vec3 calculateDirectionalLights() {

void main() {
// Lighting
vec3 color = calculateAmbient();
vec3 color = calculateAmbient() * getAmbientOcclusion();
color += calculatePointLights();
color += calculateDirectionalLights();
color += calculateSpotLights();
Expand Down
8 changes: 8 additions & 0 deletions resources/shaders/fragment/viewspace_normal.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 330 core
in vec3 Normal;

out vec3 OutNormal;

void main() {
OutNormal = normalize(Normal);
}
8 changes: 8 additions & 0 deletions resources/shaders/fragment/viewspace_position.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 330 core
in vec3 FragPos;

out vec3 OutFragPos;

void main() {
OutFragPos = FragPos;
}
14 changes: 14 additions & 0 deletions resources/shaders/vertex/depth.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 330 core

layout(location = 0) in vec3 aPos; // Vertex position

uniform mat4 projectionViewMatrix; // Model matrix
uniform mat4 modelMatrix; // Projection matrix

out float VertexDepth; // Output the depth value to the fragment shader

void main() {
vec4 viewSpacePosition = projectionViewMatrix * modelMatrix * vec4(aPos, 1.0);
gl_Position = viewSpacePosition;
VertexDepth = viewSpacePosition.z;
}
13 changes: 4 additions & 9 deletions resources/shaders/vertex/distance.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

layout(location = 0) in vec3 aPos; // Vertex position

uniform mat4 lightSpaceMatrix; // Model matrix
uniform mat4 projectionViewMatrix; // Model matrix
uniform mat4 modelMatrix; // Projection matrix

out vec3 FragPos; // Output the depth value to the fragment shader

void main() {
// Transform vertex position to world space, then to light space
vec4 lightSpacePosition = lightSpaceMatrix * modelMatrix * vec4(aPos, 1.0);

// Set the final position
gl_Position = lightSpacePosition;

// Calculate the depth in light space (NDC) range [0,1]
FragPos = lightSpacePosition.xyz;
vec4 viewSpacePosition = projectionViewMatrix * modelMatrix * vec4(aPos, 1.0);
gl_Position = viewSpacePosition;
FragPos = viewSpacePosition.xyz;
}
15 changes: 15 additions & 0 deletions resources/shaders/vertex/normal.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 330 core

layout(location = 1) in vec3 normal; // Vertex normal

uniform mat4 modelMatrix; // Model matrix
uniform mat4 projectionViewMatrix; // View matrix

out vec3 Normal; // Normal in world space

void main() {
vec3 fragPos = vec3(modelMatrix * vec4(position, 1.0));
Normal = normalize(mat3(transpose(inverse(modelMatrix))) * normal);

gl_Position = projectionViewMatrix * vec4(fragPos, 1.0);
}
2 changes: 2 additions & 0 deletions resources/shaders/vertex/position_and_normal.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ out vec3 FragPos; // Fragment position in world space
out vec3 Normal; // Normal in world space

void main() {
// Transform position and normal to world space
FragPos = vec3(modelMatrix * vec4(position, 1.0));
Normal = normalize(mat3(transpose(inverse(modelMatrix))) * normal);

// Set final vertex position
gl_Position = projectionViewMatrix * vec4(FragPos, 1.0);
}
19 changes: 0 additions & 19 deletions resources/shaders/vertex/shadowmap.glsl

This file was deleted.

16 changes: 16 additions & 0 deletions resources/shaders/vertex/viewspace_normal.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 Normal; // View-space normal

void main() {
vec4 worldPos = model * vec4(aPos, 1.0);
vec4 viewPos = view * worldPos;
Normal = mat3(transpose(inverse(view))) * aNormal; // Transform normal to view space
gl_Position = projection * viewPos;
}
17 changes: 17 additions & 0 deletions resources/shaders/vertex/viewspace_position.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 FragPos;

void main() {
vec4 worldPos = model * vec4(aPos, 1.0);
vec4 viewPos = view * worldPos;

FragPos = viewPos.xyz;
gl_Position = projection * viewPos;
}
2 changes: 1 addition & 1 deletion src/Components/CameraComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CameraComponent {
Orthographic
};

CameraComponent(float fov = 40.0f, float aspectRatio = 16.0f / 9.0f, float nearPlane = 0.01f, float farPlane = 2000.0f, ProjectionType projectionType = ProjectionType::Perspective)
CameraComponent(float fov = 40.0f, float aspectRatio = 16.0f / 9.0f, float nearPlane = 0.1f, float farPlane = 1000.0f, ProjectionType projectionType = ProjectionType::Perspective)
: fov(fov), aspectRatio(aspectRatio), nearPlane(nearPlane), farPlane(farPlane), projectionType(projectionType) {}

float fov;
Expand Down
2 changes: 2 additions & 0 deletions src/Rendering/FrameBuffers/DepthAtlasFrameBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ void DepthAtlasFrameBuffer::unbind() {
}

void DepthAtlasFrameBuffer::clear() {
bind();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
unbind();
}

void DepthAtlasFrameBuffer::setSize(uint32_t width, uint32_t height) {
Expand Down
4 changes: 2 additions & 2 deletions src/Rendering/FrameBuffers/SceneFrameBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ void SceneFrameBuffer::unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

void SceneFrameBuffer::clear() {
void SceneFrameBuffer::clear(glm::vec3 color) {
bind();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearColor(color.x, color.y, color.z, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
unbind();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rendering/FrameBuffers/SceneFrameBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SceneFrameBuffer {

void bind();
void unbind();
void clear();
void clear(glm::vec3 color = {0, 0, 0});

void setSize(uint32_t width, uint32_t height);
glm::vec2 getSize() const;
Expand Down
36 changes: 36 additions & 0 deletions src/Rendering/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,40 @@ void applyShaderToFrameBuffer(
frameBuffer.unbind();
}

template <typename ArrayType, typename... Uniforms>
void applyShaderToFrameBuffer(
SceneFrameBuffer& frameBuffer,
ShaderProgram& shaderProgram,
std::vector<std::pair<const char *, const Texture *>> textures, // List of texture bindings with texture unit indices
std::pair<const char *, std::vector<ArrayType>> array,
Uniforms... uniforms) // Parameter pack for uniform values
{
// Bind framebuffer and activate shader program
frameBuffer.bind();
shaderProgram.use();

// Bind textures to their corresponding texture units
int textureIndex = 0;
for (const auto& [name, texture] : textures) {
texture->activate(GL_TEXTURE0 + textureIndex);
shaderProgram.setUniform(name, textureIndex++);
}

int index = 0;
for(const auto& val : array.second)
{
shaderProgram.setUniform(std::string(array.first) + "[" + std::to_string(index++) + "]", val);
}

// Set uniforms using the parameter pack
(shaderProgram.setUniform(uniforms.first, uniforms.second), ...);

// Draw fullscreen quad
utils::drawFullscreenQuad();

// Stop using the shader program and unbind framebuffer
shaderProgram.stopUsing();
frameBuffer.unbind();
}

}
Loading

0 comments on commit f0dc569

Please sign in to comment.