diff --git a/README.md b/README.md index 2d5d125..a7a60ae 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ A ray marching renderer in rust ![3rd test scene](prod/3.png) ![randomly generated spheres](prod/randomspheres.png) ![randomly generated objects](prod/smolgalaxy.png) +![inside a spherical mirror](prod/insidemirror.png) ## License MIT diff --git a/prod/insidemirror.png b/prod/insidemirror.png new file mode 100644 index 0000000..fad4a33 Binary files /dev/null and b/prod/insidemirror.png differ diff --git a/scenes/insidemirror.lua b/scenes/insidemirror.lua new file mode 100644 index 0000000..4f24488 --- /dev/null +++ b/scenes/insidemirror.lua @@ -0,0 +1,42 @@ +local mirror = obj.withmaterial( + obj.negation( + obj.sphere(vec3.O, 8) + ), + material.newfromdiagonal( + colorvec.new(0, 0, 0, 0), + colorvec.new(1, 1, 1, 1), + surfacetype.REFLECTIVE + ) +) + +local objects = { + obj.withmaterial( + obj.torus(vec3.new(1, 0, -3), 2, 1), + material.newfromdiagonal( + colorvec.new(1, .5, .2, 0), + colorvec.new(1, 1, 1, 1), + surfacetype.DIFFUSE + ) + ), + obj.withmaterial( + obj.sphere(vec3.new(3, 2, 0), 1), + material.newfromdiagonal( + colorvec.new(0, 0, 0, 0), + colorvec.new(.2, .5, 1, 0), + surfacetype.DIFFUSE + ) + ), + obj.withmaterial( + obj.cuboid(vec3.new(-3, -1, 2), vec3.new(1, 1, 1)), + material.newfromdiagonal( + colorvec.new(0, 0, 0, 0), + colorvec.new(.2, 1, .5, 0), + surfacetype.DIFFUSE + ) + ) +} + +return obj.union( + util.union(objects), + mirror +) \ No newline at end of file diff --git a/src/lua/obj.rs b/src/lua/obj.rs index 5b51bcf..f3eb2dc 100644 --- a/src/lua/obj.rs +++ b/src/lua/obj.rs @@ -46,6 +46,10 @@ pub fn obj<'lua>(ctx: Context<'lua>, _env: Table<'lua>) -> rlua::Result Scene { fn main() { // get scene and camera - //let scene = scene_from_file("scenes/randomspheres.lua".to_owned()).unwrap(); - let scene = default_scene3(); + let scene = scene_from_file("scenes/insidemirror.lua".to_owned()).unwrap(); + //let scene = default_scene3(); let cam = default_cam(); // get stats on the scene we're about to render diff --git a/src/object/mod.rs b/src/object/mod.rs index 1c5aed1..10c082f 100644 --- a/src/object/mod.rs +++ b/src/object/mod.rs @@ -10,6 +10,7 @@ mod plane; mod union; mod intersection; mod exclusion; +mod negation; mod cuboid; mod cylinder; mod torus; @@ -65,6 +66,7 @@ pub use plane::Plane; pub use union::Union; pub use intersection::Intersection; pub use exclusion::Exclusion; +pub use negation::Negation; pub use cuboid::Cuboid; pub use cylinder::Cylinder; pub use torus::Torus; diff --git a/src/object/negation.rs b/src/object/negation.rs new file mode 100644 index 0000000..b3e4bdd --- /dev/null +++ b/src/object/negation.rs @@ -0,0 +1,33 @@ +use crate::object::Obj; +use crate::structs::Vec3; +use crate::material::Material; +use crate::light::Light; + +#[derive(Debug, Clone, PartialEq)] +pub struct Negation { + obj: T +} + +impl Negation { + pub fn new(obj: T) -> Negation { + Negation { obj } + } +} + +impl Obj for Negation { + fn distance_to(&self, point: Vec3) -> f64 { + -self.obj.distance_to(point) + } + fn normal_at(&self, point: Vec3) -> Vec3 { + -self.obj.normal_at(point) + } + fn material_at(&self, point: Vec3) -> Material { + self.obj.material_at(point) + } + fn get_lights(&self) -> Vec { + self.obj.get_lights() + } + fn node_count(&self) -> u32 { + self.obj.node_count() + 1 + } +}