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
use rfw_backend::{CameraView2D, CameraView3D};
use rfw_math::*;

pub mod frustrum;

pub use frustrum::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::utils::{HasMatrix, HasRotation, HasTranslation, Transform};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Camera3D {
    pub pos: [f32; 3],
    up: [f32; 3],
    pub direction: [f32; 3],
    fov: f32,
    pub aspect_ratio: f32,
    pub aperture: f32,
    pub focal_distance: f32,
    pub near_plane: f32,
    pub far_plane: f32,
    pub speed: f32,
}

impl Default for Camera3D {
    fn default() -> Self {
        Self {
            pos: [0.0; 3],
            up: [0.0, 1.0, 0.0],
            direction: [0.0, 0.0, 1.0],
            fov: 60.0,
            aspect_ratio: 1024_f32 / 768_f32,
            aperture: 0.0001,
            focal_distance: 1.0,
            near_plane: 1e-2,
            far_plane: 1e5,
            speed: 1.0,
        }
    }
}

#[allow(dead_code)]
impl Camera3D {
    pub fn zero() -> Camera3D {
        Camera3D {
            pos: [0.0; 3],
            up: [0.0; 3],
            direction: [0.0, 0.0, 1.0],
            fov: 90.0,
            aspect_ratio: 1.0,
            aperture: 0.0001,
            focal_distance: 1.0,
            near_plane: 1.0,
            far_plane: 1e5,
            speed: 1.0,
        }
    }

    pub fn new() -> Camera3D {
        Camera3D {
            pos: [0.0; 3],
            up: [0.0; 3],
            direction: [0.0, 0.0, 1.0],
            fov: 40.0,
            aspect_ratio: 1.0,
            aperture: 0.0001,
            focal_distance: 1.0,
            near_plane: 1e-2,
            far_plane: 1e5,
            speed: 1.0,
        }
    }

    pub fn get_view(&self, width: u32, height: u32) -> CameraView3D {
        let (right, up, forward) = self.calculate_matrix();
        let pos = Vec3::from(self.pos);
        let fov = self.fov;
        let spread_angle = (fov * std::f32::consts::PI / 180.0) * (1.0 / height as f32);
        let screen_size = (fov * 0.5 / (180.0 / std::f32::consts::PI)).tan();
        let center = pos + self.focal_distance * forward;

        let p1 = center - screen_size * right * self.focal_distance * self.aspect_ratio
            + screen_size * self.focal_distance * up;
        let p2 = center
            + screen_size * right * self.focal_distance * self.aspect_ratio
            + screen_size * self.focal_distance * up;
        let p3 = center
            - screen_size * right * self.focal_distance * self.aspect_ratio
            - screen_size * self.focal_distance * up;

        let aperture = self.aperture;
        let right = p2 - p1;
        let up = p3 - p1;

        CameraView3D {
            pos: pos.into(),
            lens_size: aperture,
            right: right.into(),
            p1: p1.into(),
            direction: forward.into(),
            spread_angle,
            up: up.into(),
            epsilon: crate::constants::EPSILON,
            inv_width: 1.0 / width as f32,
            inv_height: 1.0 / height as f32,
            aspect_ratio: self.aspect_ratio,
            fov: self.fov.to_radians(),
            near_plane: self.near_plane,
            far_plane: self.far_plane,
            ..Default::default()
        }
    }

    pub fn set_fov(&mut self, fov: f32) {
        self.fov = fov.min(160.0).max(20.0);
    }

    pub fn with_fov(mut self, fov: f32) -> Self {
        self.set_fov(fov);
        self
    }

    pub fn get_fov(&self) -> f32 {
        self.fov
    }

    pub fn get_aspect_ratio(&self) -> f32 {
        self.aspect_ratio
    }

    pub fn set_aspect_ratio(&mut self, aspect_ratio: f32) {
        self.aspect_ratio = aspect_ratio;
    }

