I wrote the following phong reflection model for ray tracing in Rust, but I am not sure about the calculation of a reflection vector for the specular reflection.
// Vector: from eye to sphere
let v = hit_point.normalize();
// Vector: from light to sphere
let l = (hit_point - light.pos).normalize();
// Normal vector of sphere
let n = (hit_point - self.center).normalize();
// Reflected light
let r = (l + 2.0 * dot(-l, n) * n).normalize();
let white = Vec3::new(255.0, 255.0, 255.0);
let shininess = 20.0;
let cosa = f64::max(0.0, dot(-l, n));
let cosb = f64::max(0.0, dot(-r, v));
let diffuse = kd * cosa * color;
let specular = ks * cosb.powf(shininess) * white;
let ambient = ke * color;
let color = diffuse + specular + ambient;
I wrote the following program to calculate the reflection vector of the light as shown in the next image, but it did not reflect the specular reflection as shown in the next image.
let r = (l + 2.0 * dot(-l, n) * n).normalize();
Instead, when I wrote reflected ray as following program, the specular reflection is reflected nicely as follows.
let r = (-l + 2.0 * dot(l, n) * n).normalize();
Why is my first program wrong ? Why reflected ray r = l + 2(-l・n)n
is wrong ?