From d65ddd07a43d5ffdcf2ddf90d6f86626cf9b92d8 Mon Sep 17 00:00:00 2001 From: pryazha Date: Tue, 18 Mar 2025 11:31:31 +0500 Subject: hdr --- .../5.parallax_mapping/shaders/parallax.frag | 126 +++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 advanced_lighting/5.parallax_mapping/shaders/parallax.frag (limited to 'advanced_lighting/5.parallax_mapping/shaders/parallax.frag') diff --git a/advanced_lighting/5.parallax_mapping/shaders/parallax.frag b/advanced_lighting/5.parallax_mapping/shaders/parallax.frag new file mode 100644 index 0000000..6813a30 --- /dev/null +++ b/advanced_lighting/5.parallax_mapping/shaders/parallax.frag @@ -0,0 +1,126 @@ +#version 330 core + +in VS_OUT { + vec3 frag_pos; + vec2 tex_coords; + vec3 tangent_light_pos; + vec3 tangent_view_pos; + vec3 tangent_frag_pos; +} fs_in; + +out vec4 frag_color; + +uniform sampler2D diffuse_texture; +uniform sampler2D normal_map; +uniform sampler2D depth_map; + +uniform float hscale; + +vec2 parallax_mapping(vec2 tex_coords, vec3 view_dir) +{ + float h; + vec2 p, r; + + h = texture(depth_map, tex_coords).r; + p = view_dir.xy/view_dir.z*h*hscale; + r = tex_coords-p; + + return r; +} + +vec2 step_parallax_mapping(vec2 tex_coords, vec3 view_dir) +{ + float minsteps, maxsteps, nsteps, step, dstep, depth; + vec2 p, cur_tex_coords, dtex_coords; + + minsteps = 8.0; + maxsteps = 32.0; + nsteps = mix(maxsteps, minsteps, max(dot(vec3(0.0, 0.0, 1.0), view_dir), 0.0)); + + dstep = 1.0/nsteps; + step = 0.0; + + p = view_dir.xy*hscale; + dtex_coords = p/nsteps; + + cur_tex_coords = tex_coords; + depth = texture(depth_map, cur_tex_coords).r; + + while (step < depth) { + cur_tex_coords -= dtex_coords; + depth = texture(depth_map, cur_tex_coords).r; + step += dstep; + } + + return cur_tex_coords; +} + +vec2 parallax_occlusion_mapping(vec2 tex_coords, vec3 view_dir) +{ + float minsteps, maxsteps, nsteps, step, dstep, depth, + after_depth, before_depth, weight; + vec2 p, cur_tex_coords, dtex_coords, prev_tex_coords, result; + + minsteps = 8.0; + maxsteps = 32.0; + nsteps = mix(maxsteps, minsteps, max(dot(vec3(0.0, 0.0, 1.0), view_dir), 0.0)); + + dstep = 1.0/nsteps; + step = 0.0; + + p = view_dir.xy*hscale; + dtex_coords = p/nsteps; + + cur_tex_coords = tex_coords; + depth = texture(depth_map, cur_tex_coords).r; + + while (step < depth) { + cur_tex_coords -= dtex_coords; + depth = texture(depth_map, cur_tex_coords).r; + step += dstep; + } + + prev_tex_coords = cur_tex_coords+dtex_coords; + + after_depth = depth-step; + before_depth = texture(depth_map, prev_tex_coords).r-step+dstep; + + weight = after_depth/(after_depth-before_depth); + result = prev_tex_coords*weight+cur_tex_coords*(1.0-weight); + + return result; +} + +void main(void) +{ + vec3 color, light_color, + normal, view_dir, halfway_dir, light_dir, + ambient, diffuse, specular, result; + vec2 tex_coords; + float diff, spec; + + view_dir = normalize(fs_in.tangent_view_pos-fs_in.tangent_frag_pos); + tex_coords = parallax_occlusion_mapping(fs_in.tex_coords, view_dir); + if ((tex_coords.x > 1.0) || (tex_coords.y > 1.0) || (tex_coords.x < 0.0) || (tex_coords.y < 0.0)) + discard; + + color = vec3(texture(diffuse_texture, tex_coords)); + normal = vec3(texture(normal_map, tex_coords)); + normal = normalize((normal*2.0)-1.0); + + light_color = vec3(1.0); + + ambient = vec3(0.01); + + light_dir = normalize(fs_in.tangent_light_pos-fs_in.tangent_frag_pos); + diff = max(dot(light_dir, normal), 0.0); + diffuse = diff*light_color; + + halfway_dir = normalize(light_dir+view_dir); + spec = pow(max(dot(halfway_dir, normal), 0.0), 64.0); + specular = spec*light_color; + + result = (ambient+diffuse+specular)*color; + + frag_color = vec4(result, 1.0); +} -- cgit v1.2.3