library: implement refraction

This necessitated to rework how light properties for a material were
given. A material can have either reflectivity or transparency. This
changes the parsing of materials, using a `LightProperty` structure at
its core.

This is does not implement the true Fresnel equations to take into
account the amount of reflection that an incident goes through when
encountering a transparent object.
This commit is contained in:
Bruno BELANYI 2020-03-19 16:42:54 +01:00
parent e00950c7e5
commit 552c0cb966
9 changed files with 255 additions and 93 deletions

View file

@ -47,6 +47,9 @@ objects:
r: 1.0 r: 1.0
g: 1.0 g: 1.0
b: 1.0 b: 1.0
# Optional fields (go together)
#transparency: 0.5
#index: 1.5
texture: texture:
type: uniform type: uniform
color: color:
@ -71,7 +74,7 @@ objects:
g: 1.0 g: 1.0
b: 1.0 b: 1.0
# Optional field # Optional field
# reflectivity: 0.0 #reflectivity: 0.0
texture: texture:
type: uniform type: uniform
color: color:

View file

@ -35,7 +35,6 @@ objects:
type: uniform type: uniform
diffuse: {r: 0.5, g: 0.5, b: 0.5} diffuse: {r: 0.5, g: 0.5, b: 0.5}
specular: {r: 1., g: 1., b: 1.} specular: {r: 1., g: 1., b: 1.}
reflectivity: 0.5
texture: texture:
type: uniform type: uniform
color: {r: 0.25, g: 0.5, b: 1.} color: {r: 0.25, g: 0.5, b: 1.}

View file

@ -0,0 +1,124 @@
use super::color::LinearColor;
use serde::Deserialize;
#[derive(Debug, PartialEq, Clone, Deserialize)]
#[serde(untagged)]
pub enum ReflTransEnum {
Transparency {
/// The transparency coefficient.
#[serde(rename = "transparency")]
coef: f32,
/// The diffraction index.
index: f32,
},
Reflectivity {
/// The reflectivity coefficient.
#[serde(rename = "reflectivity")]
coef: f32,
},
}
/// A structure holding all the physical proprerties relating to light at a point.
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct LightProperties {
/// The diffuse component.
pub diffuse: LinearColor,
/// The specular component.
pub specular: LinearColor,
/// The transparency or reflectivity properties.
#[serde(flatten)]
pub refl_trans: Option<ReflTransEnum>,
}
impl LightProperties {
pub fn new(
diffuse: LinearColor,
specular: LinearColor,
refl_trans: Option<ReflTransEnum>,
) -> Self {
LightProperties {
diffuse,
specular,
refl_trans,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn new_works() {
let diffuse = LinearColor::new(0.25, 0.5, 1.);
let specular = LinearColor::new(0.75, 0.375, 0.125);
let refl_trans = Some(ReflTransEnum::Reflectivity { coef: 0.5 });
let properties =
LightProperties::new(diffuse.clone(), specular.clone(), refl_trans.clone());
assert_eq!(
properties,
LightProperties {
diffuse,
specular,
refl_trans,
}
)
}
#[test]
fn deserialization_without_refl_trans_works() {
let yaml = r#"
diffuse: {r: 1.0, g: 0.5, b: 0.25}
specular: {r: 0.25, g: 0.125, b: 0.75}
"#;
let properties: LightProperties = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
properties,
LightProperties::new(
LinearColor::new(1., 0.5, 0.25),
LinearColor::new(0.25, 0.125, 0.75),
None
)
)
}
#[test]
fn deserialization_with_reflection_works() {
let yaml = r#"
diffuse: {r: 1.0, g: 0.5, b: 0.25}
specular: {r: 0.25, g: 0.125, b: 0.75}
transparency: 0.5
index: 1.5
"#;
let properties: LightProperties = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
properties,
LightProperties::new(
LinearColor::new(1., 0.5, 0.25),
LinearColor::new(0.25, 0.125, 0.75),
Some(ReflTransEnum::Transparency {
coef: 0.5,
index: 1.5
})
)
)
}
#[test]
fn deserialization_with_transparency_works() {
let yaml = r#"
diffuse: {r: 1.0, g: 0.5, b: 0.25}
specular: {r: 0.25, g: 0.125, b: 0.75}
reflectivity: 0.25
"#;
let properties: LightProperties = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
properties,
LightProperties::new(
LinearColor::new(1., 0.5, 0.25),
LinearColor::new(0.25, 0.125, 0.75),
Some(ReflTransEnum::Reflectivity { coef: 0.25 })
)
)
}
}

View file

@ -6,3 +6,6 @@ pub use color::*;
pub mod film; pub mod film;
pub use film::*; pub use film::*;
pub mod light_properties;
pub use light_properties::*;

View file

