cloudy-raytracer/shader/brdfshader.cpp
2022-11-29 13:15:48 +01:00

57 lines
2.6 KiB
C++

#include "light/light.h"
#include "scene/scene.h"
#include "shader/brdfshader.h"
BrdfShader::BrdfShader(char const *fileName, Color const &scale)
: scale(scale), brdf(std::make_unique<BRDFRead>(fileName)) {}
Color BrdfShader::shade(Scene const &scene, Ray const &ray) const {
Color illuminationColor;
static auto rebase = [](Vector3d const &axis_x,
Vector3d const &axis_y,
Vector3d const &axis_z,
Vector3d const &vec) {
auto det = axis_x.x * axis_y.y * axis_z.z +
axis_x.y * axis_y.z * axis_z.x +
axis_x.z * axis_y.x * axis_z.y -
axis_x.x * axis_y.z * axis_z.y -
axis_x.y * axis_y.x * axis_z.z -
axis_x.z * axis_y.y * axis_z.x;
// Calculate resulting vector by using inverse matrix of new base vectors
return Vector3d{
((axis_y.y * axis_z.z - axis_y.z * axis_z.y) * vec.x +
(axis_y.z * axis_z.x - axis_y.x * axis_z.z) * vec.y +
(axis_y.x * axis_z.y - axis_y.y * axis_z.x) * vec.z) / det,
((axis_x.z * axis_z.y - axis_x.y * axis_z.z) * vec.x +
(axis_x.x * axis_z.z - axis_x.x * axis_y.z) * vec.y +
(axis_x.y * axis_z.x - axis_x.x * axis_y.x) * vec.z) / det,
((axis_x.y * axis_y.z - axis_x.z * axis_y.y) * vec.x +
(axis_x.z * axis_y.x - axis_x.x * axis_y.z) * vec.y +
(axis_x.x * axis_y.y - axis_x.y * axis_y.x) * vec.z) / det
};
};
for (auto &light: scene.lights()) {
auto illum = light->illuminate(scene, ray);
auto axis = orthoNormalized(ray.normal, ray.direction, illum.direction);
auto axis_y = std::get<0>(axis);
auto axis_x = std::get<1>(axis);
auto axis_z = std::get<2>(axis);
auto N = normalized(rebase(axis_x, axis_y, axis_z, ray.normal));
auto D = normalized(rebase(axis_x, axis_y, axis_z, ray.direction));
auto L = normalized(rebase(axis_x, axis_y, axis_z, illum.direction));
D = axis_y * D.y + axis_z * D.z;
L = axis_y * L.y + axis_z * L.z;
illuminationColor += brdf->lookupBrdfValues(std::acos(dotProduct(illum.direction, ray.normal)),
0,
std::acos(dotProduct(ray.direction, ray.normal)),
std::acos(dotProduct(D, L)));
}
// IMPLEMENT ME
return illuminationColor;
}