diff --git a/fancy1.cpp b/fancy1.cpp index a5534c7..ffd0801 100644 --- a/fancy1.cpp +++ b/fancy1.cpp @@ -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(Vector3d(5.0f, 10.0f, 0.0f), 100.0f); + auto mainLight = std::make_shared(Vector3d(1.0f, -1.0f, -1.0f), 10.0f); scene.add(mainLight); scene.add(std::make_shared(0.3f)); diff --git a/light/sunlight.cpp b/light/sunlight.cpp new file mode 100644 index 0000000..dc355fb --- /dev/null +++ b/light/sunlight.cpp @@ -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; +} \ No newline at end of file diff --git a/light/sunlight.h b/light/sunlight.h new file mode 100644 index 0000000..56a5ef9 --- /dev/null +++ b/light/sunlight.h @@ -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