summaryrefslogtreecommitdiff
path: root/source/lessons/instanced_rendering/planetary models/shaders
diff options
context:
space:
mode:
authortalha <talha@talhaamir.xyz>2024-04-22 03:38:29 +0500
committertalha <talha@talhaamir.xyz>2024-04-22 03:38:29 +0500
commitca2835943ca4327ad08b54af480e0c6333df201f (patch)
tree1cac87ca6d6ceee89f91d8735f319805d4bad38e /source/lessons/instanced_rendering/planetary models/shaders
parent9900ffe840ee0e9f914b0e7956a4e80ffb553e9e (diff)
Completed main levels to progress to text-rendering and 2d development.
- Only lessons left are geometry shaders and anti-aliasing - will get to those later on soon - need to do text rendering now
Diffstat (limited to 'source/lessons/instanced_rendering/planetary models/shaders')
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/cubemap.fs.glsl9
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/cubemap.vs.glsl14
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/depth_test.fs.glsl30
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/depth_test.vs.glsl21
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/fbo.fs.glsl72
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/fbo.vs.glsl10
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/instanced_model.vs.glsl29
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/instancing.fs.glsl9
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/instancing.vs.glsl16
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/model.fs.glsl150
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/model.vs.glsl28
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/refl.fs.glsl14
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/refl.vs.glsl16
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/refr.fs.glsl15
-rw-r--r--source/lessons/instanced_rendering/planetary models/shaders/refr.vs.glsl20
15 files changed, 453 insertions, 0 deletions
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/cubemap.fs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/cubemap.fs.glsl
new file mode 100644
index 0000000..72dfe4c
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/cubemap.fs.glsl
@@ -0,0 +1,9 @@
+#version 330 core
+
+in vec3 TexCoords;
+uniform samplerCube skybox;
+out vec4 FragColor;
+
+void main() {
+ FragColor = texture(skybox, TexCoords);
+};
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/cubemap.vs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/cubemap.vs.glsl
new file mode 100644
index 0000000..956673a
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/cubemap.vs.glsl
@@ -0,0 +1,14 @@
+#version 330 core
+layout(location=0) in vec3 aPos;
+
+uniform mat4 Model;
+uniform mat4 View;
+uniform mat4 Projection;
+
+out vec3 TexCoords;
+
+void main() {
+ vec4 pos = Projection*View*vec4(aPos, 1.0);
+ gl_Position = vec4(pos.xyww);
+ TexCoords = aPos;
+};
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/depth_test.fs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/depth_test.fs.glsl
new file mode 100644
index 0000000..796d849
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/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/instanced_rendering/planetary models/shaders/depth_test.vs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/depth_test.vs.glsl
new file mode 100644
index 0000000..827da20
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/depth_test.vs.glsl
@@ -0,0 +1,21 @@
+#version 330 core
+layout(location=0) in vec3 aPos;
+layout(location=1) in vec2 aTex;
+
+uniform mat4 Model;
+layout (std140) uniform Matrices {
+ mat4 View; // start: 0 // end: 16 * 4 = 64
+ mat4 Projection; // start: 64 // end: 64 + 64 = 128
+};
+
+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/instanced_rendering/planetary models/shaders/fbo.fs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/fbo.fs.glsl
new file mode 100644
index 0000000..e12ad33
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/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 = texture(TexId, TexCoords);
+}
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/fbo.vs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/fbo.vs.glsl
new file mode 100644
index 0000000..82d7211
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/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;
+};
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/instanced_model.vs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/instanced_model.vs.glsl
new file mode 100644
index 0000000..2456a3c
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/instanced_model.vs.glsl
@@ -0,0 +1,29 @@
+#version 330 core
+layout(location=0) in vec3 aPos;
+layout(location=1) in vec3 aNormal;
+layout(location=2) in vec2 aTex;
+layout(location=3) in mat4 aTransform;
+
+// uniform mat4 Model;
+layout (std140) uniform Matrices {
+ mat4 View; // start: 0 // end: 16 * 4 = 64
+ mat4 Projection; // start: 64 // end: 64 + 64 = 128
+};
+
+uniform mat4 Model;
+
+out vec2 TexCoords;
+out vec3 VertexWorldPos;
+out vec3 FragNormal;
+
+// @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*aTransform*vec4(aPos, 1.0);
+
+ VertexWorldPos = vec3(aTransform * vec4(aPos, 1.0));
+ FragNormal = mat3(transpose(inverse(aTransform))) * aNormal;
+ FragNormal = normalize(FragNormal);
+ TexCoords = aTex;
+};
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/instancing.fs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/instancing.fs.glsl
new file mode 100644
index 0000000..1ec8011
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/instancing.fs.glsl
@@ -0,0 +1,9 @@
+#version 330 core
+
+
+// uniform sampler2D TexId;
+out vec4 FragColor;
+
+void main() {
+ FragColor = vec4(gl_FragCoord.x/1024.0, gl_FragCoord.y/768.0, 0.0, 1.0f);
+}
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/instancing.vs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/instancing.vs.glsl
new file mode 100644
index 0000000..eff2fae
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/instancing.vs.glsl
@@ -0,0 +1,16 @@
+#version 330 core
+layout(location=0) in vec3 aPos;
+layout(location=1) in mat4 aOffset;
+
+// uniform mat4 Model;
+layout (std140) uniform Matrices {
+ mat4 View; // start: 0 // end: 16 * 4 = 64
+ mat4 Projection; // start: 64 // end: 64 + 64 = 128
+};
+
+// @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*aOffset*vec4(aPos, 1.0);
+}
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/model.fs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/model.fs.glsl
new file mode 100644
index 0000000..dbe5cf4
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/model.fs.glsl
@@ -0,0 +1,150 @@
+#version 330 core
+
+#define MAX_TEXTURES 32
+struct Material {
+ sampler2D diffuse[MAX_TEXTURES];
+ sampler2D specular[MAX_TEXTURES];
+ float shininess;
+};
+
+struct DirectionalLight {
+ vec3 direction;
+
+ vec3 ambient;
+ vec3 diffuse;
+ vec3 specular;
+};
+
+struct PointLight {
+ vec3 position;
+
+ vec3 ambient;
+ vec3 diffuse;
+ vec3 specular;
+
+ // attentuation factors
+ float kC;
+ float kL;
+ float kQ;
+};
+
+struct SpotLight {
+ vec3 position;
+
+ vec3 ambient;
+ vec3 diffuse;
+ vec3 specular;
+
+ // attenuation factors
+ float kC;
+ float kL;
+ float kQ;
+
+ // vector for the direction directly in front of the spotlight
+ vec3 front;
+
+ // spot radius
+ float radius_inner;
+ float radius_outer; // to smooth out the light
+
+};
+
+// this is the result of a light creation. This contains the multipliers for each kind of a light we want
+// to have.
+struct LightFactor {
+ vec3 ambient;
+ vec3 diffuse;
+ vec3 specular;
+};
+
+in vec2 TexCoords;
+in vec3 FragNormal;
+in vec3 VertexWorldPos;
+uniform Material material;
+uniform PointLight pointLight;
+uniform DirectionalLight dirLight;
+uniform vec3 cameraPosition;
+
+out vec4 FragColor;
+
+LightFactor make_directional_light(DirectionalLight light, vec3 CONST_viewDir) {
+ LightFactor res;
+
+ vec3 DL_lightDir = normalize(-light.direction);
+ res.ambient = light.ambient;
+
+ float DL_diffuseStrength = max(dot(DL_lightDir, FragNormal), 0.0);
+ res.diffuse = light.diffuse * DL_diffuseStrength;
+
+ vec3 DL_reflectDir = reflect(-DL_lightDir, FragNormal);
+ float DL_specularity = max(dot(CONST_viewDir, DL_reflectDir), 0.0);
+ float DL_shinePower = pow(DL_specularity, material.shininess);
+ res.specular = light.specular * DL_shinePower;
+
+ return res;
+};
+
+LightFactor make_point_light(PointLight light, vec3 CONST_viewDir) {
+ LightFactor res;
+
+ float PL_lightDistance = length(light.position - VertexWorldPos);
+ float PL_attenuationFactor = 1.0 /
+ (light.kC + (light.kL * PL_lightDistance) + (light.kQ * PL_lightDistance * PL_lightDistance));
+ res.ambient = PL_attenuationFactor * light.ambient;
+
+ vec3 PL_lightDir = normalize(light.position - VertexWorldPos);
+ float PL_diffuseStrength = max(dot(PL_lightDir, FragNormal), 0.0);
+ res.diffuse = PL_attenuationFactor * light.diffuse * PL_diffuseStrength;
+
+ vec3 PL_reflectDir = reflect(-PL_lightDir, FragNormal);
+ float PL_specularity = max(dot(CONST_viewDir, PL_reflectDir), 0.0);
+ float PL_shinePower = pow(PL_specularity, material.shininess);
+ res.specular = PL_attenuationFactor * PL_shinePower * light.specular;
+
+ return res;
+}
+
+LightFactor make_spot_light(SpotLight light, vec3 CONST_viewDir) {
+ LightFactor res;
+
+ float SL_lightDistance = length(light.position - VertexWorldPos);
+ float SL_attenuationFactor = 1.0 /
+ (light.kC + (light.kL * SL_lightDistance) + (light.kQ * SL_lightDistance * SL_lightDistance));
+ vec3 SL_lightDir = normalize(light.position - VertexWorldPos);
+
+ res.ambient = SL_attenuationFactor * light.ambient;
+
+ float SL_diffAmount = dot(SL_lightDir, normalize(-light.front));
+ float SL_spotLightFadeFactor = clamp((SL_diffAmount - light.radius_outer)/(light.radius_inner - light.radius_outer), 0.0f, 1.0f);
+ float SL_diffuseStrength = max(dot(SL_lightDir, FragNormal), 0.0);
+ res.diffuse = SL_spotLightFadeFactor * SL_attenuationFactor * light.diffuse * SL_diffuseStrength;
+
+ vec3 SL_reflectDir = reflect(-SL_lightDir, FragNormal);
+ float SL_specularity = max(dot(CONST_viewDir, SL_reflectDir), 0.0);
+ float SL_shinePower = pow(SL_specularity, material.shininess);
+ res.specular = SL_spotLightFadeFactor * SL_attenuationFactor * SL_shinePower * light.specular;
+
+ return res;
+}
+
+void main() {
+ //vec3 CONST_viewDir = normalize(cameraPosition - VertexWorldPos);
+ //vec3 combinedAmbience = vec3(0.0);
+ //vec3 combinedDiffuse = vec3(0.0);
+ //vec3 combinedSpecular = vec3(0.0);
+
+ //LightFactor DL_factors = make_directional_light(dirLight, CONST_viewDir);
+ //combinedAmbience += DL_factors.ambient;
+ //combinedDiffuse += DL_factors.diffuse;
+ //combinedSpecular += DL_factors.specular;
+
+ //LightFactor PL_factors = make_point_light(pointLight, CONST_viewDir);
+ //combinedAmbience += PL_factors.ambient;
+ //combinedDiffuse += PL_factors.diffuse;
+ //combinedSpecular += PL_factors.specular;
+
+ vec3 diffuseLight = vec3(texture(material.diffuse[0], TexCoords));
+
+ vec3 color = diffuseLight;
+ FragColor = vec4(color, 1.0);
+};
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/model.vs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/model.vs.glsl
new file mode 100644
index 0000000..e4896c9
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/model.vs.glsl
@@ -0,0 +1,28 @@
+#version 330 core
+layout(location=0) in vec3 aPos;
+layout(location=1) in vec3 aNormal;
+layout(location=2) in vec2 aTex;
+
+// uniform mat4 Model;
+layout (std140) uniform Matrices {
+ mat4 View; // start: 0 // end: 16 * 4 = 64
+ mat4 Projection; // start: 64 // end: 64 + 64 = 128
+};
+
+uniform mat4 Model;
+
+out vec2 TexCoords;
+out vec3 VertexWorldPos;
+out vec3 FragNormal;
+
+// @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));
+ FragNormal = mat3(transpose(inverse(Model))) * aNormal;
+ FragNormal = normalize(FragNormal);
+ TexCoords = aTex;
+};
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/refl.fs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/refl.fs.glsl
new file mode 100644
index 0000000..6d28392
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/refl.fs.glsl
@@ -0,0 +1,14 @@
+#version 330 core
+
+in vec3 Normal;
+in vec3 Position;
+
+uniform samplerCube skybox;
+uniform vec3 cameraPos;
+out vec4 FragColor;
+
+void main() {
+ vec3 I = normalize(Position - cameraPos);
+ vec3 R = reflect(I, normalize(Normal));
+ FragColor = vec4(texture(skybox, R).rgb, 1.0);
+};
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/refl.vs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/refl.vs.glsl
new file mode 100644
index 0000000..b8f2b97
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/refl.vs.glsl
@@ -0,0 +1,16 @@
+#version 330 core
+layout(location=0) in vec3 aPos;
+layout(location=1) in vec3 aNormal;
+
+uniform mat4 View;
+uniform mat4 Model;
+uniform mat4 Projection;
+
+out vec3 Normal;
+out vec3 Position;
+
+void main() {
+ Normal = mat3(transpose(inverse(Model))) * aNormal;
+ Position = vec3(Model * vec4(aPos, 1.0));
+ gl_Position = Projection * View * Model * vec4(aPos, 1.0);
+};
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/refr.fs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/refr.fs.glsl
new file mode 100644
index 0000000..6747ded
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/refr.fs.glsl
@@ -0,0 +1,15 @@
+#version 330 core
+
+in vec3 Normal;
+in vec3 Position;
+
+uniform samplerCube skybox;
+uniform vec3 cameraPos;
+out vec4 FragColor;
+
+void main() {
+ float refr_ratio = 1.0/1.52;
+ vec3 I = normalize(Position - cameraPos);
+ vec3 R = refract(I, normalize(Normal), refr_ratio);
+ FragColor = vec4(texture(skybox, R).rgb, 1.0);
+};
diff --git a/source/lessons/instanced_rendering/planetary models/shaders/refr.vs.glsl b/source/lessons/instanced_rendering/planetary models/shaders/refr.vs.glsl
new file mode 100644
index 0000000..0554f0a
--- /dev/null
+++ b/source/lessons/instanced_rendering/planetary models/shaders/refr.vs.glsl
@@ -0,0 +1,20 @@
+#version 330 core
+layout(location=0) in vec3 aPos;
+layout(location=1) in vec3 aNormal;
+
+uniform mat4 View;
+uniform mat4 Model;
+uniform mat4 Projection;
+
+out vec3 Normal;
+out vec3 Position;
+
+void main() {
+ // @note: This is the calculation for getting the normal vector
+ // one that is unaffected by non-uniform scaling that is.
+ // look at the lighting chapter in learnopengl.com to understand this more
+ Normal = mat3(transpose(inverse(Model))) * aNormal;
+ Position = vec3(Model * vec4(aPos, 1.0));
+ gl_Position = Projection * View * Model * vec4(aPos, 1.0);
+};
+