From a98ce96041b248580c0447bf87d4c6106483cade Mon Sep 17 00:00:00 2001 From: hgn Date: Sun, 9 Oct 2022 01:51:13 +0100 Subject: [PATCH] frame rate independence --- audio.h | 2 +- build.sh | 4 ++- common.h | 3 +- main.c | 66 +++++++++++++++++++--------------- network.h | 6 ++-- player.h | 44 ++++++++++++++--------- player_animation.h | 34 +++++++++--------- player_audio.h | 6 ++-- player_physics.h | 89 +++++++++++++++++++++++++--------------------- render.h | 18 +++++----- rigidbody.h | 4 +-- traffic.h | 11 +++--- world.h | 2 ++ world_gate.h | 8 ++--- world_render.h | 2 +- world_routes.h | 23 ++++++------ world_sfd.h | 2 +- world_water.h | 12 +++---- 18 files changed, 185 insertions(+), 151 deletions(-) diff --git a/audio.h b/audio.h index a984dd0..1bf710f 100644 --- a/audio.h +++ b/audio.h @@ -243,7 +243,7 @@ static void audio_sample_occlusion( v3f origin ) } float occlusion = 1.0f - (d * (1.0f/(sample_dist*(float)sample_count))), - rate = ktimestep * k_audio_occlusion_rate, + rate = VG_TIMESTEP_FIXED * k_audio_occlusion_rate, target = powf( occlusion, 6.0f ); audio_occlusion_current = vg_lerpf( audio_occlusion_current, target, rate ); } diff --git a/build.sh b/build.sh index 2ceb2fe..28ef0e7 100755 --- a/build.sh +++ b/build.sh @@ -198,7 +198,7 @@ vg_command(){ run_command game run_command server ;; - distribution) + disb) mkdir -p ./dist run_command release run_command tools @@ -207,7 +207,9 @@ vg_command(){ run_command content run_command textures run_command server + ;; + distribution) titleit "Compressing distributions" logit "Linux" tar -chzvf dist/skaterift_linux__$TIMESTAMP.tar.gz bin/linux/ diff --git a/common.h b/common.h index d0f4c07..a36dc0a 100644 --- a/common.h +++ b/common.h @@ -5,12 +5,11 @@ #ifndef COMMON_H #define COMMON_H +#define VG_TIMESTEP_FIXED (1.0/60.0) #define VG_3D #define VG_FRAMEBUFFER_RESIZE 1 #include "vg/vg.h" -static float ktimestep = 1.0f/60.0f; - typedef struct ray_hit ray_hit; struct ray_hit { diff --git a/main.c b/main.c index 81584dd..0d4ee25 100644 --- a/main.c +++ b/main.c @@ -50,13 +50,13 @@ void vg_preload(void) .persistent = 1 }); -vg_info(" Copyright . . . -----, ,----- ,---. .---. " ); -vg_info(" 2021-2022 |\\ /| | / | | | | /| " ); -vg_info(" | \\ / | +-- / +----- +---' | / | " ); -vg_info(" | \\ / | | / | | \\ | / | " ); -vg_info(" | \\/ | | / | | \\ | / | " ); +vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" ); +vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" ); +vg_info(" | \\ / | +-- / +----- +---' | / | \n" ); +vg_info(" | \\ / | | / | | \\ | / | \n" ); +vg_info(" | \\/ | | / | | \\ | / | \n" ); vg_info(" ' ' '--' [] '----- '----- ' ' '---' " - "SOFTWARE" ); + "SOFTWARE\n" ); highscores_init( 2000, 50 ); vg_loader_highwater( NULL, highscores_save_at_exit, NULL ); @@ -106,11 +106,27 @@ void vg_update( int loaded ) { draw_origin_axis(); network_update(); - player_update(); + player_update_pre(); world_update( player.phys.rb.co ); } } +static void vg_update_fixed( int loaded ) +{ + if( loaded ) + { + player_update_fixed(); + } +} + +static void vg_update_post( int loaded ) +{ + if( loaded ) + { + player_update_post(); + } +} + static void vg_framebuffer_resize( int w, int h ) { render_fb_resize(); @@ -120,23 +136,15 @@ static void vg_framebuffer_resize( int w, int h ) static void render_main_game(void) { - /* TODO Breakup this & Gen */ -#if 0 - float speed = freecam? 0.0f: v3_length( player.phys.rb.v ); - v3f shake = { vg_randf()-0.5f, vg_randf()-0.5f, vg_randf()-0.5f }; - v3_muls( shake, speed*0.01f, shake ); -#endif - m4x4f world_4x4; m4x3_expand( player.camera_inverse, world_4x4 ); gpipeline.fov = freecam? 60.0f: 125.0f; /* 120 */ - m4x4_projection( vg_pv, gpipeline.fov, - (float)vg_window_x / (float)vg_window_y, + m4x4_projection( vg.pv, gpipeline.fov, + (float)vg.window_x / (float)vg.window_y, 0.02f, 2100.0f ); - m4x4_mul( vg_pv, world_4x4, vg_pv ); - + m4x4_mul( vg.pv, world_4x4, vg.pv ); glEnable( GL_DEPTH_TEST ); /* @@ -145,21 +153,21 @@ static void render_main_game(void) int draw_solid = player.is_dead | freecam; - render_world( vg_pv, player.camera ); + render_world( vg.pv, player.camera ); if( draw_solid ) draw_player(); render_water_texture( player.camera ); glBindFramebuffer( GL_FRAMEBUFFER, 0 ); - render_water_surface( vg_pv, player.camera ); + render_water_surface( vg.pv, player.camera ); - render_world_gates( vg_pv, player.phys.rb.co, player.camera ); + render_world_gates( vg.pv, player.phys.rb.co, player.camera ); /* Copy the RGB of what we have into the background buffer */ glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 ); glBindFramebuffer( GL_DRAW_FRAMEBUFFER, gpipeline.fb_background ); - glBlitFramebuffer( 0,0, vg_window_x, vg_window_y, - 0,0, vg_window_x, vg_window_y, + glBlitFramebuffer( 0,0, vg.window_x, vg.window_y, + 0,0, vg.window_x, vg.window_y, GL_COLOR_BUFFER_BIT, GL_LINEAR ); @@ -174,10 +182,10 @@ static void render_main_game(void) if( !draw_solid ) { - m4x4_projection( vg_pv, gpipeline.fov, - (float)vg_window_x / (float)vg_window_y, + m4x4_projection( vg.pv, gpipeline.fov, + (float)vg.window_x / (float)vg.window_y, 0.01f, 600.0f ); - m4x4_mul( vg_pv, world_4x4, vg_pv ); + m4x4_mul( vg.pv, world_4x4, vg.pv ); draw_player(); } @@ -202,7 +210,7 @@ static void render_main_game(void) void vg_render(void) { glBindFramebuffer( GL_FRAMEBUFFER, 0 ); - glViewport( 0,0, vg_window_x, vg_window_y ); + glViewport( 0,0, vg.window_x, vg.window_y ); glDisable( GL_DEPTH_TEST ); glClearColor( 0.11f, 0.35f, 0.37f, 1.0f ); @@ -214,8 +222,8 @@ void vg_render(void) /* Other shite */ glDisable(GL_BLEND); glDisable( GL_DEPTH_TEST ); - vg_lines_drawall( (float *)vg_pv ); - glViewport( 0,0, vg_window_x, vg_window_y ); + vg_lines_drawall( (float *)vg.pv ); + glViewport( 0,0, vg.window_x, vg.window_y ); } void vg_ui(void) diff --git a/network.h b/network.h index 6783769..d7c9be0 100644 --- a/network.h +++ b/network.h @@ -315,9 +315,9 @@ static void network_update(void) static double last_update = 0.0; poll_connection(); - if( vg_time > (last_update + 60.0) ) + if( vg.time > (last_update + 60.0) ) { - last_update = vg_time; + last_update = vg.time; if( steam_app_ticket_length ) { @@ -329,7 +329,7 @@ static void network_update(void) } } - if( vg_time > (last_update + 10.0) && + if( vg.time > (last_update + 10.0) && (cremote_state == k_ESteamNetworkingConnectionState_Connected )) { vg_warn( "Connected to server but no return... disconnecting\n" ); diff --git a/player.h b/player.h index 5dd9401..2c6ee19 100644 --- a/player.h +++ b/player.h @@ -60,7 +60,8 @@ static struct gplayer float vswitch, slip, slip_last, reverse; - float grab, jump; + float grab, jump, pushing, push_time; + double start_push; int in_air, on_board, jump_charge, jump_dir; m3x3f vr,vr_pstep; @@ -68,7 +69,6 @@ static struct gplayer phys, phys_gate_frame; - float pushing, push_time; int is_dead; v3f land_target; @@ -227,14 +227,11 @@ static void player_init(void) /* 1 */ vg_loader_highwater( player_model_init, player_model_free, NULL ); } -static void player_update(void) /* 2 */ +/* Deal with input etc */ +static void player_update_pre(void) { struct player_phys *phys = &player.phys; - for( int i=0; i0.0f) { player.is_dead = 0; @@ -256,18 +253,35 @@ static void player_update(void) /* 2 */ { phys->on_board ^= 0x1; } +} + +static void player_update_fixed(void) /* 2 */ +{ + struct player_phys *phys = &player.phys; -#if 0 - if( (glfwGetKey( vg_window, GLFW_KEY_O ) )) + if( player.is_dead ) { - player_ragdoll_copy_model( phys->rb.v ); - player.is_dead = 1; + player_ragdoll_iter(); } -#endif + else + { + player_do_motion(); + } + + player_audio(); /* FUTURE: can probably move this to post() + BUT, it uses deltas from fixed step physics, + AND this *might* be what we want for realtime + audio, anyway. */ +} + +static void player_update_post(void) +{ + for( int i=0; irb.v)*(1.0f/40.0f), 0.0f, 1.0f); float kicks = (vg_randf()-0.5f)*2.0f*kickspeed; float sign = vg_signf( kicks ); - speed_wobble = vg_lerpf( speed_wobble, kicks*kicks*sign, 0.1f ); - speed_wobble_2 = vg_lerpf( speed_wobble_2, speed_wobble, 0.04f ); + speed_wobble = vg_lerpf( speed_wobble, kicks*kicks*sign, 6.0f*vg.time_delta); + speed_wobble_2 = vg_lerpf( speed_wobble_2, speed_wobble, 2.4f*vg.time_delta); offset[0] *= 0.26f; offset[0] += speed_wobble_2*3.0f; @@ -108,7 +108,7 @@ static void player_animate(void) /* sliding */ { float desired = vg_clampf( lslip, 0.0f, 1.0f ); - player.fslide = vg_lerpf( player.fslide, desired, 0.04f ); + player.fslide = vg_lerpf( player.fslide, desired, 2.4f*vg.time_delta); } /* movement information */ @@ -117,9 +117,9 @@ static void player_animate(void) dirx = phys->slip < 0.0f? 0.0f: 1.0f, fly = phys->in_air? 1.0f: 0.0f; - player.fdirz = vg_lerpf( player.fdirz, dirz, 0.04f ); - player.fdirx = vg_lerpf( player.fdirx, dirx, 0.01f ); - player.ffly = vg_lerpf( player.ffly, fly, 0.04f ); + player.fdirz = vg_lerpf( player.fdirz, dirz, 2.4f*vg.time_delta ); + player.fdirx = vg_lerpf( player.fdirx, dirx, 0.6f*vg.time_delta ); + player.ffly = vg_lerpf( player.ffly, fly, 2.4f*vg.time_delta ); } struct skeleton *sk = &player.mdl.sk; @@ -129,7 +129,7 @@ static void player_animate(void) { /* when the player is moving fast he will crouch down a little bit */ float stand = 1.0f - vg_clampf( speed * 0.03f, 0.0f, 1.0f ); - player.fstand = vg_lerpf( player.fstand, stand, 0.1f ); + player.fstand = vg_lerpf( player.fstand, stand, 6.0f*vg.time_delta ); /* stand/crouch */ float dir_frame = player.fdirz * (15.0f/30.0f), @@ -145,9 +145,11 @@ static void player_animate(void) skeleton_lerp_pose( sk, apose, bpose, player.fslide, apose ); /* pushing */ - player.fpush = vg_lerpf( player.fpush, player.pushing, 0.1f ); + player.fpush = vg_lerpf( player.fpush, + player.phys.pushing, + 6.0f*vg.time_delta ); - float pt = player.push_time; + float pt = player.phys.push_time; if( phys->reverse > 0.0f ) skeleton_sample_anim( sk, player.mdl.anim_push, pt, bpose ); else @@ -158,12 +160,12 @@ static void player_animate(void) /* trick setup */ float jump_start_frame = 14.0f/30.0f; - player.fjump = vg_lerpf( player.fjump, phys->jump, 0.14f ); + player.fjump = vg_lerpf( player.fjump, phys->jump, 8.4f*vg.time_delta ); float setup_frame = phys->jump * jump_start_frame, setup_blend = vg_minf( player.fjump, 1.0f ); - float jump_frame = (vg_time - player.jump_time) + jump_start_frame; + float jump_frame = (vg.time - player.jump_time) + jump_start_frame; if( jump_frame >= jump_start_frame && jump_frame <= (40.0f/30.0f) ) setup_frame = jump_frame; @@ -178,7 +180,7 @@ static void player_animate(void) mdl_keyframe air_pose[32]; { float target = -vg_get_axis("horizontal"); - player.fairdir = vg_lerpf( player.fairdir, target, 0.04f ); + player.fairdir = vg_lerpf( player.fairdir, target, 2.4f*vg.time_delta ); float air_frame = (player.fairdir*0.5f+0.5f) * (15.0f/30.0f); @@ -267,7 +269,7 @@ static void player_animate_camera(void) static v3f lerp_cam = {0.0f,0.0f,0.0f}; v3f cam_pos; - player.fonboard = vg_lerpf(player.fonboard, phys->on_board, ktimestep*1.0f); + player.fonboard = vg_lerpf( player.fonboard, phys->on_board, vg.time_delta ); if( phys->on_board ) { @@ -302,7 +304,7 @@ static void player_animate_camera(void) } else { - float speed = ktimestep * k_look_speed; + float speed = vg.time_delta * k_look_speed; player.angles[0] += vg_get_axis( "horizontal" ) * speed; player.angles[1] += vg_get_axis( "vertical" ) * speed; diff --git a/player_audio.h b/player_audio.h index f46d670..757f495 100644 --- a/player_audio.h +++ b/player_audio.h @@ -26,7 +26,7 @@ static void player_audio(void) audio_lock(); - double revert_delta = vg_time - last_revert; + double revert_delta = vg.time - last_revert; if( phys->on_board && (!_air && l2) && (fabsf(phys->slip) > 0.5f) && (revert_delta > 0.7) ) { @@ -35,11 +35,11 @@ static void player_audio(void) audio_player_set_vol( &audio_player_extra, 2.0f ); audio_player_playclip( &audio_player_extra, &audio_lands[rand()%5] ); - last_revert = vg_time; + last_revert = vg.time; } static float air = 0.0f; - air = vg_lerpf(air, phys->in_air? 1.0f: 0.0f, 5.0f*ktimestep); + air = vg_lerpf(air, phys->in_air? 1.0f: 0.0f, 5.0f*VG_TIMESTEP_FIXED ); /* Spacial info */ v3f ears = { 1.0f,0.0f,0.0f }; diff --git a/player_physics.h b/player_physics.h index f71568b..553e5d7 100644 --- a/player_physics.h +++ b/player_physics.h @@ -25,7 +25,7 @@ static void player_start_air(void) phys->in_air = 1; - float pstep = ktimestep*10.0f; + float pstep = VG_TIMESTEP_FIXED * 10.0f; float best_velocity_delta = -9999.9f; float k_bias = 0.96f; @@ -134,7 +134,7 @@ static void player_physics_control(void) phys->slip = slip; phys->reverse = -vg_signf(vel[2]); - float substep = ktimestep * 0.2f; + float substep = VG_TIMESTEP_FIXED * 0.2f; float fwd_resistance = (vg_get_button( "break" )? 5.0f: 0.02f) * -substep; for( int i=0; i<5; i++ ) @@ -144,13 +144,9 @@ static void player_physics_control(void) vg_signf( vel[0] ) * -k_friction_lat*substep ); } - static double start_push = 0.0; - if( vg_get_button_down( "push" ) ) - start_push = vg_time; - if( vg_get_button( "jump" ) ) { - phys->jump += ktimestep * k_jump_charge_speed; + phys->jump += VG_TIMESTEP_FIXED * k_jump_charge_speed; if( !phys->jump_charge ) phys->jump_dir = phys->reverse > 0.0f? 1: 0; @@ -158,13 +154,20 @@ static void player_physics_control(void) phys->jump_charge = 1; } + static int push_button_last = 0; + int push_button = vg_get_button( "push" ); + push_button_last = push_button; + + if( push_button && !push_button_last ) + player.phys.start_push = vg.time; + if( !vg_get_button("break") && vg_get_button( "push" ) ) { - player.pushing = 1.0f; - player.push_time = vg_time-start_push; + player.phys.pushing = 1.0f; + player.phys.push_time = vg.time - player.phys.start_push; - float cycle_time = player.push_time*k_push_cycle_rate, - amt = k_push_accel * (sinf(cycle_time)*0.5f+0.5f)*ktimestep, + float cycle_time = player.phys.push_time*k_push_cycle_rate, + amt = k_push_accel * (sinf(cycle_time)*0.5f+0.5f)*VG_TIMESTEP_FIXED, current = v3_length( vel ), new_vel = vg_minf( current + amt, k_max_push_speed ); @@ -175,7 +178,7 @@ static void player_physics_control(void) /* Pumping */ static float previous = 0.0f; float delta = previous - phys->grab, - pump = delta * k_pump_force*ktimestep; + pump = delta * k_pump_force * VG_TIMESTEP_FIXED; previous = phys->grab; v3f p1; @@ -187,8 +190,10 @@ static void player_physics_control(void) m3x3_mulv( phys->rb.to_world, vel, phys->rb.v ); - float steer = vg_get_axis( "horizontal" ); - phys->iY -= vg_signf(steer)*powf(steer,2.0f) * k_steer_ground * ktimestep; + float steer = vg_get_axis( "horizontal" ), + steer_scaled = vg_signf(steer) * powf(steer,2.0f) * k_steer_ground; + + phys->iY -= steer_scaled * VG_TIMESTEP_FIXED; } /* @@ -206,7 +211,7 @@ static void player_physics_control_air(void) /* * Prediction */ - float pstep = ktimestep*10.0f; + float pstep = VG_TIMESTEP_FIXED * 10.0f; v3f pco, pco1, pv; v3_copy( phys->rb.co, pco ); @@ -255,10 +260,10 @@ static void player_physics_control_air(void) time_to_impact += pstep; } - phys->iY -= vg_get_axis( "horizontal" ) * k_steer_air * ktimestep; + phys->iY -= vg_get_axis( "horizontal" ) * k_steer_air * VG_TIMESTEP_FIXED; { float iX = vg_get_axis( "vertical" ) * - phys->reverse * k_steer_air * limiter * ktimestep; + phys->reverse * k_steer_air * limiter * VG_TIMESTEP_FIXED; static float siX = 0.0f; siX = vg_lerpf( siX, iX, k_steer_air_lerp ); @@ -364,7 +369,7 @@ static void player_walk_physics(void) { float move_norm = move * (1.0f/(1.0f-move_dead)), speed = vg_lerpf( 0.1f*k_runspeed, k_runspeed, move_norm ), - amt = k_walk_accel * ktimestep, + amt = k_walk_accel * VG_TIMESTEP_FIXED, zvel = v3_dot( phys->rb.v, forward_dir ), new_vel = vg_minf( zvel + amt, speed ), diff = new_vel - vg_minf( zvel, speed ); @@ -373,17 +378,18 @@ static void player_walk_physics(void) /* TODO move */ float walk_norm = 30.0f/(float)player.mdl.anim_walk->length, - run_norm = 30.0f/(float)player.mdl.anim_run->length ; + run_norm = 30.0f/(float)player.mdl.anim_run->length, + walk_adv = vg_lerpf( walk_norm,run_norm,move_norm ); - player.walk_timer += ktimestep * vg_lerpf( walk_norm,run_norm,move_norm ); + player.walk_timer += walk_adv * VG_TIMESTEP_FIXED; } else { player.walk_timer = 0.0f; } - phys->rb.v[0] *= 1.0f - (ktimestep*k_walk_friction); - phys->rb.v[2] *= 1.0f - (ktimestep*k_walk_friction); + phys->rb.v[0] *= 1.0f - (VG_TIMESTEP_FIXED * k_walk_friction); + phys->rb.v[2] *= 1.0f - (VG_TIMESTEP_FIXED * k_walk_friction); } /* @@ -498,7 +504,7 @@ static void player_physics(void) float grabt = vg_get_axis( "grabr" )*0.5f+0.5f; phys->grab = vg_lerpf( phys->grab, grabt, 0.14f ); - player.pushing = 0.0f; + player.phys.pushing = 0.0f; if( !phys->in_air ) { @@ -516,8 +522,8 @@ static void player_physics(void) q_mul( correction, phys->rb.q, phys->rb.q ); } - v3_muladds( phys->rb.v, phys->rb.up, - -k_downforce*ktimestep, phys->rb.v ); + float const DOWNFORCE = -k_downforce*VG_TIMESTEP_FIXED; + v3_muladds( phys->rb.v, phys->rb.up, DOWNFORCE, phys->rb.v ); player_physics_control(); @@ -540,7 +546,7 @@ static void player_physics(void) v3_muladds( phys->rb.v, jumpdir, force, phys->rb.v ); phys->jump = 0.0f; - player.jump_time = vg_time; + player.jump_time = vg.time; /* TODO: Move to audio file */ audio_lock(); @@ -558,8 +564,9 @@ static void player_physics(void) if( !phys->jump_charge ) { - phys->jump -= k_jump_charge_speed * ktimestep; + phys->jump -= k_jump_charge_speed * VG_TIMESTEP_FIXED; } + phys->jump_charge = 0; phys->jump = vg_clampf( phys->jump, 0.0f, 1.0f ); } @@ -603,8 +610,8 @@ static void player_do_motion(void) v3f prevco; v3_copy( phys->rb.co, prevco ); - apply_gravity( phys->rb.v, ktimestep ); - v3_muladds( phys->rb.co, phys->rb.v, ktimestep, phys->rb.co ); + apply_gravity( phys->rb.v, VG_TIMESTEP_FIXED ); + v3_muladds( phys->rb.co, phys->rb.v, VG_TIMESTEP_FIXED, phys->rb.co ); /* Real angular velocity integration */ v3_lerp( phys->rb.w, (v3f){0.0f,0.0f,0.0f}, 0.125f, phys->rb.w ); @@ -687,21 +694,21 @@ static void player_mouseview(void) view_vel = { 0.0f, 0.0f }; if( vg_get_button_down( "primary" ) ) - v2_copy( vg_mouse, mouse_last ); + v2_copy( vg.mouse, mouse_last ); else if( vg_get_button( "primary" ) ) { v2f delta; - v2_sub( vg_mouse, mouse_last, delta ); - v2_copy( vg_mouse, mouse_last ); + v2_sub( vg.mouse, mouse_last, delta ); + v2_copy( vg.mouse, mouse_last ); - v2_muladds( view_vel, delta, 0.001f, view_vel ); + v2_muladds( view_vel, delta, 0.06f*vg.time_delta, view_vel ); } - v2_muladds( view_vel, - (v2f){ vg_get_axis("h1"), vg_get_axis("v1") }, - 0.05f, view_vel ); - v2_muls( view_vel, 0.93f, view_vel ); + v2_muladds( view_vel, (v2f){ vg_get_axis("h1"), vg_get_axis("v1") }, + 3.0f * vg.time_delta, view_vel ); + + v2_muls( view_vel, 1.0f-4.2f*vg.time_delta, view_vel ); v2_add( view_vel, player.angles, player.angles ); player.angles[1] = vg_clampf( player.angles[1], -VG_PIf*0.5f, VG_PIf*0.5f ); } @@ -719,13 +726,13 @@ static void player_freecam(void) static v3f move_vel = { 0.0f, 0.0f, 0.0f }; if( vg_get_button( "forward" ) ) - v3_muladds( move_vel, lookdir, ktimestep * movespeed, move_vel ); + v3_muladds( move_vel, lookdir, VG_TIMESTEP_FIXED * movespeed, move_vel ); if( vg_get_button( "back" ) ) - v3_muladds( move_vel, lookdir, ktimestep *-movespeed, move_vel ); + v3_muladds( move_vel, lookdir, VG_TIMESTEP_FIXED *-movespeed, move_vel ); if( vg_get_button( "left" ) ) - v3_muladds( move_vel, sidedir, ktimestep *-movespeed, move_vel ); + v3_muladds( move_vel, sidedir, VG_TIMESTEP_FIXED *-movespeed, move_vel ); if( vg_get_button( "right" ) ) - v3_muladds( move_vel, sidedir, ktimestep * movespeed, move_vel ); + v3_muladds( move_vel, sidedir, VG_TIMESTEP_FIXED * movespeed, move_vel ); v3_muls( move_vel, 0.7f, move_vel ); v3_add( move_vel, player.camera_pos, player.camera_pos ); diff --git a/render.h b/render.h index a00c209..f97d612 100644 --- a/render.h +++ b/render.h @@ -126,7 +126,7 @@ static void pipeline_projection( m4x4f mat, float nearz, float farz ) { m4x4_projection( mat, gpipeline.fov, - (float)vg_window_x / (float)vg_window_y, + (float)vg.window_x / (float)vg.window_y, nearz, farz ); } @@ -184,19 +184,19 @@ static void fb_use( struct framebuffer *fb ) if( !fb ) { glBindFramebuffer( GL_FRAMEBUFFER, 0 ); - glViewport( 0, 0, vg_window_x, vg_window_y ); + glViewport( 0, 0, vg.window_x, vg.window_y ); } else { glBindFramebuffer( GL_FRAMEBUFFER, fb->fb ); - glViewport( 0, 0, vg_window_x / fb->div, vg_window_y / fb->div ); + glViewport( 0, 0, vg.window_x / fb->div, vg.window_y / fb->div ); } } static void fb_init( struct framebuffer *fb ) { - i32 ix = vg_window_x / fb->div, - iy = vg_window_y / fb->div; + i32 ix = vg.window_x / fb->div, + iy = vg.window_y / fb->div; glGenFramebuffers( 1, &fb->fb ); glBindFramebuffer( GL_FRAMEBUFFER, fb->fb ); @@ -240,8 +240,8 @@ static void fb_resize( struct framebuffer *fb ) if( !fb->allocated ) return; - i32 ix = vg_window_x / fb->div, - iy = vg_window_y / fb->div; + i32 ix = vg.window_x / fb->div, + iy = vg.window_y / fb->div; glBindTexture( GL_TEXTURE_2D, fb->colour ); glTexImage2D( GL_TEXTURE_2D, 0, fb->format, ix, iy, 0, @@ -256,7 +256,7 @@ static void render_fb_resize(void) if( gpipeline.ready ) { glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y, 0, + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg.window_x, vg.window_y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL ); } } @@ -271,7 +271,7 @@ static void render_init_temp_buffer(void) glGenTextures( 1, &gpipeline.rgb_background ); glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y, + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg.window_x, vg.window_y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); diff --git a/rigidbody.h b/rigidbody.h index a327488..282643a 100644 --- a/rigidbody.h +++ b/rigidbody.h @@ -1866,10 +1866,10 @@ static void rb_effect_simple_bouyency( rigidbody *ra, v4f plane, float depth = v3_dot( plane, ra->co ) - plane[3], lambda = vg_clampf( -depth, 0.0f, 1.0f ) * amt; - v3_muladds( ra->v, plane, lambda * ktimestep, ra->v ); + v3_muladds( ra->v, plane, lambda * VG_TIMESTEP_FIXED, ra->v ); if( depth < 0.0f ) - v3_muls( ra->v, 1.0f-(drag*ktimestep), ra->v ); + v3_muls( ra->v, 1.0f-(drag*VG_TIMESTEP_FIXED), ra->v ); } /* diff --git a/traffic.h b/traffic.h index 98b8c7b..004c624 100644 --- a/traffic.h +++ b/traffic.h @@ -4,6 +4,7 @@ #include "common.h" #include "model.h" #include "rigidbody.h" +#include "world.h" typedef struct traffic_node traffic_node; typedef struct traffic_driver traffic_driver; @@ -137,9 +138,9 @@ static void traffic_drive( traffic_driver *driver ) v3_muladds( next->co, next->h, -1.0f, h1 ); eval_bezier_time( p0,p1,h0,h1, driver->t, pc ); - eval_bezier_time( p0,p1,h0,h1, driver->t + ktimestep, pn ); + eval_bezier_time( p0,p1,h0,h1, driver->t + vg.time_delta, pn ); - float mod = ktimestep / v3_dist( pc, pn ); + float mod = vg.time_delta / v3_dist( pc, pn ); v3f dir,side,up; v3_sub( pn, pc, dir ); v3_normalize(dir); @@ -178,13 +179,13 @@ static void traffic_drive( traffic_driver *driver ) * Jesus take the wheel */ float steer_penalty = 1.0f-v3_dot( dir, driver->transform[0] ); - steer_penalty /= ktimestep; + steer_penalty /= vg.time_delta; steer_penalty *= 30.0f; float target_speed = vg_maxf( 16.0f * (1.0f-steer_penalty), 0.1f ), accel = target_speed - driver->speed; - driver->speed = stable_force( driver->speed, accel*ktimestep*2.0f ); - driver->t += driver->speed*mod*ktimestep; + driver->speed = stable_force( driver->speed, accel*vg.time_delta*2.0f ); + driver->t += driver->speed*mod*vg.time_delta; /* * Update transform diff --git a/world.h b/world.h index a7b5a58..bf81595 100644 --- a/world.h +++ b/world.h @@ -293,7 +293,9 @@ static void world_free( void *_ ) static void world_update( v3f pos ) { world_routes_update(); +#if 0 world_routes_debug(); +#endif int closest = 0; float min_dist = INFINITY; diff --git a/world_gate.h b/world_gate.h index dd71776..21cabe9 100644 --- a/world_gate.h +++ b/world_gate.h @@ -143,13 +143,13 @@ static int render_gate( teleport_gate *gate, v3f viewpos, m4x3f camera ) glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); #else shader_gatelq_use(); - shader_gatelq_uPv( vg_pv ); + shader_gatelq_uPv( vg.pv ); shader_gatelq_uMdl( gate_xform ); shader_gatelq_uCam( viewpos ); - shader_gatelq_uTime( vg_time*0.25f ); + shader_gatelq_uTime( vg.time*0.25f ); shader_gatelq_uInvRes( (v2f){ - 1.0f / (float)vg_window_x, - 1.0f / (float)vg_window_y }); + 1.0f / (float)vg.window_x, + 1.0f / (float)vg.window_y }); glEnable( GL_STENCIL_TEST ); glStencilOp( GL_KEEP, GL_KEEP, GL_REPLACE ); diff --git a/world_render.h b/world_render.h index aa008c5..5da3556 100644 --- a/world_render.h +++ b/world_render.h @@ -183,7 +183,7 @@ static void render_sky(m4x3f camera) shader_sky_uMdl(identity_matrix); shader_sky_uPv(full); shader_sky_uTexGarbage(0); - shader_sky_uTime( vg_time ); + shader_sky_uTime( vg.time ); vg_tex2d_bind( &tex_terrain_noise, 0 ); diff --git a/world_routes.h b/world_routes.h index f98fb0d..1a51102 100644 --- a/world_routes.h +++ b/world_routes.h @@ -21,7 +21,7 @@ enum route_special_type static void world_routes_interact(void) { - world.routes.last_interaction = vg_time; + world.routes.last_interaction = vg.time; } static void debug_sbpath( struct route_node *rna, struct route_node *rnb, @@ -410,7 +410,7 @@ static void world_routes_ui_draw( u32 route, v4f colour, float offset ) shader_routeui_use(); glBindVertexArray( pr->ui.vao ); - float fade_amt = vg_time - pr->ui.fade_timer_start; + float fade_amt = vg.time - pr->ui.fade_timer_start; fade_amt = vg_clampf( fade_amt / 1.0f, 0.0f, 1.0f ); float fade_block_size = 0.0f, @@ -430,7 +430,7 @@ static void world_routes_ui_draw( u32 route, v4f colour, float offset ) v4_copy( colour, fade_colour ); fade_colour[3] *= 1.0f-fade_amt; - float timer_delta = (vg_time - world.routes.last_interaction) * (1.0/60.0), + float timer_delta = (vg.time - world.routes.last_interaction) * (1.0/60.0), timer_scale = 1.0f - vg_minf( timer_delta, 1.0f ); /* @@ -580,7 +580,7 @@ static void world_routes_verify_run( u32 route ) pr->ui.fade_start = pr->ui.segment_start; pr->ui.fade_count = 0; - pr->ui.fade_timer_start = vg_time; + pr->ui.fade_timer_start = vg.time; int orig_seg_count = pr->ui.segment_count; @@ -603,7 +603,7 @@ static void world_routes_verify_run( u32 route ) pr->ui.fade_count ++; } - r->routes[route].latest_pass = vg_time; + r->routes[route].latest_pass = vg.time; } /* @@ -622,7 +622,7 @@ static void world_routes_activate_gate( u32 id ) r->active_gate = id; rg->timing.version = r->current_run_version; - rg->timing.time = vg_time; + rg->timing.time = vg.time; for( u32 i=0; iroute_count; i++ ) { struct route *route = &r->routes[i]; @@ -644,7 +644,7 @@ static void world_routes_activate_gate( u32 id ) { route->ui.fade_start = route->ui.segment_start; route->ui.fade_count = route->ui.segment_count; - route->ui.fade_timer_start = vg_time; + route->ui.fade_timer_start = vg.time; world_routes_ui_clear(i); vg_success( "CLEARING -> %u %u \n", route->ui.fade_start, @@ -655,7 +655,7 @@ static void world_routes_activate_gate( u32 id ) r->current_run_version ++; rc->timing.version = r->current_run_version; - rc->timing.time = vg_time; + rc->timing.time = vg.time; r->current_run_version ++; } @@ -672,7 +672,7 @@ static void world_routes_notify_reset(void) struct route *route = &r->routes[i]; if( route->active ) - world_routes_ui_notch( i, vg_time - route->latest_pass ); + world_routes_ui_notch( i, vg.time - route->latest_pass ); } } @@ -1119,11 +1119,12 @@ static void world_routes_update(void) for( int i=0; iroute_count; i++ ) { struct route *route = &r->routes[i]; - route->factive = vg_lerpf( route->factive, route->active, 0.01f ); + route->factive = vg_lerpf( route->factive, route->active, + 0.6f*vg.time_delta ); if( route->active ) { - world_routes_ui_updatetime( i, vg_time - route->latest_pass ); + world_routes_ui_updatetime( i, vg.time - route->latest_pass ); } } } diff --git a/world_sfd.h b/world_sfd.h index 5bc2aaa..71e8a94 100644 --- a/world_sfd.h +++ b/world_sfd.h @@ -89,7 +89,7 @@ static void sfd_update( struct sfd_instance *display ) float *target = &display->buffer[i], *cur = target+display->w*display->h; - float const rate = ktimestep*15.2313131414f; + float const rate = vg.time_delta * 15.2313131414f; float d1 = *target-*cur; if( fabsf(d1) > rate ) diff --git a/world_water.h b/world_water.h index 249800d..a4f6967 100644 --- a/world_water.h +++ b/world_water.h @@ -108,7 +108,7 @@ static void render_water_texture( m4x3f camera ) m4x4f projection; m4x4_projection( projection, gpipeline.fov, - (float)vg_window_x / (float)vg_window_y, + (float)vg.window_x / (float)vg.window_y, 0.1f, 900.0f ); plane_clip_projection( projection, clippa ); m4x4_mul( projection, view, projection ); @@ -132,14 +132,14 @@ static void render_water_texture( m4x3f camera ) m4x4_projection( projection, gpipeline.fov, - (float)vg_window_x / (float)vg_window_y, + (float)vg.window_x / (float)vg.window_y, 0.1f, 900.0f ); plane_clip_projection( projection, clippb ); m4x4_mul( projection, view, projection ); render_world_depth( projection, camera ); - glViewport( 0, 0, vg_window_x, vg_window_y ); + glViewport( 0, 0, vg.window_x, vg.window_y ); } static void render_water_surface( m4x4f pv, m4x3f camera ) @@ -156,14 +156,14 @@ static void render_water_surface( m4x4f pv, m4x3f camera ) vg_tex2d_bind( &tex_water_surf, 1 ); shader_water_uTexDudv( 1 ); shader_water_uInvRes( (v2f){ - 1.0f / (float)vg_window_x, - 1.0f / (float)vg_window_y }); + 1.0f / (float)vg.window_x, + 1.0f / (float)vg.window_y }); shader_link_standard_ub( _shader_water.id, 2 ); fb_bindtex( &wrender.fbdepth, 3 ); shader_water_uTexBack( 3 ); - shader_water_uTime( vg_time ); + shader_water_uTime( vg.time ); shader_water_uCamera( camera[3] ); shader_water_uSurfaceY( wrender.height ); -- 2.25.1