From 6ea296d997e1fed60da8e84d6a0d75a942b58977 Mon Sep 17 00:00:00 2001 From: talha Date: Tue, 16 Apr 2024 01:34:33 +0500 Subject: Learning cubemaps, added reflections --- .../framebuffers/shaders/blend_test.fs.glsl | 14 +++++ .../framebuffers/shaders/depth_test.fs.glsl | 30 +++++++++ .../framebuffers/shaders/depth_test.vs.glsl | 19 ++++++ source/lessons/framebuffers/shaders/fbo.fs.glsl | 72 ++++++++++++++++++++++ source/lessons/framebuffers/shaders/fbo.vs.glsl | 10 +++ 5 files changed, 145 insertions(+) create mode 100644 source/lessons/framebuffers/shaders/blend_test.fs.glsl create mode 100644 source/lessons/framebuffers/shaders/depth_test.fs.glsl create mode 100644 source/lessons/framebuffers/shaders/depth_test.vs.glsl create mode 100644 source/lessons/framebuffers/shaders/fbo.fs.glsl create mode 100644 source/lessons/framebuffers/shaders/fbo.vs.glsl (limited to 'source/lessons/framebuffers/shaders') diff --git a/source/lessons/framebuffers/shaders/blend_test.fs.glsl b/source/lessons/framebuffers/shaders/blend_test.fs.glsl new file mode 100644 index 0000000..23daa14 --- /dev/null +++ b/source/lessons/framebuffers/shaders/blend_test.fs.glsl @@ -0,0 +1,14 @@ +#version 330 core + + +in vec2 TexCoords; +in vec3 VertexWorldPos; +uniform sampler2D TexId; +uniform vec4 hlt_color; +out vec4 FragColor; + +void main() { + vec4 tex = texture(TexId, TexCoords); + + FragColor = tex; +} diff --git a/source/lessons/framebuffers/shaders/depth_test.fs.glsl b/source/lessons/framebuffers/shaders/depth_test.fs.glsl new file mode 100644 index 0000000..796d849 --- /dev/null +++ b/source/lessons/framebuffers/shaders/depth_test.fs.glsl @@ -0,0 +1,30 @@ +#version 330 core + + +in vec2 TexCoords; +in vec3 VertexWorldPos; +uniform sampler2D TexId; +out vec4 FragColor; + +uniform float near = 0.1f; +uniform float far = 100.0f; + +/* @note +float linear_fragment_depth = MakeDepthLinear(non_linear_fragment_depth); +float scaled_lfd = linear_fragment_depth/far; + +gives us the z value in eye space. +This is purely for learning purposes. +The equation used in MakeDepthLinear is derived from the PerspectiveProjectionMatrix. +Take a look at the equation for that in the codebase +or here: https://www.songho.ca/opengl/gl_projectionmatrix.html +*/ +float MakeDepthLinear(float depth) { + float ndc = 2.0f*depth - 1; + float linear_depth = (2.0 * far * near)/(far + near - ndc*(far - near)); + return linear_depth; +} + +void main() { + FragColor = texture(TexId, TexCoords); +} diff --git a/source/lessons/framebuffers/shaders/depth_test.vs.glsl b/source/lessons/framebuffers/shaders/depth_test.vs.glsl new file mode 100644 index 0000000..b3b81cc --- /dev/null +++ b/source/lessons/framebuffers/shaders/depth_test.vs.glsl @@ -0,0 +1,19 @@ +#version 330 core +layout(location=0) in vec3 aPos; +layout(location=1) in vec2 aTex; + +uniform mat4 Model; +uniform mat4 View; +uniform mat4 Projection; + +out vec2 TexCoords; +out vec3 VertexWorldPos; + +// @note: I still do not fully understand how the FragNormal calculation works. Need to make sure I intuitively +// get that + +void main() { + gl_Position = Projection*View*Model*vec4(aPos, 1.0); + VertexWorldPos = vec3(Model * vec4(aPos, 1.0)); + TexCoords = aTex; +}; diff --git a/source/lessons/framebuffers/shaders/fbo.fs.glsl b/source/lessons/framebuffers/shaders/fbo.fs.glsl new file mode 100644 index 0000000..afb9e08 --- /dev/null +++ b/source/lessons/framebuffers/shaders/fbo.fs.glsl @@ -0,0 +1,72 @@ +#version 330 core + +in vec2 TexCoords; +uniform sampler2D TexId; +out vec4 FragColor; + +vec4 filter_color_invert(vec4 color) +{ + vec4 res = vec4(vec3(1.0) - vec3(color), 1.0); + return res; +} + +vec4 filter_color_grayscale(vec4 color) +{ + // we will need to average the colors + // float average = (color.x + color.y + color.z) / 3.0f; + // in reality, human our most sensitive towards green and least to blue, so will need to weight those + float average = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b; + vec4 res = vec4(vec3(average), 1.0); + + return res; +} + +// @note: different kernels for experimentation +const float kernel_sharpen[9] = float[]( + -1, -1, -1, + -1, 9, -1, + -1, -1, -1 +); + +const float kernel_blur[9] = float[]( + 1.0/16.0, 2.0/16.0, 1.0/16.0, + 2.0/16.0, 4.0/16.0, 2.0/16.0, + 1.0/16.0, 2.0/16.0, 1.0/16.0 +); + +const float kernel_edge_detection[9] = float[]( + 1, 1, 1, + 1, -8, 1, + 1, 1, 1 +); + +vec4 filter_kernal_effects() +{ + const float offset = 1.0/300.0; + vec2 offsets[9] = vec2[]( + vec2(-offset, offset), // top left + vec2( 0, offset), // top center + vec2( offset, offset), // top right + vec2(-offset, 0), // center left + vec2( 0, 0), // center center + vec2( offset, 0), // center right + vec2(-offset, -offset), // bot left + vec2( 0, -offset), // bot center + vec2( offset, -offset) // bot right + ); + + float kernal[9] = kernel_edge_detection; + vec3 kernalValue = vec3(0.0); + vec3 sampleTex[9]; + for (int i=0; i<9; i++) { + sampleTex[i] = vec3(texture(TexId, TexCoords + offsets[i])); + kernalValue += (kernal[i] * sampleTex[i]); + } + + vec4 res = vec4(kernalValue, 1.0); + return res; +} + +void main() { + FragColor = filter_kernal_effects(); +} diff --git a/source/lessons/framebuffers/shaders/fbo.vs.glsl b/source/lessons/framebuffers/shaders/fbo.vs.glsl new file mode 100644 index 0000000..82d7211 --- /dev/null +++ b/source/lessons/framebuffers/shaders/fbo.vs.glsl @@ -0,0 +1,10 @@ +#version 330 core +layout(location=0) in vec3 aPos; +layout(location=1) in vec2 aTex; + +out vec2 TexCoords; + +void main() { + gl_Position = vec4(aPos.x, aPos.y, 0.0f, 1.0f); + TexCoords = aTex; +}; -- cgit v1.2.3