cloudy-raytracer/shader/phongshader.cpp
2022-11-30 20:30:58 +01:00

25 lines
1.1 KiB
C++

#include "light/light.h"
#include "scene/scene.h"
#include "shader/phongshader.h"
PhongShader::PhongShader(Color const &diffuseColor, float diffuseCoefficient, Color const &specularColor,
float specularCoefficient, float shininessExponent)
: diffuseColor(diffuseColor), diffuseCoefficient(diffuseCoefficient), specularColor(specularColor),
specularCoefficient(specularCoefficient), shininessExponent(shininessExponent) {}
Color PhongShader::shade(Scene const &scene, Ray const &ray) const {
Color fragmentColor;
for (auto &light: scene.lights()) {
auto illum = light->illuminate(scene, ray);
Vector3d reflection = illum.direction - 2 * dotProduct(illum.direction, ray.normal) * ray.normal;
Color diffuse = diffuseCoefficient * dotProduct(illum.direction, -ray.normal) * diffuseColor;
Color specular = specularCoefficient * specularColor
* std::pow(dotProduct(ray.direction, -reflection), shininessExponent);
fragmentColor += illum.color * diffuse + illum.color * specular;
}
return fragmentColor;
}