Add ReflectiveMaterial material type
Although there's still some issues with rendering the reflections.
This commit is contained in:
parent
5cd80ae05c
commit
8b0b8c59ba
|
|
@ -13,7 +13,7 @@ use std::rc::Rc;
|
||||||
use vanrijn::camera::partial_render_scene;
|
use vanrijn::camera::partial_render_scene;
|
||||||
use vanrijn::colour::{ColourRgbF, NamedColour};
|
use vanrijn::colour::{ColourRgbF, NamedColour};
|
||||||
use vanrijn::image::{ClampingToneMapper, ImageRgbF, ImageRgbU8, ToneMapper};
|
use vanrijn::image::{ClampingToneMapper, ImageRgbF, ImageRgbU8, ToneMapper};
|
||||||
use vanrijn::materials::{LambertianMaterial, PhongMaterial};
|
use vanrijn::materials::{LambertianMaterial, PhongMaterial, ReflectiveMaterial};
|
||||||
use vanrijn::raycasting::{Plane, Sphere};
|
use vanrijn::raycasting::{Plane, Sphere};
|
||||||
use vanrijn::scene::Scene;
|
use vanrijn::scene::Scene;
|
||||||
|
|
||||||
|
|
@ -80,9 +80,10 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
Box::new(Sphere::new(
|
Box::new(Sphere::new(
|
||||||
Vector3::new(-1.25, -0.5, 6.0),
|
Vector3::new(-1.25, -0.5, 6.0),
|
||||||
1.0,
|
1.0,
|
||||||
Rc::new(LambertianMaterial {
|
Rc::new(ReflectiveMaterial {
|
||||||
colour: ColourRgbF::from_named(NamedColour::Blue),
|
colour: ColourRgbF::from_named(NamedColour::Blue),
|
||||||
diffuse_strength: 0.1,
|
diffuse_strength: 0.01,
|
||||||
|
reflection_strength: 0.99,
|
||||||
}),
|
}),
|
||||||
)),
|
)),
|
||||||
Box::new(Sphere::new(
|
Box::new(Sphere::new(
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use nalgebra::{RealField, Vector3};
|
use nalgebra::{convert, RealField, Vector3};
|
||||||
|
|
||||||
use super::colour::{ColourRgbF, NamedColour};
|
use super::colour::{ColourRgbF, NamedColour};
|
||||||
|
|
||||||
|
|
@ -68,3 +68,39 @@ impl<T: RealField> Material<T> for PhongMaterial<T> {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ReflectiveMaterial<T: RealField> {
|
||||||
|
pub colour: ColourRgbF<T>,
|
||||||
|
pub diffuse_strength: T,
|
||||||
|
pub reflection_strength: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: RealField> Material<T> for ReflectiveMaterial<T> {
|
||||||
|
fn bsdf<'a>(
|
||||||
|
&'a self,
|
||||||
|
) -> Box<dyn Fn(Vector3<T>, Vector3<T>, ColourRgbF<T>) -> ColourRgbF<T> + 'a> {
|
||||||
|
Box::new(
|
||||||
|
move |w_o: Vector3<T>, w_i: Vector3<T>, colour_in: ColourRgbF<T>| {
|
||||||
|
if w_i.z < T::zero() || w_o.z < T::zero() {
|
||||||
|
ColourRgbF::new(T::zero(), T::one(), T::one())
|
||||||
|
} else {
|
||||||
|
let reflection_vector = Vector3::new(-w_o.x, -w_o.y, w_o.z);
|
||||||
|
let reflection_colour = colour_in * self.reflection_strength;
|
||||||
|
let diffuse_colour = self.colour * colour_in * self.diffuse_strength;
|
||||||
|
let sigma: T = convert(0.001);
|
||||||
|
let two: T = convert(2.0);
|
||||||
|
let reflection_factor = (-w_i.dot(&reflection_vector).acos().powf(two)
|
||||||
|
/ (two * sigma * sigma))
|
||||||
|
.exp();
|
||||||
|
reflection_colour * reflection_factor
|
||||||
|
+ diffuse_colour * (T::one() - reflection_factor)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sample(&self, w_o: &Vector3<T>) -> Vec<Vector3<T>> {
|
||||||
|
vec![Vector3::new(-w_o.x, -w_o.y, w_o.z)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue