-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.rs
More file actions
271 lines (214 loc) · 6.96 KB
/
camera.rs
File metadata and controls
271 lines (214 loc) · 6.96 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use crate::na::{Vector3, Perspective3, Isometry3, Rotation, Rotation3};
use crate::ray::Ray;
use std::f64;
use crate::geometry::random_point_on_disc;
pub trait Camera {
// Given a point (x=0-1, y=0-1) as a proportion of the way into the previously sized image
// and (sx=0-1, sy=0-1), subsamples within that pixel, generate a ray for that pixel
fn get_ray(&self, x: f64, y: f64, sx: f64, sy: f64) -> Ray;
}
pub struct SimpleCamera {
location: Vector3<f64>,
camx: Vector3<f64>,
camy: Vector3<f64>,
camz: Vector3<f64>,
tax: f64,
tay: f64
}
impl SimpleCamera {
pub fn new(lookat: Vector3<f64>, location:Vector3<f64>, up:Vector3<f64>, angle: f64, height: u32, width: u32) -> SimpleCamera {
let camz = (lookat - location).normalize();
let camx = up.cross(&camz).normalize();
let camy = camx.cross(
&(Vector3::new(0f64,0f64,0f64) - camz)
).normalize();
let aspect_ratio = (height as f64) / (width as f64);
//let viewPlaneHalfWidth= (fieldOfView / 2.).tan()
//let viewPlaneHalfHeight = aspectRatio*viewPlaneHalfWidth
SimpleCamera {
location,
camz,
camx: camx * aspect_ratio,
camy,
tax: angle.tan(),
tay: angle.tan()
}
}
}
impl Camera for SimpleCamera {
// x, y, supersamples
fn get_ray(&self, x: f64, y: f64, sx: f64, sy: f64) -> Ray {
let xdir = self.camx * (x + sx - 0.5) * self.tax;
let ydir = self.camy * (y + sy - 0.5) * self.tay;
let dest = self.camz + xdir + ydir;
Ray {
ro: self.location,
rd: dest
}
}
}
/*
pub struct PerspectiveCamera {
frustum: PerspectiveMatrix3<f64>,
// isometry: Isometry3<f64>,
cam_to_world: Rotation3<f64>,
// up: Vector3<f64>,
location: Vector3<f64>,
// lookat: Vector3<f64>,
// camx: Vector3<f64>,
// camy: Vector3<f64>,
// camz: Vector3<f64>,
//angle: f64,
// tax: f64,
// tay: f64
}
impl PerspectiveCamera {
pub fn new(lookat: Vector3<f64>, location:Vector3<f64>, up:Vector3<f64>, angle: f64, height: u32, width: u32) -> PerspectiveCamera {
/*
let camz = (lookat - location).normalize();
let camx = up.cross(&camz).normalize();
*/
let frustum = PerspectiveMatrix3::new(
width as f64 / height as f64, // aspect
angle,// fovy
-1., // znear
1.,// zfar
);
let isometry = Isometry3::new_observer_frame(&location.to_point(), &lookat.to_point(), &up);
PerspectiveCamera {
frustum: frustum,
// isometry: isometry,
cam_to_world: isometry.rotation,
// lookat: lookat,
location: location,
// up: up,
// angle: angle,
/*
camz: camz,
camx: camx,
camy: camx.cross(
&(Vector3::new(0f64,0f64,0f64) - camz)
).normalize(),
tax: angle.tan(), // recip of len
tay: ((height as f64 / width as f64) * angle).tan()
*/
}
}
pub fn get_ray(&self, x: f64, y: f64) -> Ray {
/*
let xdir = self.camx * (x - 0.5) * self.tax;
let ydir = self.camy * (y - 0.5) * self.tay;
let dest = self.camz + xdir + ydir;
*/
let cam_rd = Vector3::new(x - 0.5, y - 0.5, 1.).normalize();
//println!("cam_rd: {}", cam_rd);
//let dw = self.frustum.project_vector(&cam_rd);
//println!("dw: {}", dw);
let rd = self.cam_to_world.rotate(&cam_rd);
//println!("rd: {}", rd);
Ray {
ro: self.location,
rd: rd.normalize()
}
}
// Inverse of get ray
// BROKEN
pub fn get_coord_for_point(&self, point: Vector3<f64>) -> (f64,f64) {
// Generate ray
let vec = (point - self.location).normalize();
let vec_cam = self.cam_to_world.inverse_rotate(&vec);
return (vec_cam.x + 0.5, vec_cam.y + 0.5);
}
}
*/
pub struct FlatLensCamera {
location: Vector3<f64>,
camx: Vector3<f64>,
camy: Vector3<f64>,
camz: Vector3<f64>,
tax: f64,
tay: f64,
aperture: f64,
focus: f64,
}
impl FlatLensCamera {
pub fn new(
lookat: Vector3<f64>,
location:Vector3<f64>,
up:Vector3<f64>,
angle: f64,
height: u32,
width: u32,
aperture: f64
) -> FlatLensCamera {
let camz = (lookat - location).normalize();
let mut camx = up.cross(&camz).normalize();
if camx.y.is_nan() {
// Looking vertical. In this situation we arbitrarily pick an axis.
camx = Vector3::new(1., 0., 0.);
}
let camy = camx.cross(
&(Vector3::new(0f64,0f64,0f64) - camz)
).normalize();
let aspect_ratio = (height as f64) / (width as f64);
let focus = (lookat - location).norm();
//let viewPlaneHalfWidth= (fieldOfView / 2.).tan()
//let viewPlaneHalfHeight = aspectRatio*viewPlaneHalfWidth
FlatLensCamera {
location,
camz,
camx: camx * aspect_ratio,
camy,
tax: angle.tan(),
tay: angle.tan(),
aperture,
focus,
}
}
}
impl Camera for FlatLensCamera {
fn get_ray(&self, x: f64, y: f64, sx: f64, sy: f64) -> Ray {
let xdir = self.camx * (x + sx - 0.5) * self.tax;
let ydir = self.camy * (y + sy - 0.5) * self.tay;
let pinhole_dest = self.camz + xdir + ydir;
let focal_point = self.location + pinhole_dest * self.focus;
let point_lens = random_point_on_disc(self.aperture);
let ro = self.location + Vector3::new(point_lens[0], point_lens[1], 0.0);
Ray {
ro,
rd: (focal_point - ro).normalize()
}
}
}
#[cfg(test)]
macro_rules! assert_approx_eq(
($a:expr, $b:expr) => ({
let (a, b) = (&$a, &$b);
assert!((*a - *b).abs() < 1.0e-6, "{} is not approximately equal to {}", *a, *b);
}));
#[cfg(test)]
mod tests {
use super::*;
use crate::na::Vector3;
#[test]
fn get_ray() {
let _width = 200;
let _height = 100;
/*
let c = Camera::new(
>>>>>>> 91eac9d7d76ebfa2585d691dba97e70c3c74de25
Vector3::new(0f64,0f64,0f64),
Vector3::new(0f64, 1f64, -1f64),
Vector3::new(0f64,1f64,0f64),
35.0,
width, height
);
assert_approx_eq!(c.get_ray(0.1, 0.1, 0., 0.).ro.x, 0f64);
assert_approx_eq!(c.get_ray(0.1, 0.1, 0., 0.).ro.y, 1f64);
assert_approx_eq!(c.get_ray(0.1, 0.1, 0., 0.).ro.z, -1f64);
assert_approx_eq!(c.get_ray(0.05, 0.1, 0., 0.).rd.x, -0.21321662418650297);
assert_approx_eq!(c.get_ray(0.05, 0.1, 0., 0.).rd.y, -1.052729238967664);
assert_approx_eq!(c.get_ray(0.05, 0.1, 0., 0.).rd.z, 0.361484323405431);
*/
}
}