-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.rs
145 lines (137 loc) · 4.92 KB
/
world.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::Dielectric;
use crate::Hit;
use crate::Lambertian;
use crate::Material;
use crate::Metal;
use crate::Object;
use crate::Ray;
use crate::Sphere;
use crate::Vec3;
use crate::AABB;
use rand::Rng;
use std::f32;
use crate::BVH;
use crate::ConstantTexture;
use crate::CheckerTexture;
use crate::Camera;
use crate::NoiseTexture;
use crate::ImageTexture;
use std::sync::Arc;
pub struct World {
//pub objs: Vec<Box<Object>>,
pub bvh : Box<BVH>
}
impl Object for World {
fn bounding_box(&self, t0: f32, t1: f32) -> Option<AABB>{
/*let mut boxes = self.objs.iter().filter_map(|x| x.bounding_box(t0, t1));
boxes.next().map(|first| boxes.fold(first, |p,c| {
p.merge(&c)
}))*/
self.bvh.bounding_box(t0, t1)
}
fn hits<'a>(&'a self, ray: &Ray, t_min: f32, t_max: f32) -> Option<Hit<'a>> {
self.bvh.hits(ray, t_min, t_max)
/*self.objs
.iter()
//.map(|obj| (*obj).hits(ray, 0.01, f32::MAX))
.filter_map(|obj| (*obj).hits(ray, t_min.max(0.01), t_max))
.min_by(|a, b| a.t.partial_cmp(&b.t).unwrap_or(std::cmp::Ordering::Equal))*/
}
}
impl World {
pub fn new(objs: Vec<Box<Object>>, t0: f32, t1: f32) -> Self {
Self { bvh:BVH::new(objs, t0, t1) }
}
pub fn build_random_scene(t0:f32, t1:f32, aspect: f32) -> (World, Camera) {
let rand = || rand::thread_rng().gen::<f32>();
let mut list: Vec<Box<Object>> = (-11..11)
.flat_map(|a| {
(-11..11).map(move |b| {
let choose = rand();
let center =
Vec3::new((a as f32) + 0.9 * rand(), 0.2, (b as f32) + 0.9 * rand());
if (center - Vec3::new(4.0, 0.2, 0.0)).len() > 0.9 {
let mat: Box<Material> = if choose < 0.8 {
Box::new(Lambertian::new(Arc::new(ConstantTexture::new(Vec3::new(
rand() * rand(),
rand() * rand(),
rand() * rand(),
)))))
} else if choose < 0.95 {
Box::new(Metal::new(
Vec3::new(
0.5 * (1.0 + rand()),
0.5 * (1.0 + rand()),
0.5 * (1.0 + rand()),
),
0.5 * rand(),
))
} else {
Box::new(Dielectric::new(1.5))
};
let tmp: Box<Object> = Box::new(Sphere::new(center, 0.2, mat));
Some(tmp)
} else {
None
}
})
})
.flat_map(|a| a)
.collect::<Vec<Box<Object>>>();
list.push(Box::new(Sphere::new(
Vec3::new(0.0, -1000.0, 0.0),
1000.0,
Box::new(Lambertian::new(Arc::new(CheckerTexture::new(
Arc::new(ConstantTexture::new(Vec3::new(0.2,0.3,0.1))),
Arc::new(ConstantTexture::new(Vec3::new(0.9,0.9,0.9)))
)))),
)));
list.push(Box::new(Sphere::new(
Vec3::new(0.0, 1.0, 0.0),
1.0,
Box::new(Dielectric::new(1.5)),
)));
list.push(Box::new(Sphere::new(
Vec3::new(-4.0, 1.0, 0.0),
1.0,
Box::new(Lambertian::new(Arc::new(ConstantTexture::new(Vec3::new(0.4, 0.2, 0.1))))),
)));
list.push(Box::new(Sphere::new(
Vec3::new(4.0, 1.0, 0.0),
1.0,
Box::new(Metal::new(Vec3::new(0.7, 0.6, 0.5), 0.0)),
)));
let from = Vec3::new(13.0, 2.0, 3.0);
//let from = Vec3::new(0.0,0.0,0.0);
let to = Vec3::new(0.0, 2.0, 0.0);
let dist_to_focus = (to - from).len();
//let dist_to_focus = 10.0;
let aperature = 0.01;
(World::new(list, t0, t1), Camera::new(
from,
to,
Vec3::new(0.0, 1.0, 0.0),
20.0,
aspect,
aperature,
dist_to_focus,
))
}
pub fn two_perlin_spheres(t0:f32, t1:f32, aspect:f32) -> (World, Camera) {
let pertext = Arc::new(NoiseTexture::new(10.0));
let from = Vec3::new(13.0, 2.0, 3.0);
let to = Vec3::from(0.0);
let focus = 10.0;
let aperature = 0.0;
(World::new(vec![
Box::new(Sphere::new(
Vec3::new(0.0, -1000.0, 0.0), 1000.0,
Box::new(Lambertian::new(pertext.clone()))
)),
Box::new(Sphere::new(
Vec3::new(0.0, 2.0, 0.0), 2.0,
Box::new(Lambertian::new(Arc::new(ImageTexture::new("earthmap.jpg"))))
))
], t0, t1), Camera::new(from, to, Vec3::new(0.0, 1.0, 0.0), 20.0, aspect, aperature, focus))
}
}