4 typedef struct camera camera
;
9 float fov
, nearz
, farz
;
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
);
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
);
37 static void camera_copy( camera
*a
, camera
*d
){
38 v3_copy( a
->pos
, d
->pos
);
39 v3_copy( a
->angles
, d
->angles
);
43 static void m4x3_transform_camera( m4x3f m
, camera
*cam
){
44 m4x3_mulv( m
, cam
->pos
, cam
->pos
);
47 v3_angles_vector( cam
->angles
, v0
);
48 m3x3_mulv( m
, v0
, v0
);
50 v3_angles( v0
, cam
->angles
);
54 * 1) [angles, pos] -> transform
56 static void camera_update_transform( camera
*cam
)
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] );
62 q_mul( qyaw
, qpitch
, qcam
);
63 q_m3x3( qcam
, cam
->transform
);
64 v3_copy( cam
->pos
, cam
->transform
[3] );
68 * 2) [transform] -> transform_inverse, view matrix
70 static void camera_update_view( camera
*cam
)
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
);
78 * 3) [fov,nearz,farz] -> projection matrix
80 static void camera_update_projection( camera
*cam
)
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
);
89 * 4) [projection matrix, view matrix] -> previous pv, new pv
91 static void camera_finalize( camera
*cam
)
93 m4x4_copy( cam
->mtx
.pv
, cam
->mtx_prev
.pv
);
94 m4x4_mul( cam
->mtx
.p
, cam
->mtx
.v
, cam
->mtx
.pv
);