From 745dfdd5869898277e6e723ea6939d9fa9de65d3 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Mon, 9 Dec 2024 20:47:06 +0100 Subject: [PATCH] feat(5): Task 5 --- src/5_perlin_noise.glsl | 28 ++++++++++++++++++++++++++-- src/5_procedural_color.fs | 33 +++++++++++++++++++++++++++++++-- src/5_random_direction.glsl | 12 ++++++++++-- src/5_smooth_step.glsl | 9 +++++---- 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/5_perlin_noise.glsl b/src/5_perlin_noise.glsl index 2e83b8a..2f43ca6 100644 --- a/src/5_perlin_noise.glsl +++ b/src/5_perlin_noise.glsl @@ -9,8 +9,32 @@ float perlin_noise( vec3 st) { ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - return 0; + // Replace with your code + vec3 i = floor(st); + vec3 f = fract(st); + + // Corners of 3D cube + float n000 = dot(random_direction(i + vec3(0.0, 0.0, 0.0)), f - vec3(0.0, 0.0, 0.0)); + float n100 = dot(random_direction(i + vec3(1.0, 0.0, 0.0)), f - vec3(1.0, 0.0, 0.0)); + float n010 = dot(random_direction(i + vec3(0.0, 1.0, 0.0)), f - vec3(0.0, 1.0, 0.0)); + float n110 = dot(random_direction(i + vec3(1.0, 1.0, 0.0)), f - vec3(1.0, 1.0, 0.0)); + float n001 = dot(random_direction(i + vec3(0.0, 0.0, 1.0)), f - vec3(0.0, 0.0, 1.0)); + float n101 = dot(random_direction(i + vec3(1.0, 0.0, 1.0)), f - vec3(1.0, 0.0, 1.0)); + float n011 = dot(random_direction(i + vec3(0.0, 1.0, 1.0)), f - vec3(0.0, 1.0, 1.0)); + float n111 = dot(random_direction(i + vec3(1.0, 1.0, 1.0)), f - vec3(1.0, 1.0, 1.0)); + + // Interpolate along x the corner values + float nx00 = mix(n000, n100, smooth_step(f.x)); + float nx10 = mix(n010, n110, smooth_step(f.x)); + float nx01 = mix(n001, n101, smooth_step(f.x)); + float nx11 = mix(n011, n111, smooth_step(f.x)); + + // Interpolate the mixd values along y + float nxy0 = mix(nx00, nx10, smooth_step(f.y)); + float nxy1 = mix(nx01, nx11, smooth_step(f.y)); + + // Interpolate along z + return mix(nxy0, nxy1, smooth_step(f.z)); ///////////////////////////////////////////////////////////////////////////// } diff --git a/src/5_procedural_color.fs b/src/5_procedural_color.fs index eb3a852..5688f9c 100644 --- a/src/5_procedural_color.fs +++ b/src/5_procedural_color.fs @@ -39,7 +39,36 @@ void main() marble_noise = clamp(marble_noise,0,1); ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - color = marble_noise * vec3(1,1,1); + // Replace with your code + // Exactly the same as 4_lit.fs, but multiply the base colors by 'marble_noise' + 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); + + // 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 + + // Apply the marble noise to the base colors + kd *= marble_noise; + + // Compute the Blinn-Phong shading + color = blinn_phong(ka, kd, ks, p, n, v, l); ///////////////////////////////////////////////////////////////////////////// } diff --git a/src/5_random_direction.glsl b/src/5_random_direction.glsl index 129ef22..fa3fa4d 100644 --- a/src/5_random_direction.glsl +++ b/src/5_random_direction.glsl @@ -9,7 +9,15 @@ vec3 random_direction( vec3 seed) { ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - return vec3(1,0,0); + // Replace with your code + // Poolcoordinaten + float theta = 2.0 * M_PI * random2(seed.xy).x; + float phi = acos(2.0 * random2(seed.yz).x - 1.0); + // Cartesische coordinaten + return vec3( + sin(phi) * cos(theta), + sin(phi) * sin(theta), + cos(phi) + ); ///////////////////////////////////////////////////////////////////////////// } diff --git a/src/5_smooth_step.glsl b/src/5_smooth_step.glsl index 103426a..c6b896b 100644 --- a/src/5_smooth_step.glsl +++ b/src/5_smooth_step.glsl @@ -9,14 +9,15 @@ float smooth_step( float f) { ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - return f; + // Replace with your code + // Use the improved version + return 6 * pow(f, 5) - 15 * pow(f, 4) + 10 * pow(f, 3); ///////////////////////////////////////////////////////////////////////////// } vec3 smooth_step( vec3 f) { ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - return f; + // Replace with your code + return vec3(smooth_step(f.x), smooth_step(f.y), smooth_step(f.z)); ///////////////////////////////////////////////////////////////////////////// }