summaryrefslogtreecommitdiff
path: root/advanced_lighting/5.parallax_mapping
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-03-18 11:31:31 +0500
committerpryazha <pryadeiniv@mail.ru>2025-03-18 11:31:31 +0500
commitd65ddd07a43d5ffdcf2ddf90d6f86626cf9b92d8 (patch)
tree672efce6089ec9c1103be0c1b70a28ec415eeeab /advanced_lighting/5.parallax_mapping
parentd64a159d05a45e75870f61c37f0defa94f03793e (diff)
Diffstat (limited to 'advanced_lighting/5.parallax_mapping')
-rwxr-xr-xadvanced_lighting/5.parallax_mapping/build.sh5
-rw-r--r--advanced_lighting/5.parallax_mapping/parallax_mapping.c253
-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
7 files changed, 497 insertions, 0 deletions
diff --git a/advanced_lighting/5.parallax_mapping/build.sh b/advanced_lighting/5.parallax_mapping/build.sh
new file mode 100755
index 0000000..e36f110
--- /dev/null
+++ b/advanced_lighting/5.parallax_mapping/build.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+. ../../config
+TARGET='parallax_mapping'
+set -x
+gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS
diff --git a/advanced_lighting/5.parallax_mapping/parallax_mapping.c b/advanced_lighting/5.parallax_mapping/parallax_mapping.c
new file mode 100644
index 0000000..35859d2
--- /dev/null
+++ b/advanced_lighting/5.parallax_mapping/parallax_mapping.c
@@ -0,0 +1,253 @@
+#include "GL/glew.h"
+#include "GLFW/glfw3.h"
+
+#include "pwyazh.h"
+#include "pwyazh_GL.h"
+
+#include "common.h"
+
+U32 gen_quad_with_tan()
+{
+ /* Positions */
+ V3F p1 = v3f(-1.0f, 1.0f, 0.0f);
+ V3F p2 = v3f(-1.0f, -1.0f, 0.0f);
+ V3F p3 = v3f(1.0f, -1.0f, 0.0f);
+ V3F p4 = v3f(1.0f, 1.0f, 0.0f);
+ /* Texture ccoordinates */
+ V2F uv1 = v2f(0.0f, 1.0f);
+ V2F uv2 = v2f(0.0f, 0.0f);
+ V2F uv3 = v2f(1.0f, 0.0f);
+ V2F uv4 = v2f(1.0f, 1.0f);
+ /* Normal vector */
+ V3F nm = v3f(0.0f, 0.0f, 1.0f);
+
+ V3F edge1, edge2;
+ V2F duv1, duv2;
+ F32 f;
+ V3F t1, bit1, t2, bit2;
+
+ edge1 = v3f_sub(p2, p1);
+ edge2 = v3f_sub(p3, p1);
+ duv1 = v2f_sub(uv2, uv1);
+ duv2 = v2f_sub(uv3, uv1);
+
+ f = 1.0f/(duv1.x*duv2.y-duv2.x*duv1.y);
+
+ t1.x = f*(duv2.y*edge1.x-duv1.y*edge2.x);
+ t1.y = f*(duv2.y*edge1.y-duv1.y*edge2.y);
+ t1.z = f*(duv2.y*edge1.z-duv1.y*edge2.z);
+
+ bit1.x = f*(-duv2.x*edge1.x+duv1.x*edge2.x);
+ bit1.y = f*(-duv2.x*edge1.y+duv1.x*edge2.y);
+ bit1.z = f*(-duv2.x*edge1.z+duv1.x*edge2.z);
+
+ edge1 = v3f_sub(p3, p1);
+ edge2 = v3f_sub(p4, p1);
+ duv1 = v2f_sub(uv3, uv1);
+ duv2 = v2f_sub(uv4, uv1);
+
+ f = 1.0f/(duv1.x*duv2.y-duv2.x*duv1.y);
+
+ t2.x = f*(duv2.y*edge1.x-duv1.y*edge2.x);
+ t2.y = f*(duv2.y*edge1.y-duv1.y*edge2.y);
+ t2.z = f*(duv2.y*edge1.z-duv1.y*edge2.z);
+
+ bit2.x = f*(-duv2.x*edge1.x+duv1.x*edge2.x);
+ bit2.y = f*(-duv2.x*edge1.y+duv1.x*edge2.y);
+ bit2.z = f*(-duv2.x*edge1.z+duv1.x*edge2.z);
+
+ F32 quad_vertices[] = {
+ p1.x, p1.y, p1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, t1.x, t1.y, t1.z, bit1.x, bit1.y, bit1.z,
+ p2.x, p2.y, p2.z, nm.x, nm.y, nm.z, uv2.x, uv2.y, t1.x, t1.y, t1.z, bit1.x, bit1.y, bit1.z,
+ p3.x, p3.y, p3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, t1.x, t1.y, t1.z, bit1.x, bit1.y, bit1.z,
+
+ p1.x, p1.y, p1.z, nm.x, nm.y, nm.z, uv1.x, uv1.y, t2.x, t2.y, t2.z, bit2.x, bit2.y, bit2.z,
+ p3.x, p3.y, p3.z, nm.x, nm.y, nm.z, uv3.x, uv3.y, t2.x, t2.y, t2.z, bit2.x, bit2.y, bit2.z,
+ p4.x, p4.y, p4.z, nm.x, nm.y, nm.z, uv4.x, uv4.y, t2.x, t2.y, t2.z, bit2.x, bit2.y, bit2.z
+ };
+ U32 quad_vao, vbo;
+ glGenVertexArrays(1, &quad_vao);
+ glBindVertexArray(quad_vao);
+ glGenBuffers(1, &vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vertices), quad_vertices, GL_STATIC_DRAW);
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 14*sizeof(F32), (void *)0);
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 14*sizeof(F32), (void *)(3*sizeof(F32)));
+ glEnableVertexAttribArray(2);
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 14*sizeof(F32), (void *)(6*sizeof(F32)));
+ glEnableVertexAttribArray(3);
+ glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 14*sizeof(F32), (void *)(8*sizeof(F32)));
+ glEnableVertexAttribArray(4);
+ glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 14*sizeof(F32), (void *)(11*sizeof(F32)));
+ glBindVertexArray(0);
+
+ return quad_vao;
+}
+
+int main(void)
+{
+ GLFWwindow *window;
+ State state;
+ Input input = {0};
+ F64 time, last_time;
+ V3F camera_dp;
+ Arena *arena = 0;
+ S32 width, height;
+
+ U32 vao, parallax_shader, normals_debug_shader;
+ U32 diffuse_texture, normal_texture, depth_texture;
+
+ V3F light_pos;
+
+ width = 800;
+ height = 600;
+
+ /* NOTE(pryazha): GLFW init */
+ glfwSetErrorCallback(error_callback);
+
+ if (glfwInit() == GLFW_FALSE)
+ return(1);
+
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+ window = glfwCreateWindow(width, height, "Point shadows", 0, 0);
+ if (!window)
+ goto error;
+
+ glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
+
+ glfwMakeContextCurrent(window);
+
+ if (glewInit() != GLEW_OK)
+ goto error;
+
+ /* NOTE(pryazha): Program init */
+ arena = arena_alloc(Kilobytes(4));
+
+ input.first_mouse = 1;
+
+ state.camera = (Camera){ v3f(0.0f, 0.0f, 3.0f), 90.0f, 0.1f, 100.0f, 0.0f, 0.0f };
+ camera_dp = v3f_zero();
+
+ light_pos = v3f(1.0f, 2.0f, 1.0f);
+
+
+ /* NOTE(pryazha): Meshes */
+ vao = gen_quad_with_tan();
+
+ diffuse_texture = load_texture("../../data/textures/bricks2.jpg");
+ normal_texture = load_texture("../../data/textures/bricks2_normal.jpg");
+ depth_texture = load_texture("../../data/textures/bricks2_disp.jpg");
+
+ /* NOTE(pryazha): Shaders */
+ parallax_shader = create_shader_program("shaders/parallax.vert", "shaders/parallax.frag");
+ normals_debug_shader = create_shader_program_geom("shaders/normals_debug.vert",
+ "shaders/normals_debug.frag",
+ "shaders/normals_debug.geom");
+
+ glUseProgram(parallax_shader);
+ shader_set_1i(parallax_shader, "diffuse_texture", 0);
+ shader_set_1i(parallax_shader, "normal_map", 1);
+ shader_set_1i(parallax_shader, "depth_map", 2);
+ glUseProgram(0);
+
+ last_time = glfwGetTime();
+
+ while (!glfwWindowShouldClose(window)) {
+ glfwPollEvents();
+
+ /* NOTE(pryazha): Update */
+ F32 speed, sensitivity;
+ V3F dv;
+
+ glfwGetFramebufferSize(window, &width, &height);
+ process_glfw_keyboard(window, &input);
+ process_glfw_mouse_pos(window, &input);
+
+ if (key_first_press(input.exit))
+ glfwSetWindowShouldClose(window, GLFW_TRUE);
+
+ speed = 2.0f;
+ dv = get_dv_camera_first_person(&input, &state.camera, speed, state.dt);
+ if (key_is_pressed(input.action_up))
+ dv = v3f_scalef(dv, 3.0f);
+ camera_dp = v3f_add(camera_dp, dv);
+ camera_dp = v3f_scalef(camera_dp, 0.8f);
+ state.camera.pos = v3f_add(state.camera.pos, camera_dp);
+
+ sensitivity = 0.1f;
+ input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity);
+ state.camera.yaw += input.mouse_offset.x;
+ state.camera.pitch += input.mouse_offset.y;
+ if (state.camera.pitch > 89.0f)
+ state.camera.pitch = 89.0f;
+ if (state.camera.pitch < -89.0f)
+ state.camera.pitch = -89.0f;
+
+ input_update_last_state(&input);
+
+ /* NOTE(pryazha): Render */
+ F32 ar;
+ MAT4 proj, view, model;
+
+ ar = (F32)width/(F32)height;
+ proj = camera_persp(state.camera, ar);
+ view = get_view_matrix(&state.camera);
+ model = mat4_identity();
+
+ glViewport(0, 0, width, height);
+ glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ /* NOTE(pryazha): Draw the quad */
+ glUseProgram(parallax_shader);
+ shader_set_mat4fv(parallax_shader, "proj", proj);
+ shader_set_mat4fv(parallax_shader, "view", view);
+ shader_set_mat4fv(parallax_shader, "model", model);
+ shader_set_3fv(parallax_shader, "light_pos", light_pos);
+ shader_set_3fv(parallax_shader, "view_pos", state.camera.pos);
+ shader_set_1f(parallax_shader, "hscale", 0.1f);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, diffuse_texture);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, normal_texture);
+ glActiveTexture(GL_TEXTURE2);
+ glBindTexture(GL_TEXTURE_2D, depth_texture);
+ glBindVertexArray(vao);
+ glDrawArrays(GL_TRIANGLES, 0, 6);
+ glBindVertexArray(0);
+ glUseProgram(0);
+
+ /* NOTE(pryazha): Draw normals */
+ glUseProgram(normals_debug_shader);
+ shader_set_mat4fv(normals_debug_shader, "proj", proj);
+ shader_set_mat4fv(normals_debug_shader, "model", model);
+ shader_set_mat4fv(normals_debug_shader, "view", view);
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, diffuse_texture);
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, normal_texture);
+ glBindVertexArray(vao);
+ glDrawArrays(GL_TRIANGLES, 0, 6);
+ glBindVertexArray(0);
+ glUseProgram(0);
+
+ glfwSwapBuffers(window);
+
+ time = glfwGetTime();
+ state.dt = time-last_time;
+ last_time = time;
+ }
+
+ glDeleteVertexArrays(1, &vao);
+ arena_release(arena);
+ glfwTerminate();
+ return(0);
+
+error:
+ arena_release(arena);
+ glfwTerminate();
+ return(1);
+}
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;
+}