Commit 78b38890 authored by Stephen D's avatar Stephen D
Browse files

generify some stuff

parent d87f77cd
......@@ -27,10 +27,10 @@ impl BlockTexture {
let texture = ImageTexture::new(&filename)?;
return Some(Self {
top: Box::new(Lambertian::new(Box::new(texture.clone()))),
front: Box::new(Lambertian::new(Box::new(texture.clone()))),
side: Box::new(Lambertian::new(Box::new(texture.clone()))),
bottom: Box::new(Lambertian::new(Box::new(texture.clone()))),
top: Box::new(Lambertian::new(texture.clone())),
front: Box::new(Lambertian::new(texture.clone())),
side: Box::new(Lambertian::new(texture.clone())),
bottom: Box::new(Lambertian::new(texture.clone())),
emit: false,
});
}
......@@ -91,10 +91,10 @@ impl BlockTexture {
Box<dyn Material>,
) = if emit {
(
Box::new(DiffuseLight::new(Box::new(top?))),
Box::new(DiffuseLight::new(Box::new(front?))),
Box::new(DiffuseLight::new(Box::new(side?))),
Box::new(DiffuseLight::new(Box::new(bottom?))),
Box::new(DiffuseLight::new(top?)),
Box::new(DiffuseLight::new(front?)),
Box::new(DiffuseLight::new(side?)),
Box::new(DiffuseLight::new(bottom?)),
)
}
/*else if transparent {
......@@ -107,10 +107,10 @@ impl BlockTexture {
}*/
else {
(
Box::new(Lambertian::new(Box::new(top?))),
Box::new(Lambertian::new(Box::new(front?))),
Box::new(Lambertian::new(Box::new(side?))),
Box::new(Lambertian::new(Box::new(bottom?))),
Box::new(Lambertian::new(top?)),
Box::new(Lambertian::new(front?)),
Box::new(Lambertian::new(side?)),
Box::new(Lambertian::new(bottom?)),
)
};
......
......@@ -39,21 +39,17 @@ pub trait Material: Send + Sync + Clone {
}
#[derive(Clone)]
pub struct Lambertian {
texture: Box<dyn Texture>,
pub struct Lambertian<T: Texture + Clone> {
texture: T,
}
impl Lambertian {
pub fn new(texture: Box<dyn Texture>) -> Self {
impl<T: Texture + Clone> Lambertian<T> {
pub fn new(texture: T) -> Self {
Self { texture }
}
pub fn from_color(color: Color) -> Self {
Self::new(Box::new(SolidColor::new(color)))
}
}
impl Material for Lambertian {
impl<T: Texture + Clone> Material for Lambertian<T> {
fn scatter(&self, _r_in: &Ray, rec: &HitRecord) -> Option<ScatterData> {
let uvw = Onb::build_from_w(rec.normal);
let direction = uvw.local(Vec3::random_cosine_direction(&mut thread_rng()));
......@@ -157,22 +153,24 @@ impl Material for Dielectric {
*/
#[derive(Clone)]
pub struct DiffuseLight {
emit: Box<dyn Texture>,
pub struct DiffuseLight<T: Texture + Clone> {
emit: T,
}
impl DiffuseLight {
pub fn new(emit: Box<dyn Texture>) -> Self {
impl<T: Texture + Clone> DiffuseLight<T> {
pub fn new(emit: T) -> Self {
Self { emit }
}
}
impl DiffuseLight<SolidColor> {
pub fn from_color(color: Color) -> Self {
let emit = Box::new(SolidColor::new(color));
let emit = SolidColor::new(color);
Self { emit }
}
}
impl Material for DiffuseLight {
impl<T: Texture + Clone> Material for DiffuseLight<T> {
fn scatter(&self, _: &Ray, _: &HitRecord) -> Option<ScatterData> {
None
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment