Skip to content

Commit

Permalink
refactor: object is now a enum and its either primitive or a group
Browse files Browse the repository at this point in the history
  • Loading branch information
karolzwolak committed Mar 16, 2024
1 parent ad10696 commit 05a16bb
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 177 deletions.
34 changes: 17 additions & 17 deletions src/playing_around/cubes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ use crate::{
};

pub fn run(width: usize, height: usize) -> Canvas {
let skybox = Object::new(
let skybox = Object::primitive(
Shape::Cube,
Material::with_pattern(Pattern::Const(Color::new(0.2, 0.35, 0.78))),
Matrix::scaling_uniform(10.),
);

let floor = Object::new(
let floor = Object::primitive(
Shape::Plane,
Material {
pattern: Pattern::checkers(
Expand All @@ -53,31 +53,31 @@ pub fn run(width: usize, height: usize) -> Canvas {

let leg_scaling = Matrix::identity().scale(0.08, 1., 0.08).transformed();

let leg1 = Object::new(
let leg1 = Object::primitive(
Shape::Cube,
wood_material.clone(),
leg_scaling.clone().translate(1., 0.5, 1.).transformed(),
);

let leg2 = Object::new(
let leg2 = Object::primitive(
Shape::Cube,
wood_material.clone(),
leg_scaling.clone().translate(-1., 0.5, 1.).transformed(),
);

let leg3 = Object::new(
let leg3 = Object::primitive(
Shape::Cube,
wood_material.clone(),
leg_scaling.clone().translate(1., 0.5, -1.).transformed(),
);

let leg4 = Object::new(
let leg4 = Object::primitive(
Shape::Cube,
wood_material.clone(),
leg_scaling.clone().translate(-1., 0.5, -1.).transformed(),
);

let table_top = Object::new(
let table_top = Object::primitive(
Shape::Cube,
Material {
reflectivity: 0.05,
Expand All @@ -91,7 +91,7 @@ pub fn run(width: usize, height: usize) -> Canvas {

let walls_width = 5.;

let walls = Object::new(
let walls = Object::primitive(
Shape::Cube,
Material::with_pattern(Pattern::stripe(
Color::new(0.42, 0.55, 0.42),
Expand All @@ -113,7 +113,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
let mirror_scale = Matrix::scaling(2.5, 1.5, 0.01);
let mirror_translate = Matrix::translation(0., 2.25, -walls_width).transformed();

let mirror_frame1 = Object::new(
let mirror_frame1 = Object::primitive(
Shape::Cube,
wood_material.clone(),
frame_scaling_ver
Expand All @@ -123,7 +123,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
.transformed(),
);

let mirror_frame2 = Object::new(
let mirror_frame2 = Object::primitive(
Shape::Cube,
wood_material.clone(),
frame_scaling_ver
Expand All @@ -133,7 +133,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
.transformed(),
);

let mirror_frame3 = Object::new(
let mirror_frame3 = Object::primitive(
Shape::Cube,
wood_material.clone(),
frame_scaling_hor
Expand All @@ -144,7 +144,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
.transformed(),
);

let mirror_frame4 = Object::new(
let mirror_frame4 = Object::primitive(
Shape::Cube,
wood_material,
frame_scaling_hor
Expand All @@ -155,7 +155,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
.transformed(),
);

let mirror = Object::new(
let mirror = Object::primitive(
Shape::Cube,
Material::mirror(),
Matrix::identity()
Expand All @@ -165,7 +165,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
.transformed(),
);

let glass_cube = Object::new(
let glass_cube = Object::primitive(
Shape::Cube,
Material::glass(),
Matrix::identity()
Expand All @@ -174,7 +174,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
.transformed(),
);

let tinted_cube = Object::new(
let tinted_cube = Object::primitive(
Shape::Cube,
Material {
pattern: Pattern::Const(Color::new(0.4, 0.2, 0.3)),
Expand All @@ -190,7 +190,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
let c1 = Color::new(0.4, 0.2, 0.3);
let c2 = Color::new(0.2, 0.3, 0.4);

let pattern_cube = Object::new(
let pattern_cube = Object::primitive(
Shape::Cube,
Material::with_pattern(Pattern::checkers(
c1,
Expand All @@ -204,7 +204,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
.transformed(),
);

let mirror_cube = Object::new(
let mirror_cube = Object::primitive(
Shape::Cube,
Material::mirror(),
Matrix::identity()
Expand Down
12 changes: 6 additions & 6 deletions src/playing_around/cylinders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
};

pub fn run(width: usize, height: usize) -> Canvas {
let skybox = Object::new(
let skybox = Object::primitive(
Shape::Cube,
Material::with_pattern(Pattern::Const(Color::new(0.2, 0.35, 0.78))),
Matrix::scaling_uniform(10.),
Expand All @@ -29,14 +29,14 @@ pub fn run(width: usize, height: usize) -> Canvas {
.translate(-0.5, 0., 0.)
.transformed();

let arrow_body = Object::new(
let arrow_body = Object::primitive(
Shape::unit_cylinder(),
arrow_material.clone(),
Matrix::scaling(0.1, 1., 0.1)
.transform(arrow_rotation)
.transformed(),
);
let arrow_head = Object::new(
let arrow_head = Object::primitive(
Shape::unit_cone(),
arrow_material,
Matrix::scaling(0.2, 0.5, 0.2)
Expand All @@ -45,23 +45,23 @@ pub fn run(width: usize, height: usize) -> Canvas {
.transformed(),
);

let ice_cream_cone = Object::new(
let ice_cream_cone = Object::primitive(
Shape::cone(1., 0.5, false),
Material::with_pattern(Pattern::Const(Color::new(0.67, 0.57, 0.38))),
Matrix::scaling(0.5, 1., 0.5)
.translate(1., -0.15, 0.)
.transformed(),
);

let vanilla_scoop = Object::new(
let vanilla_scoop = Object::primitive(
Shape::Sphere,
Material::with_pattern(Pattern::Const(Color::new(0.95, 0.89, 0.67))),
Matrix::scaling(0.5, 0.5, 0.5)
.translate(1., 1., 0.)
.transformed(),
);

let choc_scoop = Object::new(
let choc_scoop = Object::primitive(
Shape::Sphere,
Material::with_pattern(Pattern::Const(Color::new(0.48, 0.24, 0.))),
Matrix::scaling(0.5, 0.5, 0.5)
Expand Down
9 changes: 5 additions & 4 deletions src/playing_around/making_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
color::Color,
light::PointLightSource,
material::Material,
object::{shape::Shape, Object},
object::{shape::Shape, Object, PrimitiveObject},
pattern::Pattern,
world::World,
},
Expand All @@ -14,16 +14,17 @@ use crate::{
use super::making_scene;

pub fn run(width: usize, height: usize) -> Canvas {
let floor = Object::with_shape_material(
let floor = PrimitiveObject::with_shape_material(
Shape::Plane,
Material::with_pattern(Pattern::ring(
Color::new(0.15, 0.6, 0.7),
Color::new(0.5, 0.1, 0.4),
Some(Matrix::scaling_uniform(0.25)),
)),
);
)
.into();

let sphere = Object::new(
let sphere = Object::primitive(
Shape::Sphere,
Material::with_pattern(Pattern::checkers(
Color::white(),
Expand Down
69 changes: 41 additions & 28 deletions src/playing_around/making_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,67 @@ use crate::{
vector::Vector,
},
render::{
camera::Camera, canvas::Canvas, color::Color, light::PointLightSource, material::Material,
object::shape::Shape, object::Object, pattern::Pattern, world::World,
camera::Camera,
canvas::Canvas,
color::Color,
light::PointLightSource,
material::Material,
object::{shape::Shape, Object, PrimitiveObject},
pattern::Pattern,
world::World,
},
};

pub fn scene_objects() -> Vec<Object> {
let mut middle_sphere =
Object::with_transformation(Shape::Sphere, Matrix::translation(-0.5, 1., 0.5));

middle_sphere.set_material(Material {
pattern: Pattern::Const(Color::new(0.1, 1., 0.5)),
diffuse: 0.7,
specular: 0.3,
..Default::default()
});
let middle_sphere = Object::primitive(
Shape::Sphere,
Material {
pattern: Pattern::Const(Color::new(0.1, 1., 0.5)),
diffuse: 0.7,
specular: 0.3,
..Default::default()
},
Matrix::translation(-0.5, 1., 0.5),
);

let mut right_sphere = Object::new(
let mut right_sphere = Object::primitive(
Shape::Sphere,
middle_sphere.material().clone(),
Material {
pattern: Pattern::Const(Color::new(0.5, 1., 0.1)),
..middle_sphere.material().clone()
},
Matrix::scaling_uniform(0.5)
.translate(1.5, 0.5, -0.5)
.transformed(),
);
right_sphere.material_mut().pattern = Pattern::Const(Color::new(0.5, 1., 0.1));

let mut left_sphere = Object::with_transformation(
let mut left_sphere = Object::primitive(
Shape::Sphere,
Material {
pattern: Pattern::Const(Color::new(1., 0.8, 0.1)),
diffuse: 0.7,
specular: 0.3,
..Default::default()
},
Matrix::scaling_uniform(0.33)
.translate(-1.5, 0.33, -0.75)
.transformed(),
);

left_sphere.set_material(Material {
pattern: Pattern::Const(Color::new(1., 0.8, 0.1)),
diffuse: 0.7,
specular: 0.3,
..Default::default()
});

vec![middle_sphere, right_sphere, left_sphere]
}

pub fn scene_walls() -> Vec<Object> {
let mut floor = Object::with_transformation(Shape::Sphere, Matrix::scaling(10., 0.01, 10.));
floor.material_mut().specular = 0.;
floor.material_mut().pattern = Pattern::Const(Color::new(1., 0.9, 0.9));
let floor = Object::primitive(
Shape::Sphere,
Material {
specular: 0.,
pattern: Pattern::Const(Color::new(1., 0.9, 0.9)),
..Default::default()
},
Matrix::scaling(10., 0.01, 10.),
);

let left_wall = Object::new(
let left_wall = Object::primitive(
Shape::Sphere,
floor.material().clone(),
Matrix::scaling(10., 0.01, 10.)
Expand All @@ -65,7 +78,7 @@ pub fn scene_walls() -> Vec<Object> {
.transformed(),
);

let right_wall = Object::new(
let right_wall = Object::primitive(
Shape::Sphere,
floor.material().clone(),
Matrix::scaling(10., 0.01, 10.)
Expand Down
18 changes: 12 additions & 6 deletions src/playing_around/reflections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ pub fn get_walls() -> Vec<Object> {

let dist = 12.;

let mut floor = Object::new(Shape::Plane, material.clone(), Matrix::identity());
floor.material_mut().reflectivity = 0.4;
let mut floor = Object::primitive(
Shape::Plane,
Material {
reflectivity: 0.4,
..material.clone()
},
Matrix::identity(),
);

let left_wall = Object::new(
let left_wall = Object::primitive(
Shape::Plane,
material.clone(),
Matrix::rotation_x(FRAC_PI_2)
Expand All @@ -35,7 +41,7 @@ pub fn get_walls() -> Vec<Object> {
.transformed(),
);

let right_wall = Object::new(
let right_wall = Object::primitive(
Shape::Plane,
material,
Matrix::rotation_x(FRAC_PI_2)
Expand Down Expand Up @@ -70,7 +76,7 @@ pub fn run(width: usize, height: usize) -> Canvas {

let mirror_dist = 3.;

let mirror_wall = Object::new(
let mirror_wall = Object::primitive(
Shape::Plane,
mirror.clone(),
Matrix::rotation_x(FRAC_PI_2)
Expand All @@ -79,7 +85,7 @@ pub fn run(width: usize, height: usize) -> Canvas {
.transformed(),
);

let mirror_wall2 = Object::new(
let mirror_wall2 = Object::primitive(
Shape::Plane,
mirror,
Matrix::rotation_x(FRAC_PI_2)
Expand Down
Loading

0 comments on commit 05a16bb

Please sign in to comment.