This commit is contained in:
Eryn Wells 2013-09-13 18:56:07 -07:00
parent 3dbde9a60d
commit 16a37f6fb3

View file

@ -208,19 +208,41 @@ Scene::trace_ray(const Ray &ray, const int depth)
} }
Material shape_material = intersected_shape->get_material(); Material shape_material = intersected_shape->get_material();
Color shape_color = shape_material.get_diffuse_color();
Vector3 intersection = ray.parameterize(nearest_t); Vector3 intersection = ray.parameterize(nearest_t);
Vector3 normal = intersected_shape->compute_normal(intersection); Vector3 normal = intersected_shape->compute_normal(intersection);
Vector3 light_direction;
float ldotn, diffuse_level, ambient_level;
Ray shadow_ray;
for (PointLight *l : lights) { for (PointLight *l : lights) {
Vector3 light_direction = (intersection - l->get_origin()).normalize(); light_direction = (intersection - l->get_origin()).normalize();
float ldotn = light_direction.dot(normal); ldotn = light_direction.dot(normal);
if (ldotn < 0.0) {
if (ldotn < 0) {
ldotn = 0.0; ldotn = 0.0;
} }
float diffuse_level = shape_material.get_diffuse_level();
float ambient_level = 1 - diffuse_level; diffuse_level = shape_material.get_diffuse_level();
out_color += shape_material.get_diffuse_color() * ( ambient_level * ambient->compute_color_contribution() ambient_level = 1.0 - diffuse_level;
+ diffuse_level * ldotn);
shadow_ray = Ray(intersection, light_direction);
for (Shape *s : shapes) {
// Skip the intersected shape.
if (s == intersected_shape) {
continue;
}
// Figure out if we're in shadow.
if (s->does_intersect(shadow_ray, NULL) > 0) {
diffuse_level = 0.0;
break;
}
}
out_color += shape_color * ( ambient_level * ambient->compute_color_contribution()
+ diffuse_level * ldotn);
} }
return out_color; return out_color;