Add recursion limit to ray tracing

This commit is contained in:
Matthew Gordon 2020-02-28 23:25:51 -05:00
parent 0574dff685
commit 2b8794c884
2 changed files with 30 additions and 8 deletions

View File

@ -67,6 +67,8 @@ impl<T: Real> ImageSampler<T> {
}
}
const RECURSION_LIMIT: u16 = 32;
pub fn partial_render_scene<T: Real>(
scene: Arc<Scene<T>>,
tile: Tile,
@ -103,7 +105,9 @@ pub fn partial_render_scene<T: Real>(
let hit = sampler.sample(&ray);
let colour = match hit {
None => ColourRgbF::from_named(NamedColour::Black),
Some(intersection_info) => integrator.integrate(&sampler, &intersection_info),
Some(intersection_info) => {
integrator.integrate(&sampler, &intersection_info, RECURSION_LIMIT)
}
};
output_image_tile.set_colour(row, column, colour);
}

View File

@ -8,7 +8,12 @@ use super::util::algebra_utils::try_change_of_basis_matrix;
use crate::Real;
pub trait Integrator<T: Real> {
fn integrate(&self, sampler: &Sampler<T>, info: &IntersectionInfo<T>) -> ColourRgbF<T>;
fn integrate(
&self,
sampler: &Sampler<T>,
info: &IntersectionInfo<T>,
recursion_limit: u16,
) -> ColourRgbF<T>;
}
pub struct DirectionalLight<T: Real> {
@ -24,7 +29,12 @@ pub struct WhittedIntegrator<T: Real> {
// TODO: Get rid of the magic bias number, which should be calculated base on expected error
// bounds and tangent direction
impl<T: Real> Integrator<T> for WhittedIntegrator<T> {
fn integrate(&self, sampler: &Sampler<T>, info: &IntersectionInfo<T>) -> ColourRgbF<T> {
fn integrate(
&self,
sampler: &Sampler<T>,
info: &IntersectionInfo<T>,
recursion_limit: u16,
) -> ColourRgbF<T> {
let world_to_bsdf_space =
try_change_of_basis_matrix(&info.tangent, &info.cotangent, &info.normal)
.expect("Normal, tangent and cotangent don't for a valid basis.");
@ -58,11 +68,19 @@ impl<T: Real> Integrator<T> for WhittedIntegrator<T> {
.bias(convert(0.000_000_1)),
) {
Some(recursive_hit) => {
if recursion_limit > 0 {
info.material.bsdf()(
world_to_bsdf_space * info.retro,
*direction,
self.integrate(&sampler, &recursive_hit),
self.integrate(
&sampler,
&recursive_hit,
recursion_limit - 1,
),
) * world_space_direction.dot(&info.normal).abs()
} else {
ColourRgbF::new(T::zero(), T::zero(), T::zero())
}
}
None => ColourRgbF::new(T::zero(), T::zero(), T::zero()),
}