    pub fn with_aspect_ratio(mut self, aspect_ratio: f32) -> Self {
        self.aspect_ratio = aspect_ratio;
        self
    }

    pub fn get_transform(&mut self) -> Transform<Self> {
        Transform {
            translation: self.pos.into(),
            rotation: Quat::IDENTITY,
            scale: Vec3::default(),
            handle: self,
            changed: false,
        }
    }

    pub fn with_position<T: Into<[f32; 3]>>(mut self, position: T) -> Self {
        self.pos = position.into();
        self
    }

    pub fn with_direction<T: Into<[f32; 3]>>(mut self, direction: T) -> Self {
        let direction: Vec3 = Vec3::from(direction.into());
        self.direction = direction.normalize().into();
        self
    }

    pub fn translate_relative<T: Into<[f32; 3]>>(&mut self, delta: T) {
        let delta = Vec3::from(delta.into());
        let delta = delta * self.speed;
        let (right, up, forward) = self.calculate_matrix();
        self.pos =
            (Vec3::from(self.pos) + (delta.x * right + delta.y * up + delta.z * forward)).into();
    }

    pub fn translate_target<T: Into<[f32; 3]>>(&mut self, delta: T) {
        let (right, up, forward) = self.calculate_matrix();
        let delta: [f32; 3] = delta.into();
        self.direction =
            (Vec3::from(self.direction) + delta[0] * right + delta[1] * up + delta[2] * forward)
                .normalize()
                .into();
    }

    pub fn look_at<T: Into<[f32; 3]>>(&mut self, origin: T, target: T) {
        let origin: Vec3 = Vec3::from(origin.into());
        let target: Vec3 = Vec3::from(target.into());
        self.pos = origin.into();
        self.direction = (target - origin).normalize().into();
    }

    pub fn get_rh_matrix(&self) -> Mat4 {
        let up = Vec3::new(0.0, 1.0, 0.0);
        let fov = self.fov.to_radians();

        let projection =
            Mat4::perspective_rh_gl(fov, self.aspect_ratio, self.near_plane, self.far_plane);

        let pos = Vec3::from(self.pos);
        let dir = Vec3::from(self.direction);

        let view = Mat4::look_at_rh(pos, pos + dir, up);

        projection * view
    }

    pub fn get_lh_matrix(&self) -> Mat4 {
        let up = Vec3::new(0.0, 1.0, 0.0);
        let fov = self.fov.to_radians();

        let projection =
            Mat4::perspective_lh(fov, self.aspect_ratio, self.near_plane, self.far_plane);

        let pos = Vec3::from(self.pos);
        let dir = Vec3::from(self.direction);

        let view = Mat4::look_at_lh(pos, pos + dir, up);

        projection * view
    }

    pub fn get_rh_projection(&self) -> Mat4 {
        let fov = self.fov.to_radians();
        Mat4::perspective_rh_gl(fov, self.aspect_ratio, self.near_plane, self.far_plane)
    }

    pub fn get_lh_projection(&self) -> Mat4 {
        let fov = self.fov.to_radians();
        Mat4::perspective_lh(fov, self.aspect_ratio, self.near_plane, self.far_plane)
    }

    pub fn get_rh_view_matrix(&self) -> Mat4 {
        let up = Vec3::new(0.0, 1.0, 0.0);

        let pos = Vec3::from(self.pos);
        let dir = Vec3::from(self.direction);

        Mat4::look_at_rh(pos, pos + dir, up)
    }

    pub fn get_lh_view_matrix(&self) -> Mat4 {
        let up = Vec3::new(0.0, 1.0, 0.0);

        let pos = Vec3::from(self.pos);
        let dir = Vec3::from(self.direction);

        Mat4::look_at_lh(pos, pos + dir, up)
    }

