Add recursion limit to ray tracing
This commit is contained in:
parent
0574dff685
commit
2b8794c884
|
|
@ -67,6 +67,8 @@ impl<T: Real> ImageSampler<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const RECURSION_LIMIT: u16 = 32;
|
||||||
|
|
||||||
pub fn partial_render_scene<T: Real>(
|
pub fn partial_render_scene<T: Real>(
|
||||||
scene: Arc<Scene<T>>,
|
scene: Arc<Scene<T>>,
|
||||||
tile: Tile,
|
tile: Tile,
|
||||||
|
|
@ -103,7 +105,9 @@ pub fn partial_render_scene<T: Real>(
|
||||||
let hit = sampler.sample(&ray);
|
let hit = sampler.sample(&ray);
|
||||||
let colour = match hit {
|
let colour = match hit {
|
||||||
None => ColourRgbF::from_named(NamedColour::Black),
|
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);
|
output_image_tile.set_colour(row, column, colour);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,12 @@ use super::util::algebra_utils::try_change_of_basis_matrix;
|
||||||
use crate::Real;
|
use crate::Real;
|
||||||
|
|
||||||
pub trait Integrator<T: 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> {
|
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
|
// TODO: Get rid of the magic bias number, which should be calculated base on expected error
|
||||||
// bounds and tangent direction
|
// bounds and tangent direction
|
||||||
impl<T: Real> Integrator<T> for WhittedIntegrator<T> {
|
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 =
|
let world_to_bsdf_space =
|
||||||
try_change_of_basis_matrix(&info.tangent, &info.cotangent, &info.normal)
|
try_change_of_basis_matrix(&info.tangent, &info.cotangent, &info.normal)
|
||||||
.expect("Normal, tangent and cotangent don't for a valid basis.");
|
.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)),
|
.bias(convert(0.000_000_1)),
|
||||||
) {
|
) {
|
||||||
Some(recursive_hit) => {
|
Some(recursive_hit) => {
|
||||||
info.material.bsdf()(
|
if recursion_limit > 0 {
|
||||||
world_to_bsdf_space * info.retro,
|
info.material.bsdf()(
|
||||||
*direction,
|
world_to_bsdf_space * info.retro,
|
||||||
self.integrate(&sampler, &recursive_hit),
|
*direction,
|
||||||
) * world_space_direction.dot(&info.normal).abs()
|
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()),
|
None => ColourRgbF::new(T::zero(), T::zero(), T::zero()),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue