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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use crate::*;
impl Ray {
/// Returns the origin of the ray
#[inline(always)]
pub const fn origin(&self) -> Point3 { self.origin }
/// Returns the (normalized) direction of the ray
#[inline(always)]
pub const fn direction(&self) -> Vector3 { self.direction }
}
impl Camera {
/// Returns the position of camera,
/// the forth column of the camera matrix.
///
/// # Examples
/// ```
/// use truck_platform::*;
/// use truck_base::cgmath64::*;
/// let mut camera = Camera::default();
/// camera.matrix = Matrix4::from_translation(Vector3::new(1.0, 2.0, 3.0));
/// assert_eq!(camera.position(), Point3::new(1.0, 2.0, 3.0));
/// ```
#[inline(always)]
pub fn position(&self) -> Point3 { Point3::from_vec(self.matrix[3].truncate()) }
/// Returns the eye direction of camera.
/// the inverse of the z-axis of the camera matrix.
///
/// # Examples
/// ```
/// use std::f64::consts::PI;
/// use truck_platform::*;
/// use truck_base::{cgmath64::*, tolerance::Tolerance};
/// let mut camera = Camera::default();
/// camera.matrix = Matrix4::from_axis_angle(
/// Vector3::new(1.0, 1.0, 1.0).normalize(),
/// Rad(2.0 * PI / 3.0),
/// );
/// assert!(camera.eye_direction().near(&-Vector3::unit_x()));
/// ```
#[inline(always)]
pub fn eye_direction(&self) -> Vector3 { -self.matrix[2].truncate() }
/// Returns the direction of the head vector, the y-axis of the camera matrix.
/// # Examples
/// ```
/// use std::f64::consts::PI;
/// use truck_platform::*;
/// use truck_base::{cgmath64::*, tolerance::Tolerance};
/// let mut camera = Camera::default();
/// camera.matrix = Matrix4::from_axis_angle(
/// Vector3::new(1.0, 1.0, 1.0).normalize(),
/// Rad(2.0 * PI / 3.0),
/// );
/// assert!(camera.head_direction().near(&Vector3::unit_z()));
/// ```
#[inline(always)]
pub fn head_direction(&self) -> Vector3 { self.matrix[1].truncate() }
/// Returns the projection type of the camera.
/// # Examples
/// ```
/// use truck_platform::*;
/// // the projection type of the default camera is perspective.
/// assert_eq!(Camera::default().projection_type(), ProjectionType::Perspective);
/// ```
#[inline(always)]
pub const fn projection_type(&self) -> ProjectionType { self.projection_type }
/// Creates a perspective camera.
/// # Arguments
/// * `matrix`: camera matrix
/// * `field_of_view`: FOV, based on the vertical direction of the screen.
/// * `near_clip`: distance to the nearest face of the view volume
/// * `far_clip`: distance to the farthest face of the view volume
/// # Examples
/// ```
/// use std::f64::consts::PI;
/// use truck_base::{cgmath64::*, tolerance::Tolerance};
/// use truck_platform::*;
/// let matrix = Matrix4::look_at_rh(
/// Point3::new(1.0, 1.0, 1.0),
/// Point3::origin(),
/// Vector3::new(0.0, 1.0, 0.0),
/// );
/// let camera = Camera::perspective_camera(
/// // depends on the difference of the style with cgmath,
/// // the matrix must be inverted
/// matrix.invert().unwrap(),
/// Rad(PI / 4.0),
/// 0.1,
/// 1.0,
/// );
/// assert!(camera.eye_direction().near(&-Vector3::new(1.0, 1.0, 1.0).normalize()));
/// assert_eq!(camera.projection_type(), ProjectionType::Perspective);
/// ```
#[inline(always)]
pub fn perspective_camera<R: Into<Rad<f64>>>(
matrix: Matrix4,
field_of_view: R,
near_clip: f64,
far_clip: f64,
) -> Camera {
let projection = perspective(field_of_view.into(), 1.0, near_clip, far_clip);
Camera {
matrix,
projection,
projection_type: ProjectionType::Perspective,
}
}
/// Creates a parallel camera.
/// # Arguments
/// * `matrix`: camera matrix
/// * `screen_size`: screen size, based on the vertical direction of the screen.`
/// * `near_clip`: distance to the nearest face of the view volume
/// * `far_clip`: distance to the farthest face of the view volume
/// # Examples
/// ```
/// use truck_base::{cgmath64::*, tolerance::Tolerance};
/// use truck_platform::*;
/// let matrix = Matrix4::look_at_rh(
/// Point3::new(1.0, 1.0, 1.0),
/// Point3::origin(),
/// Vector3::new(0.0, 1.0, 0.0),
/// );
/// let camera = Camera::parallel_camera(
/// // depends on the difference of the style with cgmath,
/// // the matrix must be inverted
/// matrix.invert().unwrap(),
/// 1.0,
/// 0.1,
/// 1.0,
/// );
/// assert!(camera.head_direction().near(&Vector3::new(-0.5, 1.0, -0.5).normalize()));
/// assert_eq!(camera.projection_type(), ProjectionType::Parallel);
/// ```
#[inline(always)]
pub fn parallel_camera(
matrix: Matrix4,
screen_size: f64,
near_clip: f64,
far_clip: f64,
) -> Camera {
let a = screen_size / 2.0;
let projection = Matrix4::new(
1.0 / a,
0.0,
0.0,
0.0,
0.0,
1.0 / a,
0.0,
0.0,
0.0,
0.0,
-1.0 / (far_clip - near_clip),
0.0,
0.0,
0.0,
-near_clip / (far_clip - near_clip),
1.0,
);
Camera {
matrix,
projection,
projection_type: ProjectionType::Parallel,
}
}
/// Returns the projection matrix into the normalized view volume.
/// # Arguments
/// `as_rat`: the aspect ratio, x-resolution / y-resulution.
/// # Examples
/// ```
/// // perspective camera
/// use std::f64::consts::PI;
/// use truck_base::{assert_near, cgmath64::*, tolerance::*};
/// use truck_platform::*;
///
/// let fov = PI / 4.0;
/// let as_rat = 1.2;
/// let matrix = Matrix4::look_at_rh(
/// Point3::new(1.0, 1.0, 1.0),
/// Point3::origin(),
/// Vector3::new(0.0, 1.0, 0.0),
/// );
/// let camera = Camera::perspective_camera(
/// matrix.invert().unwrap(),
/// Rad(fov),
/// 0.1,
/// 10.0,
/// );
///
/// // calculation by the ray-tracing
/// let pt = Point3::new(-1.5, -1.4, -2.5);
/// let vec = pt - camera.position();
/// let far = 1.0 / (fov / 2.0).tan();
/// let dir = camera.eye_direction();
/// let y_axis = camera.head_direction();
/// let x_axis = dir.cross(y_axis);
/// let proj_length = dir.dot(vec);
/// let h = (vec - proj_length * dir) * far / proj_length;
/// let u = h.dot(x_axis) / as_rat;
/// let v = h.dot(y_axis);
///
/// // check the answer
/// let uv = camera.projection(as_rat).transform_point(pt);
/// assert_near!(u, uv[0]);
/// assert_near!(v, uv[1]);
/// ```
/// ```
/// // parallel camera
/// use truck_base::{assert_near, cgmath64::*, tolerance::*};
/// use truck_platform::*;
///
/// let size = 3.0;
/// let as_rat = 1.2;
/// let matrix = Matrix4::look_at_rh(
/// Point3::new(1.0, 1.0, 1.0),
/// Point3::origin(),
/// Vector3::new(0.0, 1.0, 0.0),
/// );
/// let camera = Camera::parallel_camera(
/// matrix.invert().unwrap(),
/// size,
/// 0.1,
/// 10.0,
/// );
///
/// // calculation by the ray-tracing
/// let pt = Point3::new(-1.5, -1.4, -2.5);
/// let vec = pt - camera.position();
/// let dir = camera.eye_direction();
/// let y_axis = camera.head_direction();
/// let x_axis = dir.cross(y_axis);
/// let h = vec - vec.dot(dir) * dir;
/// let u = h.dot(x_axis) / (size / 2.0) / as_rat;
/// let v = h.dot(y_axis) / (size / 2.0);
///
/// // check the answer
/// let uv = camera.projection(as_rat).transform_point(pt);
/// assert_near!(u, uv[0]);
/// assert_near!(v, uv[1]);
/// ```
#[inline(always)]
pub fn projection(&self, as_rat: f64) -> Matrix4 {
Matrix4::from_nonuniform_scale(1.0 / as_rat, 1.0, 1.0)
* self.projection
* self.matrix.invert().unwrap()
}
fn camera_info(&self, as_rat: f64) -> CameraInfo {
CameraInfo {
camera_matrix: self.matrix.cast().unwrap().into(),
camera_projection: self.projection(as_rat).cast().unwrap().into(),
}
}
/// Creates a `UNIFORM` buffer of camera.
///
/// The bind group provides [`Scene`] holds this uniform buffer.
///
/// # Shader Example
/// ```glsl
/// layout(set = 0, binding = 0) uniform Camera {
/// mat4 camera_matrix; // the camera matrix
/// mat4 camera_projection; // the projection into the normalized view volume
/// };
/// ```
pub fn buffer(&self, as_rat: f64, device: &Device) -> BufferHandler {
BufferHandler::from_slice(&[self.camera_info(as_rat)], device, BufferUsages::UNIFORM)
}
/// Returns the ray from camera with aspect-ratio = 1.0.
///
/// # Examples
/// ```
/// // Perspective case
/// use std::f64::consts::PI;
/// use truck_base::{assert_near, cgmath64::*, tolerance::Tolerance};
/// use truck_platform::*;
///
///
/// let matrix = Matrix4::look_at_rh(
/// Point3::new(1.0, 1.0, 1.0),
/// Point3::origin(),
/// Vector3::new(0.0, 1.0, 0.0),
/// );
/// let camera = Camera::perspective_camera(
/// // depends on the difference of the style with cgmath,
/// // the matrix must be inverted
/// matrix.invert().unwrap(),
/// Rad(PI / 4.0),
/// 0.1,
/// 1.0,
/// );
///
/// // take a point in the 3D space
/// let point = Point3::new(0.1, 0.15, 0.0);
/// // project to the normalized view volume
/// let uvz = camera.projection(1.0).transform_point(point);
/// // coordinate on the screen
/// let uv = Point2::new(uvz.x, uvz.y);
///
/// let ray = camera.ray(uv);
/// // the origin of the ray is camera position
/// assert_near!(ray.origin(), camera.position());
/// // the direction of the ray is the normalized vector of point - camera.position().
/// assert_near!(ray.direction(), (point - camera.position()).normalize());
/// ```
/// ```
/// // Parallel case
/// use truck_base::{assert_near, cgmath64::*, tolerance::*};
/// use truck_platform::*;
///
/// let matrix = Matrix4::look_at_rh(
/// Point3::new(1.0, 1.0, 1.0),
/// Point3::origin(),
/// Vector3::new(0.0, 1.0, 0.0),
/// );
/// let camera = Camera::parallel_camera(
/// matrix.invert().unwrap(),
/// 3.0,
/// 0.1,
/// 10.0,
/// );
///
/// // take a point in the 3D space
/// let point = Point3::new(0.1, 0.15, 0.0);
/// // the projection of the point to the screen
/// let projed = point
/// - camera.eye_direction() * camera.eye_direction().dot(point - camera.position());
/// // project to the normalized view volume
/// let uvz = camera.projection(1.0).transform_point(point);
/// // coordinate on the screen
/// let uv = Point2::new(uvz.x, uvz.y);
///
/// let ray = camera.ray(uv);
/// // the origin of the ray is the projection of the point.
/// assert_near!(ray.origin(), projed);
/// // the direction of the ray is eye direction.
/// assert_near!(ray.direction(), camera.eye_direction());
/// ```
pub fn ray(&self, coord: Point2) -> Ray {
match self.projection_type {
ProjectionType::Perspective => {
let mat = self
.projection(1.0)
.invert()
.expect("non-invertible projection");
let x = mat.transform_point(Point3::new(coord.x, coord.y, 0.5));
let y = mat.transform_point(Point3::new(coord.x, coord.y, 1.0));
Ray {
origin: self.position(),
direction: (y - x).normalize(),
}
}
ProjectionType::Parallel => {
let a = self.projection[0][0];
let axis_x = self.matrix[0].truncate() / a;
let axis_y = self.matrix[1].truncate() / a;
Ray {
origin: self.position() + coord.x * axis_x + coord.y * axis_y,
direction: self.eye_direction(),
}
}
}
}
}
impl Default for Camera {
#[inline(always)]
fn default() -> Camera {
Camera::perspective_camera(
Matrix4::identity(),
Rad(std::f64::consts::PI / 4.0),
0.1,
10.0,
)
}
}