New lighting object tree: AmbientLight -> PointLight
This commit is contained in:
parent
9fef3c33d2
commit
55ef253c10
5 changed files with 99 additions and 32 deletions
|
@ -41,8 +41,10 @@ main(int argc,
|
||||||
scene.add_shape(s2);
|
scene.add_shape(s2);
|
||||||
scene.add_shape(s3);
|
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(l1);
|
||||||
|
//scene.add_light(l2);
|
||||||
|
|
||||||
// Render.
|
// Render.
|
||||||
scene.render();
|
scene.render();
|
||||||
|
|
83
src/light.cc
83
src/light.cc
|
@ -9,38 +9,85 @@
|
||||||
#include "light.h"
|
#include "light.h"
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
|
|
||||||
|
#pragma mark - Ambient Lights
|
||||||
|
|
||||||
Light::Light()
|
AmbientLight::AmbientLight()
|
||||||
: Light(1.0)
|
: AmbientLight(Color::White)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
|
||||||
Light::Light(float i)
|
AmbientLight::AmbientLight(const Color &c)
|
||||||
: Light(Vector3::Zero, i)
|
: AmbientLight(c, 1.0)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
|
||||||
Light::Light(Vector3 o, float i)
|
AmbientLight::AmbientLight(const Color &c, const float &i)
|
||||||
: Object(o),
|
: color(c),
|
||||||
intensity(i)
|
intensity(i)
|
||||||
{ }
|
{
|
||||||
|
_clamp_intensity();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
const Color &
|
||||||
* Light::get_intensity --
|
AmbientLight::get_color()
|
||||||
* Light::set_intensity --
|
const
|
||||||
*
|
{
|
||||||
* Get and set the intensity of this light.
|
return color;
|
||||||
*/
|
}
|
||||||
float
|
|
||||||
Light::get_intensity()
|
|
||||||
|
const float &
|
||||||
|
AmbientLight::get_intensity()
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
return intensity;
|
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)
|
||||||
|
{ }
|
||||||
|
|
33
src/light.h
33
src/light.h
|
@ -12,19 +12,36 @@
|
||||||
#include "object.h"
|
#include "object.h"
|
||||||
|
|
||||||
|
|
||||||
class Light
|
class AmbientLight
|
||||||
: public Object
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Light();
|
AmbientLight();
|
||||||
Light(float i);
|
AmbientLight(const Color &c);
|
||||||
Light(Vector3 o, float i);
|
AmbientLight(const Color &c, const float &i);
|
||||||
|
|
||||||
float get_intensity() const;
|
const Color &get_color() const;
|
||||||
void set_intensity(float i);
|
const float &get_intensity() const;
|
||||||
|
|
||||||
|
Color compute_color_contribution() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Color color;
|
||||||
|
float intensity;
|
||||||
|
|
||||||
private:
|
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
|
#endif
|
||||||
|
|
|
@ -140,7 +140,7 @@ Scene::add_shape(Shape *shape)
|
||||||
* Add a light to the scene.
|
* Add a light to the scene.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Scene::add_light(Light *light)
|
Scene::add_light(PointLight *light)
|
||||||
{
|
{
|
||||||
lights.push_back(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 intersection = ray.parameterize(nearest_t);
|
||||||
Vector3 normal = intersected_shape->compute_normal(intersection);
|
Vector3 normal = intersected_shape->compute_normal(intersection);
|
||||||
|
|
||||||
for (Light *l : lights) {
|
for (PointLight *l : lights) {
|
||||||
Vector3 light_direction = (intersection - l->get_origin()).normalize();
|
Vector3 light_direction = (intersection - l->get_origin()).normalize();
|
||||||
float ldotn = light_direction.dot(normal);
|
float ldotn = light_direction.dot(normal);
|
||||||
out_color.red *= ((ldotn >= 0.0) ? ldotn : 0.0) * l->get_intensity();
|
out_color.red *= ((ldotn >= 0.0) ? ldotn : 0.0) * l->get_intensity();
|
||||||
|
|
|
@ -16,7 +16,8 @@
|
||||||
#include "basics.h"
|
#include "basics.h"
|
||||||
|
|
||||||
|
|
||||||
class Light;
|
class AmbientLight;
|
||||||
|
class PointLight;
|
||||||
class Shape;
|
class Shape;
|
||||||
class Writer;
|
class Writer;
|
||||||
|
|
||||||
|
@ -37,7 +38,7 @@ public:
|
||||||
void render();
|
void render();
|
||||||
|
|
||||||
void add_shape(Shape *obj);
|
void add_shape(Shape *obj);
|
||||||
void add_light(Light *light);
|
void add_light(PointLight *light);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Color trace_ray(const Ray &ray, const int depth);
|
Color trace_ray(const Ray &ray, const int depth);
|
||||||
|
@ -50,7 +51,7 @@ private:
|
||||||
|
|
||||||
// Scene objects.
|
// Scene objects.
|
||||||
std::list<Shape *> shapes;
|
std::list<Shape *> shapes;
|
||||||
std::list<Light *> lights;
|
std::list<PointLight *> lights;
|
||||||
|
|
||||||
// Rendering stats
|
// Rendering stats
|
||||||
unsigned int nrays;
|
unsigned int nrays;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue