27 lines
1.1 KiB
C++
27 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);
|
|
|
|
auto omegaI = normalized(illum.direction);
|
|
auto omegaR = 2 * dotProduct(omegaI, ray.normal) * ray.normal - omegaI;
|
|
auto cosPsi = dotProduct(ray.direction, omegaR);
|
|
auto colorI = diffuseCoefficient * diffuseColor / PI +
|
|
(specularCoefficient * specularColor * std::pow(cosPsi, shininessExponent));
|
|
|
|
fragmentColor += illum.color * colorI;
|
|
}
|
|
|
|
return fragmentColor;
|
|
}
|