medfall

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

commit 623f6139956149d2c317ab80f6292ab313ab7ebb
parent 4bf7286b953e9add20b869c0b57d598b4792452a
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Jun 17 23:25:52 +0100

Bad implementation of good fog

Diffstat:
hm.cc | 7++++---
terrain_manager.cc | 14+++++++++-----
terrain_manager.h | 3++-
3 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/hm.cc b/hm.cc @@ -239,10 +239,10 @@ extern "C" GAME_FRAME( game_frame ) { terrain_update( &state->tm, state->pos ); - const glm::mat4 VP = glm::translate( + const glm::mat4 V = glm::translate( glm::rotate( glm::rotate( - P, + glm::mat4(), state->angles.x, glm::vec3( 1, 0, 0 ) ), @@ -251,8 +251,9 @@ extern "C" GAME_FRAME( game_frame ) { ), -state->pos ); + const glm::mat4 VP = P * V; - terrain_render( &state->tm, VP, 0.3f ); + terrain_render( &state->tm, V, VP, 0.3f ); glDisable( GL_DEPTH_TEST ); glUseProgram( state->test_shader ); diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -19,22 +19,23 @@ static const GLchar * vert_src = GLSL( in float lit; out vec3 n; - out float depth; + out vec4 pos; out float l; + uniform mat4 v; uniform mat4 vp; void main() { n = normal; l = lit; + pos = v * vec4( position, 1.0 ); gl_Position = vp * vec4( position, 1.0 ); - depth = gl_Position.z; } ); static const GLchar * frag_src = GLSL( in vec3 n; - in float depth; + in vec4 pos; in float l; out vec4 colour; @@ -55,7 +56,8 @@ static const GLchar * frag_src = GLSL( vec3 fog = vec3( 0.6, 0.6, 0.6 ); - float t = smoothstep( 400, 600, depth ); + float depth = length( pos ); + float t = smoothstep( 200, 400, depth ); colour = vec4( ( 1.0 - t ) * ground * light + t * fog, 1.0 ); } @@ -114,6 +116,7 @@ void terrain_init( tm->at_normal = glGetAttribLocation( tm->shader, "normal" ); tm->at_lit = glGetAttribLocation( tm->shader, "lit" ); + tm->un_v = glGetUniformLocation( tm->shader, "v" ); tm->un_vp = glGetUniformLocation( tm->shader, "vp" ); tm->un_sun = glGetUniformLocation( tm->shader, "sun" ); @@ -249,11 +252,12 @@ void terrain_update( TerrainManager * tm, glm::vec3 position ) { } } -void terrain_render( const TerrainManager * tm, glm::mat4 VP, float sun_slope ) { +void terrain_render( const TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float sun_slope ) { glm::vec3 sun = glm::normalize( glm::vec3( 1, 0, -sun_slope ) ); glUseProgram( tm->shader ); + glUniformMatrix4fv( tm->un_v, 1, GL_FALSE, glm::value_ptr( V ) ); glUniformMatrix4fv( tm->un_vp, 1, GL_FALSE, glm::value_ptr( VP ) ); glUniform3fv( tm->un_sun, 1, glm::value_ptr( sun ) ); diff --git a/terrain_manager.h b/terrain_manager.h @@ -30,6 +30,7 @@ struct TerrainManager { GLint at_normal; GLint at_lit; + GLint un_v; GLint un_vp; GLint un_sun; @@ -47,7 +48,7 @@ struct TerrainManager { void terrain_init( TerrainManager * tm, const char * tiles_dir, MemoryArena * arena ); void terrain_teleport( TerrainManager * tm, glm::vec3 position ); void terrain_update( TerrainManager * tm, glm::vec3 position ); -void terrain_render( const TerrainManager * tm, glm::mat4 VP, float sun_slope ); +void terrain_render( const TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float sun_slope ); float terrain_height( const TerrainManager * tm, glm::vec3 position ); #endif // _TERRAIN_MANAGER_H_