    fn calculate_matrix(&self) -> (Vec3, Vec3, Vec3) {
        let y: Vec3 = Vec3::new(0.0, 1.0, 0.0);
        let z: Vec3 = Vec3::from(self.direction).normalize();
        let x: Vec3 = z.cross(y).normalize();
        let y: Vec3 = x.cross(z).normalize();
        (x, y, z)
    }

    pub fn calculate_frustrum(&self) -> frustrum::FrustrumG {
        frustrum::FrustrumG::from_matrix(self.get_rh_matrix())
    }

    #[cfg(feature = "serde")]
    pub fn serialize<S: AsRef<std::path::Path>>(
        &self,
        path: S,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let path = path.as_ref().with_extension("cam");
        use std::io::Write;
        let encoded: Vec<u8> = bincode::serialize(self)?;
        let mut file = std::fs::File::create(&path)?;
        file.write_all(encoded.as_ref())?;
        Ok(())
    }

    #[cfg(feature = "serde")]
    pub fn deserialize<S: AsRef<std::path::Path>>(
        path: S,
    ) -> Result<Camera, Box<dyn std::error::Error>> {
        let path = path.as_ref().with_extension("cam");
        let file = std::fs::File::open(path)?;
        let reader = std::io::BufReader::new(file);
        let object: Self = bincode::deserialize_from(reader)?;
        Ok(object)
    }
}

impl HasMatrix for Camera3D {
    fn update(&mut self, t: Vec3, r: Quat, _s: Vec3) {
        self.pos = t.into();
        self.direction = r.mul_vec3(self.direction.into()).into();
    }
}

impl HasTranslation for Camera3D {}
impl HasRotation for Camera3D {}

#[derive(Debug, Clone, Copy)]
pub struct Fov(f32);

#[derive(Debug, Clone, Copy)]
pub struct Dimensions {
    pub left: f32,
    pub right: f32,
    pub bottom: f32,
    pub top: f32,
    pub near: f32,
    pub far: f32,
}

#[derive(Debug, Clone, Copy)]
pub struct Camera2D {
    pub dimensions: Dimensions,
}

impl Camera2D {
    pub fn get_view(&self) -> CameraView2D {
        self.into()
    }

    pub fn from_width_height(width: u32, height: u32, scale_factor: Option<f64>) -> Self {
        let scale_fcator = scale_factor.unwrap_or(1.0) as f32;
        let w = width as f32 * scale_fcator / 2.0;
        let h = height as f32 * scale_fcator / 2.0;
        Self {
            dimensions: Dimensions {
                left: -w,
                right: w,
                bottom: -h,
                top: h,
                near: 10.0,
                far: -10.0,
            },
        }
    }

    pub fn width(&self) -> f32 {
        (self.dimensions.right - self.dimensions.left).abs()
    }

    pub fn height(&self) -> f32 {
        (self.dimensions.top - self.dimensions.bottom).abs()
    }
}

impl From<Camera2D> for CameraView2D {
    fn from(c: Camera2D) -> Self {
        CameraView2D {
            matrix: Mat4::orthographic_rh(
                c.dimensions.left,
                c.dimensions.right,
                c.dimensions.bottom,
                c.dimensions.top,
                c.dimensions.near,
                c.dimensions.far,
            ),
        }
    }
}

impl From<&Camera2D> for CameraView2D {
    fn from(c: &Camera2D) -> Self {
        CameraView2D {
            matrix: Mat4::orthographic_rh(
                c.dimensions.left,
                c.dimensions.right,
                c.dimensions.bottom,
                c.dimensions.top,
                c.dimensions.near,
                c.dimensions.far,
            ),
        }
    }
}

impl From<&mut Camera2D> for CameraView2D {
    fn from(c: &mut Camera2D) -> Self {
        CameraView2D {
            matrix: Mat4::orthographic_rh(
                c.dimensions.left,
                c.dimensions.right,
                c.dimensions.bottom,
                c.dimensions.top,
                c.dimensions.near,
                c.dimensions.far,
            ),
        }
    }
}