32 lines
992 B
C++
32 lines
992 B
C++
//
|
|
// Created by marcel on 21.11.22.
|
|
//
|
|
|
|
#include "light/ambientlight.h"
|
|
#include "scene/scene.h"
|
|
|
|
AmbientLight::AmbientLight(float intensity, const Color &color) : Light(intensity, color) {}
|
|
|
|
Light::Illumination AmbientLight::illuminate(Scene const &scene, Ray const &ray) const {
|
|
Vector3d const target = ray.origin + (ray.length - LGT_EPS) * ray.direction;
|
|
|
|
Illumination illum;
|
|
illum.direction = -ray.normal;
|
|
|
|
// Precompute the distance from the light source
|
|
float const distance = length(ray.normal);
|
|
|
|
|
|
// Define a secondary ray from the surface point to the light source.
|
|
Ray lightRay;
|
|
lightRay.origin = target;
|
|
lightRay.direction = -illum.direction;
|
|
lightRay.length = distance - LGT_EPS;
|
|
|
|
// If the target is not in shadow...
|
|
if (!scene.findOcclusion(lightRay))
|
|
// ... compute the attenuation and light color
|
|
illum.color = 1.0f / (distance * distance) * this->color * this->intensity;
|
|
return illum;
|
|
return illum;
|
|
}
|