2022-11-25 14:58:29 +01:00
|
|
|
#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)
|
2022-11-28 20:24:29 +01:00
|
|
|
: diffuseColor(diffuseColor), diffuseCoefficient(diffuseCoefficient), specularColor(specularColor),
|
|
|
|
specularCoefficient(specularCoefficient), shininessExponent(shininessExponent) {}
|
2022-11-25 14:58:29 +01:00
|
|
|
|
|
|
|
Color PhongShader::shade(Scene const &scene, Ray const &ray) const {
|
2022-11-28 20:24:29 +01:00
|
|
|
Color fragmentColor;
|
2022-11-25 14:58:29 +01:00
|
|
|
|
|
|
|
|
2022-11-28 20:24:29 +01:00
|
|
|
for (auto &light: scene.lights()) {
|
|
|
|
auto illum = light->illuminate(scene, ray);
|
|
|
|
|
2022-11-29 00:56:08 +01:00
|
|
|
Vector3d omegaI = normalized(illum.direction);
|
|
|
|
Vector3d omegaR = 2 * dotProduct(omegaI, ray.normal) * ray.normal - omegaI;
|
|
|
|
float cosPsi = std::max(0.0f, dotProduct(ray.direction, omegaR));
|
|
|
|
fragmentColor += illum.color * diffuseCoefficient * diffuseColor / PI
|
|
|
|
+ illum.color * specularCoefficient * specularColor * std::pow(cosPsi, shininessExponent);
|
2022-11-28 20:24:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return fragmentColor;
|
2022-11-25 14:58:29 +01:00
|
|
|
}
|