cloudy-raytracer/shader/mirrorshader.cpp

20 lines
679 B
C++
Raw Permalink 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 {
// Calculate the reflection vector
2022-11-18 11:43:53 +01:00
Vector3d const reflection = ray.direction - 2 * dotProduct(ray.normal, ray.direction) * ray.normal;
2022-11-17 17:41:50 +01:00
2022-11-11 14:39:48 +01:00
// Create a new reflection ray
2022-11-18 11:43:53 +01:00
Ray reflectionRay = ray;
reflectionRay.origin = ray.origin + (ray.length - REFR_EPS) * ray.direction;
reflectionRay.direction = normalized(reflection);
reflectionRay.length = INFINITY;
reflectionRay.primitive = nullptr;
2022-11-17 14:39:11 +01:00
2022-11-11 14:39:48 +01:00
// Send the new ray out into the scene and return the result
2022-11-18 11:43:53 +01:00
return scene.traceRay(reflectionRay);
2022-11-11 14:39:48 +01:00
}