update helpers/location to 'frosted' ui
[carveJwlIkooP6JGAAIwe30JlM.git] / camera.h
1 #pragma once
2 #if 0
3
4 typedef struct camera camera;
5 struct camera{
6 /* Input */
7 v3f angles;
8 v3f pos;
9 float fov, nearz, farz;
10
11 /* Output */
12 m4x3f transform,
13 transform_inverse;
14
15 struct camera_mtx{
16 m4x4f p,
17 v,
18 pv;
19 }
20 mtx,
21 mtx_prev;
22 };
23
24 static void camera_lerp_angles( v3f a, v3f b, float t, v3f d ){
25 d[0] = vg_alerpf( a[0], b[0], t );
26 d[1] = vg_lerpf( a[1], b[1], t );
27 d[2] = vg_lerpf( a[2], b[2], t );
28 }
29
30 /* lerp position, fov, and angles */
31 static void camera_lerp( camera *a, camera *b, float t, camera *d ){
32 v3_lerp( a->pos, b->pos, t, d->pos );
33 camera_lerp_angles( a->angles, b->angles, t, d->angles );
34 d->fov = vg_lerpf( a->fov, b->fov, t );
35 }
36
37 static void camera_copy( camera *a, camera *d ){
38 v3_copy( a->pos, d->pos );
39 v3_copy( a->angles, d->angles );
40 d->fov = a->fov;
41 }
42
43 static void m4x3_transform_camera( m4x3f m, camera *cam ){
44 m4x3_mulv( m, cam->pos, cam->pos );
45
46 v3f v0;
47 v3_angles_vector( cam->angles, v0 );
48 m3x3_mulv( m, v0, v0 );
49 v3_normalize( v0 );
50 v3_angles( v0, cam->angles );
51 }
52
53 /*
54 * 1) [angles, pos] -> transform
55 */
56 static void camera_update_transform( camera *cam )
57 {
58 v4f qyaw, qpitch, qcam;
59 q_axis_angle( qyaw, (v3f){ 0.0f, 1.0f, 0.0f }, -cam->angles[0] );
60 q_axis_angle( qpitch, (v3f){ 1.0f, 0.0f, 0.0f }, -cam->angles[1] );
61
62 q_mul( qyaw, qpitch, qcam );
63 q_m3x3( qcam, cam->transform );
64 v3_copy( cam->pos, cam->transform[3] );
65 }
66
67 /*
68 * 2) [transform] -> transform_inverse, view matrix
69 */
70 static void camera_update_view( camera *cam )
71 {
72 m4x4_copy( cam->mtx.v, cam->mtx_prev.v );
73 m4x3_invert_affine( cam->transform, cam->transform_inverse );
74 m4x3_expand( cam->transform_inverse, cam->mtx.v );
75 }
76
77 /*
78 * 3) [fov,nearz,farz] -> projection matrix
79 */
80 static void camera_update_projection( camera *cam )
81 {
82 m4x4_copy( cam->mtx.p, cam->mtx_prev.p );
83 m4x4_projection( cam->mtx.p, cam->fov,
84 (float)vg.window_x / (float)vg.window_y,
85 cam->nearz, cam->farz );
86 }
87
88 /*
89 * 4) [projection matrix, view matrix] -> previous pv, new pv
90 */
91 static void camera_finalize( camera *cam )
92 {
93 m4x4_copy( cam->mtx.pv, cam->mtx_prev.pv );
94 m4x4_mul( cam->mtx.p, cam->mtx.v, cam->mtx.pv );
95 }
96 #endif