fixed merge issues

This commit is contained in:
arvid schröder 2022-12-06 14:30:52 +01:00
parent c5cfe34861
commit 728b324c5b

View file

@ -8,80 +8,53 @@ CookTorranceShader::CookTorranceShader(Color const &diffCol, Color const &ctCol,
m(roughness) {} m(roughness) {}
float CookTorranceShader::D(float NdotH) const { float CookTorranceShader::D(float NdotH) const {
// Beckmann distribution // Beckmann distribution
float const r2 = m * m; float const r2 = m * m;
float const NdotH2 = NdotH * NdotH; float const NdotH2 = NdotH * NdotH;
return expf((NdotH2 - 1.0f) / (r2 * NdotH2)) / (4.0f * r2 * powf(NdotH, 4.0f)); return expf((NdotH2 - 1.0f) / (r2 * NdotH2)) / (4.0f * r2 * powf(NdotH, 4.0f));
} }
float CookTorranceShader::F(float VdotH) const { float CookTorranceShader::F(float VdotH) const {
// Schlicks approximation // Schlicks approximation
return F0 + (1.0f - F0) * powf(1.0f - VdotH, 5); return F0 + (1.0f - F0) * powf(1.0f - VdotH, 5);
} }
float CookTorranceShader::G(float NdotH, float NdotV, float VdotH, float NdotL) const { return std::min(1.0f, std::min(2.0f * NdotH * NdotV / VdotH, 2.0f * NdotH * NdotL / VdotH)); } float CookTorranceShader::G(float NdotH, float NdotV, float VdotH, float NdotL) const {
return std::min(1.0f, std::min(
2.0f * NdotH * NdotV / VdotH, 2.0f * NdotH * NdotL / VdotH));
}
Color CookTorranceShader::shade(Scene const &scene, Ray const &ray) const { Color CookTorranceShader::shade(Scene const &scene, Ray const &ray) const {
Color fragmentColor; Color fragmentColor;
if (m >= 0.0f) { if (m >= 0.0f) {
// Accumulate the light over all light sources // Accumulate the light over all light sources
for (const auto &light : scene.lights()) { for (const auto &light: scene.lights()) {
Light::Illumination illum; Light::Illumination illum;
illum = light->illuminate(scene, ray); illum = light->illuminate(scene, ray);
float const NdotL = std::max(0.0f, dotProduct(-illum.direction, ray.normal)); float const NdotL = std::max(0.0f, dotProduct(-illum.direction, ray.normal));
if (NdotL <= 0.0f) if (NdotL <= 0.0f)
continue; continue;
// Diffuse term // Diffuse term
Color const diffuse = this->diffuseColor / float(PI); Color const diffuse = this->diffuseColor / float(PI);
fragmentColor += diffuse * NdotL * illum.color; fragmentColor += diffuse * NdotL * illum.color;
// Cook-Torrance term // Cook-Torrance term
// half angle vector // half angle vector
Vector3d const H = normalized(-illum.direction - ray.direction); Vector3d const H = normalized(-illum.direction - ray.direction);
float const NdotH = std::max(0.0f, dotProduct(ray.normal, H)); float const NdotH = std::max(0.0f, dotProduct(ray.normal, H));
float const NdotV = std::max(0.0f, dotProduct(ray.normal, -ray.direction)); float const NdotV = std::max(0.0f, dotProduct(ray.normal, -ray.direction));
float const VdotH = std::max(0.0f, dotProduct(-ray.direction, H)); float const VdotH = std::max(0.0f, dotProduct(-ray.direction, H));
if (NdotV * NdotL > EPSILON) { if (NdotV * NdotL > EPSILON) {
Color const specular = this->ctColor * (F(VdotH) * D(NdotH) * G(NdotH, NdotV, VdotH, NdotL)) / (float(PI) * NdotV * NdotL); Color const specular = this->ctColor * (F(VdotH) * D(NdotH) * G(NdotH, NdotV, VdotH, NdotL)) /
(float(PI) * NdotV * NdotL);
fragmentColor += specular * NdotL * illum.color; fragmentColor += specular * NdotL * illum.color;
} }
}
} }
}
auto NV = dotProduct(N, V);
auto NL = dotProduct(N, L);
auto NH = dotProduct(N, H);
auto VH = dotProduct(V, H);
//Cook-Torrance diffuse Term
float rhoD = 1.0f / PI;
//Cook-Torrance Specular Term
//F = Fresnel; D = microfacet Distribution; G = Geometrical factor
float rhoS = F(VH) * D(NH) * G(NH, NV, VH, NL) / (4 * NV * NL);
//BRDF
Color rhoBD = diffuseColor * rhoD + ctColor * rhoS;
fragmentColor += illum.color * std::abs(NL) * rhoBD;
}
return diffuseColor * fragmentColor; 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));
}