6 typedef struct camera camera
;
11 float fov
, nearz
, farz
;
26 static void camera_lerp_angles( v3f a
, v3f b
, float t
, v3f d
){
27 d
[0] = vg_alerpf( a
[0], b
[0], t
);
28 d
[1] = vg_lerpf( a
[1], b
[1], t
);
29 d
[2] = vg_lerpf( a
[2], b
[2], t
);
32 static void camera_lerp( camera
*a
, camera
*b
, float t
, camera
*d
){
33 v3_lerp( a
->pos
, b
->pos
, t
, d
->pos
);
34 d
->angles
[0] = vg_alerpf( a
->angles
[0], b
->angles
[0], t
);
35 d
->angles
[1] = vg_lerpf( a
->angles
[1], b
->angles
[1], t
);
36 d
->angles
[2] = vg_lerpf( a
->angles
[2], b
->angles
[2], t
);
37 d
->fov
= vg_lerpf( a
->fov
, b
->fov
, t
);
40 static void camera_copy( camera
*a
, camera
*d
){
41 v3_copy( a
->pos
, d
->pos
);
42 v3_copy( a
->angles
, d
->angles
);
47 * 1) [angles, pos] -> transform
49 static void camera_update_transform( camera
*cam
)
51 v4f qyaw
, qpitch
, qcam
;
52 q_axis_angle( qyaw
, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, -cam
->angles
[0] );
53 q_axis_angle( qpitch
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, -cam
->angles
[1] );
55 q_mul( qyaw
, qpitch
, qcam
);
56 q_m3x3( qcam
, cam
->transform
);
57 v3_copy( cam
->pos
, cam
->transform
[3] );
61 * 2) [transform] -> transform_inverse, view matrix
63 static void camera_update_view( camera
*cam
)
65 m4x4_copy( cam
->mtx
.v
, cam
->mtx_prev
.v
);
66 m4x3_invert_affine( cam
->transform
, cam
->transform_inverse
);
67 m4x3_expand( cam
->transform_inverse
, cam
->mtx
.v
);
71 * 3) [fov,nearz,farz] -> projection matrix
73 static void camera_update_projection( camera
*cam
)
75 m4x4_copy( cam
->mtx
.p
, cam
->mtx_prev
.p
);
76 m4x4_projection( cam
->mtx
.p
, cam
->fov
,
77 (float)vg
.window_x
/ (float)vg
.window_y
,
78 cam
->nearz
, cam
->farz
);
82 * 4) [projection matrix, view matrix] -> previous pv, new pv
84 static void camera_finalize( camera
*cam
)
86 m4x4_copy( cam
->mtx
.pv
, cam
->mtx_prev
.pv
);
87 m4x4_mul( cam
->mtx
.p
, cam
->mtx
.v
, cam
->mtx
.pv
);
91 * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
93 static void m4x4_clip_projection( m4x4f mat
, v4f plane
)
97 (vg_signf(plane
[0]) + mat
[2][0]) / mat
[0][0],
98 (vg_signf(plane
[1]) + mat
[2][1]) / mat
[1][1],
100 (1.0f
+ mat
[2][2]) / mat
[3][2]
103 v4_muls( plane
, 2.0f
/ v4_dot(plane
,c
), c
);
107 mat
[2][2] = c
[2] + 1.0f
;
112 * Undoes the above operation
114 static void m4x4_reset_clipping( m4x4f mat
, float ffar
, float fnear
)
118 mat
[2][2] = -(ffar
+ fnear
) / (ffar
- fnear
);
119 mat
[3][2] = -2.0f
* ffar
* fnear
/ (ffar
- fnear
);
122 #endif /* CAMERA_H */