cloudy-raytracer/light/pointlight.cpp

31 lines
1.2 KiB
C++

#include "light/pointlight.h"
#include "scene/scene.h"
PointLight::PointLight(Vector3d const &position, float intensity, Color const &color) : Light(intensity, color), position(position) {}
Light::Illumination PointLight::illuminate(Scene const &scene, Ray const &ray) const {
// IMPLEMENT ME
// Get the point on the surface
Vector3d intersectionPoint = ray.origin + ray.length * ray.direction;
// Create an instance of the Illumination object, fill in the direction (from
// surface point to light source)
Light::Illumination illum;
illum.direction = normalized(this->position - intersectionPoint);
// Define a secondary ray from the surface point to the light source.
Ray lightRay = Ray(intersectionPoint, this->position - intersectionPoint);
// If the target is not in shadow... (use scene.findOcclusion())
bool inShadow = scene.findOcclusion(lightRay);
// Compute the brightness-color of this light using inverse squared distance
// to light source (i.e. 1/(d^2)), the color of this light source and the
// intensity
if (!inShadow) {
illum.color = (this->intensity / pow(length(this->position - intersectionPoint), 2)) * this->color;
}
return illum;
}