Merge branch 'master' into multithread_renderer
This commit is contained in:
commit
2e1b66f1fe
4 changed files with 78 additions and 13 deletions
|
@ -113,7 +113,12 @@ bool Triangle::intersect(Ray &ray) const {
|
|||
|
||||
// Calculate the normal
|
||||
// IMPLEMENT smooth triangles, if available
|
||||
ray.normal = normalized(crossProduct(edge1, edge2));
|
||||
if (this->normal[0] == Vector3d{} || this->normal[1] == Vector3d{} || this->normal[2] == Vector3d{}) {
|
||||
ray.normal = normalized(crossProduct(edge1, edge2));
|
||||
} else {
|
||||
ray.normal = u * this->normal[1] + v * this->normal[2] + (1 - u - v) * this->normal[0];
|
||||
}
|
||||
|
||||
|
||||
// Calculate the surface position
|
||||
ray.surface = u * this->surface[1] + v * this->surface[2] + (1 - u - v) * this->surface[0];
|
||||
|
|
|
@ -2,12 +2,57 @@
|
|||
#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) {}
|
||||
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;
|
||||
Color fragmentColor;
|
||||
|
||||
// IMPLEMENT ME
|
||||
// IMPLEMENT ME
|
||||
for (auto &light: scene.lights()) {
|
||||
auto illum = light->illuminate(scene, ray);
|
||||
auto N = ray.normal;
|
||||
auto H = Vector3d{};
|
||||
auto V = ray.direction;
|
||||
auto L = illum.direction;
|
||||
|
||||
return fragmentColor;
|
||||
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) / (PI * NV * NL);
|
||||
auto rhoPD = diffuseColor * rhoD + ctColor * rhoS;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fragmentColor += illum.color * rhoPD;
|
||||
}
|
||||
|
||||
return fragmentColor * diffuseColor;
|
||||
}
|
||||
|
||||
float CookTorranceShader::D(float NdotH) const {
|
||||
float divisor = 4.0f * m * m * std::pow(cos(NdotH), 4);
|
||||
float exponent = -std::pow(tan(NdotH) / 2, 2);
|
||||
return 1 / divisor * std::exp(exponent);
|
||||
}
|
||||
|
||||
float CookTorranceShader::F(float VdotH) const {
|
||||
return 0.1f;
|
||||
}
|
||||
|
||||
float CookTorranceShader::G(float NdotH, float NdotV, float VdotH, float NdotL) const {
|
||||
return 1;
|
||||
}
|
|
@ -8,6 +8,10 @@ Color LambertShader::shade(Scene const &scene, Ray const &ray) const {
|
|||
Color fragmentColor;
|
||||
|
||||
// IMPLEMENT ME
|
||||
|
||||
return fragmentColor;
|
||||
for (auto& light : scene.lights()) {
|
||||
auto illum = light->illuminate(scene, ray);
|
||||
auto lightAngle = dotProduct(ray.normal, normalized(illum.direction));
|
||||
fragmentColor += illum.color * std::abs(lightAngle);
|
||||
}
|
||||
return fragmentColor * diffuseColor;
|
||||
}
|
||||
|
|
|
@ -4,13 +4,24 @@
|
|||
|
||||
PhongShader::PhongShader(Color const &diffuseColor, float diffuseCoefficient, Color const &specularColor,
|
||||
float specularCoefficient, float shininessExponent)
|
||||
: diffuseColor(diffuseColor), diffuseCoefficient(diffuseCoefficient), specularColor(specularColor),
|
||||
specularCoefficient(specularCoefficient), shininessExponent(shininessExponent) {}
|
||||
: diffuseColor(diffuseColor), diffuseCoefficient(diffuseCoefficient), specularColor(specularColor),
|
||||
specularCoefficient(specularCoefficient), shininessExponent(shininessExponent) {}
|
||||
|
||||
Color PhongShader::shade(Scene const &scene, Ray const &ray) const {
|
||||
Color fragmentColor;
|
||||
Color fragmentColor;
|
||||
|
||||
// IMPLEMENT ME
|
||||
|
||||
return 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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue