medfall

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

commit db8d6679a990fc4ca843cfcbe27babd08461e438
parent de1de161b0ec09ffb3f35c32abe165b03bafe3a0
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Dec 30 23:22:46 +0200

Update the camera code for all the other modules

Diffstat:
bsp.cc | 4++--
hm.cc | 58++++++++++++++++++++++++++++++++++------------------------
mod_btt.cc | 92++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
shadow_map.cc | 67+++++++++++++++++++++++++++++--------------------------------------
skybox.cc | 5+----
skybox.h | 2+-
6 files changed, 135 insertions(+), 93 deletions(-)
diff --git a/bsp.cc b/bsp.cc @@ -341,8 +341,8 @@ extern "C" GAME_FRAME( game_frame ) { 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->pitch += dpitch * dt * 100; + game->yaw += dyaw * dt * 100; const v3 world_up = v3( 0, 0, 1 ); const v3 forward = v3_forward( game->pitch, game->yaw ); diff --git a/hm.cc b/hm.cc @@ -89,7 +89,9 @@ extern "C" GAME_INIT( game_init ) { net_init(); game->pos = v3( 1500, 1500, 250 ); - game->angles = radians( v3( -90, 45, 0 ) ); + game->pitch = 0; + game->yaw = -45; + terrain_init( &game->tm, "terrains/gta.png.parts", &mem->persistent_arena, &game->background_tasks ); terrain_teleport( &game->tm, game->pos ); @@ -181,40 +183,48 @@ extern "C" GAME_FRAME( game_frame ) { renderer_begin_frame( CLEARCOLOUR_DONT ); } - const int fb = input->keys[ KEY_W ] - input->keys[ KEY_S ]; - const int lr = input->keys[ KEY_A ] - input->keys[ KEY_D ]; - const int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; + int fb = input->keys[ KEY_W ] - input->keys[ KEY_S ]; + int lr = input->keys[ KEY_D ] - input->keys[ KEY_A ]; + int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; - int pitch = input->keys[ KEY_UPARROW ] - input->keys[ KEY_DOWNARROW ]; - int yaw = input->keys[ KEY_RIGHTARROW ] - input->keys[ KEY_LEFTARROW ]; + int dpitch = input->keys[ KEY_DOWNARROW ] - input->keys[ KEY_UPARROW ]; + int dyaw = input->keys[ KEY_LEFTARROW ] - input->keys[ KEY_RIGHTARROW ]; - pitch += input->keys[ KEY_I ] - input->keys[ KEY_K ]; - yaw += input->keys[ KEY_L ] - input->keys[ KEY_J ]; + dpitch += input->keys[ KEY_I ] - input->keys[ KEY_K ]; + dyaw += input->keys[ KEY_L ] - input->keys[ KEY_J ]; - game->angles.x += pitch * dt * 2; - game->angles.y += yaw * dt * 2; + game->pitch += dpitch * dt * 100; + game->yaw += dyaw * dt * 100; - game->angles.x += float( input->mouse_dy * 0.01 ); - game->angles.y += float( input->mouse_dx * 0.01 ); + game->pitch -= float( input->mouse_dy * 0.1 ); + game->yaw -= float( input->mouse_dx * 0.1 ); const float dsun = ( input->keys[ KEY_EQUALS ] - input->keys[ KEY_MINUS ] ) * dt; game->test_sun += dsun; if( dsun != 0 ) printf( "sun: %.4f\n", game->test_sun ); - // const float speed = 6.0f; - const float speed = 100.0f; - game->pos += angles_to_vector_xy( game->angles ) * speed * dt * ( float ) fb; - const v3 sideways = v3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); - game->pos += sideways * speed * dt * ( float ) lr; + // const float speed = 20.0f; + const float speed = 6.0f; + + const v3 world_up = v3( 0, 0, 1 ); + v3 forward = v3_forward( game->pitch, game->yaw ); + v3 right = normalize( cross( forward, world_up ) ); + v3 up = normalize( cross( right, forward ) ); + + game->pos += forward * dt * fb * speed; + game->pos += right * dt * lr * speed; + game->pos.z += dt * dz * speed; + // game->pos.z = terrain_height( &game->tm, game->pos ) + 1.8f; - game->pos.z += dz * 50.0f * dt; + // game->pos.z += dz * 50.0f * dt; terrain_update( &game->tm, game->pos ); 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 ); + m4 Vsky = m4_view( forward, right, up, v3( 0 ) ); - skybox_render( &game->skybox, game->angles, game->test_sun ); + skybox_render( &game->skybox, Vsky, P, game->test_sun ); terrain_render( &game->tm, V, P, game->test_sun, current_time ); RenderState immediate_render_state; @@ -229,13 +239,13 @@ extern "C" GAME_FRAME( game_frame ) { float t; v3 normal; - v3 seg_end = game->pos + 1024.0f * angles_to_vector( game->angles ); + v3 seg_end = game->pos + 1024.0f * forward; if( segment_vs_terrain( &game->tm, game->pos, seg_end, &t, &normal ) ) { - v3 impact = game->pos + angles_to_vector( game->angles ) * t; + v3 impact = game->pos + forward * t; immediate_arrow( &imm, impact, normal, 16, v4( 1, 0, 0, 1 ) ); } - m4 VP = V * P; + m4 VP = P * V; renderer_ub_data( ub_sphere, &VP, sizeof( VP ) ); RenderState impact_render_state; @@ -247,7 +257,7 @@ extern "C" GAME_FRAME( game_frame ) { if( input->keys[ KEY_F ] ) { Fireball * fireball = fireballs.acquire(); fireball->pos = game->pos; - fireball->velocity = 128.0f * angles_to_vector( game->angles ); + fireball->velocity = 128.0f * forward; } { diff --git a/mod_btt.cc b/mod_btt.cc @@ -9,8 +9,6 @@ #define TERRAIN_NAME "mountains512.png" -static ImmediateTriangle triangles[ megabytes( 16 ) ]; -static ImmediateContext imm; static const char * vert_src = GLSL( in vec3 position; @@ -154,7 +152,8 @@ extern "C" GAME_INIT( game_init ) { PROFILE_FUNCTION(); game->pos = v3( 100, 200, 100 ); - game->angles = radians( v3( -90, 45, 0 ) ); + game->pitch = 0; + game->yaw = 45; game->test_sun = 0.3f; @@ -242,32 +241,73 @@ static void draw_qt( ImmediateContext * imm, AABBu32 aabb, const array< Heightma } } +template< typename T > +static T lerp( T a, float t, T b ) { + ASSERT( t >= 0.0f && t <= 1.0f ); + return a * ( 1.0f - t ) + b * t; +} + +template< typename T > +static T bilerp( const array2d< T > arr, float x, float y ) { + size_t xi = ( size_t ) x; + size_t yi = ( size_t ) y; + size_t xi1 = min( xi + 1, arr.w - 1 ); + size_t yi1 = min( yi + 1, arr.h - 1 ); + + float xf = x - xi; + float yf = y - yi; + + T a = arr( xi, yi ); + T b = arr( xi1, yi ); + T c = arr( xi, yi1 ); + T d = arr( xi1, yi1 ); + + T ab = lerp( a, xf, b ); + T cd = lerp( c, xf, d ); + + return lerp( ab, yf, cd ); +} + extern "C" GAME_FRAME( game_frame ) { renderer_begin_frame( CLEARCOLOUR_DONT ); - skybox_render( &game->skybox, game->angles, game->test_sun ); - const int fb = input->keys[ KEY_W ] - input->keys[ KEY_S ]; - const int lr = input->keys[ KEY_A ] - input->keys[ KEY_D ]; - const int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; + int fb = input->keys[ KEY_W ] - input->keys[ KEY_S ]; + int lr = input->keys[ KEY_D ] - input->keys[ KEY_A ]; + int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; + + int dpitch = input->keys[ KEY_DOWNARROW ] - input->keys[ KEY_UPARROW ]; + int dyaw = input->keys[ KEY_LEFTARROW ] - input->keys[ KEY_RIGHTARROW ]; + + dpitch += input->keys[ KEY_I ] - input->keys[ KEY_K ]; + dyaw += input->keys[ KEY_L ] - input->keys[ KEY_J ]; + + game->pitch += dpitch * dt * 100; + game->yaw += dyaw * dt * 100; + + game->pitch -= float( input->mouse_dy * 0.1 ); + game->yaw -= float( input->mouse_dx * 0.1 ); + + const float speed = 50.0f; + + const v3 world_up = v3( 0, 0, 1 ); + v3 forward = v3_forward( game->pitch, game->yaw ); + v3 right = normalize( cross( forward, world_up ) ); + v3 up = normalize( cross( right, forward ) ); + + game->pos += forward * dt * fb * speed; + game->pos += right * dt * lr * speed; + game->pos.z += dt * dz * speed; const float dsun = ( input->keys[ KEY_EQUALS ] - input->keys[ KEY_MINUS ] ) * dt; game->test_sun += dsun; if( dsun != 0 ) printf( "sun: %.4f\n", game->test_sun ); - const int pitch = input->keys[ KEY_UPARROW ] - input->keys[ KEY_DOWNARROW ]; - const int yaw = input->keys[ KEY_RIGHTARROW ] - input->keys[ KEY_LEFTARROW ]; - - game->angles.x += pitch * dt * 2; - game->angles.y += yaw * dt * 2; + m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); + m4 V = m4_view( forward, right, up, game->pos ); + m4 Vsky = m4_view( forward, right, up, v3( 0 ) ); + m4 VP = P * V; - // const float speed = 6.0f; - const float speed = 100.0f; - game->pos += angles_to_vector_xy( game->angles ) * speed * dt * ( float ) fb; - const v3 sideways = v3( -cosf( game->angles.y ), sinf( game->angles.y ), 0 ); - game->pos += sideways * speed * dt * ( float ) lr; - game->pos.z += dz * 50.0f * dt; - - m4 VP = camera_to_vp( game->pos, game->angles ); + skybox_render( &game->skybox, Vsky, P, game->test_sun ); FSData fs_data = { }; fs_data.dimensions = v2( game->hm.width, game->hm.height ); @@ -282,6 +322,9 @@ extern "C" GAME_FRAME( game_frame ) { render_state.ubs[ UB_FS_HOT ] = ub_fs; gpubtt_render( &game->gpubtt, render_state ); + static ImmediateTriangle triangles[ megabytes( 16 ) ]; + ImmediateContext imm; + { immediate_init( &imm, triangles, ARRAY_COUNT( triangles ) ); for( size_t i = 0; i < game->hm.width; i++ ) { @@ -292,13 +335,14 @@ extern "C" GAME_FRAME( game_frame ) { } float t; - v3 ray_dir = angles_to_vector( game->angles ); + v3 ray_dir = forward; v3 inv_dir = v3( 1.0f / ray_dir.x, 1.0f / ray_dir.y, 1.0f / ray_dir.z ); v3 xnormal; if( ray_vs_quadtree( &qt, game->pos, ray_dir, inv_dir, &t, &xnormal ) ) { - v3 impact = game->pos + angles_to_vector( game->angles ) * t; - printf( "impact (%f) %f %f %f\n", t, impact.x, impact.y, impact.z ); - immediate_sphere( &imm, impact, 4, v4( 1, 0, 0, 1 ) ); + v3 impact = game->pos + forward * t; + immediate_sphere( &imm, impact, 2, v4( 1, 0, 0, 1 ) ); + v3 smooth_normal = normalize( bilerp( normals, impact.x, impact.y ) ); + immediate_arrow( &imm, impact, smooth_normal, 8, v4( 1, 0, 0, 1 ) ); } else printf( "nope\n" ); diff --git a/shadow_map.cc b/shadow_map.cc @@ -87,44 +87,11 @@ static void draw_scene( Shader shader ) { immediate_render( &imm, shader ); } -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 void update_camera( const GameInput * input, float dt, v3 position, v3 angles, - v3 * out_position, v3 * out_angles + v3 * out_position, v3 * out_pitch, m4 * V ) { - float speed = 6.0f; - float angular_speed = 2.0f; - - float dx = ( float ) input->keys[ KEY_A ] - ( float ) input->keys[ KEY_D ]; - float dy = ( float ) input->keys[ KEY_W ] - ( float ) input->keys[ KEY_S ]; - float dz = ( float ) input->keys[ KEY_SPACE ] - ( float ) input->keys[ KEY_LEFTSHIFT ]; - - v3 forward = angles_to_vector( angles ); - v3 sideways = v3( -cosf( angles.y ), sinf( angles.y ), 0 ); - - *out_position = position - + forward * dt * dy * speed - + sideways * dt * dx * speed; - out_position->z += dt * dz * speed; - - float pitch = ( float ) input->keys[ KEY_UPARROW ] - ( float ) input->keys[ KEY_DOWNARROW ]; - float yaw = ( float ) input->keys[ KEY_RIGHTARROW ] - ( float ) input->keys[ KEY_LEFTARROW ]; - - *out_angles = angles + v3( dt * pitch * angular_speed, dt * yaw * angular_speed, 0 ); -} - -static m4 camera_to_vp( v3 position, v3 angles ) { - const m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, 25.0f ); - - return m4_translation( -position ) * m4_rotz( -angles.y ) * m4_rotx( -angles.x ) * P; } const u32 SHADOW_WIDTH = 1024; @@ -157,7 +124,8 @@ extern "C" GAME_INIT( game_init ) { glBindFramebuffer( GL_FRAMEBUFFER, 0 ); game->pos = v3( -10, -10, 5 ); - game->angles = radians( ( v3( -90, 45, 0 ) ) ); + game->pitch = 0; + game->yaw = 45; float verts[] = { -1, -1, -1, 1, 1, -1, 1, 1 }; glGenVertexArrays( 1, &screen_vao ); @@ -172,11 +140,34 @@ extern "C" GAME_INIT( game_init ) { } extern "C" GAME_FRAME( game_frame ) { - update_camera( input, dt, game->pos, game->angles, &game->pos, &game->angles ); - m4 vp = camera_to_vp( game->pos, game->angles ); + const float speed = 6.0f; + const float angular_speed = 100.0f; + + float fb = input->keys[ KEY_W ] - input->keys[ KEY_S ]; + float lr = input->keys[ KEY_D ] - input->keys[ KEY_A ]; + float dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; + + float dpitch = input->keys[ KEY_DOWNARROW ] - input->keys[ KEY_UPARROW ]; + float dyaw = input->keys[ KEY_LEFTARROW ] - input->keys[ KEY_RIGHTARROW ]; + + game->pitch += dpitch * dt * angular_speed; + game->yaw += dyaw * dt * angular_speed; + + const v3 world_up = v3( 0, 0, 1 ); + v3 forward = v3_forward( game->pitch, game->yaw ); + v3 right = normalize( cross( forward, world_up ) ); + v3 up = normalize( cross( right, forward ) ); + + game->pos += forward * dt * fb * speed; + game->pos += right * dt * lr * speed; + game->pos.z += dt * dz * speed; + + const m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, 25.0f ); + m4 V = m4_view( forward, right, up, game->pos ); + m4 VP = P * V; glUseProgram( prog ); - glUniformMatrix4fv( un_vp, 1, GL_FALSE, ( float * ) &vp ); + glUniformMatrix4fv( un_vp, 1, GL_FALSE, ( float * ) &VP ); glViewport( 0, 0, SHADOW_WIDTH, SHADOW_HEIGHT ); glBindFramebuffer( GL_DRAW_FRAMEBUFFER, fbo ); diff --git a/skybox.cc b/skybox.cc @@ -84,10 +84,7 @@ static m4 angles_to_view( v3 angles ) { return m4_rotz( -angles.y ) * m4_rotx( -angles.x ); } -void skybox_render( const Skybox * skybox, v3 view_angles, float sun ) { - m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); - m4 V = angles_to_view( view_angles ); - +void skybox_render( const Skybox * skybox, const m4 & V, const m4 & P, float sun ) { VSData vsdata = { V, P }; FSData fsdata = { sun }; renderer_ub_data( skybox->vertex_uniforms, &vsdata, sizeof( vsdata ) ); diff --git a/skybox.h b/skybox.h @@ -11,5 +11,5 @@ struct Skybox { }; void skybox_init( Skybox * skybox ); -void skybox_render( const Skybox * skybox, v3 angles, float sun ); +void skybox_render( const Skybox * skybox, const m4 & V, const m4 & P, float sun ); void skybox_destroy( Skybox * skybox );