cloudy-raytracer/shader/mirrorshader.cpp

18 lines
539 B
C++
Raw Normal View History

2022-11-11 14:39:48 +01:00
#include "scene/scene.h"
#include "shader/mirrorshader.h"
2022-11-17 20:34:37 +01:00
MirrorShader::MirrorShader() = default;
2022-11-11 14:39:48 +01:00
Color MirrorShader::shade(Scene const &scene, Ray const &ray) const {
2022-11-17 17:41:50 +01:00
if (ray.getRemainingBounces() <= 0) {
2022-11-17 20:34:37 +01:00
return {0, 0, 0};
2022-11-17 17:41:50 +01:00
}
2022-11-17 14:39:11 +01:00
Vector3d newDirection = ray.direction - 2 * dotProduct(ray.normal, ray.direction) * ray.normal;
Ray mirroredRay = Ray(ray.origin + ray.length * ray.direction, newDirection);
2022-11-17 17:41:50 +01:00
mirroredRay.setRemainingBounces(ray.getRemainingBounces() - 1);
2022-11-17 14:39:11 +01:00
return scene.traceRay(mirroredRay);
2022-11-11 14:39:48 +01:00
}