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
pub(crate) use self::proj::{IntoProjection, Projection};

use {
    crate::{
        layout::Plain,
        shader,
        transform::{IntoQuat, Quat},
    },
    std::cell::Cell,
    wgpu::{BindGroup, BindGroupLayout, Buffer, Device, Queue},
};

mod proj {
    use super::{Orthographic, Perspective};

    #[derive(Clone, Copy)]
    pub enum Projection {
        Perspective(Perspective),
        Orthographic(Orthographic),
    }

    pub trait IntoProjection {
        fn into_projection(self) -> Projection;
    }
}

pub(crate) struct Camera {
    view: View<Projection>,
    size: Cell<Option<(u32, u32)>>,
    buffer: Buffer,
    bind_group: BindGroup,
}

impl Camera {
    pub fn new(device: &Device, layout: &BindGroupLayout) -> Self {
        use wgpu::{
            util::{BufferInitDescriptor, DeviceExt},
            *,
        };

        const BINDING: u32 = {
            assert!(shader::TEXTURED_CAMERA_BINDING == shader::COLOR_CAMERA_BINDING);
            shader::TEXTURED_CAMERA_BINDING
        };

        let uniform = CameraUniform {
            view_proj: IDENTITY,
        };

        let buffer = device.create_buffer_init(&BufferInitDescriptor {
            label: Some("camera buffer"),
            contents: uniform.as_bytes(),
            usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
        });

        let bind_group = device.create_bind_group(&BindGroupDescriptor {
            layout,
            entries: &[BindGroupEntry {
                binding: BINDING,
                resource: buffer.as_entire_binding(),
            }],
            label: Some("camera bind group"),
        });

        Self {
            view: View::<Orthographic>::default().into_projection_view(),
            size: Cell::new(None),
            buffer,
            bind_group,
        }
    }

    pub fn set_view(&mut self, view: View<Projection>) {
        self.view = view;
        self.size.set(None);
    }

    pub fn resize(&self, size @ (width, height): (u32, u32), queue: &Queue) {
        if self
            .size
            .get()
            .map(|(w, h)| width == w && height == h)
            .unwrap_or_default()
        {
            return;
        }

        self.size.set(Some(size));
        let uniform = CameraUniform {
            view_proj: self.view.build_mat((width as f32, height as f32)),
        };
        queue.write_buffer(&self.buffer, 0, uniform.as_bytes());
    }

    pub fn bind_group(&self) -> &BindGroup {
        &self.bind_group
    }
}

/// The camera view.
#[derive(Clone, Copy)]
pub struct View<P = Perspective> {
    /// Eye 3d point.
    pub eye: [f32; 3],

    /// Look at 3d point.
    pub look: [f32; 3],

    /// Camera projection.
    /// Can be a [`Perspective`] or [`Orthographic`].
    pub proj: P,
}

impl<P> View<P> {
    pub fn into_projection_view(self) -> View<Projection>
    where
        P: IntoProjection,
    {
        View {
            eye: self.eye,
            look: self.look,
            proj: self.proj.into_projection(),
        }
    }

    pub fn rotation_quat(&self) -> Quat {
        let [xe, ye, ze] = self.eye;
        let [xl, yl, zl] = self.look;
        let [sx, sy, sz] = normalize([xe - xl, ye - yl, ze - zl]);

        let pitch = sy.asin();
        let angle = sx.atan2(sz);
        let (pitch_sin, pitch_cos) = (pitch * 0.5).sin_cos();
        let (angle_sin, angle_cos) = (-angle * 0.5).sin_cos();

        Quat([
            pitch_sin * angle_cos,
            pitch_cos * angle_sin,
            pitch_sin * angle_sin,
            pitch_cos * angle_cos,
        ])
    }
}

