summaryrefslogtreecommitdiff
path: root/advanced_lighting/5.parallax_mapping/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'advanced_lighting/5.parallax_mapping/shaders')
-rw-r--r--advanced_lighting/5.parallax_mapping/shaders/normals_debug.frag10
-rw-r--r--advanced_lighting/5.parallax_mapping/shaders/normals_debug.geom44
-rw-r--r--advanced_lighting/5.parallax_mapping/shaders/normals_debug.vert22
-rw-r--r--advanced_lighting/5.parallax_mapping/shaders/parallax.frag126
-rw-r--r--advanced_lighting/5.parallax_mapping/shaders/parallax.vert37
5 files changed, 239 insertions, 0 deletions
diff --git a/advanced_lighting/5.parallax_mapping/shaders/normals_debug.frag b/advanced_lighting/5.parallax_mapping/shaders/normals_debug.frag
new file mode 100644
index 0000000..32e36a7
--- /dev/null
+++ b/advanced_lighting/5.parallax_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/5.parallax_mapping/shaders/normals_debug.geom b/advanced_lighting/5.parallax_mapping/shaders/normals_debug.geom
new file mode 100644
index 0000000..79fd4f6
--- /dev/null
+++ b/advanced_lighting/5.parallax_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/5.parallax_mapping/shaders/normals_debug.vert b/advanced_lighting/5.parallax_mapping/shaders/normals_debug.vert
new file mode 100644
index 0000000..935fb84
--- /dev/null
+++ b/advanced_lighting/5.parallax_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/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);
+}
diff --git a/advanced_lighting/5.parallax_mapping/shaders/parallax.vert b/advanced_lighting/5.parallax_mapping/shaders/parallax.vert
new file mode 100644
index 0000000..a3451c3
--- /dev/null
+++ b/advanced_lighting/5.parallax_mapping/shaders/parallax.vert
@@ -0,0 +1,37 @@
+#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 {
+ vec3 frag_pos;
+ vec2 tex_coords;
+ vec3 tangent_light_pos;
+ vec3 tangent_view_pos;
+ vec3 tangent_frag_pos;
+} vs_out;
+
+uniform mat4 proj;
+uniform mat4 view;
+uniform mat4 model;
+
+uniform vec3 light_pos;
+uniform vec3 view_pos;
+
+void main(void)
+{
+ gl_Position = proj*view*model*vec4(apos, 1.0);
+ vs_out.frag_pos = vec3(model*vec4(apos, 1.0));
+ vs_out.tex_coords = atex_coords;
+
+ vec3 T = normalize(mat3(model)*atangent);
+ vec3 B = normalize(mat3(model)*abitangent);
+ vec3 N = normalize(mat3(model)*anormal);
+ 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*vs_out.frag_pos;
+}