From 4f3af74c2282408383cd9fb729a953b9eb7febf4 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 17 Mar 2020 20:16:55 +0100 Subject: [PATCH] library: render: add Object implementation --- src/render/mod.rs | 2 + src/render/object.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/render/object.rs diff --git a/src/render/mod.rs b/src/render/mod.rs index e69de29..099ed81 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -0,0 +1,2 @@ +pub mod object; +pub use object::*; diff --git a/src/render/object.rs b/src/render/object.rs new file mode 100644 index 0000000..56c790e --- /dev/null +++ b/src/render/object.rs @@ -0,0 +1,90 @@ +use super::super::material::Material; +use super::super::shape::Shape; +use super::super::texture::Texture; + +/// An object being rendered in the scene. +#[derive(Debug)] +pub struct Object<'a> { + pub shape: &'a dyn Shape, + pub material: &'a dyn Material, + pub texture: &'a dyn Texture, +} + +impl<'a> Object<'a> { + #[allow(dead_code)] // FIXME: remove this when used + pub fn new(shape: &'a dyn Shape, material: &'a dyn Material, texture: &'a dyn Texture) -> Self { + Object { + shape, + material, + texture, + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + use crate::core::color::LinearColor; + use crate::*; + use bvh::{ + aabb::{Bounded, AABB}, + ray::Ray, + }; + + /// NOTE(Bruno): those dummy implementations could be used somewhere else ? + + #[derive(Debug)] + struct DummyShape {} + + impl Bounded for DummyShape { + fn aabb(&self) -> AABB { + todo!() + } + } + + impl Shape for DummyShape { + /// Return the distance at which the object intersects with the ray, or None if it does not. + fn intersect(&self, _: &Ray) -> Option { + todo!() + } + /// Return the unit vector corresponding to the normal at this point of the shape. + fn normal(&self, _: &Point) -> Vector { + todo!() + } + /// Project the point from the shape's surface to its texel coordinates. + fn project_texel(&self, _: &Point) -> Point2D { + todo!() + } + } + + #[derive(Debug)] + struct DummyMaterial {} + + impl Material for DummyMaterial { + fn diffuse(&self, _: Point2D) -> LinearColor { + todo!() + } + fn specular(&self, _: Point2D) -> LinearColor { + todo!() + } + } + + #[derive(Debug)] + struct DummyTexture {} + + impl Texture for DummyTexture { + fn texel_color(&self, _: Point2D) -> LinearColor { + todo!() + } + } + + #[test] + fn new_works() { + let shape = DummyShape {}; + let material = DummyMaterial {}; + let texture = DummyTexture {}; + let _ = Object::new(&shape, &material, &texture); + // Can't compare the results... Just make sure the new compiles. + } +}