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
384
385
386
387
388
389
390
391
392
393
394
use {
    super::{Camera, Category},
    crate::math::{vec3_is_finite, Cone, CoordF, Mat4, Sphere, Vec2, Vec3},
    std::ops::Range,
};

// TODO: Play around with this and see if depth should be RangeInclusive instead?
/// A standard perspective camera.
#[derive(Clone)]
pub struct Perspective {
    aspect_ratio: f32,
    depth: Range<f32>,
    eye: Vec3,
    fov: f32, // Stored as radians, measures field of view of the Y axis from our z reference vector (so half the field of view)
    fov_tan: f32,
    proj: Mat4,
    sphere_factor: Vec2,
    target: Vec3,
    up: Vec3,
    view: Mat4,
    view_inv: Mat4,
    x: Vec3, // Reference vector aka "local x" or "right"
    y: Vec3, // Reference vector aka "local y" or "up"
    z: Vec3, // Reference vector aka "local z" - this points straight through our "lens"
}

impl Perspective {
    /// Creates a new perspective camera using +y as the default "up" vector.
    ///
    /// # Arguments
    ///
    /// * `eye` - Position this camera is pointing from.
    /// * `target` - Position this camera is pointing towards.
    /// * `depth` - Range of distance this camera can see.
    /// * `fov` - Full field of view on the X axis, in degrees.
    /// * `aspect_ratio` - Width of the view of this camera divided by height.
    pub fn new(eye: Vec3, target: Vec3, depth: Range<f32>, fov: f32, aspect_ratio: f32) -> Self {
        let mut res = Self {
            aspect_ratio,
            depth,
            eye,
            fov: Default::default(),
            fov_tan: Default::default(),
            proj: Default::default(),
            sphere_factor: Default::default(),
            target,
            up: -Vec3::unit_y(),
            view: Default::default(),
            view_inv: Default::default(),
            x: Default::default(),
            y: Default::default(),
            z: Default::default(),
        };
        res.set_fov(fov);
        res.update_view();
        res
    }

    /// Creates a new perspective camera using +y as the default "up" vector.
    ///
    /// # Arguments
    ///
    /// * `eye` - Position this camera is pointing from.
    /// * `target` - Position this camera is pointing towards.
    /// * `depth` - Range of distance this camera can see.
    /// * `fov` - Full field of view on the X axis, in degrees.
    /// * `shape` - Defines the aspect ratio of the view of this camera.
    pub fn new_view<S: Into<CoordF>>(
        eye: Vec3,
        target: Vec3,
        depth: Range<f32>,
        fov: f32,
        shape: S,
    ) -> Self {
        let shape = shape.into();

        assert!(shape.is_finite());
        assert!(shape.x > 0.0);
        assert!(shape.y > 0.0);

        Self::new(eye, target, depth, fov, shape.x / shape.y)
    }

    /// Returns the width of the view of this camera compared to the height.
    pub fn aspect_ratio(&self) -> f32 {
        self.aspect_ratio
    }

    /// Returns the axial category and sign of a non-overlapping point, or `None`. Tests in
    /// Z-Y-X order.
    fn classify_point(&self, p: Vec3) -> Option<Category> {
        let dir = p - self.eye;

        // The point must be between our near and far planes, which are parallel
        let mut len = dir.dot(self.z);
        if len < self.depth.start {
            return Some(Category::Z(false));
        } else if len > self.depth.end {
            return Some(Category::Z(true));
        }

        len *= self.fov_tan;

        // Compare point to the top and bottom planes, which are not parallel
        let axis = dir.dot(self.y);
        if axis < -len {
            return Some(Category::Y(false));
        } else if axis > len {
            return Some(Category::Y(true));
        }

        len *= self.aspect_ratio;

        // Compare point to the left and right planes, which are not parallel
        let axis = dir.dot(self.x);
        if axis < -len {
            return Some(Category::X(false));
        } else if axis > len {
            return Some(Category::X(true));
        }

        None
    }

    /// Returns the axial category and sign of a non-overlapping sphere, or `None`. Tests in
    /// Z-Y-X order.
    fn classify_sphere(&self, s: Sphere) -> Option<Category> {
        // Note: This implementation is based on the 'radar' approach detailed here:
        // http://www.lighthouse3d.com/tutorials/view-frustum-culling/
        let dir = s.center() - self.eye;

        // The sphere must be between our near and far planes, which are parallel
        let mut len = dir.dot(self.z);
        if len < self.depth.start - s.radius() {
            return Some(Category::Z(false));
        } else if len > self.depth.end + s.radius() {
            return Some(Category::Z(true));
        }

        len *= self.fov_tan;

        // Compare sphere to the top and bottom planes, which are not parallel
        let axis = dir.dot(self.y);
        let radius = self.sphere_factor.y * s.radius();
        if axis < -len - radius {
            return Some(Category::Y(false));
        } else if axis > len + radius {
            return Some(Category::Y(true));
        }

        len *= self.aspect_ratio;

        // Compare sphere to the left and right planes, which are not parallel
        let axis = dir.dot(self.x);
        let radius = self.sphere_factor.x * s.radius();
        if axis < -len - radius {
            return Some(Category::X(false));
        } else if axis > len + radius {
            return Some(Category::X(true));
        }

        // Not overlapping
        None
    }

    /// Returns the position this camera is pointing from.
    pub fn eye(&self) -> Vec3 {
        self.eye
    }

    /// Returns the maximum distance this camera can see.
    pub const fn far(&self) -> f32 {
        self.depth.end
    }

    /// Returns the full field of view of the X axis, in degrees.
    pub fn fov(&self) -> f32 {
        self.fov.to_degrees() * 2.0 * self.aspect_ratio
    }

    /// Returns the minimum distance this camera can see.
    pub const fn near(&self) -> f32 {
        self.depth.start
    }

    /// Returns the position this camera is pointing towards.
    pub fn target(&self) -> Vec3 {
        self.target
    }

    /// Returns the orientation of the view of this camera, which is +y by default.
    pub fn up(&self) -> Vec3 {
        self.up
    }

    /// Modifies the shape of this camera.
    ///
    /// # Arguments
    ///
    /// * `val` - Width of the output of this camera divided by height.
    pub fn set_aspect_ratio(&mut self, val: f32) {
        assert!(val.is_finite());
        assert!(val > 0.0);

        self.aspect_ratio = val;
        self.update_proj();
    }

    /// Modifies the near and far planes of this camera, which defines the distance this camera can see.
    pub fn set_depth(&mut self, val: Range<f32>) {
        self.depth = val;
        self.update_proj();
    }

    /// Modifies the position which this camera is pointing from.
    pub fn set_eye(&mut self, val: Vec3) {
        self.eye = val;
        self.update_view();
    }

    /// Modifies the field of view of this camera.
    pub fn set_fov(&mut self, val: f32) {
        assert!(val.is_finite());
        assert!(val > 0.0);
        assert!(val < 180.0);

        self.fov = val.to_radians() * 0.5 / self.aspect_ratio;
        self.update_proj();
    }

    /// Modifies the position which this camera is pointing towards.
    pub fn set_target(&mut self, val: Vec3) {
        self.target = val;
        self.update_view();
    }

    /// Modifies the orientation of the view of this camera.
    pub fn set_up(&mut self, val: Vec3) {
        self.up = val;
        self.update_view();
    }

    /// Modifies the shape and field of view of this camera.
    ///
    /// # Arguments
    ///
    /// * `shape` - Defines the aspect ratio of the view of this camera.
    /// * `fov` - Full field of view on the X axis, in degrees.
    pub fn set_view<S: Into<CoordF>>(&mut self, shape: S, fov: f32) {
        let shape = shape.into();

        assert!(shape.is_finite());
        assert!(shape.x > 0.0);
        assert!(shape.y > 0.0);
        assert!(fov.is_finite());
        assert!(fov > 0.0);
        assert!(fov < 180.0);

        self.aspect_ratio = shape.x / shape.y;
        self.fov = fov.to_radians() * 0.5 / self.aspect_ratio;
        self.update_proj();
    }

    fn update_proj(&mut self) {
        assert!(self.aspect_ratio.is_finite());
        assert!(self.aspect_ratio > 0.0);
        assert!(self.depth.end.is_finite());
        assert!(self.depth.start.is_finite());
        assert!(self.depth.start > 0.0);
        assert!(self.fov.is_finite());
        assert!(self.depth.start < self.depth.end);

        // Update the projection matrix
        self.proj = Mat4::perspective_lh(
            self.aspect_ratio,
            self.fov * 2.0,
            self.depth.start,
            self.depth.end,
        );

        // Update values we use for frustum-sphere intersection checks
        self.fov_tan = self.fov.tan();
        self.sphere_factor.x = 1.0 / (self.fov_tan * self.aspect_ratio).atan().cos();
        self.sphere_factor.y = 1.0 / self.fov.cos();
    }

    fn update_view(&mut self) {
        assert!(vec3_is_finite(self.eye));
        assert!((self.eye - self.target).length_squared() > 0.0);
        assert!(vec3_is_finite(self.target));
        assert!(vec3_is_finite(self.up));
        assert!(self.up.length_squared() > 0.0);

        // Update the view matrices
        self.view = Mat4::look_at_lh(self.eye, self.target, self.up);
        self.view_inv = self.view.inverse();

        // Update the local X/Y/Z axes aka the reference vectors
        self.z = (self.eye - self.target).normalize();
        self.x = self.up.cross(self.z).normalize();
        self.y = -self.z.cross(self.x);
    }
}

impl Camera for Perspective {
    fn depth(&self) -> &Range<f32> {
        &self.depth
    }

    fn eye(&self) -> Vec3 {
        self.eye
    }

    fn overlaps_cone(&self, c: Cone) -> bool {
        // Test the apex point, hoping to early-out if it overlaps
        let apex = self.classify_point(c.apex());
        if apex.is_none() {
            return true;
        }

        // Test a sphere around the base, hoping to early-out if it overlaps
        let base =
            self.classify_sphere(Sphere::new(c.apex() + c.normal() * c.height(), c.radius()));
        if apex.is_none() {
            return true;
        }

        let apex = apex.unwrap();
        let base = base.unwrap();

        // The cone *may* overlap, so the next step is to throw out the common case we are certain of:
        // - If `apex` and `base` are on the same side of the same axis then they cannot overlap.
        match apex {
            Category::X(apex) => {
                if let Category::X(base) = base {
                    return apex != base;
                }
            }
            Category::Y(apex) => {
                if let Category::Y(base) = base {
                    return apex != base;
                }
            }
            Category::Z(apex) => {
                if let Category::Z(base) = base {
                    return apex != base;
                }
            }
        }

        // TODO: Further refine by classifying yet-unknown X and Y cases - Will require re-doing the math for Z and Y

        // Not sure if they overlap yet, but we can switch to a loose-fitting sphere test which is good enough
        // TODO: Not correct, need better sphere!
        let s = Sphere::new(c.apex() + c.normal() * c.height(), c.radius());
        self.classify_sphere(s).is_none()
    }

    fn overlaps_point(&self, p: Vec3) -> bool {
        self.classify_point(p).is_none()
    }

    fn overlaps_sphere(&self, s: Sphere) -> bool {
        self.classify_sphere(s).is_none()
    }

    fn project_point(&self, p: Vec3) -> Vec3 {
        // TODO: These should be view projection transforms!
        self.proj.transform_point3(p)
    }

    fn projection(&self) -> Mat4 {
        self.proj
    }

    fn unproject_point(&self, p: Vec3) -> Vec3 {
        // TODO: These should be view projection transforms!
        self.proj.inverse().transform_point3(p) // TODO: Oh no no no
    }

    fn view(&self) -> Mat4 {
        self.view
    }

    fn view_inv(&self) -> Mat4 {
        self.view_inv
    }
}

impl Default for Perspective {
    fn default() -> Self {
        Self::new(Vec3::zero(), Vec3::unit_z(), 0.0..1.0, 45.0, 1.0)
    }
}