medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit de1de161b0ec09ffb3f35c32abe165b03bafe3a0
parent df530cce6214ed1f47af331f5e1b0d29ac21c088
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Dec 30 21:38:49 +0200

Simplify camera code in the BSP renderer

Diffstat:
bsp.cc | 48++++++++++++++++++++++++------------------------
game.h | 3++-
linear_algebra.h | 20++++++++++++++++++++
3 files changed, 46 insertions(+), 25 deletions(-)
diff --git a/bsp.cc b/bsp.cc @@ -7,6 +7,7 @@ #include "renderer.h" #include "bsp.h" #include "bsp_renderer.h" +#include "text_renderer.h" static ImmediateTriangle triangles[ 512000 ]; static ImmediateContext imm; @@ -299,14 +300,6 @@ BSP_Leaf & BSP::position_to_leaf( v3 pos ) const { return leaves[ -( node_idx + 1 ) ]; } -static v3 angles_to_vector( v3 angles ) { - return v3( - -sin( angles.y ) * sin( angles.x ), - -cos( angles.y ) * sin( angles.x ), - -cos( angles.x ) - ); -} - static UB test_ub; static Shader texture_shader; @@ -316,7 +309,8 @@ extern "C" GAME_INIT( game_init ) { MemoryArena arena = memarena_push_arena( &mem->persistent_arena, megabytes( 10 ) ); game->pos = v3( 0, -100, 450 ); - game->angles = radians( v3( -90, 135, 0 ) ); + game->pitch = 0; + game->yaw = 0; game->test_shader = renderer_new_shader( vert_src, frag_src ); @@ -334,10 +328,6 @@ extern "C" GAME_INIT( game_init ) { test_ub = renderer_new_ub(); } -static m4 camera_to_view( v3 position, v3 angles ) { - return m4_translation( -position ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ); -} - struct FSData { m4 V; m4 P; @@ -345,23 +335,26 @@ struct FSData { extern "C" GAME_FRAME( game_frame ) { const int fb = input->keys[ KEY_W ] - input->keys[ KEY_S ]; - const int lr = input->keys[ KEY_A ] - input->keys[ KEY_D ]; + const int lr = input->keys[ KEY_D ] - input->keys[ KEY_A ]; const int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; - const int pitch = input->keys[ KEY_UPARROW ] - input->keys[ KEY_DOWNARROW ]; - const int yaw = input->keys[ KEY_RIGHTARROW ] - input->keys[ KEY_LEFTARROW ]; + const int dpitch = input->keys[ KEY_DOWNARROW ] - input->keys[ KEY_UPARROW ]; + const int dyaw = input->keys[ KEY_LEFTARROW ] - input->keys[ KEY_RIGHTARROW ]; + + game->pitch += dpitch * dt * 50; + game->yaw += dyaw * dt * 50; - game->angles.x += pitch * dt * 2; - game->angles.y += yaw * dt * 2; + const v3 world_up = v3( 0, 0, 1 ); + const v3 forward = v3_forward( game->pitch, game->yaw ); + const v3 right = normalize( cross( forward, world_up ) ); + const v3 up = normalize( cross( right, forward ) ); - const v3 forward = angles_to_vector( game->angles ); - game->pos += forward * 100.0f * dt * ( float ) fb; - const v3 sideways = v3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); - game->pos += sideways * 100.0f * dt * ( float ) lr; - game->pos.z += ( float ) dz * 100.0f * dt; + game->pos += forward * 100.0f * dt * fb; + game->pos += right * 100.0f * dt * lr; + game->pos.z += dz * 100.0f * dt; m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); - m4 V = camera_to_view( game->pos, game->angles ); + m4 V = m4_view( forward, right, up, game->pos ); FSData fsdata = { V, P }; @@ -396,4 +389,11 @@ extern "C" GAME_FRAME( game_frame ) { immediate_render_state.ubs[ UB_VS_HOT ] = test_ub; immediate_render( &imm, immediate_render_state ); + char buf[ 256 ]; + snprintf( buf, sizeof( buf ), "pos: (%.2f %.2f %.2f) pitch: %.2f yaw: %.2f forward: (%.2f %.2f %.2f) right: (%.2f %.2f %.2f) up: (%.2f %.2f %.2f)", game->pos.x, game->pos.y, game->pos.z, game->pitch, game->yaw, + forward.x, forward.y, forward.z, + right.x, right.y, right.z, + up.x, up.y, up.z + ); + draw_text( buf, 2, 2, 16 ); } diff --git a/game.h b/game.h @@ -29,7 +29,8 @@ struct GameState { GameState() { } v3 pos; - v3 angles; + float pitch, yaw; + TerrainManager tm; BSP bsp; diff --git a/linear_algebra.h b/linear_algebra.h @@ -397,6 +397,16 @@ forceinline v3 normalize( v3 v ) { return v / length( v ); } +forceinline v3 v3_forward( float pitch, float yaw ) { + pitch = deg_to_rad( pitch ); + yaw = deg_to_rad( yaw ); + return v3( + cosf( pitch ) * cosf( yaw ), + cosf( pitch ) * sinf( yaw ), + sinf( pitch ) + ); +} + /* * v3u32 */ @@ -723,6 +733,16 @@ forceinline m4 operator-( const m4 & m ) { return m4( -m.col0, -m.col1, -m.col2, -m.col3 ); } +forceinline m4 m4_view( const v3 & forward, const v3 & right, const v3 & up, const v3 & position ) { + m4 rotation( + right.x, right.y, right.z, 0, + up.x, up.y, up.z, 0, + -forward.x, -forward.y, -forward.z, 0, + 0, 0, 0, 1 + ); + return rotation * m4_translation( -position ); +} + /* * quat */