impl View<Projection> {
    fn build_mat(&self, (width, height): (f32, f32)) -> [[f32; 4]; 4] {
        let proj = match self.proj {
            Projection::Perspective(Perspective { fovy, znear, zfar }) => {
                let (sin_fov, cos_fov) = (0.5 * fovy).sin_cos();
                let h = cos_fov / sin_fov;
                let w = (h * height) / width;
                let r = zfar / (znear - zfar);

                [
                    [w, 0., 0., 0.],
                    [0., h, 0., 0.],
                    [0., 0., r, -1.],
                    [0., 0., r * znear, 0.],
                ]
            }
            Projection::Orthographic(Orthographic {
                width_factor,
                height_factor,
                near,
                far,
            }) => {
                let factor_width = 1. / (width * width_factor);
                let factor_height = 1. / (height * height_factor);
                let r = 1. / (near - far);

                [
                    [factor_width + factor_width, 0., 0., 0.],
                    [0., factor_height + factor_height, 0., 0.],
                    [0., 0., r, 0.],
                    [factor_width, factor_height, r * near, 1.],
                ]
            }
        };

        let view = {
            let [xe, ye, ze] = self.eye;
            let [xl, yl, zl] = self.look;
            let [xf, yf, zf] = normalize([xe - xl, ye - yl, ze - zl]);
            let [xr, yr, zr] = normalize(cross([0., 1., 0.], [xf, yf, zf]));
            let [xu, yu, zu] = cross([xf, yf, zf], [xr, yr, zr]);

            let tx = xr * xe + yr * ye + zr * ze;
            let ty = xu * xe + yu * ye + zu * ze;
            let tz = xf * xe + yf * ye + zf * ze;

            [
                [xr, xu, xf, 0.],
                [yr, yu, yf, 0.],
                [zr, zu, zf, 0.],
                [-tx, -ty, -tz, 1.],
            ]
        };

        mul_mat4(proj, view)
    }
}

impl<P> Default for View<P>
where
    P: Default,
{
    fn default() -> Self {
        Self {
            eye: [0., 0., 1.],
            look: [0.; 3],
            proj: P::default(),
        }
    }
}

impl<P> IntoQuat for View<P> {
    fn into_quat(self) -> Quat {
        self.rotation_quat()
    }
}

/// Perspective projection.
#[derive(Clone, Copy)]
pub struct Perspective {
    pub fovy: f32,
    pub znear: f32,
    pub zfar: f32,
}

impl Default for Perspective {
    fn default() -> Self {
        Self {
            fovy: 1.6,
            znear: 0.1,
            zfar: 100.,
        }
    }
}

impl IntoProjection for Perspective {
    fn into_projection(self) -> Projection {
        Projection::Perspective(self)
    }
}

/// Orthographic projection.
#[derive(Clone, Copy)]
pub struct Orthographic {
    pub width_factor: f32,
    pub height_factor: f32,
    pub near: f32,
    pub far: f32,
}

impl Default for Orthographic {
    fn default() -> Self {
        Self {
            width_factor: 1.,
            height_factor: 1.,
            near: -100.,
            far: 100.,
        }
    }
}

impl IntoProjection for Orthographic {
    fn into_projection(self) -> Projection {
        Projection::Orthographic(self)
    }
}

#[repr(C)]
#[derive(Copy, Clone)]
pub(crate) struct CameraUniform {
    view_proj: [[f32; 4]; 4],
}

unsafe impl Plain for CameraUniform {}

const IDENTITY: [[f32; 4]; 4] = [
    [1., 0., 0., 0.],
    [0., 1., 0., 0.],
    [0., 0., 1., 0.],
    [0., 0., 0., 1.],
];

fn normalize([x, y, z]: [f32; 3]) -> [f32; 3] {
    let len = (x * x + y * y + z * z).sqrt();
    if len == 0. {
        [0., 1., 0.]
    } else {
        [x / len, y / len, z / len]
    }
}

fn cross([xa, ya, za]: [f32; 3], [xb, yb, zb]: [f32; 3]) -> [f32; 3] {
    [ya * zb - yb * za, za * xb - zb * xa, xa * yb - xb * ya]
}

fn mul_vector(mat: [[f32; 4]; 4], [x, y, z, w]: [f32; 4]) -> [f32; 4] {
    let [[xa, ya, za, wa], [xb, yb, zb, wb], [xc, yc, zc, wc], [xd, yd, zd, wd]] = mat;

    [
        x * xa + y * xb + z * xc + w * xd,
        x * ya + y * yb + z * yc + w * yd,
        x * za + y * zb + z * zc + w * zd,
        x * wa + y * wb + z * wc + w * wd,
    ]
}

fn mul_mat4(a: [[f32; 4]; 4], b: [[f32; 4]; 4]) -> [[f32; 4]; 4] {
    b.map(|v| mul_vector(a, v))
}