library: texture: add UniformTexture implementation
This commit is contained in:
parent
63feed70c6
commit
dd6b50f785
|
@ -6,3 +6,6 @@ pub trait Texture: std::fmt::Debug {
|
||||||
/// Get the color at a given texel coordinate
|
/// Get the color at a given texel coordinate
|
||||||
fn texel_color(&self, point: Point2D) -> LinearColor;
|
fn texel_color(&self, point: Point2D) -> LinearColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod uniform;
|
||||||
|
pub use uniform::*;
|
||||||
|
|
45
src/texture/uniform.rs
Normal file
45
src/texture/uniform.rs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
use super::super::core::LinearColor;
|
||||||
|
use super::super::Point2D;
|
||||||
|
use super::Texture;
|
||||||
|
|
||||||
|
/// A texture with the same color on all points.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct UniformTexture {
|
||||||
|
color: LinearColor,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UniformTexture {
|
||||||
|
pub fn new(color: LinearColor) -> Self {
|
||||||
|
UniformTexture { color }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Texture for UniformTexture {
|
||||||
|
fn texel_color(&self, _: Point2D) -> LinearColor {
|
||||||
|
self.color.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn new_works() {
|
||||||
|
let color = LinearColor::new(0.2, 0.4, 0.6);
|
||||||
|
let texture = UniformTexture::new(color.clone());
|
||||||
|
assert_eq!(texture, UniformTexture { color })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn simple_texture() -> UniformTexture {
|
||||||
|
UniformTexture::new(LinearColor::new(0.25, 0.5, 1.))
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn texel_color_works() {
|
||||||
|
let texture = simple_texture();
|
||||||
|
assert_eq!(
|
||||||
|
texture.texel_color(Point2D::origin()),
|
||||||
|
LinearColor::new(0.25, 0.5, 1.)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue