Use Box<Hit> instead of reference in HitableList.
This commit is contained in:
parent
196a4b76bc
commit
5ba579a374
@ -35,37 +35,41 @@ fn main() -> Result<(), std::io::Error> {
|
||||
let ny = 100;
|
||||
let ns = 100;
|
||||
println!("P3\n{} {}\n255", nx, ny);
|
||||
let objects = vec![
|
||||
Sphere::new(
|
||||
Vec3::new(0., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Lambertian::new(Vec3::new(0.1, 0.2, 0.5))),
|
||||
),
|
||||
Sphere::new(
|
||||
Vec3::new(0., -100.5, -1.),
|
||||
100.,
|
||||
Box::new(Lambertian::new(Vec3::new(0.8, 0.8, 0.))),
|
||||
),
|
||||
Sphere::new(
|
||||
Vec3::new(1., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.2)),
|
||||
),
|
||||
Sphere::new(Vec3::new(-1., 0., -1.), 0.5, Box::new(Dielectric::new(1.5))),
|
||||
Sphere::new(
|
||||
Vec3::new(-1., 0., -1.),
|
||||
-0.45,
|
||||
Box::new(Dielectric::new(1.5)),
|
||||
),
|
||||
];
|
||||
let cam = Camera::new_lookfrom_vfov(
|
||||
Vec3::new(-2., 2., 1.),
|
||||
Vec3::new(0., 0., -1.),
|
||||
Vec3::new(0., 1., 0.),
|
||||
90.,
|
||||
45.,
|
||||
nx as f32 / ny as f32,
|
||||
);
|
||||
let world = HitableList::new(objects.iter().map(|o| o).collect());
|
||||
// TODO(wathiede): Box this instead of using references.
|
||||
let world = HitableList::new(vec![
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(0., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Lambertian::new(Vec3::new(0.1, 0.2, 0.5))),
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(0., -100.5, -1.),
|
||||
100.,
|
||||
Box::new(Lambertian::new(Vec3::new(0.8, 0.8, 0.))),
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(1., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.2)),
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(-1., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Dielectric::new(1.5)),
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(-1., 0., -1.),
|
||||
-0.45,
|
||||
Box::new(Dielectric::new(1.5)),
|
||||
)),
|
||||
]);
|
||||
for j in (0..ny).rev() {
|
||||
for i in 0..nx {
|
||||
let mut col: Vec3 = Default::default();
|
||||
|
||||
@ -35,31 +35,34 @@ fn main() -> Result<(), std::io::Error> {
|
||||
let ny = 100;
|
||||
let ns = 100;
|
||||
println!("P3\n{} {}\n255", nx, ny);
|
||||
let objects = vec![
|
||||
Sphere::new(
|
||||
let world = HitableList::new(vec![
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(0., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Lambertian::new(Vec3::new(0.1, 0.2, 0.5))),
|
||||
),
|
||||
Sphere::new(
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(0., -100.5, -1.),
|
||||
100.,
|
||||
Box::new(Lambertian::new(Vec3::new(0.8, 0.8, 0.))),
|
||||
),
|
||||
Sphere::new(
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(1., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.2)),
|
||||
),
|
||||
Sphere::new(Vec3::new(-1., 0., -1.), 0.5, Box::new(Dielectric::new(1.5))),
|
||||
Sphere::new(
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(-1., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Dielectric::new(1.5)),
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(-1., 0., -1.),
|
||||
-0.45,
|
||||
Box::new(Dielectric::new(1.5)),
|
||||
),
|
||||
];
|
||||
)),
|
||||
]);
|
||||
let cam = Camera::new2x1();
|
||||
let world = HitableList::new(objects.iter().map(|o| o).collect());
|
||||
for j in (0..ny).rev() {
|
||||
for i in 0..nx {
|
||||
let mut col: Vec3 = Default::default();
|
||||
|
||||
@ -34,30 +34,29 @@ fn main() -> Result<(), std::io::Error> {
|
||||
let ny = 100;
|
||||
let ns = 100;
|
||||
println!("P3\n{} {}\n255", nx, ny);
|
||||
let objects = vec![
|
||||
Sphere::new(
|
||||
let world = HitableList::new(vec![
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(0., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Lambertian::new(Vec3::new(0.8, 0.3, 0.3))),
|
||||
),
|
||||
Sphere::new(
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(0., -100.5, -1.),
|
||||
100.,
|
||||
Box::new(Lambertian::new(Vec3::new(0.8, 0.8, 0.))),
|
||||
),
|
||||
Sphere::new(
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(1., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.2)),
|
||||
),
|
||||
Sphere::new(
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(-1., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Metal::new(Vec3::new(0.8, 0.8, 0.8), 0.8)),
|
||||
),
|
||||
];
|
||||
)),
|
||||
]);
|
||||
let cam = Camera::new2x1();
|
||||
let world = HitableList::new(objects.iter().map(|o| o).collect());
|
||||
for j in (0..ny).rev() {
|
||||
for i in 0..nx {
|
||||
let mut col: Vec3 = Default::default();
|
||||
|
||||
@ -35,29 +35,33 @@ fn main() -> Result<(), std::io::Error> {
|
||||
let ny = 100;
|
||||
let ns = 100;
|
||||
println!("P3\n{} {}\n255", nx, ny);
|
||||
let objects = vec![
|
||||
Sphere::new(
|
||||
let world = HitableList::new(vec![
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(0., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Lambertian::new(Vec3::new(0.1, 0.2, 0.5))),
|
||||
),
|
||||
Sphere::new(
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(0., -100.5, -1.),
|
||||
100.,
|
||||
Box::new(Lambertian::new(Vec3::new(0.8, 0.8, 0.))),
|
||||
),
|
||||
Sphere::new(
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(1., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.2)),
|
||||
),
|
||||
Sphere::new(Vec3::new(-1., 0., -1.), 0.5, Box::new(Dielectric::new(1.5))),
|
||||
Sphere::new(
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(-1., 0., -1.),
|
||||
0.5,
|
||||
Box::new(Dielectric::new(1.5)),
|
||||
)),
|
||||
Box::new(Sphere::new(
|
||||
Vec3::new(-1., 0., -1.),
|
||||
-0.45,
|
||||
Box::new(Dielectric::new(1.5)),
|
||||
),
|
||||
];
|
||||
)),
|
||||
]);
|
||||
let cam = Camera::new_lookfrom_vfov(
|
||||
Vec3::new(-2., 2., 1.),
|
||||
Vec3::new(0., 0., -1.),
|
||||
@ -65,7 +69,6 @@ fn main() -> Result<(), std::io::Error> {
|
||||
90.,
|
||||
nx as f32 / ny as f32,
|
||||
);
|
||||
let world = HitableList::new(objects.iter().map(|o| o).collect());
|
||||
for j in (0..ny).rev() {
|
||||
for i in 0..nx {
|
||||
let mut col: Vec3 = Default::default();
|
||||
|
||||
@ -9,6 +9,6 @@ pub struct HitRecord<'m> {
|
||||
pub material: &'m Material,
|
||||
}
|
||||
|
||||
pub trait Hit {
|
||||
pub trait Hit: Sync {
|
||||
fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord>;
|
||||
}
|
||||
|
||||
@ -3,26 +3,17 @@ use hitable::HitRecord;
|
||||
use ray::Ray;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct HitableList<'a, H>
|
||||
where
|
||||
H: Hit + 'a,
|
||||
{
|
||||
list: Vec<&'a H>,
|
||||
pub struct HitableList {
|
||||
list: Vec<Box<Hit>>,
|
||||
}
|
||||
|
||||
impl<'a, H> HitableList<'a, H>
|
||||
where
|
||||
H: Hit,
|
||||
{
|
||||
pub fn new(list: Vec<&'a H>) -> HitableList<H> {
|
||||
impl HitableList {
|
||||
pub fn new(list: Vec<Box<Hit>>) -> HitableList {
|
||||
HitableList { list }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, H> Hit for HitableList<'a, H>
|
||||
where
|
||||
H: Hit,
|
||||
{
|
||||
impl Hit for HitableList {
|
||||
fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> {
|
||||
let mut min_hit = None;
|
||||
let mut closest_so_far = t_max;
|
||||
|
||||
@ -28,7 +28,7 @@ pub struct ScatterResponse {
|
||||
pub reflected: bool,
|
||||
}
|
||||
|
||||
pub trait Material {
|
||||
pub trait Material: Sync {
|
||||
fn scatter(&self, r_in: &Ray, rec: &HitRecord) -> ScatterResponse;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user