From f9ad6fa902c1167d7622ee7af2617d14b62bee21 Mon Sep 17 00:00:00 2001 From: pryazha Date: Wed, 19 Feb 2025 22:26:48 +0500 Subject: quite a lot of changes that I, of course, are not going to describe;) --- .../3.1.shadow_mapping/shaders/color.frag | 8 +++ .../3.1.shadow_mapping/shaders/color.fs | 9 --- .../3.1.shadow_mapping/shaders/color.vert | 11 ++++ .../3.1.shadow_mapping/shaders/color.vs | 12 ---- .../3.1.shadow_mapping/shaders/debug_quad.frag | 25 ++++++++ .../3.1.shadow_mapping/shaders/debug_quad.fs | 27 -------- .../3.1.shadow_mapping/shaders/debug_quad.vert | 14 +++++ .../3.1.shadow_mapping/shaders/debug_quad.vs | 14 ----- .../3.1.shadow_mapping/shaders/shadow.frag | 70 +++++++++++++++++++++ .../3.1.shadow_mapping/shaders/shadow.fs | 72 ---------------------- .../3.1.shadow_mapping/shaders/shadow.vert | 25 ++++++++ .../3.1.shadow_mapping/shaders/shadow.vs | 26 -------- .../3.1.shadow_mapping/shaders/simple_depth.frag | 5 ++ .../3.1.shadow_mapping/shaders/simple_depth.fs | 6 -- .../3.1.shadow_mapping/shaders/simple_depth.vert | 10 +++ .../3.1.shadow_mapping/shaders/simple_depth.vs | 11 ---- 16 files changed, 168 insertions(+), 177 deletions(-) create mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/color.frag delete mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/color.fs create mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/color.vert delete mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/color.vs create mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.frag delete mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.fs create mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vert delete mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vs create mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/shadow.frag delete mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/shadow.fs create mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/shadow.vert delete mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/shadow.vs create mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.frag delete mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.fs create mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vert delete mode 100644 advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vs (limited to 'advanced_lighting/3.1.shadow_mapping/shaders') diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/color.frag b/advanced_lighting/3.1.shadow_mapping/shaders/color.frag new file mode 100644 index 0000000..c01da99 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/color.frag @@ -0,0 +1,8 @@ +#version 330 core + +out vec4 frag_color; + +void main(void) +{ + frag_color = vec4(1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/color.fs b/advanced_lighting/3.1.shadow_mapping/shaders/color.fs deleted file mode 100644 index bd00d82..0000000 --- a/advanced_lighting/3.1.shadow_mapping/shaders/color.fs +++ /dev/null @@ -1,9 +0,0 @@ -#version 330 core - -out vec4 frag_color; - -void -main(void) -{ - frag_color = vec4(1.0); -} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/color.vert b/advanced_lighting/3.1.shadow_mapping/shaders/color.vert new file mode 100644 index 0000000..6aa621e --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/color.vert @@ -0,0 +1,11 @@ +#version 330 core +layout(location = 0) in vec3 apos; + +uniform mat4 proj; +uniform mat4 view; +uniform mat4 model; + +void main(void) +{ + gl_Position = proj * view * model * vec4(apos, 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/color.vs b/advanced_lighting/3.1.shadow_mapping/shaders/color.vs deleted file mode 100644 index ade669b..0000000 --- a/advanced_lighting/3.1.shadow_mapping/shaders/color.vs +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 core -layout(location = 0) in vec3 apos; - -uniform mat4 projection; -uniform mat4 view; -uniform mat4 model; - -void -main(void) -{ - gl_Position = projection*view*model*vec4(apos, 1.0); -} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.frag b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.frag new file mode 100644 index 0000000..4cb6df1 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.frag @@ -0,0 +1,25 @@ +#version 330 core + +in vec2 tex_coords; + +out vec4 frag_color; + +uniform sampler2D depth_map; +uniform float near; +uniform float far; + +float linearize_depth(float depth) +{ + float z = (depth+1.0)*0.5; + return((2.0*near*far)/(far+near-z*(far-near))); +} + +void main(void) +{ + float depth = texture(depth_map, tex_coords).r; + + /* frag_color = vec4(vec3(depth), 1.0); */ + + /* NOTE(pryazha): For visual debugging of perspective projection light */ + frag_color = vec4(vec3(linearize_depth(depth)/far), 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.fs b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.fs deleted file mode 100644 index d250894..0000000 --- a/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.fs +++ /dev/null @@ -1,27 +0,0 @@ -#version 330 core - -in vec2 tex_coords; - -out vec4 frag_color; - -uniform sampler2D depth_map; -uniform float near; -uniform float far; - -float -linearize_depth(float depth) -{ - float z = (depth+1.0)*0.5; - return((2.0*near*far)/(far+near-z*(far-near))); -} - -void -main(void) -{ - float depth = texture(depth_map, tex_coords).r; - - /* frag_color = vec4(vec3(depth), 1.0); */ - - /* NOTE(pryazha): For visual debugging of perspective projection light */ - frag_color = vec4(vec3(linearize_depth(depth)/far), 1.0); -} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vert b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vert new file mode 100644 index 0000000..5717608 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vert @@ -0,0 +1,14 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +out vec2 tex_coords; + +uniform mat4 model; + +void +main(void) +{ + tex_coords = atex_coords; + gl_Position = model*vec4(apos, 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vs b/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vs deleted file mode 100644 index 5717608..0000000 --- a/advanced_lighting/3.1.shadow_mapping/shaders/debug_quad.vs +++ /dev/null @@ -1,14 +0,0 @@ -#version 330 core -layout(location = 0) in vec3 apos; -layout(location = 1) in vec2 atex_coords; - -out vec2 tex_coords; - -uniform mat4 model; - -void -main(void) -{ - tex_coords = atex_coords; - gl_Position = model*vec4(apos, 1.0); -} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/shadow.frag b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.frag new file mode 100644 index 0000000..3403d1d --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.frag @@ -0,0 +1,70 @@ +#version 330 core + +in VS_OUT { + vec4 frag_pos_light_space; + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} fs_in; + +out vec4 frag_color; + +uniform vec3 light_pos; +uniform vec3 view_pos; + +uniform sampler2D diffuse_texture; +uniform sampler2D shadow_map; + +float calculate_shadow() +{ + vec3 proj_coords = fs_in.frag_pos_light_space.xyz/fs_in.frag_pos_light_space.w; + proj_coords = proj_coords*0.5+0.5; + float closest_depth = texture(shadow_map, proj_coords.xy).r; + float current_depth = proj_coords.z; + vec3 normal = normalize(fs_in.normal); + vec3 light_dir = normalize(light_pos-fs_in.frag_pos); + float bias = max(0.04*(1.0-dot(normal, light_dir)), 0.004); + float shadow = 0; + vec2 texel_size = 1.0/textureSize(shadow_map, 0); + for (int x = -1; x <= 1; x++) + { + for (int y = -1; y <= 1; y++) + { + float pcf_depth = texture(shadow_map, proj_coords.xy+vec2(x, y)*texel_size).r; + shadow += (current_depth-bias > pcf_depth) ? 1.0 : 0.0; + } + } + shadow /= 9.0; + + if (proj_coords.z > 1.0) + shadow = 0.0; + + return(shadow); +} + +void main(void) +{ + vec3 color = texture(diffuse_texture, fs_in.tex_coords).rgb; + vec3 normal = normalize(fs_in.normal); + vec3 light_color = vec3(0.3); + + float ambient_strength = 0.01; + vec3 ambient = color*ambient_strength; + + vec3 light_dir = normalize(light_pos-fs_in.frag_pos); + float diff = max(dot(light_dir, normal), 0.0); + vec3 diffuse = diff*color; + + vec3 view_dir = normalize(view_pos-fs_in.frag_pos); + vec3 halfway_dir = normalize(light_dir+view_dir); + float spec = pow(max(dot(halfway_dir, normal), 0.0), 64.0); + vec3 specular = spec*color; + + float shadow = calculate_shadow(); + vec3 lighting = ambient+((diffuse+specular)*(1.0-shadow)*light_color); + + float gamma = 2.2; + lighting = pow(lighting, vec3(1.0/gamma)); + + frag_color = vec4(lighting, 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/shadow.fs b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.fs deleted file mode 100644 index 1c7018a..0000000 --- a/advanced_lighting/3.1.shadow_mapping/shaders/shadow.fs +++ /dev/null @@ -1,72 +0,0 @@ -#version 330 core - -in VS_OUT { - vec4 frag_pos_light_space; - vec3 frag_pos; - vec3 normal; - vec2 tex_coords; -} fs_in; - -out vec4 frag_color; - -uniform vec3 light_pos; -uniform vec3 view_pos; - -uniform sampler2D diffuse_texture; -uniform sampler2D shadow_map; - -float -calculate_shadow() -{ - vec3 proj_coords = fs_in.frag_pos_light_space.xyz/fs_in.frag_pos_light_space.w; - proj_coords = proj_coords*0.5+0.5; - float closest_depth = texture(shadow_map, proj_coords.xy).r; - float current_depth = proj_coords.z; - vec3 normal = normalize(fs_in.normal); - vec3 light_dir = normalize(light_pos-fs_in.frag_pos); - float bias = max(0.04*(1.0-dot(normal, light_dir)), 0.004); - float shadow = 0; - vec2 texel_size = 1.0/textureSize(shadow_map, 0); - for (int x = -1; x <= 1; x++) - { - for (int y = -1; y <= 1; y++) - { - float pcf_depth = texture(shadow_map, proj_coords.xy+vec2(x, y)*texel_size).r; - shadow += (current_depth-bias > pcf_depth) ? 1.0 : 0.0; - } - } - shadow /= 9.0; - - if (proj_coords.z > 1.0) - shadow = 0.0; - - return(shadow); -} - -void -main(void) -{ - vec3 color = texture(diffuse_texture, fs_in.tex_coords).rgb; - vec3 normal = normalize(fs_in.normal); - vec3 light_color = vec3(0.3); - - float ambient_strength = 0.01; - vec3 ambient = color*ambient_strength; - - vec3 light_dir = normalize(light_pos-fs_in.frag_pos); - float diff = max(dot(light_dir, normal), 0.0); - vec3 diffuse = diff*color; - - vec3 view_dir = normalize(view_pos-fs_in.frag_pos); - vec3 halfway_dir = normalize(light_dir+view_dir); - float spec = pow(max(dot(halfway_dir, normal), 0.0), 64.0); - vec3 specular = spec*color; - - float shadow = calculate_shadow(); - vec3 lighting = ambient+((diffuse+specular)*(1.0-shadow)*light_color); - - float gamma = 2.2; - lighting = pow(lighting, vec3(1.0/gamma)); - - frag_color = vec4(lighting, 1.0); -} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/shadow.vert b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.vert new file mode 100644 index 0000000..3f14663 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.vert @@ -0,0 +1,25 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; +layout(location = 2) in vec2 atex_coords; + +out VS_OUT { + vec4 frag_pos_light_space; + vec3 frag_pos; + vec3 normal; + vec2 tex_coords; +} vs_out; + +uniform mat4 proj; +uniform mat4 view; +uniform mat4 model; +uniform mat4 light_space_matrix; + +void main(void) +{ + vs_out.frag_pos = vec3(model*vec4(apos, 1.0)); + vs_out.frag_pos_light_space = light_space_matrix*vec4(vs_out.frag_pos, 1.0); + vs_out.normal = mat3(transpose(inverse(model)))*anormal; + vs_out.tex_coords = atex_coords; + gl_Position = proj*view*vec4(vs_out.frag_pos, 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/shadow.vs b/advanced_lighting/3.1.shadow_mapping/shaders/shadow.vs deleted file mode 100644 index 3ecd5b1..0000000 --- a/advanced_lighting/3.1.shadow_mapping/shaders/shadow.vs +++ /dev/null @@ -1,26 +0,0 @@ -#version 330 core -layout(location = 0) in vec3 apos; -layout(location = 1) in vec3 anormal; -layout(location = 2) in vec2 atex_coords; - -out VS_OUT { - vec4 frag_pos_light_space; - vec3 frag_pos; - vec3 normal; - vec2 tex_coords; -} vs_out; - -uniform mat4 projection; -uniform mat4 view; -uniform mat4 model; -uniform mat4 light_space_matrix; - -void -main(void) -{ - vs_out.frag_pos = vec3(model*vec4(apos, 1.0)); - vs_out.frag_pos_light_space = light_space_matrix*vec4(vs_out.frag_pos, 1.0); - vs_out.normal = mat3(transpose(inverse(model)))*anormal; - vs_out.tex_coords = atex_coords; - gl_Position = projection*view*vec4(vs_out.frag_pos, 1.0); -} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.frag b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.frag new file mode 100644 index 0000000..d99aab2 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.frag @@ -0,0 +1,5 @@ +#version 330 core + +void main(void) +{ +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.fs b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.fs deleted file mode 100644 index 16b9908..0000000 --- a/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.fs +++ /dev/null @@ -1,6 +0,0 @@ -#version 330 core - -void -main(void) -{ -} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vert b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vert new file mode 100644 index 0000000..d3adca9 --- /dev/null +++ b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vert @@ -0,0 +1,10 @@ +#version 330 core +layout(location = 0) in vec3 apos; + +uniform mat4 light_space_matrix; +uniform mat4 model; + +void main(void) +{ + gl_Position = light_space_matrix*model*vec4(apos, 1.0); +} diff --git a/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vs b/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vs deleted file mode 100644 index b9752ed..0000000 --- a/advanced_lighting/3.1.shadow_mapping/shaders/simple_depth.vs +++ /dev/null @@ -1,11 +0,0 @@ -#version 330 core -layout(location = 0) in vec3 apos; - -uniform mat4 light_space_matrix; -uniform mat4 model; - -void -main(void) -{ - gl_Position = light_space_matrix*model*vec4(apos, 1.0); -} -- cgit v1.2.3