From d55a4543b406c29e0a18cfd15082a3ddc06bf77c Mon Sep 17 00:00:00 2001 From: weiss Date: Sat, 19 Jul 2008 14:16:43 +0000 Subject: Make the recharge ripple fade nicely git-svn-id: svn://cook.msm.cam.ac.uk:745/thrust3d/thrust3d@144 84d2e878-0bd5-11dd-ad15-13eda11d74c5 --- data/shaders/swirlytron.frag | 20 +++++++++++--------- src/game.c | 1 + src/physics.c | 6 +++++- src/render.c | 27 ++++++++++++++++++++++----- src/types.h | 1 + 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/data/shaders/swirlytron.frag b/data/shaders/swirlytron.frag index 81c80c3..7d8aaec 100644 --- a/data/shaders/swirlytron.frag +++ b/data/shaders/swirlytron.frag @@ -12,26 +12,28 @@ varying vec2 coords; varying vec2 incoords; uniform float time; +uniform float rrb; /* Recharge ripple brightness */ uniform bool rechargeripple; void main() { vec3 colour; - float f; - float r; - r = pow(abs(coords.x), 2.0) + pow(abs(coords.y), 2.0); - f = 1.0; colour.r = 0.0; colour.g = 0.0; - colour.b = 1.0 * cos(2*f*3.141*r - (time/250.0) ); + colour.b = 0.5*max(0.0, cos(2*3.141*coords.x-(time/250.0))) + 0.5*max(0.0, cos(2*3.141*coords.y-(time/250.0))); if ( rechargeripple ) { + + const float f = 1.0; + float r; + r = pow(abs(incoords.x), 2.0) + pow(abs(incoords.y), 2.0); - f = 1.0; - colour.r += 0.8 * cos(2*f*3.141*r + (time/250.0) ); - colour.g += 0.3 * cos(2*f*3.141*r + (time/250.0) ); - colour.b += 0.1; + + colour.r += rrb * 0.8 * cos(2*f*3.141*r + (time/250.0) ); + colour.g += rrb * 0.3 * cos(2*f*3.141*r + (time/250.0) ); + colour.b += rrb * 0.1; + } gl_FragColor = vec4(colour.r, colour.g, colour.b, 1.0); diff --git a/src/game.c b/src/game.c index c51dd7f..a155c69 100644 --- a/src/game.c +++ b/src/game.c @@ -252,6 +252,7 @@ Game *game_new(int width, int height, GameOptions gameopts) { g->radiation = 0.1; g->platform_rel_x = 0.0; g->platform_rel_y = 0.0; + g->time_of_landing_event = -1500.0; /* Force the platform recharge ripple to be 'bright' */ /* Renderer setup */ g->render = render_setup(width, height, gameopts.disable_vbos, gameopts.disable_fbos, gameopts.disable_shaders); diff --git a/src/physics.c b/src/physics.c index ad2dae6..4575b88 100644 --- a/src/physics.c +++ b/src/physics.c @@ -316,6 +316,7 @@ static void physics_process(ModelInstance *obj, Uint32 dt, Game *game) { obj->recharging = 1; game->platform_rel_x = coll.obj->x - obj->x; game->platform_rel_y = coll.obj->y - obj->y; + game->time_of_landing_event = game->tlast + sttc; } } else { @@ -357,7 +358,10 @@ void physics_step(Game *game, Uint32 t) { game->lander->vz += THRUST * dt; game->fuel -= 0.0002; game->lander->landed = 0; - game->lander->recharging = 0; + if ( game->lander->recharging ) { + game->time_of_landing_event = game->tlast + dt; + game->lander->recharging = 0; + } } } if ( game->forward && !game->lander->landed ) { diff --git a/src/render.c b/src/render.c index 044dd42..e18b3a6 100644 --- a/src/render.c +++ b/src/render.c @@ -643,11 +643,28 @@ void render_draw(Game *game, Uint32 t) { glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r->swirly_fbo); glClear(GL_COLOR_BUFFER_BIT); glViewport(0, 0, 64, 64); - if ( r->shaders ) glUseProgram(r->swirly_program); - if ( r->shaders ) glUniform1f(glGetUniformLocation(game->render->swirly_program, "time"), t); - if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->swirly_program, "landed"), game->lander->recharging); - if ( r->shaders ) glUniform1i(glGetUniformLocation(game->render->swirly_program, "rechargeripple"), game->lander->recharging); - if ( r->shaders ) glUniform2f(glGetUniformLocation(game->render->swirly_program, "lander"), game->platform_rel_x, game->platform_rel_y); + if ( r->shaders ) { + + GLfloat rrb = 0.0; + + glUseProgram(r->swirly_program); + glUniform1f(glGetUniformLocation(game->render->swirly_program, "time"), t); + glUniform1i(glGetUniformLocation(game->render->swirly_program, "landed"), game->lander->recharging); + if ( game->lander->recharging ) { + /* Fade in */ + rrb = fminf(((GLfloat)t-game->time_of_landing_event)/1500.0, 1.0); + glUniform1i(glGetUniformLocation(game->render->swirly_program, "rechargeripple"), 1); + } else if ( game->time_of_landing_event - t < 750 ) { + /* Fade out */ + rrb = fmaxf(1.0 - ((GLfloat)t-game->time_of_landing_event)/750.0, 0.0); + glUniform1i(glGetUniformLocation(game->render->swirly_program, "rechargeripple"), 1); + } else { + glUniform1i(glGetUniformLocation(game->render->swirly_program, "rechargeripple"), 0); + } + glUniform1f(glGetUniformLocation(game->render->swirly_program, "rrb"), rrb); + glUniform2f(glGetUniformLocation(game->render->swirly_program, "lander"), game->platform_rel_x, game->platform_rel_y); + + } /* else this is all a little pointless... */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); diff --git a/src/types.h b/src/types.h index 928493e..bfbe36a 100644 --- a/src/types.h +++ b/src/types.h @@ -256,6 +256,7 @@ typedef struct { GLfloat platform_rel_x; GLfloat platform_rel_y; + GLfloat time_of_landing_event; } Game; -- cgit v1.2.3