library: light: add DirectionalLight implementation
This commit is contained in:
parent
22167a1b93
commit
d759c16f5d
66
src/light/directional_light.rs
Normal file
66
src/light/directional_light.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
use super::super::core::LinearColor;
|
||||
use super::super::{Point, Vector};
|
||||
use super::{Light, SpatialLight};
|
||||
|
||||
/// Represent a light emanating from a far away source, with parallel rays on all points.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct DirectionalLight {
|
||||
direction: Vector,
|
||||
color: LinearColor,
|
||||
}
|
||||
|
||||
impl DirectionalLight {
|
||||
pub fn new(direction: Vector, color: LinearColor) -> Self {
|
||||
DirectionalLight {
|
||||
direction: direction.normalize(),
|
||||
color,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Light for DirectionalLight {
|
||||
fn illumination(&self, _: &Point) -> LinearColor {
|
||||
self.color.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl SpatialLight for DirectionalLight {
|
||||
fn to_source(&self, _: &Point) -> (Vector, f32) {
|
||||
(self.direction * -1., std::f32::INFINITY)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn new_works() {
|
||||
let direction = Vector::new(1., 0., 0.);
|
||||
let color = LinearColor::new(1., 1., 1.);
|
||||
let light = DirectionalLight::new(direction, color.clone());
|
||||
let res = DirectionalLight { direction, color };
|
||||
assert_eq!(light, res)
|
||||
}
|
||||
|
||||
fn simple_light() -> impl SpatialLight {
|
||||
let direction = Vector::new(1., 0., 0.);
|
||||
let color = LinearColor::new(1., 1., 1.);
|
||||
DirectionalLight::new(direction, color)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn illumination_is_correct() {
|
||||
let light = simple_light();
|
||||
let lum = light.illumination(&Point::new(1., 1., 1.));
|
||||
assert_eq!(lum, LinearColor::new(1., 1., 1.))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_source_is_correct() {
|
||||
let light = simple_light();
|
||||
let ans = light.to_source(&Point::new(1., 0., 0.));
|
||||
let expected = (Vector::new(-1., 0., 0.), std::f32::INFINITY);
|
||||
assert_eq!(ans, expected)
|
||||
}
|
||||
}
|
|
@ -13,5 +13,8 @@ pub trait SpatialLight: Light {
|
|||
fn to_source(&self, origin: &Point) -> (Vector, f32);
|
||||
}
|
||||
|
||||
pub mod directional_light;
|
||||
pub use directional_light::*;
|
||||
|
||||
pub mod point_light;
|
||||
pub use point_light::*;
|
||||
|
|
Loading…
Reference in a new issue