Implements SunLight

This commit is contained in:
Maximilian Giller 2023-01-24 11:31:09 +01:00
parent e5575c03f5
commit d7d38944ea
3 changed files with 53 additions and 1 deletions

View file

@ -21,6 +21,7 @@
#include "common/noise/perlinnoise.h"
#include "shader/noiseshader.h"
#include "common/noise/cloudnoise.h"
#include "light/sunlight.h"
int main()
{
@ -28,7 +29,7 @@ int main()
scene.setBackgroundColor(Color(0.529f, 0.808f, 0.922f));
// Add lights
auto mainLight = std::make_shared<PointLight>(Vector3d(5.0f, 10.0f, 0.0f), 100.0f);
auto mainLight = std::make_shared<SunLight>(Vector3d(1.0f, -1.0f, -1.0f), 10.0f);
scene.add(mainLight);
scene.add(std::make_shared<AmbientLight>(0.3f));

31
light/sunlight.cpp Normal file
View file

@ -0,0 +1,31 @@
#include "sunlight.h"
SunLight::SunLight(Vector3d const &direction, float intensity, Color const &color) : Light(intensity, color),
direction(normalized(direction))
{}
Light::Illumination SunLight::illuminate(Scene const &scene, Ray const &ray) const
{
Vector3d const target = ray.origin + (ray.length - LGT_EPS) * ray.direction;
// Illumination object
Illumination illum;
illum.direction = this->direction;
// Define a secondary ray from the surface point to the light source.
Ray lightRay;
lightRay.origin = target;
lightRay.direction = -illum.direction;
lightRay.length = INFINITY;
// If the target is not in shadow...
if (!scene.findOcclusion(lightRay))
{
// Look at angleIntensity of light
float angleIntensity = dotProduct(-ray.normal,
this->direction); // 0 if light is behind surface, 1 if light is in front of surface
illum.color = this->color * this->intensity * angleIntensity;
}
return illum;
}

20
light/sunlight.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef CG1_TRACER_SUNLIGHT_H
#define CG1_TRACER_SUNLIGHT_H
#include "scene/scene.h"
#include "light.h"
class SunLight : public Light
{
public:
SunLight(Vector3d const &direction, float intensity, Color const &color = Color(1, 1, 1));
// Light functions
Illumination illuminate(Scene const &scene, Ray const &ray) const override;
protected:
Vector3d direction;
};
#endif //CG1_TRACER_SUNLIGHT_H