From f844f7c541a2428647683b132170f9e72931e445 Mon Sep 17 00:00:00 2001 From: Joey de Vries Date: Thu, 1 Jun 2017 22:04:34 +0200 Subject: [PATCH] Code and content re-work: advanced OpenGL and advanced lighting. --- .../asteroids_instanced.cpp | 4 +- .../{11.2.aa_post.fs => 11.aa_post.fs} | 0 .../{11.2.aa_post.vs => 11.aa_post.vs} | 0 ...1.anti_aliasing.fs => 11.anti_aliasing.fs} | 0 ...1.anti_aliasing.vs => 11.anti_aliasing.vs} | 2 +- .../anti_aliasing_offscreen.cpp | 4 +- .../9.1.geometry_shader.gs | 12 +-- .../9.2.geometry_shader.gs | 6 +- .../9.3.default.vs | 2 +- .../9.3.normal_visualization.gs | 6 +- .../3.1.1.debug_quad_depth.fs | 2 +- .../3.1.1.shadow_mapping.fs | 78 ------------------- .../3.1.1.shadow_mapping.vs | 27 ------- .../3.1.1.shadow_mapping_depth.vs | 2 +- .../3.1.2.debug_quad.vs | 11 +++ .../3.1.2.debug_quad_depth.fs | 22 ++++++ .../3.1.2.shadow_mapping.fs | 8 +- .../3.1.2.shadow_mapping.vs | 2 +- .../3.1.2.shadow_mapping_depth.fs | 6 ++ .../3.1.2.shadow_mapping_depth.vs | 10 +++ .../3.1.3.shadow_mapping/3.1.3.debug_quad.vs | 11 +++ .../3.1.3.debug_quad_depth.fs | 22 ++++++ .../3.1.3.shadow_mapping.vs | 2 +- .../3.1.3.shadow_mapping_depth.fs | 6 ++ .../3.1.3.shadow_mapping_depth.vs | 10 +++ .../3.2.1.point_shadows.fs | 10 +-- .../3.2.2.point_shadows.fs | 12 +-- .../3.2.2.point_shadows.vs | 2 +- .../3.2.2.point_shadows_depth.fs | 16 ++++ .../3.2.2.point_shadows_depth.gs | 22 ++++++ .../3.2.2.point_shadows_depth.vs | 9 +++ .../4.normal_mapping/4.normal_mapping.fs | 2 +- 32 files changed, 184 insertions(+), 144 deletions(-) rename src/4.advanced_opengl/11.anti_aliasing_offscreen/{11.2.aa_post.fs => 11.aa_post.fs} (100%) rename src/4.advanced_opengl/11.anti_aliasing_offscreen/{11.2.aa_post.vs => 11.aa_post.vs} (100%) rename src/4.advanced_opengl/11.anti_aliasing_offscreen/{11.1.anti_aliasing.fs => 11.anti_aliasing.fs} (100%) rename src/4.advanced_opengl/11.anti_aliasing_offscreen/{11.1.anti_aliasing.vs => 11.anti_aliasing.vs} (67%) delete mode 100644 src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping.fs delete mode 100644 src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping.vs create mode 100644 src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.debug_quad.vs create mode 100644 src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.debug_quad_depth.fs create mode 100644 src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping_depth.fs create mode 100644 src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping_depth.vs create mode 100644 src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.debug_quad.vs create mode 100644 src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.debug_quad_depth.fs create mode 100644 src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping_depth.fs create mode 100644 src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping_depth.vs create mode 100644 src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.fs create mode 100644 src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.gs create mode 100644 src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.vs diff --git a/src/4.advanced_opengl/10.3.asteroids_instanced/asteroids_instanced.cpp b/src/4.advanced_opengl/10.3.asteroids_instanced/asteroids_instanced.cpp index 1fc105f..b9c0c7b 100644 --- a/src/4.advanced_opengl/10.3.asteroids_instanced/asteroids_instanced.cpp +++ b/src/4.advanced_opengl/10.3.asteroids_instanced/asteroids_instanced.cpp @@ -120,7 +120,7 @@ int main() glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, amount * sizeof(glm::mat4), &modelMatrices[0], GL_STATIC_DRAW); - // Set transformation matrices as an instance vertex attribute (with divisor 1) + // set transformation matrices as an instance vertex attribute (with divisor 1) // note: we're cheating a little by taking the, now publicly declared, VAO of the model's mesh(es) and adding new vertexAttribPointers // normally you'd want to do this in a more organized fashion, but for learning purposes this will do. // ----------------------------------------------------------------------------------------------------------------------------------- @@ -128,7 +128,7 @@ int main() { unsigned int VAO = rock.meshes[i].VAO; glBindVertexArray(VAO); - // Set attribute pointers for matrix (4 times vec4) + // set attribute pointers for matrix (4 times vec4) glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)0); glEnableVertexAttribArray(4); diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.2.aa_post.fs b/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.aa_post.fs similarity index 100% rename from src/4.advanced_opengl/11.anti_aliasing_offscreen/11.2.aa_post.fs rename to src/4.advanced_opengl/11.anti_aliasing_offscreen/11.aa_post.fs diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.2.aa_post.vs b/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.aa_post.vs similarity index 100% rename from src/4.advanced_opengl/11.anti_aliasing_offscreen/11.2.aa_post.vs rename to src/4.advanced_opengl/11.anti_aliasing_offscreen/11.aa_post.vs diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.1.anti_aliasing.fs b/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.anti_aliasing.fs similarity index 100% rename from src/4.advanced_opengl/11.anti_aliasing_offscreen/11.1.anti_aliasing.fs rename to src/4.advanced_opengl/11.anti_aliasing_offscreen/11.anti_aliasing.fs diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.1.anti_aliasing.vs b/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.anti_aliasing.vs similarity index 67% rename from src/4.advanced_opengl/11.anti_aliasing_offscreen/11.1.anti_aliasing.vs rename to src/4.advanced_opengl/11.anti_aliasing_offscreen/11.anti_aliasing.vs index 4a80ebb..cc518f1 100644 --- a/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.1.anti_aliasing.vs +++ b/src/4.advanced_opengl/11.anti_aliasing_offscreen/11.anti_aliasing.vs @@ -7,5 +7,5 @@ uniform mat4 projection; void main() { - gl_Position = projection * view * model * vec4(aPos, 1.0f); + gl_Position = projection * view * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/4.advanced_opengl/11.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp b/src/4.advanced_opengl/11.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp index 1ae985a..2aa0552 100644 --- a/src/4.advanced_opengl/11.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp +++ b/src/4.advanced_opengl/11.anti_aliasing_offscreen/anti_aliasing_offscreen.cpp @@ -72,8 +72,8 @@ int main() // build and compile shaders // ------------------------- - Shader shader("11.1.anti_aliasing.vs", "11.1.anti_aliasing.fs"); - Shader screenShader("11.2.aa_post.vs", "11.2.aa_post.fs"); + Shader shader("11.anti_aliasing.vs", "11.anti_aliasing.fs"); + Shader screenShader("11.aa_post.vs", "11.aa_post.fs"); // set up vertex data (and buffer(s)) and configure vertex attributes // ------------------------------------------------------------------ diff --git a/src/4.advanced_opengl/9.1.geometry_shader_houses/9.1.geometry_shader.gs b/src/4.advanced_opengl/9.1.geometry_shader_houses/9.1.geometry_shader.gs index 4f24075..ae3a390 100644 --- a/src/4.advanced_opengl/9.1.geometry_shader_houses/9.1.geometry_shader.gs +++ b/src/4.advanced_opengl/9.1.geometry_shader_houses/9.1.geometry_shader.gs @@ -11,16 +11,16 @@ out vec3 fColor; void build_house(vec4 position) { fColor = gs_in[0].color; // gs_in[0] since there's only one input vertex - gl_Position = position + vec4(-0.2f, -0.2f, 0.0f, 0.0f); // 1:bottom-left + gl_Position = position + vec4(-0.2, -0.2, 0.0, 0.0); // 1:bottom-left EmitVertex(); - gl_Position = position + vec4( 0.2f, -0.2f, 0.0f, 0.0f); // 2:bottom-right + gl_Position = position + vec4( 0.2, -0.2, 0.0, 0.0); // 2:bottom-right EmitVertex(); - gl_Position = position + vec4(-0.2f, 0.2f, 0.0f, 0.0f); // 3:top-left + gl_Position = position + vec4(-0.2, 0.2, 0.0, 0.0); // 3:top-left EmitVertex(); - gl_Position = position + vec4( 0.2f, 0.2f, 0.0f, 0.0f); // 4:top-right + gl_Position = position + vec4( 0.2, 0.2, 0.0, 0.0); // 4:top-right EmitVertex(); - gl_Position = position + vec4( 0.0f, 0.4f, 0.0f, 0.0f); // 5:top - fColor = vec3(1.0f, 1.0f, 1.0f); + gl_Position = position + vec4( 0.0, 0.4, 0.0, 0.0); // 5:top + fColor = vec3(1.0, 1.0, 1.0); EmitVertex(); EndPrimitive(); } diff --git a/src/4.advanced_opengl/9.2.geometry_shader_exploding/9.2.geometry_shader.gs b/src/4.advanced_opengl/9.2.geometry_shader_exploding/9.2.geometry_shader.gs index 1e23bd2..c3c70da 100644 --- a/src/4.advanced_opengl/9.2.geometry_shader_exploding/9.2.geometry_shader.gs +++ b/src/4.advanced_opengl/9.2.geometry_shader_exploding/9.2.geometry_shader.gs @@ -12,9 +12,9 @@ uniform float time; vec4 explode(vec4 position, vec3 normal) { - float magnitude = 2.0f; - vec3 direction = normal * ((sin(time) + 1.0f) / 2.0f) * magnitude; - return position + vec4(direction, 0.0f); + float magnitude = 2.0; + vec3 direction = normal * ((sin(time) + 1.0) / 2.0) * magnitude; + return position + vec4(direction, 0.0); } vec3 GetNormal() diff --git a/src/4.advanced_opengl/9.3.geometry_shader_normals/9.3.default.vs b/src/4.advanced_opengl/9.3.geometry_shader_normals/9.3.default.vs index f7c5c9c..ecfc50a 100644 --- a/src/4.advanced_opengl/9.3.geometry_shader_normals/9.3.default.vs +++ b/src/4.advanced_opengl/9.3.geometry_shader_normals/9.3.default.vs @@ -11,5 +11,5 @@ uniform mat4 model; void main() { TexCoords = aTexCoords; - gl_Position = projection * view * model * vec4(aPos, 1.0f); + gl_Position = projection * view * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/4.advanced_opengl/9.3.geometry_shader_normals/9.3.normal_visualization.gs b/src/4.advanced_opengl/9.3.geometry_shader_normals/9.3.normal_visualization.gs index e4bba23..1bea216 100644 --- a/src/4.advanced_opengl/9.3.geometry_shader_normals/9.3.normal_visualization.gs +++ b/src/4.advanced_opengl/9.3.geometry_shader_normals/9.3.normal_visualization.gs @@ -19,7 +19,7 @@ void GenerateLine(int index) void main() { - GenerateLine(0); // First vertex normal - GenerateLine(1); // Second vertex normal - GenerateLine(2); // Third vertex normal + GenerateLine(0); // first vertex normal + GenerateLine(1); // second vertex normal + GenerateLine(2); // third vertex normal } \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.debug_quad_depth.fs b/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.debug_quad_depth.fs index 545f501..f4ff9b4 100644 --- a/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.debug_quad_depth.fs +++ b/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.debug_quad_depth.fs @@ -17,6 +17,6 @@ float LinearizeDepth(float depth) void main() { float depthValue = texture(depthMap, TexCoords).r; - // color = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective + // FragColor = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective FragColor = vec4(vec3(depthValue), 1.0); // orthographic } \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping.fs b/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping.fs deleted file mode 100644 index 3d08cbb..0000000 --- a/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping.fs +++ /dev/null @@ -1,78 +0,0 @@ -#version 330 core -out vec4 FragColor; - -in VS_OUT { - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; - vec4 FragPosLightSpace; -} fs_in; - -uniform sampler2D diffuseTexture; -uniform sampler2D shadowMap; - -uniform vec3 lightPos; -uniform vec3 viewPos; - -uniform bool shadows; - -float ShadowCalculation(vec4 fragPosLightSpace) -{ - // perform perspective divide - vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; - // Transform to [0,1] range - projCoords = projCoords * 0.5 + 0.5; - // Get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) - float closestDepth = texture(shadowMap, projCoords.xy).r; - // Get depth of current fragment from light's perspective - float currentDepth = projCoords.z; - // Calculate bias (based on depth map resolution and slope) - vec3 normal = normalize(fs_in.Normal); - vec3 lightDir = normalize(lightPos - fs_in.FragPos); - float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005); - // Check whether current frag pos is in shadow - // float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0; - // PCF - float shadow = 0.0; - vec2 texelSize = 1.0 / textureSize(shadowMap, 0); - for(int x = -1; x <= 1; ++x) - { - for(int y = -1; y <= 1; ++y) - { - float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r; - shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0; - } - } - shadow /= 9.0; - - // Keep the shadow at 0.0 when outside the far_plane region of the light's frustum. - if(projCoords.z > 1.0) - shadow = 0.0; - - return shadow; -} - -void main() -{ - vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb; - vec3 normal = normalize(fs_in.Normal); - vec3 lightColor = vec3(0.3); - // Ambient - vec3 ambient = 0.3 * color; - // Diffuse - vec3 lightDir = normalize(lightPos - fs_in.FragPos); - float diff = max(dot(lightDir, normal), 0.0); - vec3 diffuse = diff * lightColor; - // Specular - vec3 viewDir = normalize(viewPos - fs_in.FragPos); - vec3 reflectDir = reflect(-lightDir, normal); - float spec = 0.0; - vec3 halfwayDir = normalize(lightDir + viewDir); - spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0); - vec3 specular = spec * lightColor; - // Calculate shadow - float shadow = shadows ? ShadowCalculation(fs_in.FragPosLightSpace) : 0.0; - vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color; - - FragColor = vec4(lighting, 1.0f); -} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping.vs b/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping.vs deleted file mode 100644 index b30b1f2..0000000 --- a/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping.vs +++ /dev/null @@ -1,27 +0,0 @@ -#version 330 core -layout (location = 0) in vec3 position; -layout (location = 1) in vec3 normal; -layout (location = 2) in vec2 texCoords; - -out vec2 TexCoords; - -out VS_OUT { - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; - vec4 FragPosLightSpace; -} vs_out; - -uniform mat4 projection; -uniform mat4 view; -uniform mat4 model; -uniform mat4 lightSpaceMatrix; - -void main() -{ - gl_Position = projection * view * model * vec4(position, 1.0f); - vs_out.FragPos = vec3(model * vec4(position, 1.0)); - vs_out.Normal = transpose(inverse(mat3(model))) * normal; - vs_out.TexCoords = texCoords; - vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); -} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping_depth.vs b/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping_depth.vs index ddbcaae..947562f 100644 --- a/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping_depth.vs +++ b/src/5.advanced_lighting/3.1.1.shadow_mapping_depth/3.1.1.shadow_mapping_depth.vs @@ -6,5 +6,5 @@ uniform mat4 model; void main() { - gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0f); + gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.debug_quad.vs b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.debug_quad.vs new file mode 100644 index 0000000..9f93e29 --- /dev/null +++ b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.debug_quad.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoords; + +out vec2 TexCoords; + +void main() +{ + TexCoords = aTexCoords; + gl_Position = vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.debug_quad_depth.fs b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.debug_quad_depth.fs new file mode 100644 index 0000000..f4ff9b4 --- /dev/null +++ b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.debug_quad_depth.fs @@ -0,0 +1,22 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoords; + +uniform sampler2D depthMap; +uniform float near_plane; +uniform float far_plane; + +// required when using a perspective projection matrix +float LinearizeDepth(float depth) +{ + float z = depth * 2.0 - 1.0; // Back to NDC + return (2.0 * near_plane * far_plane) / (far_plane + near_plane - z * (far_plane - near_plane)); +} + +void main() +{ + float depthValue = texture(depthMap, TexCoords).r; + // FragColor = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective + FragColor = vec4(vec3(depthValue), 1.0); // orthographic +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping.fs b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping.fs index a2c0797..46bc723 100644 --- a/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping.fs +++ b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping.fs @@ -18,13 +18,13 @@ float ShadowCalculation(vec4 fragPosLightSpace) { // perform perspective divide vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; - // Transform to [0,1] range + // transform to [0,1] range projCoords = projCoords * 0.5 + 0.5; - // Get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) + // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) float closestDepth = texture(shadowMap, projCoords.xy).r; - // Get depth of current fragment from light's perspective + // get depth of current fragment from light's perspective float currentDepth = projCoords.z; - // Check whether current frag pos is in shadow + // check whether current frag pos is in shadow float shadow = currentDepth > closestDepth ? 1.0 : 0.0; return shadow; diff --git a/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping.vs b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping.vs index a930299..9ab66cb 100644 --- a/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping.vs +++ b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping.vs @@ -23,5 +23,5 @@ void main() vs_out.Normal = transpose(inverse(mat3(model))) * aNormal; vs_out.TexCoords = aTexCoords; vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); - gl_Position = projection * view * model * vec4(aPos, 1.0f); + gl_Position = projection * view * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping_depth.fs b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping_depth.fs new file mode 100644 index 0000000..ffae5f0 --- /dev/null +++ b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping_depth.fs @@ -0,0 +1,6 @@ +#version 330 core + +void main() +{ + // gl_FragDepth = gl_FragCoord.z; +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping_depth.vs b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping_depth.vs new file mode 100644 index 0000000..947562f --- /dev/null +++ b/src/5.advanced_lighting/3.1.2.shadow_mapping_base/3.1.2.shadow_mapping_depth.vs @@ -0,0 +1,10 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 lightSpaceMatrix; +uniform mat4 model; + +void main() +{ + gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.debug_quad.vs b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.debug_quad.vs new file mode 100644 index 0000000..9f93e29 --- /dev/null +++ b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.debug_quad.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoords; + +out vec2 TexCoords; + +void main() +{ + TexCoords = aTexCoords; + gl_Position = vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.debug_quad_depth.fs b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.debug_quad_depth.fs new file mode 100644 index 0000000..f4ff9b4 --- /dev/null +++ b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.debug_quad_depth.fs @@ -0,0 +1,22 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoords; + +uniform sampler2D depthMap; +uniform float near_plane; +uniform float far_plane; + +// required when using a perspective projection matrix +float LinearizeDepth(float depth) +{ + float z = depth * 2.0 - 1.0; // Back to NDC + return (2.0 * near_plane * far_plane) / (far_plane + near_plane - z * (far_plane - near_plane)); +} + +void main() +{ + float depthValue = texture(depthMap, TexCoords).r; + // FragColor = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective + FragColor = vec4(vec3(depthValue), 1.0); // orthographic +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping.vs b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping.vs index a930299..9ab66cb 100644 --- a/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping.vs +++ b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping.vs @@ -23,5 +23,5 @@ void main() vs_out.Normal = transpose(inverse(mat3(model))) * aNormal; vs_out.TexCoords = aTexCoords; vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); - gl_Position = projection * view * model * vec4(aPos, 1.0f); + gl_Position = projection * view * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping_depth.fs b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping_depth.fs new file mode 100644 index 0000000..ffae5f0 --- /dev/null +++ b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping_depth.fs @@ -0,0 +1,6 @@ +#version 330 core + +void main() +{ + // gl_FragDepth = gl_FragCoord.z; +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping_depth.vs b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping_depth.vs new file mode 100644 index 0000000..947562f --- /dev/null +++ b/src/5.advanced_lighting/3.1.3.shadow_mapping/3.1.3.shadow_mapping_depth.vs @@ -0,0 +1,10 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 lightSpaceMatrix; +uniform mat4 model; + +void main() +{ + gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.2.1.point_shadows/3.2.1.point_shadows.fs b/src/5.advanced_lighting/3.2.1.point_shadows/3.2.1.point_shadows.fs index 411cb84..500b897 100644 --- a/src/5.advanced_lighting/3.2.1.point_shadows/3.2.1.point_shadows.fs +++ b/src/5.advanced_lighting/3.2.1.point_shadows/3.2.1.point_shadows.fs @@ -40,22 +40,22 @@ void main() vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb; vec3 normal = normalize(fs_in.Normal); vec3 lightColor = vec3(0.3); - // Ambient + // ambient vec3 ambient = 0.3 * color; - // Diffuse + // diffuse vec3 lightDir = normalize(lightPos - fs_in.FragPos); float diff = max(dot(lightDir, normal), 0.0); vec3 diffuse = diff * lightColor; - // Specular + // specular vec3 viewDir = normalize(viewPos - fs_in.FragPos); vec3 reflectDir = reflect(-lightDir, normal); float spec = 0.0; vec3 halfwayDir = normalize(lightDir + viewDir); spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0); vec3 specular = spec * lightColor; - // Calculate shadow + // calculate shadow float shadow = shadows ? ShadowCalculation(fs_in.FragPos) : 0.0; vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color; - FragColor = vec4(lighting, 1.0f); + FragColor = vec4(lighting, 1.0); } \ No newline at end of file diff --git a/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows.fs b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows.fs index 766f526..c72d1d7 100644 --- a/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows.fs +++ b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows.fs @@ -20,11 +20,11 @@ uniform bool shadows; // array of offset direction for sampling vec3 gridSamplingDisk[20] = vec3[] ( - vec3(1, 1, 1), vec3(1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1), - vec3(1, 1, -1), vec3(1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1), - vec3(1, 1, 0), vec3(1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0), - vec3(1, 0, 1), vec3(-1, 0, 1), vec3(1, 0, -1), vec3(-1, 0, -1), - vec3(0, 1, 1), vec3(0, -1, 1), vec3(0, -1, -1), vec3(0, 1, -1) + vec3(1, 1, 1), vec3( 1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1), + vec3(1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1), + vec3(1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0), + vec3(1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1), + vec3(0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1) ); float ShadowCalculation(vec3 fragPos) @@ -101,5 +101,5 @@ void main() float shadow = shadows ? ShadowCalculation(fs_in.FragPos) : 0.0; vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color; - FragColor = vec4(lighting, 1.0f); + FragColor = vec4(lighting, 1.0); } \ No newline at end of file diff --git a/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows.vs b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows.vs index 33f453b..4b15f15 100644 --- a/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows.vs +++ b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows.vs @@ -25,5 +25,5 @@ void main() else vs_out.Normal = transpose(inverse(mat3(model))) * aNormal; vs_out.TexCoords = aTexCoords; - gl_Position = projection * view * model * vec4(aPos, 1.0f); + gl_Position = projection * view * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.fs b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.fs new file mode 100644 index 0000000..37600c6 --- /dev/null +++ b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.fs @@ -0,0 +1,16 @@ +#version 330 core +in vec4 FragPos; + +uniform vec3 lightPos; +uniform float far_plane; + +void main() +{ + float lightDistance = length(FragPos.xyz - lightPos); + + // map to [0;1] range by dividing by far_plane + lightDistance = lightDistance / far_plane; + + // write this as modified depth + gl_FragDepth = lightDistance; +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.gs b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.gs new file mode 100644 index 0000000..96d5627 --- /dev/null +++ b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.gs @@ -0,0 +1,22 @@ +#version 330 core +layout (triangles) in; +layout (triangle_strip, max_vertices=18) out; + +uniform mat4 shadowMatrices[6]; + +out vec4 FragPos; // FragPos from GS (output per emitvertex) + +void main() +{ + for(int face = 0; face < 6; ++face) + { + gl_Layer = face; // built-in variable that specifies to which face we render. + for(int i = 0; i < 3; ++i) // for each triangle's vertices + { + FragPos = gl_in[i].gl_Position; + gl_Position = shadowMatrices[face] * FragPos; + EmitVertex(); + } + EndPrimitive(); + } +} \ No newline at end of file diff --git a/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.vs b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.vs new file mode 100644 index 0000000..8c71938 --- /dev/null +++ b/src/5.advanced_lighting/3.2.2.point_shadows_soft/3.2.2.point_shadows_depth.vs @@ -0,0 +1,9 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 model; + +void main() +{ + gl_Position = model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/src/5.advanced_lighting/4.normal_mapping/4.normal_mapping.fs b/src/5.advanced_lighting/4.normal_mapping/4.normal_mapping.fs index 12e435e..088f2cc 100644 --- a/src/5.advanced_lighting/4.normal_mapping/4.normal_mapping.fs +++ b/src/5.advanced_lighting/4.normal_mapping/4.normal_mapping.fs @@ -37,5 +37,5 @@ void main() float spec = pow(max(dot(normal, halfwayDir), 0.0), 32.0); vec3 specular = vec3(0.2) * spec; - FragColor = vec4(ambient + diffuse + specular, 1.0f); + FragColor = vec4(ambient + diffuse + specular, 1.0); } \ No newline at end of file