Skip to content

Commit

Permalink
Use real light dist for shadow offset computation
Browse files Browse the repository at this point in the history
  • Loading branch information
szellmann committed Oct 13, 2024
1 parent 3eb8065 commit a64e856
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
6 changes: 3 additions & 3 deletions renderer/DirectLight_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ bool shade(ScreenSample &ss, Ray &ray, unsigned worldID,
viewDir,
ls.dir,
ls.intensity);
shadedColor = shadedColor * safe_rcp(ls.pdf) / (ls.dist*ls.dist);
shadedColor = shadedColor * safe_rcp(ls.pdf) / ls.dist2;
}
else
shadedColor = hrv.albedo * ls.intensity * safe_rcp(ls.pdf) / (ls.dist*ls.dist);
shadedColor = hrv.albedo * ls.intensity * safe_rcp(ls.pdf) / ls.dist2;
} else {
const auto &geom = onDevice.geometries[group.geoms[hr.geom_id]];
const auto &mat = onDevice.materials[group.materials[hr.geom_id]];
Expand All @@ -190,7 +190,7 @@ bool shade(ScreenSample &ss, Ray &ray, unsigned worldID,
viewDir,
ls.dir,
ls.intensity);
shadedColor = shadedColor * safe_rcp(ls.pdf) / (ls.dist*ls.dist);
shadedColor = shadedColor * safe_rcp(ls.pdf) / ls.dist2;
}
}
else if (rendererState.renderMode == RenderMode::Ng)
Expand Down
4 changes: 2 additions & 2 deletions renderer/Raycast_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ inline PixelSample renderSample(ScreenSample &ss, Ray ray, unsigned worldID,
for (unsigned lightID=0; lightID<world.numLights; ++lightID) {
const dco::Light &light = onDevice.lights[world.allLights[lightID]];

light_sample<float> ls = sampleLight(light, hitPos, ss.random);
LightSample ls = sampleLight(light, hitPos, ss.random);

float3 brdf = evalMaterial(mat,
onDevice.samplers,
Expand All @@ -76,7 +76,7 @@ inline PixelSample renderSample(ScreenSample &ss, Ray ray, unsigned worldID,
viewDir,
ls.dir,
ls.intensity);
shadedColor += brdf / ls.pdf / (ls.dist*ls.dist);
shadedColor += brdf / ls.pdf / ls.dist2;
}

shadedColor +=
Expand Down
41 changes: 30 additions & 11 deletions renderer/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -741,27 +741,46 @@ inline vec3 evalMaterial(const dco::Material &mat,
return shadedColor;
}

typedef light_sample<float> LightSample;
struct LightSample : light_sample<float>
{
float3 intensity;
float3 dir;
float pdf;
float dist;
float dist2;
};

VSNRAY_FUNC
inline LightSample sampleLight(const dco::Light &light, vec3f hitPos, Random &rnd)
{
LightSample result;
light_sample<float> ls;
if (light.type == dco::Light::Point) {
result = light.asPoint.sample(hitPos, rnd);
result.intensity = light.asPoint.intensity(hitPos);
ls = light.asPoint.sample(hitPos, rnd);
ls.intensity = light.asPoint.intensity(hitPos);
} else if (light.type == dco::Light::Quad) {
result = light.asQuad.sample(hitPos, rnd);
result.intensity = light.asQuad.intensity(hitPos);
ls = light.asQuad.sample(hitPos, rnd);
ls.intensity = light.asQuad.intensity(hitPos);
} else if (light.type == dco::Light::Directional) {
result = light.asDirectional.sample(hitPos, rnd);
result.intensity = light.asDirectional.intensity(hitPos);
ls = light.asDirectional.sample(hitPos, rnd);
ls.intensity = light.asDirectional.intensity(hitPos);
} else if (light.type == dco::Light::HDRI) {
result = light.asHDRI.sample(hitPos, rnd);
result.intensity = light.asHDRI.intensity(result.dir);
ls = light.asHDRI.sample(hitPos, rnd);
ls.intensity = light.asHDRI.intensity(result.dir);
}
float dist = result.dist;
result.dist = light.type == dco::Light::Directional||dco::Light::HDRI ? 1.f : dist;

result.intensity = ls.intensity;
result.dir = ls.dir;
result.pdf = ls.pdf;
result.dist = ls.dist;

if (light.type == dco::Light::Directional
||light.type == dco::Light::HDRI) {
result.dist2 = 1.f; // infinite lights are not attenuated by distance!
} else {
result.dist2 = ls.dist*ls.dist;
}

return result;
}

Expand Down

0 comments on commit a64e856

Please sign in to comment.