@ -1,18 +1,7 @@
use super::core::color::LinearColor; use super::core::LightProperties;
use super::Point2D; use super::Point2D;
use serde::Deserialize; use serde::Deserialize;
/// A structure holding all the physical proprerties relating to light at a point.
#[derive(Debug, PartialEq, Clone)]
pub struct LightProperties {
/// The diffuse component.
pub diffuse: LinearColor,
/// The specular component,
pub specular: LinearColor,
/// The reflectivity coefficient,
pub reflectivity: f32,
}
/// All the existing `Material` implementation. /// All the existing `Material` implementation.
#[serde(tag = "type")] #[serde(tag = "type")]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]

View file

@ -1,88 +1,53 @@
use super::LightProperties;
use super::Material; use super::Material;
use crate::core::color::LinearColor; use crate::core::LightProperties;
use crate::Point2D; use crate::Point2D;
use serde::Deserialize; use serde::Deserialize;
/// A material with the same characteristics on all points. /// A material with the same characteristics on all points.
#[derive(Clone, Debug, PartialEq, Deserialize)] #[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct UniformMaterial { pub struct UniformMaterial {
diffuse: LinearColor, #[serde(flatten)]
specular: LinearColor, properties: LightProperties,
#[serde(default)]
reflectivity: f32,
} }
impl UniformMaterial { impl UniformMaterial {
pub fn new(diffuse: LinearColor, specular: LinearColor, reflectivity: f32) -> Self { pub fn new(properties: LightProperties) -> Self {
UniformMaterial { UniformMaterial { properties }
diffuse,
specular,
reflectivity,
}
} }
} }
impl Material for UniformMaterial { impl Material for UniformMaterial {
fn properties(&self, _: Point2D) -> LightProperties { fn properties(&self, _: Point2D) -> LightProperties {
LightProperties { self.properties.clone()
diffuse: self.diffuse.clone(),
specular: self.specular.clone(),
reflectivity: self.reflectivity,
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::core::color::LinearColor;
use crate::core::ReflTransEnum;
#[test] #[test]
fn new_works() { fn new_works() {
let diffuse = LinearColor::new(0., 0.5, 0.); let properties = LightProperties {
let specular = LinearColor::new(1., 1., 1.); diffuse: LinearColor::new(0., 0.5, 0.),
let reflectivity = 0.5; specular: LinearColor::new(1., 1., 1.),
let mat = UniformMaterial::new(diffuse.clone(), specular.clone(), reflectivity); refl_trans: None,
assert_eq!( };
mat, let mat = UniformMaterial::new(properties.clone());
UniformMaterial { assert_eq!(mat, UniformMaterial { properties })
diffuse,
specular,
reflectivity
}
)
} }
fn simple_material() -> impl Material { #[test]
UniformMaterial::new( fn properties_works() {
LinearColor::new(0.5, 0.5, 0.5), let properties = LightProperties::new(
LinearColor::new(0., 0.5, 0.),
LinearColor::new(1., 1., 1.), LinearColor::new(1., 1., 1.),
0.5, None,
) );
} let mat = UniformMaterial::new(properties.clone());
assert_eq!(mat.properties(Point2D::origin()), properties)
#[test]
fn diffuse_works() {
let mat = simple_material();
assert_eq!(
mat.properties(Point2D::origin()).diffuse,
LinearColor::new(0.5, 0.5, 0.5)
)
}
#[test]
fn specular_works() {
let mat = simple_material();
assert_eq!(
mat.properties(Point2D::origin()).specular,
LinearColor::new(1., 1., 1.)
)
}
#[test]
fn reflectivity_works() {
let mat = simple_material();
assert!(mat.properties(Point2D::origin()).reflectivity - 0.5 < std::f32::EPSILON)
} }
#[test] #[test]
@ -95,11 +60,11 @@ mod test {
let material: UniformMaterial = serde_yaml::from_str(yaml).unwrap(); let material: UniformMaterial = serde_yaml::from_str(yaml).unwrap();
assert_eq!( assert_eq!(
material, material,
UniformMaterial::new( UniformMaterial::new(LightProperties::new(
LinearColor::new(1., 0.5, 0.25), LinearColor::new(1., 0.5, 0.25),
LinearColor::new(0.25, 0.125, 0.75), LinearColor::new(0.25, 0.125, 0.75),
0.25 Some(ReflTransEnum::Reflectivity { coef: 0.25 })
) ))
) )
} }
} }

View file

@ -45,6 +45,7 @@ impl BHShape for Object {
mod test { mod test {
use super::*; use super::*;
use crate::core::color::LinearColor; use crate::core::color::LinearColor;
use crate::core::LightProperties;
use crate::material::UniformMaterial; use crate::material::UniformMaterial;
use crate::shape::Sphere; use crate::shape::Sphere;
use crate::texture::UniformTexture; use crate::texture::UniformTexture;
@ -52,11 +53,11 @@ mod test {
fn simple_object() -> Object { fn simple_object() -> Object {
let shape = Sphere::new(Point::new(5., 0., 0.), 1.); let shape = Sphere::new(Point::new(5., 0., 0.), 1.);
let material = UniformMaterial::new( let material = UniformMaterial::new(LightProperties::new(
LinearColor::new(0.5, 0.5, 0.5), LinearColor::new(0.5, 0.5, 0.5),
LinearColor::new(1., 1., 1.), LinearColor::new(1., 1., 1.),
0.5, None,
); ));
let texture = UniformTexture::new(LinearColor::new(0.25, 0.5, 1.)); let texture = UniformTexture::new(LinearColor::new(0.25, 0.5, 1.));
Object::new(shape.into(), material.into(), texture.into()) Object::new(shape.into(), material.into(), texture.into())
} }
@ -64,11 +65,11 @@ mod test {
#[test] #[test]
fn new_works() { fn new_works() {
let shape = Sphere::new(Point::new(5., 0., 0.), 1.); let shape = Sphere::new(Point::new(5., 0., 0.), 1.);
let material = UniformMaterial::new( let material = UniformMaterial::new(LightProperties::new(
LinearColor::new(0.5, 0.5, 0.5), LinearColor::new(0.5, 0.5, 0.5),
LinearColor::new(1., 1., 1.), LinearColor::new(1., 1., 1.),
0.5, None,
); ));
let texture = UniformTexture::new(LinearColor::new(0.25, 0.5, 1.)); let texture = UniformTexture::new(LinearColor::new(0.25, 0.5, 1.));
assert_eq!( assert_eq!(
simple_object(), simple_object(),
@ -93,7 +94,6 @@ mod test {
type: uniform type: uniform
diffuse: {r: 0.5, g: 0.5, b: 0.5} diffuse: {r: 0.5, g: 0.5, b: 0.5}
specular: {r: 1., g: 1., b: 1.} specular: {r: 1., g: 1., b: 1.}
reflectivity: 0.5
texture: texture:
type: uniform type: uniform
color: {r: 0.25, g: 0.5, b: 1.} color: {r: 0.25, g: 0.5, b: 1.}

View file

@ -1,7 +1,7 @@
use super::{light_aggregate::LightAggregate, object::Object}; use super::{light_aggregate::LightAggregate, object::Object};
use crate::{ use crate::{
core::{Camera, LinearColor}, core::{Camera, LightProperties, LinearColor, ReflTransEnum},
material::{LightProperties, Material}, material::Material,
shape::Shape, shape::Shape,
texture::Texture, texture::Texture,
{Point, Vector}, {Point, Vector},
@ -20,6 +20,7 @@ pub struct Scene {
bvh: BVH, bvh: BVH,
aliasing_limit: u32, aliasing_limit: u32,
reflection_limit: u32, reflection_limit: u32,
diffraction_index: f32,
} }
impl Scene { impl Scene {
@ -29,6 +30,7 @@ impl Scene {
mut objects: Vec<Object>, mut objects: Vec<Object>,
aliasing_limit: u32, aliasing_limit: u32,
reflection_limit: u32, reflection_limit: u32,
diffraction_index: f32,
) -> Self { ) -> Self {
let bvh = BVH::build(&mut objects); let bvh = BVH::build(&mut objects);
Scene { Scene {
@ -38,6 +40,7 @@ impl Scene {
bvh, bvh,
aliasing_limit, aliasing_limit,
reflection_limit, reflection_limit,
diffraction_index,
} }
} }
@ -82,7 +85,13 @@ impl Scene {
let direction = (pixel - self.camera.origin()).normalize(); let direction = (pixel - self.camera.origin()).normalize();
self.cast_ray(Ray::new(pixel, direction)) self.cast_ray(Ray::new(pixel, direction))
.map_or_else(LinearColor::black, |(t, obj)| { .map_or_else(LinearColor::black, |(t, obj)| {
self.color_at(pixel + direction * t, obj, direction, self.reflection_limit) self.color_at(
pixel + direction * t,
obj,
direction,
self.reflection_limit,
self.diffraction_index,
)
}) })
} }
@ -105,6 +114,7 @@ impl Scene {
// NOTE(Bruno): should be written using iterators // NOTE(Bruno): should be written using iterators
let mut shot_obj: Option<&Object> = None; let mut shot_obj: Option<&Object> = None;
let mut t = std::f32::INFINITY; let mut t = std::f32::INFINITY;
// NOTE: we don't care about all objects... Only the closest one
for object in self.bvh.traverse(&ray, &self.objects).iter() { for object in self.bvh.traverse(&ray, &self.objects).iter() {
match object.shape.intersect(&ray) { match object.shape.intersect(&ray) {
Some(dist) if dist < t => { Some(dist) if dist < t => {
@ -123,31 +133,82 @@ impl Scene {
object: &Object, object: &Object,
incident_ray: Vector, incident_ray: Vector,
reflection_limit: u32, reflection_limit: u32,
diffraction_index: f32,
) -> LinearColor { ) -> LinearColor {
let normal = object.shape.normal(&point);
let reflected = reflected(incident_ray, normal);
let texel = object.shape.project_texel(&point); let texel = object.shape.project_texel(&point);
let properties = object.material.properties(texel); let properties = object.material.properties(texel);
let object_color = object.texture.texel_color(texel); let object_color = object.texture.texel_color(texel);
self.illuminate(point, object_color, &properties, normal, reflected) let normal = object.shape.normal(&point);
+ self.reflection(point, &properties, reflected, reflection_limit) let reflected = reflected(incident_ray, normal);
let lighting = self.illuminate(point, object_color, &properties, normal, reflected);
match properties.refl_trans {
None => lighting,
Some(ReflTransEnum::Transparency { coef, index }) => {
// Calculate the refracted ray, if it was refracted
refracted(incident_ray, normal, diffraction_index, index).map_or_else(
// Total reflection
|| self.reflection(point, 1., reflected, reflection_limit, diffraction_index),
// Refraction
|r| {
self.refraction(point, coef, r, reflection_limit, index)
+ lighting * (1. - coef)
},
)
}
Some(ReflTransEnum::Reflectivity { coef }) => {
self.reflection(point, coef, reflected, reflection_limit, diffraction_index)
+ lighting * (1. - coef)
}
}
}
fn refraction(
&self,
point: Point,
transparency: f32,
refracted: Vector,
reflection_limit: u32,
new_index: f32,
) -> LinearColor {
if transparency > 1e-5 && reflection_limit > 0 {
let refraction_start = point + refracted * 0.001;
if let Some((t, obj)) = self.cast_ray(Ray::new(refraction_start, refracted)) {
let resulting_position = refraction_start + refracted * t;
let refracted = self.color_at(
resulting_position,
obj,
refracted,
reflection_limit - 1,
new_index,
);
return refracted * transparency;
}
}
LinearColor::black()
} }
fn reflection( fn reflection(
&self, &self,
point: Point, point: Point,
properties: &LightProperties, reflectivity: f32,
reflected: Vector, reflected: Vector,
reflection_limit: u32, reflection_limit: u32,
diffraction_index: f32,
) -> LinearColor { ) -> LinearColor {
let reflectivity = properties.reflectivity; // FIXME: use fresnel reflection too
if reflectivity > 1e-5 && reflection_limit > 0 { if reflectivity > 1e-5 && reflection_limit > 0 {
let reflection_start = point + reflected * 0.001; let reflection_start = point + reflected * 0.001;
if let Some((t, obj)) = self.cast_ray(Ray::new(reflection_start, reflected)) { if let Some((t, obj)) = self.cast_ray(Ray::new(reflection_start, reflected)) {
let resulting_position = reflection_start + reflected * t; let resulting_position = reflection_start + reflected * t;
let color = self.color_at(resulting_position, obj, reflected, reflection_limit - 1); let color = self.color_at(
resulting_position,
obj,
reflected,
reflection_limit - 1,
diffraction_index,
);
return color * reflectivity; return color * reflectivity;
} }
}; };
@ -208,6 +269,18 @@ fn reflected(incident: Vector, normal: Vector) -> Vector {
incident - delt incident - delt
} }
fn refracted(incident: Vector, normal: Vector, n_1: f32, n_2: f32) -> Option<Vector> {
let cos = incident.dot(&normal);
let normal = if cos < 0. { normal } else { -normal };
let eta = n_1 / n_2;
let k = 1. - eta * eta * (1. - cos * cos);
if k < 0. {
None
} else {
Some(eta * incident + (eta * cos.abs() - f32::sqrt(k)) * normal)
}
}
#[derive(Debug, PartialEq, Deserialize)] #[derive(Debug, PartialEq, Deserialize)]
struct SerializedScene { struct SerializedScene {
camera: Camera, camera: Camera,
@ -219,6 +292,8 @@ struct SerializedScene {
aliasing_limit: u32, aliasing_limit: u32,
#[serde(default)] #[serde(default)]
reflection_limit: u32, reflection_limit: u32,
#[serde(default = "crate::serialize::default_identity")]
starting_diffraction: f32,
} }
impl From<SerializedScene> for Scene { impl From<SerializedScene> for Scene {
@ -229,6 +304,7 @@ impl From<SerializedScene> for Scene {
scene.objects, scene.objects,
scene.aliasing_limit, scene.aliasing_limit,
scene.reflection_limit, scene.reflection_limit,
scene.starting_diffraction,
) )
} }
} }

View file

@ -0,0 +1,3 @@
pub fn default_identity() -> f32 {
1.
}