X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=camera.h;h=7aa1b14a92eb6b29f03338765587b3afe07dd949;hb=HEAD;hp=07205de223417ce84a392b4a8406043f0a260248;hpb=06e35432f5cf2b4e9ad2f537393511867f64d29a;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/camera.h b/camera.h index 07205de..7aa1b14 100644 --- a/camera.h +++ b/camera.h @@ -1,26 +1,96 @@ -#ifndef CAMERA_H -#define CAMERA_H +#pragma once +#if 0 -#include "common.h" +typedef struct camera camera; +struct camera{ + /* Input */ + v3f angles; + v3f pos; + float fov, nearz, farz; -static v2f camera_angles; -static v3f camera_pos; + /* Output */ + m4x3f transform, + transform_inverse; -static m4x3f camera_mtx, - camera_mtx_inverse; + struct camera_mtx{ + m4x4f p, + v, + pv; + } + mtx, + mtx_prev; +}; -static void camera_update(void) +static void camera_lerp_angles( v3f a, v3f b, float t, v3f d ){ + d[0] = vg_alerpf( a[0], b[0], t ); + d[1] = vg_lerpf( a[1], b[1], t ); + d[2] = vg_lerpf( a[2], b[2], t ); +} + +/* lerp position, fov, and angles */ +static void camera_lerp( camera *a, camera *b, float t, camera *d ){ + v3_lerp( a->pos, b->pos, t, d->pos ); + camera_lerp_angles( a->angles, b->angles, t, d->angles ); + d->fov = vg_lerpf( a->fov, b->fov, t ); +} + +static void camera_copy( camera *a, camera *d ){ + v3_copy( a->pos, d->pos ); + v3_copy( a->angles, d->angles ); + d->fov = a->fov; +} + +static void m4x3_transform_camera( m4x3f m, camera *cam ){ + m4x3_mulv( m, cam->pos, cam->pos ); + + v3f v0; + v3_angles_vector( cam->angles, v0 ); + m3x3_mulv( m, v0, v0 ); + v3_normalize( v0 ); + v3_angles( v0, cam->angles ); +} + +/* + * 1) [angles, pos] -> transform + */ +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 + */ +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 ); +} - m4x3_invert_affine( camera_mtx, camera_mtx_inverse ); +/* + * 3) [fov,nearz,farz] -> projection matrix + */ +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 ); } -#endif /* CAMERA_H */ +/* + * 4) [projection matrix, view matrix] -> previous pv, new pv + */ +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 ); +} +#endif