From e024fea6f3948f020e072cab40475c547e30282c Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Thu, 12 Dec 2024 21:54:04 +0100 Subject: [PATCH] Sync --- src/6_bump.fs | 37 +++++++++++++++++++++++++++++++++++-- src/6_bump_position.glsl | 6 ++++-- src/6_tangent.glsl | 10 +++++++--- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/6_bump.fs b/src/6_bump.fs index d784656..848a352 100644 --- a/src/6_bump.fs +++ b/src/6_bump.fs @@ -25,7 +25,40 @@ void main() float terrain_color = clamp(1+5*b,0,1); ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - color = terrain_color * vec3(1,1,1); + // Replace with your code + float r = 10.0; + float h = 8.0; + float f = 8.0; + float p = 1000.0; + + float x = r * cos(2.0 * M_PI * animation_seconds / f); + float z = r * sin(2.0 * M_PI * animation_seconds / f); + vec3 light_pos = vec3(x, h, z); + + // Calculate the light direction + vec3 l = normalize(light_pos - sphere_fs_in); + + // Calculate the view direction + vec3 v = normalize(view_pos_fs_in.xyz - sphere_fs_in); + v = normalize(-view_pos_fs_in.xyz); + + // Calculate the normal direction + vec3 n = normalize(normal_fs_in); + // Apply the bump map + vec3 T; + vec3 B; + tangent(n, T, B); + float eps = 0.0001; + n = ((bump_position(is_moon, n + eps * T) - bump_position(is_moon, s)) / eps) + * ((bump_position(is_moon, n + eps * B) - bump_position(is_moon, s)) / eps); + n = normalize(n); + + // Base colors + vec3 ka = vec3(0.1, 0.1, 0.1); // Ambient color + vec3 kd = is_moon ? vec3(0.5, 0.45, 0.5) : vec3(0.2, 0.3, 0.8); // Diffuse color + vec3 ks = vec3(1.0, 1.0, 1.0); // Specular color + + // Compute the Blinn-Phong shading + color = blinn_phong(ka, kd, ks, p, n, v, l); ///////////////////////////////////////////////////////////////////////////// } diff --git a/src/6_bump_position.glsl b/src/6_bump_position.glsl index 6c3e470..4642626 100644 --- a/src/6_bump_position.glsl +++ b/src/6_bump_position.glsl @@ -14,7 +14,9 @@ vec3 bump_position(bool is_moon , vec3 s) { ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - return s; + // Replace with your code + float bump = bump_height(is_moon, s); + /* p' = p + bump * n but on a unit sphere object */ + return s + bump_height(is_moon, s) * s; ///////////////////////////////////////////////////////////////////////////// } diff --git a/src/6_tangent.glsl b/src/6_tangent.glsl index 2478e3d..a7e107e 100644 --- a/src/6_tangent.glsl +++ b/src/6_tangent.glsl @@ -10,8 +10,12 @@ void tangent(in vec3 N, out vec3 T, out vec3 B) { ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - T = vec3(1,0,0); - B = vec3(0,1,0); + // Replace with your code + // Calculate a vector that is perpendicular to N + vec3 doesnotmatter = abs(N.y) < 0.999 ? vec3(0,1,0) : vec3(1,0,0); + T = normalize(cross(doesnotmatter, N)); + // Now T is perpendicular to N + // Take B as the cross product of N and T, to make it perpendicular to both + B = cross(N, T); ///////////////////////////////////////////////////////////////////////////// }