X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=camera.h;h=7aa1b14a92eb6b29f03338765587b3afe07dd949;hb=304647a7672165dd35ffe54884ed9aedcc9bf363;hp=405f055e216328450bef5a804b342a2be4959c87;hpb=34a8df54eb962f3ad2e036355041f5bc5cabe5a0;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/camera.h b/camera.h index 405f055..7aa1b14 100644 --- a/camera.h +++ b/camera.h @@ -1,12 +1,8 @@ -#ifndef CAMERA_H -#define CAMERA_H - -#include "common.h" +#pragma once +#if 0 typedef struct camera camera; - -struct camera -{ +struct camera{ /* Input */ v3f angles; v3f pos; @@ -16,40 +12,51 @@ struct camera m4x3f transform, transform_inverse; - struct camera_mtx - { + struct camera_mtx{ m4x4f p, v, pv; } mtx, mtx_prev; -} -VG_STATIC main_camera, gate_camera; +}; -VG_STATIC void camera_lerp_angles( v3f a, v3f b, float t, v3f d ) -{ +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 ); } -VG_STATIC void camera_lerp( camera *a, camera *b, float t, camera *d ) -{ +/* 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 ); - d->angles[0] = vg_alerpf( a->angles[0], b->angles[0], t ); - d->angles[1] = vg_lerpf( a->angles[1], b->angles[1], t ); - d->angles[2] = vg_lerpf( a->angles[2], b->angles[2], t ); + 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 */ -VG_STATIC void camera_update_transform( camera *cam ) +static void camera_update_transform( camera *cam ) { v4f qyaw, qpitch, qcam; - q_axis_angle( qyaw, (v3f){ 0.0f, 1.0f, 0.0f }, -cam->angles[0] ); + 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 ); @@ -60,7 +67,7 @@ VG_STATIC void camera_update_transform( camera *cam ) /* * 2) [transform] -> transform_inverse, view matrix */ -VG_STATIC void camera_update_view( camera *cam ) +static void camera_update_view( camera *cam ) { m4x4_copy( cam->mtx.v, cam->mtx_prev.v ); m4x3_invert_affine( cam->transform, cam->transform_inverse ); @@ -70,7 +77,7 @@ VG_STATIC void camera_update_view( camera *cam ) /* * 3) [fov,nearz,farz] -> projection matrix */ -VG_STATIC void camera_update_projection( camera *cam ) +static void camera_update_projection( camera *cam ) { m4x4_copy( cam->mtx.p, cam->mtx_prev.p ); m4x4_projection( cam->mtx.p, cam->fov, @@ -81,42 +88,9 @@ VG_STATIC void camera_update_projection( camera *cam ) /* * 4) [projection matrix, view matrix] -> previous pv, new pv */ -VG_STATIC void camera_finalize( camera *cam ) +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 ); - - 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 */ +#endif