Skip to content

Commit

Permalink
Avoid division by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
szellmann committed May 10, 2024
1 parent bbf8439 commit 3a7c2db
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions renderer/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,12 +668,13 @@ inline vec3 evalPhysicallyBasedMaterial(const dco::Material &mat,

const float alpha = roughness*roughness;

constexpr float EPS = 1e-14f;
const vec3 H = normalize(lightDir+viewDir);
const float NdotH = fmaxf(0.f,dot(Ns,H));
const float NdotL = fmaxf(0.f,dot(Ns,lightDir));
const float NdotV = fmaxf(0.f,dot(Ns,viewDir));
const float VdotH = fmaxf(0.f,dot(viewDir,H));
const float LdotH = fmaxf(0.f,dot(lightDir,H));
const float NdotH = fmaxf(EPS,dot(Ns,H));
const float NdotL = fmaxf(EPS,dot(Ns,lightDir));
const float NdotV = fmaxf(EPS,dot(Ns,viewDir));
const float VdotH = fmaxf(EPS,dot(viewDir,H));
const float LdotH = fmaxf(EPS,dot(lightDir,H));

// Diffuse:
vec3 diffuseColor = getRGBA(
Expand Down Expand Up @@ -701,7 +702,7 @@ inline vec3 evalPhysicallyBasedMaterial(const dco::Material &mat,
/ (NdotV + sqrtf(alpha*alpha + (1.f-alpha*alpha) * NdotV*NdotV)));

float denom = 4.f * NdotV * NdotL;
vec3 specularBRDF = (F * D * G) / max(1e-4f,denom);
vec3 specularBRDF = (F * D * G) / max(EPS,denom);

return (diffuseBRDF + specularBRDF) * lightIntensity;
}
Expand Down

0 comments on commit 3a7c2db

Please sign in to comment.