cloudy-raytracer/shader/cooktorranceshader.cpp

52 lines
1.8 KiB
C++

#include "light/light.h"
#include "scene/scene.h"
#include "shader/cooktorranceshader.h"
CookTorranceShader::CookTorranceShader(Color const &diffCol, Color const &ctCol, float IOR, float roughness,
float diffCoeff, float ctCoeff) : diffuseColor(diffCol * diffCoeff),
ctColor(ctCol * ctCoeff), F0(IOR),
m(roughness) {}
Color CookTorranceShader::shade(Scene const &scene, Ray const &ray) const {
Color fragmentColor;
// IMPLEMENT ME
for (auto &light: scene.lights()) {
auto illum = light->illuminate(scene, ray);
auto N = ray.normal;
auto V = ray.direction;
auto L = illum.direction;
auto H = normalized(V + L);
auto NV = dotProduct(N, V);
auto NL = dotProduct(N, L);
auto NH = dotProduct(N, H);
auto VH = dotProduct(V, H);
float rhoD = 1.0f / PI;
auto rhoS = F(VH) * D(NH) * G(NH, NV, VH, NL) / (4 * NV * NL);
auto rhoPD = diffuseColor * rhoD + ctColor * rhoS;
fragmentColor += illum.color * std::abs(NL) * rhoPD;
}
return diffuseColor * fragmentColor;
}
float CookTorranceShader::D(float NdotH) const {
float divisor = 4.0f * m * m * std::pow(NdotH, 4.0f);
float exponent = (NdotH * NdotH - 1.0f) / (m * m * NdotH * NdotH);
return 1 / divisor * std::exp(exponent);
}
float CookTorranceShader::F(float VdotH) const {
return F0 + (1.0f - F0) * std::pow(1.0f - VdotH, 5.0f);
}
float CookTorranceShader::G(float NdotH, float NdotV, float VdotH, float NdotL) const {
return std::min(1.0f, std::min(2 * NdotH * NdotV / VdotH, 2 * NdotH * NdotL / VdotH));
}