diff --git a/src/4_blinn_phong.glsl b/src/4_blinn_phong.glsl index f2e3ef8..a40a3c8 100644 --- a/src/4_blinn_phong.glsl +++ b/src/4_blinn_phong.glsl @@ -2,7 +2,7 @@ // surface and a light direction. Assume the light has a low // ambient intensity of 0.1. To avoid confusion: this Blinn-Phong shading model // should only consist of ambient, diffuse and specular lighting. -// Do not divide by rē (the squared distance to the point light). +// Do not divide by r^2 (the squared distance to the point light). // Inputs: // ka rgb ambient color @@ -23,8 +23,18 @@ vec3 blinn_phong( vec3 l) { ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - return vec3(1,1,1); + // Replace with your code + float AMBIENT_INTENSITY = 0.1; + + /* Ambient lighting */ + vec3 rgb = ka * AMBIENT_INTENSITY; + /* Diffuse component (Lambertian shading) */ + rgb += kd * max(dot(n, l), 0.0); + /* Specular component (Blinn-Phong shading) */ + vec3 h = normalize(v + l); + rgb += ks * pow(max(0.0, dot(n, h)), p); + + return rgb; ///////////////////////////////////////////////////////////////////////////// } diff --git a/src/4_lit.fs b/src/4_lit.fs index b08d514..8955a5d 100644 --- a/src/4_lit.fs +++ b/src/4_lit.fs @@ -23,7 +23,32 @@ out vec3 color; void main() { ///////////////////////////////////////////////////////////////////////////// - // Replace with your code - 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); + + // 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); ///////////////////////////////////////////////////////////////////////////// }