diff --git a/src/scene.cc b/src/scene.cc index dd304f5..085e881 100644 --- a/src/scene.cc +++ b/src/scene.cc @@ -94,10 +94,6 @@ void Scene::render() { pixels = new Color[width * height]; - if (pixels == NULL) { - // TODO: Print an error. - return; - } Ray primary_ray; Vector3 o, d; @@ -173,6 +169,20 @@ Scene::trace_ray(const Ray &ray, const int depth) return Color::Black; } - // TODO: Lighting. - return intersected_shape->get_material().get_color(); + Color out_color = intersected_shape->get_material().get_color(); + + Vector3 intersection = ray.parameterize(nearest_t); + Vector3 normal = intersected_shape->compute_normal(intersection); + + for (Light *l : lights) { + Vector3 light_direction = intersection - l->get_origin(); + light_direction.normalize(); + + float ldotn = light_direction.dot(normal); + out_color.red *= ((ldotn >= 0.0) ? ldotn : 0.0) * l->get_intensity(); + out_color.green *= ((ldotn >= 0.0) ? ldotn : 0.0) * l->get_intensity(); + out_color.blue *= ((ldotn >= 0.0) ? ldotn : 0.0) * l->get_intensity(); + } + + return out_color; }