diff --git a/src/charles.cc b/src/charles.cc index eadee98..5b01214 100644 --- a/src/charles.cc +++ b/src/charles.cc @@ -41,8 +41,10 @@ main(int argc, scene.add_shape(s2); scene.add_shape(s3); - Light *l1 = new Light(Vector3(500.0, 100.0, 600.0), 1.0); + PointLight *l1 = new PointLight(Vector3(0.0, 240.0, 100.0), Color(1.0, 1.0, 1.0), 1.0); + //Light *l2 = new Light(Vector3(640.0, 240.0, 10000.0), 0.2); scene.add_light(l1); + //scene.add_light(l2); // Render. scene.render(); diff --git a/src/light.cc b/src/light.cc index 00a8475..4289ff7 100644 --- a/src/light.cc +++ b/src/light.cc @@ -9,38 +9,85 @@ #include "light.h" #include "object.h" +#pragma mark - Ambient Lights -Light::Light() - : Light(1.0) +AmbientLight::AmbientLight() + : AmbientLight(Color::White) { } -Light::Light(float i) - : Light(Vector3::Zero, i) +AmbientLight::AmbientLight(const Color &c) + : AmbientLight(c, 1.0) { } -Light::Light(Vector3 o, float i) - : Object(o), +AmbientLight::AmbientLight(const Color &c, const float &i) + : color(c), intensity(i) -{ } +{ + _clamp_intensity(); +} -/* - * Light::get_intensity -- - * Light::set_intensity -- - * - * Get and set the intensity of this light. - */ -float -Light::get_intensity() +const Color & +AmbientLight::get_color() + const +{ + return color; +} + + +const float & +AmbientLight::get_intensity() const { return intensity; } -void -Light::set_intensity(float i) + +Color +AmbientLight::compute_color_contribution() + const { - intensity = i; + return color * intensity; } + + +void +AmbientLight::_clamp_intensity() +{ + if (intensity < 0.0) { + intensity = 0.0; + } + else if (intensity > 1.0) { + intensity = 1.0; + } +} + +#pragma mark - Point Lights + +PointLight::PointLight() + : AmbientLight(), + Object() +{ } + + +PointLight::PointLight(const Vector3 &o) + : AmbientLight(), + Object(o) +{ } + + +PointLight::PointLight(const Vector3 &o, + const Color &c) + : AmbientLight(c), + Object(o) +{ } + + +PointLight::PointLight(const Vector3 &o, + const Color &c, + const float &i) + : AmbientLight(c, i), + Object(o) +{ } diff --git a/src/light.h b/src/light.h index 45fab48..1983e2a 100644 --- a/src/light.h +++ b/src/light.h @@ -12,19 +12,36 @@ #include "object.h" -class Light - : public Object +class AmbientLight { public: - Light(); - Light(float i); - Light(Vector3 o, float i); + AmbientLight(); + AmbientLight(const Color &c); + AmbientLight(const Color &c, const float &i); - float get_intensity() const; - void set_intensity(float i); + const Color &get_color() const; + const float &get_intensity() const; + + Color compute_color_contribution() const; + +protected: + Color color; + float intensity; private: - float intensity; + void _clamp_intensity(); +}; + + +class PointLight + : public AmbientLight, + public Object +{ +public: + PointLight(); + PointLight(const Vector3 &o); + PointLight(const Vector3 &o, const Color &c); + PointLight(const Vector3 &o, const Color &c, const float &i); }; #endif diff --git a/src/scene.cc b/src/scene.cc index f8434d8..669c3b6 100644 --- a/src/scene.cc +++ b/src/scene.cc @@ -140,7 +140,7 @@ Scene::add_shape(Shape *shape) * Add a light to the scene. */ void -Scene::add_light(Light *light) +Scene::add_light(PointLight *light) { lights.push_back(light); } @@ -186,7 +186,7 @@ Scene::trace_ray(const Ray &ray, const int depth) Vector3 intersection = ray.parameterize(nearest_t); Vector3 normal = intersected_shape->compute_normal(intersection); - for (Light *l : lights) { + for (PointLight *l : lights) { Vector3 light_direction = (intersection - l->get_origin()).normalize(); float ldotn = light_direction.dot(normal); out_color.red *= ((ldotn >= 0.0) ? ldotn : 0.0) * l->get_intensity(); diff --git a/src/scene.h b/src/scene.h index a979e48..58031b4 100644 --- a/src/scene.h +++ b/src/scene.h @@ -16,7 +16,8 @@ #include "basics.h" -class Light; +class AmbientLight; +class PointLight; class Shape; class Writer; @@ -37,7 +38,7 @@ public: void render(); void add_shape(Shape *obj); - void add_light(Light *light); + void add_light(PointLight *light); private: Color trace_ray(const Ray &ray, const int depth); @@ -50,7 +51,7 @@ private: // Scene objects. std::list shapes; - std::list lights; + std::list lights; // Rendering stats unsigned int nrays;