X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;ds=sidebyside;f=camera.h;h=596409937ceb3cf2f532c7d22f2c999533bd9fa7;hb=f7db507815e2822d971031c30f25e02b45e9c914;hp=1c265a7515dd3bdf3209c9b9e5f1e9f791940e3f;hpb=75703291fbf045008a3b1ebb20fc46a2617b6b3b;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/camera.h b/camera.h index 1c265a7..5964099 100644 --- a/camera.h +++ b/camera.h @@ -3,24 +3,104 @@ #include "common.h" -VG_STATIC v2f camera_angles; -VG_STATIC v3f camera_pos; +typedef struct camera camera; -VG_STATIC m4x3f camera_mtx, - camera_mtx_inverse; +struct camera +{ + /* Input */ + v2f angles; + v3f pos; + float fov, nearz, farz; + + /* Output */ + m4x3f transform, + transform_inverse; -VG_STATIC void camera_update(void) + struct camera_mtx + { + m4x4f p, + v, + pv; + } + mtx, + mtx_prev; +} +VG_STATIC main_camera; + +/* + * 1) [angles, pos] -> transform + */ +VG_STATIC void camera_update_transform( camera *cam ) { - /* Update camera matrices */ v4f qyaw, qpitch, qcam; - q_axis_angle( qyaw, (v3f){ 0.0f, 1.0f, 0.0f }, -camera_angles[0] ); - q_axis_angle( qpitch, (v3f){ 1.0f, 0.0f, 0.0f }, -camera_angles[1] ); + q_axis_angle( qyaw, (v3f){ 0.0f, 1.0f, 0.0f }, -cam->angles[0] ); + q_axis_angle( qpitch, (v3f){ 1.0f, 0.0f, 0.0f }, -cam->angles[1] ); q_mul( qyaw, qpitch, qcam ); - q_m3x3( qcam, camera_mtx ); - v3_copy( camera_pos, camera_mtx[3] ); + q_m3x3( qcam, cam->transform ); + v3_copy( cam->pos, cam->transform[3] ); +} + +/* + * 2) [transform] -> transform_inverse, view matrix + */ +VG_STATIC void camera_update_view( camera *cam ) +{ + m4x4_copy( cam->mtx.v, cam->mtx_prev.v ); + m4x3_invert_affine( cam->transform, cam->transform_inverse ); + m4x3_expand( cam->transform_inverse, cam->mtx.v ); +} + +/* + * 3) [fov,nearz,farz] -> projection matrix + */ +VG_STATIC void camera_update_projection( camera *cam ) +{ + m4x4_copy( cam->mtx.p, cam->mtx_prev.p ); + m4x4_projection( cam->mtx.p, cam->fov, + (float)vg.window_x / (float)vg.window_y, + cam->nearz, cam->farz ); +} + +/* + * 4) [projection matrix, view matrix] -> previous pv, new pv + */ +VG_STATIC void camera_finalize( camera *cam ) +{ + m4x4_copy( cam->mtx.pv, cam->mtx_prev.pv ); + m4x4_mul( cam->mtx.p, cam->mtx.v, cam->mtx.pv ); +} + +/* + * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf + */ +VG_STATIC void m4x4_clip_projection( m4x4f mat, v4f plane ) +{ + v4f c = + { + (vg_signf(plane[0]) + mat[2][0]) / mat[0][0], + (vg_signf(plane[1]) + mat[2][1]) / mat[1][1], + -1.0f, + (1.0f + mat[2][2]) / mat[3][2] + }; + + v4_muls( plane, 2.0f / v4_dot(plane,c), c ); - m4x3_invert_affine( camera_mtx, camera_mtx_inverse ); + mat[0][2] = c[0]; + mat[1][2] = c[1]; + mat[2][2] = c[2] + 1.0f; + mat[3][2] = c[3]; +} + +/* + * Undoes the above operation + */ +VG_STATIC void m4x4_reset_clipping( m4x4f mat, float ffar, float fnear ) +{ + mat[0][2] = 0.0f; + mat[1][2] = 0.0f; + mat[2][2] = -(ffar + fnear) / (ffar - fnear); + mat[3][2] = -2.0f * ffar * fnear / (ffar - fnear); } #endif /* CAMERA_H */