{
// surface normal at hit point of object
Vec3f normal = ray.hit()->GetNormal(ray);
- // turn to fron if angle < 90°
+ // turn to front if angle > 90°
if(normal.dot(ray.direction()) < 0) {
normal = normal * -1;
}
// direction of reflection
- Vec3f refl_dir = ray.direction() - normal * 2 * (normal % ray.direction());
+ Vec3f refl_dir = ray.direction() - normal * 2 * normal.dot(ray.direction());
refl_dir.normalize();
- // ambient color term - radiance is 1 since we have point light
- Vec3f amb_color = m_ambient_color * m_ka * 1.0f;
+ // ambient color term
+ Vec3f amb_color = m_ambient_color * m_ka;
Vec3f intensity, light_dir;
Ray light_ray;
(*it)->Illuminate(light_ray, intensity);
diff_sum += intensity * light_ray.direction().dot(normal);
- spec_sum += intensity * light_ray.direction().dot(refl_dir);
+ Vec3f view_dir = ray.direction()*-1; // direction from hitpoint to viewer
+ spec_sum += intensity * pow(fmax(view_dir.dot(refl_dir),0), m_ke);
}
Vec3f diff_color = m_diffuse_color * m_kd * diff_sum;
Vec3f spec_color = m_specular_color * m_ks * spec_sum;
- return amb_color + diff_color + spec_sum;
+ return amb_color + diff_color + spec_color;
}