rustracer/src/texture.rs
2022-07-06 08:48:00 +02:00

57 lines
1.3 KiB
Rust

use crate::{Point3, Color};
pub trait Texture: Sync + Send {
fn value(&self, u: f64, v: f64, point: &Point3) -> Color;
}
pub struct SolidColor {
color_value: Color
}
impl SolidColor {
pub fn new(red: f64, green: f64, blue: f64) -> Self {
SolidColor {
color_value: Color::new(red, green, blue)
}
}
}
impl From<Color> for SolidColor {
fn from(color: Color) -> Self {
SolidColor { color_value: color }
}
}
impl Texture for SolidColor {
fn value(&self, _u: f64, _v: f64, _point: &Point3) -> Color {
self.color_value
}
}
pub struct CheckerTexture {
pub odd: Box<dyn Texture>,
pub even: Box<dyn Texture>
}
impl CheckerTexture {
pub fn colored(color1: Color, color2: Color) -> Self {
CheckerTexture {
even: Box::new(SolidColor::from(color1)),
odd: Box::new(SolidColor::from(color2)),
}
}
}
impl Texture for CheckerTexture {
fn value(&self, u: f64, v: f64, point: &Point3) -> Color {
let sines = (10.0 * point.x()).sin() *
(10.0 * point.y()).sin() *
(10.0 * point.z()).sin();
match sines {
sines if sines < 0.0 => self.odd.value(u, v, point),
_ => self.even.value(u, v, point),
}
}
}