Skip to content

Commit

Permalink
Merge and fix branch 'refactor-object'
Browse files Browse the repository at this point in the history
  • Loading branch information
karolzwolak committed Mar 16, 2024
2 parents f9f5f06 + 05a16bb commit 097e6b0
Show file tree
Hide file tree
Showing 27 changed files with 497 additions and 331 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_chain(&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
10 changes: 5 additions & 5 deletions src/playing_around/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use crate::{
canvas::Canvas,
color::Color,
light::PointLightSource,
object::{cylinder::Cylinder, group::ObjectGroup, shape::Shape, Object},
object::{cylinder::Cylinder, group::ObjectGroup, shape::Shape, Object, PrimitiveObject},
world::World,
},
};

fn hexagon() -> Object {
let corner_sphere = Object::sphere(Point::new(0., 0., -1.), 0.25);
let cylinder = Object::with_transformation(
let corner_sphere = Object::from(PrimitiveObject::sphere(Point::new(0., 0., -1.), 0.25));
let cylinder = Object::primitive_with_transformation(
Shape::Cylinder(Cylinder {
y_min: 0.,
y_max: 1.,
Expand All @@ -32,15 +32,15 @@ fn hexagon() -> Object {
);
let mut hexagon_group = ObjectGroup::new(vec![cylinder.clone(), corner_sphere.clone()]);
hexagon_group.add_bounding_box_as_obj();
let hexagon_part = Object::with_shape(hexagon_group.into_shape());
let hexagon_part = Object::from_group(hexagon_group);

let mut hexagon = ObjectGroup::new(vec![hexagon_part.clone()]);

for _ in 0..6 {
hexagon.transform(&Matrix::rotation_y(consts::FRAC_PI_3));
hexagon.add_child(hexagon_part.clone());
}
Object::with_shape(hexagon.into_shape())
Object::from_group(hexagon)
}

pub fn run(width: usize, height: usize) -> Canvas {
Expand Down
4 changes: 2 additions & 2 deletions src/playing_around/light_and_shading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
intersection::IntersectionCollection,
light::{color_of_illuminated_point, PointLightSource},
material::Material,
object::Object,
object::{Object, PrimitiveObject},
pattern::Pattern,
ray::Ray,
},
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn run() -> Canvas {
..Default::default()
};

let mut sphere_obj = Object::sphere(Point::new(0., 0., 0.), radius);
let mut sphere_obj = Object::from(PrimitiveObject::sphere(Point::new(0., 0., 0.), radius));
sphere_obj.set_material(material);

let light = PointLightSource::new(
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
73 changes: 43 additions & 30 deletions src/playing_around/making_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,79 @@ 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},
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 right_sphere = Object::primitive(
Shape::Sphere,
middle_sphere.material().clone(),
Material {
pattern: Pattern::Const(Color::new(0.5, 1., 0.1)),
..middle_sphere.material_unwrapped().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 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(),
floor.material_unwrapped().clone(),
Matrix::scaling(10., 0.01, 10.)
.rotate_x(FRAC_PI_2)
.rotate_y(-FRAC_PI_4)
.translate(0., 0., 5.)
.transformed(),
);

let right_wall = Object::new(
let right_wall = Object::primitive(
Shape::Sphere,
floor.material().clone(),
floor.material_unwrapped().clone(),
Matrix::scaling(10., 0.01, 10.)
.rotate_x(FRAC_PI_2)
.rotate_y(FRAC_PI_4)
Expand Down
Loading

0 comments on commit 097e6b0

Please sign in to comment.