fixed merge issues
This commit is contained in:
parent
c5cfe34861
commit
728b324c5b
1 changed files with 33 additions and 60 deletions
|
@ -8,80 +8,53 @@ CookTorranceShader::CookTorranceShader(Color const &diffCol, Color const &ctCol,
|
|||
m(roughness) {}
|
||||
|
||||
float CookTorranceShader::D(float NdotH) const {
|
||||
// Beckmann distribution
|
||||
float const r2 = m * m;
|
||||
float const NdotH2 = NdotH * NdotH;
|
||||
return expf((NdotH2 - 1.0f) / (r2 * NdotH2)) / (4.0f * r2 * powf(NdotH, 4.0f));
|
||||
// Beckmann distribution
|
||||
float const r2 = m * m;
|
||||
float const NdotH2 = NdotH * NdotH;
|
||||
return expf((NdotH2 - 1.0f) / (r2 * NdotH2)) / (4.0f * r2 * powf(NdotH, 4.0f));
|
||||
}
|
||||
|
||||
float CookTorranceShader::F(float VdotH) const {
|
||||
// Schlicks approximation
|
||||
return F0 + (1.0f - F0) * powf(1.0f - VdotH, 5);
|
||||
// Schlicks approximation
|
||||
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 fragmentColor;
|
||||
|
||||
if (m >= 0.0f) {
|
||||
// Accumulate the light over all light sources
|
||||
for (const auto &light : scene.lights()) {
|
||||
Light::Illumination illum;
|
||||
illum = light->illuminate(scene, ray);
|
||||
if (m >= 0.0f) {
|
||||
// Accumulate the light over all light sources
|
||||
for (const auto &light: scene.lights()) {
|
||||
Light::Illumination illum;
|
||||
illum = light->illuminate(scene, ray);
|
||||
|
||||
float const NdotL = std::max(0.0f, dotProduct(-illum.direction, ray.normal));
|
||||
if (NdotL <= 0.0f)
|
||||
continue;
|
||||
float const NdotL = std::max(0.0f, dotProduct(-illum.direction, ray.normal));
|
||||
if (NdotL <= 0.0f)
|
||||
continue;
|
||||
|
||||
// Diffuse term
|
||||
Color const diffuse = this->diffuseColor / float(PI);
|
||||
fragmentColor += diffuse * NdotL * illum.color;
|
||||
// Diffuse term
|
||||
Color const diffuse = this->diffuseColor / float(PI);
|
||||
fragmentColor += diffuse * NdotL * illum.color;
|
||||
|
||||
// Cook-Torrance term
|
||||
// half angle vector
|
||||
Vector3d const H = normalized(-illum.direction - ray.direction);
|
||||
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 VdotH = std::max(0.0f, dotProduct(-ray.direction, H));
|
||||
// Cook-Torrance term
|
||||
// half angle vector
|
||||
Vector3d const H = normalized(-illum.direction - ray.direction);
|
||||
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 VdotH = std::max(0.0f, dotProduct(-ray.direction, H));
|
||||
|
||||
if (NdotV * NdotL > EPSILON) {
|
||||
Color const specular = this->ctColor * (F(VdotH) * D(NdotH) * G(NdotH, NdotV, VdotH, NdotL)) / (float(PI) * NdotV * NdotL);
|
||||
if (NdotV * NdotL > EPSILON) {
|
||||
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;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue