6 typedef struct camera camera
;
13 float fov
, nearz
, farz
;
28 VG_STATIC main_camera
;
31 * 1) [angles, pos] -> transform
33 VG_STATIC
void camera_update_transform( camera
*cam
)
35 v4f qyaw
, qpitch
, qcam
;
36 q_axis_angle( qyaw
, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, -cam
->angles
[0] );
37 q_axis_angle( qpitch
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, -cam
->angles
[1] );
39 q_mul( qyaw
, qpitch
, qcam
);
40 q_m3x3( qcam
, cam
->transform
);
41 v3_copy( cam
->pos
, cam
->transform
[3] );
45 * 2) [transform] -> transform_inverse, view matrix
47 VG_STATIC
void camera_update_view( camera
*cam
)
49 m4x4_copy( cam
->mtx
.v
, cam
->mtx_prev
.v
);
50 m4x3_invert_affine( cam
->transform
, cam
->transform_inverse
);
51 m4x3_expand( cam
->transform_inverse
, cam
->mtx
.v
);
55 * 3) [fov,nearz,farz] -> projection matrix
57 VG_STATIC
void camera_update_projection( camera
*cam
)
59 m4x4_copy( cam
->mtx
.p
, cam
->mtx_prev
.p
);
60 m4x4_projection( cam
->mtx
.p
, cam
->fov
,
61 (float)vg
.window_x
/ (float)vg
.window_y
,
62 cam
->nearz
, cam
->farz
);
66 * 4) [projection matrix, view matrix] -> previous pv, new pv
68 VG_STATIC
void camera_finalize( camera
*cam
)
70 m4x4_copy( cam
->mtx
.pv
, cam
->mtx_prev
.pv
);
71 m4x4_mul( cam
->mtx
.p
, cam
->mtx
.v
, cam
->mtx
.pv
);
75 * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
77 VG_STATIC
void m4x4_clip_projection( m4x4f mat
, v4f plane
)
81 (vg_signf(plane
[0]) + mat
[2][0]) / mat
[0][0],
82 (vg_signf(plane
[1]) + mat
[2][1]) / mat
[1][1],
84 (1.0f
+ mat
[2][2]) / mat
[3][2]
87 v4_muls( plane
, 2.0f
/ v4_dot(plane
,c
), c
);
91 mat
[2][2] = c
[2] + 1.0f
;
96 * Undoes the above operation
98 VG_STATIC
void m4x4_reset_clipping( m4x4f mat
, float ffar
, float fnear
)
102 mat
[2][2] = -(ffar
+ fnear
) / (ffar
- fnear
);
103 mat
[3][2] = -2.0f
* ffar
* fnear
/ (ffar
- fnear
);
106 #endif /* CAMERA_H */