shapes: create generic Shape object with Sphere implementation.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-07-19 19:51:12 -07:00
parent 7741766635
commit 7de0f07f56
8 changed files with 167 additions and 127 deletions

View File

@@ -4,7 +4,7 @@ use rtchallenge::{
canvas::Canvas,
matrices::Matrix4x4,
rays::Ray,
spheres::{intersect, Sphere},
shapes::{intersect, Shape},
tuples::{Color, Tuple},
Float,
};
@@ -20,7 +20,7 @@ fn main() -> Result<()> {
let pixel_size = wall_size / w as Float;
let half = wall_size / 2.;
let color = Color::new(1., 0., 0.);
let mut shape = Sphere::default();
let mut shape = Shape::sphere();
shape.set_transform(
Matrix4x4::shearing(1., 0., 0., 0., 0., 0.) * Matrix4x4::scaling(0.5, 1., 1.0),
);

View File

@@ -5,7 +5,7 @@ use rtchallenge::{
lights::PointLight,
materials::{lighting, Material},
rays::Ray,
spheres::{intersect, Sphere},
shapes::{intersect, Shape},
tuples::{Color, Tuple},
Float, WHITE,
};
@@ -20,14 +20,14 @@ fn main() -> Result<()> {
let wall_size = 7.;
let pixel_size = wall_size / w as Float;
let half = wall_size / 2.;
let mut shape = Sphere::default();
shape.material = Material {
let mut shape = Shape::sphere();
shape.set_material(Material {
color: Color::new(1., 0.2, 1.),
specular: 0.5,
diffuse: 0.7,
shininess: 30.,
..Material::default()
};
});
let light_position = Tuple::point(-10., 10., -10.);
let light_color = WHITE;
let light = PointLight::new(light_position, light_color);
@@ -44,7 +44,14 @@ fn main() -> Result<()> {
let point = r.position(hit.t);
let normal = hit.object.normal_at(point);
let eye = -r.direction;
let color = lighting(&hit.object.material, &light, point, eye, normal, in_shadow);
let color = lighting(
&hit.object.material(),
&light,
point,
eye,
normal,
in_shadow,
);
c.set(x, y, color);
}
}

View File

@@ -9,7 +9,7 @@ use rtchallenge::{
lights::PointLight,
materials::Material,
matrices::Matrix4x4,
spheres::Sphere,
shapes::Shape,
transformations::view_transform,
tuples::{Color, Tuple},
world::World,
@@ -39,60 +39,60 @@ fn main() -> Result<()> {
camera.set_transform(view_transform(from, to, up));
camera.render_strategy = opt.render_strategy;
let mut floor = Sphere::default();
let mut floor = Shape::sphere();
floor.set_transform(Matrix4x4::scaling(10., 0.01, 10.));
floor.material = Material {
floor.set_material(Material {
color: Color::new(1., 0.9, 0.9),
specular: 0.,
..Material::default()
};
});
let mut left_wall = Sphere::default();
let mut left_wall = Shape::sphere();
left_wall.set_transform(
Matrix4x4::translation(0., 0., 5.)
* Matrix4x4::rotation_y(-PI / 4.)
* Matrix4x4::rotation_x(PI / 2.)
* Matrix4x4::scaling(10., 0.01, 10.),
);
left_wall.material = floor.material.clone();
left_wall.set_material(floor.material().clone());
let mut right_wall = Sphere::default();
let mut right_wall = Shape::sphere();
right_wall.set_transform(
Matrix4x4::translation(0., 0., 5.)
* Matrix4x4::rotation_y(PI / 4.)
* Matrix4x4::rotation_x(PI / 2.)
* Matrix4x4::scaling(10., 0.01, 10.),
);
right_wall.material = floor.material.clone();
right_wall.set_material(floor.material().clone());
let mut middle = Sphere::default();
let mut middle = Shape::sphere();
middle.set_transform(Matrix4x4::translation(-0.5, 1., 0.5));
middle.material = Material {
middle.set_material(Material {
color: Color::new(0.1, 1., 0.5),
diffuse: 0.7,
specular: 0.3,
..Material::default()
};
});
let mut right = Sphere::default();
let mut right = Shape::sphere();
right.set_transform(Matrix4x4::translation(1.5, 0.5, -0.5) * Matrix4x4::scaling(0.5, 0.5, 0.5));
right.material = Material {
right.set_material(Material {
color: Color::new(0.5, 1., 0.1),
diffuse: 0.7,
specular: 0.3,
..Material::default()
};
});
let mut left = Sphere::default();
let mut left = Shape::sphere();
left.set_transform(
Matrix4x4::translation(-1.5, 0.33, -0.75) * Matrix4x4::scaling(0.33, 0.33, 0.33),
);
left.material = Material {
left.set_material(Material {
color: Color::new(1., 0.8, 0.1),
diffuse: 0.7,
specular: 0.3,
..Material::default()
};
});
let mut world = World::default();
world.lights = vec![light];

View File

@@ -9,7 +9,7 @@ use rtchallenge::{
lights::PointLight,
materials::Material,
matrices::Matrix4x4,
spheres::Sphere,
shapes::Shape,
transformations::view_transform,
tuples::{Color, Tuple},
world::World,
@@ -51,60 +51,60 @@ fn main() -> Result<()> {
camera.render_strategy = opt.render_strategy;
camera.samples_per_pixel = opt.samples;
let mut floor = Sphere::default();
let mut floor = Shape::sphere();
floor.set_transform(Matrix4x4::scaling(10., 0.01, 10.));
floor.material = Material {
floor.set_material(Material {
color: Color::new(1., 0.9, 0.9),
specular: 0.,
..Material::default()
};
});
let mut left_wall = Sphere::default();
let mut left_wall = Shape::sphere();
left_wall.set_transform(
Matrix4x4::translation(0., 0., 5.)
* Matrix4x4::rotation_y(-PI / 4.)
* Matrix4x4::rotation_x(PI / 2.)
* Matrix4x4::scaling(10., 0.01, 10.),
);
left_wall.material = floor.material.clone();
left_wall.set_material(floor.material().clone());
let mut right_wall = Sphere::default();
let mut right_wall = Shape::sphere();
right_wall.set_transform(
Matrix4x4::translation(0., 0., 5.)
* Matrix4x4::rotation_y(PI / 4.)
* Matrix4x4::rotation_x(PI / 2.)
* Matrix4x4::scaling(10., 0.00001, 10.),
);
right_wall.material = floor.material.clone();
right_wall.set_material(floor.material().clone());
let mut middle = Sphere::default();
let mut middle = Shape::sphere();
middle.set_transform(Matrix4x4::translation(-0.5, 1., 0.5));
middle.material = Material {
middle.set_material(Material {
color: Color::new(0.1, 1., 0.5),
diffuse: 0.7,
specular: 0.3,
..Material::default()
};
});
let mut right = Sphere::default();
let mut right = Shape::sphere();
right.set_transform(Matrix4x4::translation(1.5, 0.5, -0.5) * Matrix4x4::scaling(0.5, 0.5, 0.5));
right.material = Material {
right.set_material(Material {
color: Color::new(1., 1., 1.),
diffuse: 0.7,
specular: 0.0,
..Material::default()
};
});
let mut left = Sphere::default();
let mut left = Shape::sphere();
left.set_transform(
Matrix4x4::translation(-1.5, 0.33, -0.75) * Matrix4x4::scaling(0.33, 0.33, 0.33),
);
left.material = Material {
left.set_material(Material {
color: Color::new(1., 0.8, 0.1),
diffuse: 0.7,
specular: 0.3,
..Material::default()
};
});
let mut world = World::default();
world.lights = vec![light1, light2, light3];