library: render: scene: add reflection
This required changing the place were the normal, the texel projection, and the reflection were calculated to avoid possibly expensive lookups. I also changed the style a bit to make it more readable.
This commit is contained in:
parent
d8f5f880ec
commit
3386617c69
|
@ -1,7 +1,7 @@
|
||||||
use super::{light_aggregate::LightAggregate, object::Object};
|
use super::{light_aggregate::LightAggregate, object::Object};
|
||||||
use crate::core::Camera;
|
use crate::core::Camera;
|
||||||
use crate::core::LinearColor;
|
use crate::core::LinearColor;
|
||||||
use crate::{Point, Vector};
|
use crate::{Point, Point2D, Vector};
|
||||||
use bvh::ray::Ray;
|
use bvh::ray::Ray;
|
||||||
use image::RgbImage;
|
use image::RgbImage;
|
||||||
use rand::prelude::thread_rng;
|
use rand::prelude::thread_rng;
|
||||||
|
@ -13,6 +13,7 @@ pub struct Scene<'a> {
|
||||||
lights: LightAggregate,
|
lights: LightAggregate,
|
||||||
objects: Vec<Object<'a>>,
|
objects: Vec<Object<'a>>,
|
||||||
aliasing_limit: u32,
|
aliasing_limit: u32,
|
||||||
|
reflection_limit: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Scene<'a> {
|
impl<'a> Scene<'a> {
|
||||||
|
@ -87,18 +88,50 @@ impl<'a> Scene<'a> {
|
||||||
shot_obj.map(|obj| (t, obj))
|
shot_obj.map(|obj| (t, obj))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn color_at(&self, point: Point, object: &Object, incident_ray: Vector) -> LinearColor {
|
fn color_at(
|
||||||
self.illuminate(point, object, incident_ray)
|
&self,
|
||||||
// FIXME: add reflection
|
point: Point,
|
||||||
}
|
object: &Object,
|
||||||
|
incident_ray: Vector,
|
||||||
fn illuminate(&self, point: Point, object: &Object, incident_ray: Vector) -> LinearColor {
|
reflection_limit: u32,
|
||||||
let texel = object.shape.project_texel(&point);
|
) -> LinearColor {
|
||||||
let normal = object.shape.normal(&point);
|
let normal = object.shape.normal(&point);
|
||||||
let reflected = reflected(incident_ray, normal);
|
let reflected = reflected(incident_ray, normal);
|
||||||
|
let texel = object.shape.project_texel(&point);
|
||||||
|
self.illuminate(point, object, texel, normal, reflected)
|
||||||
|
+ self.reflection(point, object, texel, reflected, reflection_limit)
|
||||||
|
}
|
||||||
|
|
||||||
self.illuminate_ambient(object.texture.texel_color(texel))
|
fn reflection(
|
||||||
+ self.illuminate_spatial(point.clone(), object, normal, reflected)
|
&self,
|
||||||
|
point: Point,
|
||||||
|
object: &Object,
|
||||||
|
texel: Point2D,
|
||||||
|
reflected: Vector,
|
||||||
|
reflection_limit: u32,
|
||||||
|
) -> LinearColor {
|
||||||
|
let reflectivity = object.material.reflectivity(texel);
|
||||||
|
if reflectivity > 1e-5 && reflection_limit > 0 {
|
||||||
|
let reflection_start = point + reflected * 0.001;
|
||||||
|
if let Some((t, obj)) = self.cast_ray(Ray::new(reflection_start, reflected)) {
|
||||||
|
let resulting_position = reflection_start + reflected * t;
|
||||||
|
return self.color_at(resulting_position, obj, reflected, reflection_limit - 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
LinearColor::black()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn illuminate(
|
||||||
|
&self,
|
||||||
|
point: Point,
|
||||||
|
object: &Object,
|
||||||
|
texel: Point2D,
|
||||||
|
normal: Vector,
|
||||||
|
reflected: Vector,
|
||||||
|
) -> LinearColor {
|
||||||
|
let ambient = self.illuminate_ambient(object.texture.texel_color(texel));
|
||||||
|
let spatial = self.illuminate_spatial(point, object, texel, normal, reflected);
|
||||||
|
ambient + spatial
|
||||||
}
|
}
|
||||||
|
|
||||||
fn illuminate_ambient(&self, color: LinearColor) -> LinearColor {
|
fn illuminate_ambient(&self, color: LinearColor) -> LinearColor {
|
||||||
|
@ -112,10 +145,10 @@ impl<'a> Scene<'a> {
|
||||||
&self,
|
&self,
|
||||||
point: Point,
|
point: Point,
|
||||||
object: &Object,
|
object: &Object,
|
||||||
|
texel: Point2D,
|
||||||
normal: Vector,
|
normal: Vector,
|
||||||
reflected: Vector,
|
reflected: Vector,
|
||||||
) -> LinearColor {
|
) -> LinearColor {
|
||||||
let texel = object.shape.project_texel(&point);
|
|
||||||
let k_d = object.material.diffuse(texel);
|
let k_d = object.material.diffuse(texel);
|
||||||
let k_s = object.material.specular(texel);
|
let k_s = object.material.specular(texel);
|
||||||
self.lights
|
self.lights
|
||||||
|
|
Loading…
Reference in a new issue