diff options
Diffstat (limited to 'advanced_lighting/4.normal_mapping/shaders')
12 files changed, 182 insertions, 185 deletions
diff --git a/advanced_lighting/4.normal_mapping/shaders/color.frag b/advanced_lighting/4.normal_mapping/shaders/color.frag new file mode 100644 index 0000000..c01da99 --- /dev/null +++ b/advanced_lighting/4.normal_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/4.normal_mapping/shaders/color.vert b/advanced_lighting/4.normal_mapping/shaders/color.vert new file mode 100644 index 0000000..a8b1247 --- /dev/null +++ b/advanced_lighting/4.normal_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/4.normal_mapping/shaders/depth.fs b/advanced_lighting/4.normal_mapping/shaders/depth.fs deleted file mode 100644 index 88fb9e6..0000000 --- a/advanced_lighting/4.normal_mapping/shaders/depth.fs +++ /dev/null @@ -1,14 +0,0 @@ -#version 330 core - -in vec4 frag_pos; - -uniform vec3 light_pos; -uniform float far; - -void -main(void) -{ - float light_distance = length(frag_pos.xyz-light_pos); - light_distance = light_distance/far; - gl_FragDepth = light_distance; -} diff --git a/advanced_lighting/4.normal_mapping/shaders/depth.gs b/advanced_lighting/4.normal_mapping/shaders/depth.gs deleted file mode 100644 index 638e2b9..0000000 --- a/advanced_lighting/4.normal_mapping/shaders/depth.gs +++ /dev/null @@ -1,27 +0,0 @@ -#version 330 core -layout(triangles) in; -layout(triangle_strip, max_vertices=18) out; - -uniform mat4 shadow_transforms[6]; - -out vec4 frag_pos; - -void -main(void) -{ - for (int face = 0; - face < 6; - ++face) - { - gl_Layer = face; - for (int vertex = 0; - vertex < 3; - ++vertex) - { - frag_pos = gl_in[vertex].gl_Position; - gl_Position = shadow_transforms[face]*frag_pos; - EmitVertex(); - } - EndPrimitive(); - } -} diff --git a/advanced_lighting/4.normal_mapping/shaders/depth.vs b/advanced_lighting/4.normal_mapping/shaders/depth.vs deleted file mode 100644 index 37a3484..0000000 --- a/advanced_lighting/4.normal_mapping/shaders/depth.vs +++ /dev/null @@ -1,10 +0,0 @@ -#version 330 core -layout(location = 0) in vec3 apos; - -uniform mat4 model; - -void -main(void) -{ - gl_Position = model*vec4(apos, 1.0); -} diff --git a/advanced_lighting/4.normal_mapping/shaders/normal_map.frag b/advanced_lighting/4.normal_mapping/shaders/normal_map.frag new file mode 100644 index 0000000..964d6f8 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/normal_map.frag @@ -0,0 +1,51 @@ +#version 330 core + +in VS_OUT { + /* mat3 TBN; */ + vec3 frag_pos; + vec3 tangent_light_pos; + vec3 tangent_view_pos; + vec3 tangent_frag_pos; + vec2 tex_coords; +} fs_in; + +out vec4 frag_color; + +/* uniform vec3 light_pos; */ +/* uniform vec3 view_pos; */ + +uniform sampler2D diffuse_texture; +uniform sampler2D normal_map; + +void main(void) +{ + vec3 color = vec3(texture(diffuse_texture, fs_in.tex_coords)); + vec3 normal = vec3(texture(normal_map, fs_in.tex_coords)); + normal = normalize((normal*2.0)-1.0); + /* + normal = normalize(fs_in.TBN*normal); + + vec3 light_dir = fs_in.TBN * normalize(light_pos - fs_in.frag_pos); + vec3 view_dir = fs_in.TBN * normalize(view_pos - fs_in.frag_pos); + */ + + vec3 light_color = vec3(1.0); + + float ambient = 0.01; + + /* vec3 light_dir = normalize(light_pos-fs_in.frag_pos); */ + vec3 light_dir = normalize(fs_in.tangent_light_pos - fs_in.tangent_frag_pos); + float diff = max(dot(light_dir, normal), 0.0); + vec3 diffuse = diff*light_color; + + /* vec3 view_dir = normalize(view_pos-fs_in.frag_pos); */ + vec3 view_dir = normalize(fs_in.tangent_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); + float spec_strength = 1.0; + vec3 specular = spec_strength*spec*light_color; + + vec3 result = (ambient+diffuse+specular)*color; + + frag_color = vec4(result, 1.0); +} diff --git a/advanced_lighting/4.normal_mapping/shaders/normal_map.vert b/advanced_lighting/4.normal_mapping/shaders/normal_map.vert new file mode 100644 index 0000000..3b089b5 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/normal_map.vert @@ -0,0 +1,36 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; +layout(location = 2) in vec2 atex_coords; +layout(location = 3) in vec3 atangent; +layout(location = 4) in vec3 abitangent; + +out VS_OUT { + /* mat3 TBN; */ + vec3 frag_pos; + vec3 tangent_light_pos; + vec3 tangent_view_pos; + vec3 tangent_frag_pos; + vec2 tex_coords; +} vs_out; + +uniform mat4 proj; +uniform mat4 view; +uniform mat4 model; + +uniform vec3 light_pos; +uniform vec3 view_pos; + +void main(void) +{ + vs_out.frag_pos = vec3(model * vec4(apos, 1.0)); + vs_out.tex_coords = atex_coords; + vec3 T = normalize(vec3(model * vec4(atangent, 0.0))); + vec3 B = normalize(vec3(model * vec4(abitangent, 0.0))); + vec3 N = normalize(vec3(model * vec4(anormal, 0.0))); + mat3 TBN = transpose(mat3(T, B, N)); + vs_out.tangent_light_pos = TBN * light_pos; + vs_out.tangent_view_pos = TBN * view_pos; + vs_out.tangent_frag_pos = TBN * vec3(model * vec4(apos, 0.0)); + gl_Position = proj * view * vec4(vs_out.frag_pos, 1.0); +} diff --git a/advanced_lighting/4.normal_mapping/shaders/normals_debug.frag b/advanced_lighting/4.normal_mapping/shaders/normals_debug.frag new file mode 100644 index 0000000..04dadd4 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/normals_debug.frag @@ -0,0 +1,10 @@ +#version 330 core + +in vec3 vcolor; + +out vec4 frag_color; + +void main(void) +{ + frag_color = vec4(vcolor, 1.0); +} diff --git a/advanced_lighting/4.normal_mapping/shaders/normals_debug.geom b/advanced_lighting/4.normal_mapping/shaders/normals_debug.geom new file mode 100644 index 0000000..666c08a --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/normals_debug.geom @@ -0,0 +1,44 @@ +#version 330 core +layout(triangles) in; +layout(line_strip, max_vertices=18) out; + +in VS_OUT { + vec4 tangent; + vec4 bitangent; + vec4 normal; +} gs_in[]; + +out vec3 vcolor; + +uniform mat4 proj; + +void gen_normals(int index) +{ + vcolor = vec3(1.0, 0.0, 0.0); + gl_Position = proj*(gl_in[index].gl_Position); + EmitVertex(); + gl_Position = proj*(gl_in[index].gl_Position+0.5*gs_in[index].tangent); + EmitVertex(); + EndPrimitive(); + + vcolor = vec3(0.0, 0.0, 1.0); + gl_Position = proj*(gl_in[index].gl_Position); + EmitVertex(); + gl_Position = proj*(gl_in[index].gl_Position+0.5*gs_in[index].bitangent); + EmitVertex(); + EndPrimitive(); + + vcolor = vec3(0.0, 1.0, 0.0); + gl_Position = proj*(gl_in[index].gl_Position); + EmitVertex(); + gl_Position = proj*(gl_in[index].gl_Position+0.5*gs_in[index].normal); + EmitVertex(); + EndPrimitive(); +} + +void main(void) +{ + gen_normals(0); + gen_normals(1); + gen_normals(2); +} diff --git a/advanced_lighting/4.normal_mapping/shaders/normals_debug.vert b/advanced_lighting/4.normal_mapping/shaders/normals_debug.vert new file mode 100644 index 0000000..d3eed77 --- /dev/null +++ b/advanced_lighting/4.normal_mapping/shaders/normals_debug.vert @@ -0,0 +1,22 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; +layout(location = 3) in vec3 atangent; +layout(location = 4) in vec3 abitangent; + +out VS_OUT { + vec4 tangent; + vec4 bitangent; + vec4 normal; +} vs_out; + +uniform mat4 view; +uniform mat4 model; + +void main(void) +{ + vs_out.tangent = view*model*vec4(normalize(atangent), 0.0); + vs_out.bitangent = view*model*vec4(normalize(abitangent), 0.0); + vs_out.normal = view*model*vec4(normalize(anormal), 0.0); + gl_Position = view*model*vec4(apos, 1.0); +} diff --git a/advanced_lighting/4.normal_mapping/shaders/shadow.fs b/advanced_lighting/4.normal_mapping/shaders/shadow.fs deleted file mode 100644 index 6a3a8b0..0000000 --- a/advanced_lighting/4.normal_mapping/shaders/shadow.fs +++ /dev/null @@ -1,105 +0,0 @@ -#version 330 core - -in VS_OUT { - vec3 frag_pos; - vec3 normal; - vec2 tex_coords; -} fs_in; - -out vec4 frag_color; - -uniform vec3 light_pos; -uniform vec3 view_pos; -uniform float far; - -uniform sampler2D diffuse_texture; -uniform samplerCube depth_cubemap; - -vec3 grid_sampling_disk[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) -); - -float -calculate_shadow(vec3 frag_pos) -{ - vec3 frag_to_light = frag_pos-light_pos; - /* float closest_depth = texture(depth_cubemap, frag_to_light).r; */ - /* closest_depth *= far; */ - float current_depth = length(frag_to_light); - /* float bias = 0.05; */ - /* float shadow = current_depth-bias > closest_depth ? 1.0 : 0.0; */ - /* NOTE(pryazha): Display depth value for debugging */ - /* frag_color = vec4(vec3(closest_depth/far), 1.0); */ - /* - float shadow = 0.0; - float offset = 0.1; - float samples = 4.0; - for (float x = -offset; - x < offset; - x += offset/(samples*0.5)) - { - for (float y = -offset; - y < offset; - y += offset/(samples*0.5)) - { - for (float z = -offset; - z < offset; - z += offset/(samples*0.5)) - { - float closest_depth = texture(depth_cubemap, frag_to_light+vec3(x, y, z)).r; - closest_depth *= far; - if (current_depth-bias > closest_depth) - shadow += 1.0; - } - } - } - shadow /= (samples*samples*samples); - */ - - float shadow = 0.0; - float bias = 0.15; - int samples = 20; - float view_distance = length(view_pos-frag_pos); - float disk_radius = (1.0+(view_distance/far))/25.0; - for (int sample = 0; - sample < samples; - ++sample) - { - float closest_depth = texture(depth_cubemap, frag_to_light+ - grid_sampling_disk[sample]*disk_radius).r; - closest_depth *= far; - if (current_depth-bias > closest_depth) - shadow += 1.0; - } - shadow /= float(samples); - 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.5); - - vec3 ambient = 0.5*light_color; - - vec3 light_dir = normalize(light_pos-fs_in.frag_pos); - float diff = max(dot(light_dir, normal), 0.0); - vec3 diffuse = diff*light_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*light_color; - - float shadow = calculate_shadow(fs_in.frag_pos); - vec3 lighting = (ambient+(1.0-shadow)*(diffuse+specular))*color; - - frag_color = vec4(lighting, 1.0); -} diff --git a/advanced_lighting/4.normal_mapping/shaders/shadow.vs b/advanced_lighting/4.normal_mapping/shaders/shadow.vs deleted file mode 100644 index ac8ee4f..0000000 --- a/advanced_lighting/4.normal_mapping/shaders/shadow.vs +++ /dev/null @@ -1,29 +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 { - vec3 frag_pos; - vec3 normal; - vec2 tex_coords; -} vs_out; - -uniform mat4 projection; -uniform mat4 view; -uniform mat4 model; - -uniform bool reverse_normals; - -void -main(void) -{ - vs_out.frag_pos = vec3(model*vec4(apos, 1.0)); - if (reverse_normals) - vs_out.normal = mat3(transpose(inverse(model)))*(-1.0*anormal); - else - 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); -} |