Vec3f
ReflectiveEyeLightShader::Shade(Ray& ray)
{
- Vec3f eyeColor = EyeLightShader::Shade(ray);
- Vec3f reflColor;
+ Vec3f N = ray.hit()->GetNormal(ray);
+ // turn normal to front
+ if(N.dot(ray.direction()) > 0)
+ N *= -1;
+
+ float cos_phi = fabs(ray.direction().dot(N));
- // get normal and turn towards ray if angle > 90°
- Vec3f n = ray.hit()->GetNormal(ray);
- float cos_theta = n.dot(ray.direction());
- // we just need the sign, no value
- if( cos_theta > 0 ) {
- std::cout << "cos_theta="<<cos_theta <<", flipping normal" <<std::endl;
- n = n * -1;
- }
-
- // shoot secondary rays from intersection
- Ray sec(ray.origin() + ray.direction() * (ray.t() - Epsilon),
- ray.direction() + n * 2);
- sec.setRecursionDepth(ray.recursionDepth() - 1);
-
- if(sec.recursionDepth() + 1 > 0) {
- reflColor = m_scene->RayTrace(sec);
- //reflColor = Shade(sec);
- }
- return eyeColor + (reflColor * m_reflectivity);
+ Vec3f color = m_color * cos_phi;
+
+ if(ray.recursionDepth() < RecursionDepth)
+ {
+ // generate reflected ray
+ // ray origin = hitpoint
+ Vec3f origin = ray.origin() + ray.direction()*ray.t();
+ Vec3f dir = ray.direction()-N*2*N.dot(ray.direction());
+ dir.normalize();
+
+ // spawn new ray
+ Ray reflection_ray(origin, dir, ray.recursionDepth()+1);
+ reflection_ray.setT(Infinity);
+
+ // trace reflection ray
+ Vec3f reflected_color = m_scene->RayTrace(reflection_ray);
+ color += reflected_color * m_reflectivity;
+ }
+
+ color.clamp();
+
+ return color;
}