medfall

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

commit d7116799ba151092a467a1aa26e4558bf91d1d57
parent e45c9898abff3eaef982727d7836e5222246ca30
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon Sep  5 22:06:47 -0700

Draw BTTs in the terrain manager. heightmap.cc still needs some cleanup

Diffstat:
heightmap.cc | 106-------------------------------------------------------------------------------
terrain_manager.cc | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
terrain_manager.h | 13+++++++++----
3 files changed, 68 insertions(+), 136 deletions(-)
diff --git a/heightmap.cc b/heightmap.cc @@ -50,116 +50,10 @@ void heightmap_init( hm->pixels = pixels; hm->width = width; hm->height = height; - - MEMARENA_SCOPED_CHECKPOINT( arena ); - - GLfloat * const vertices = memarena_push_many( arena, GLfloat, width * height * 3 ); - GLfloat * const normals = memarena_push_many( arena, GLfloat, width * height * 3 ); - GLuint * const indices = memarena_push_many( arena, GLuint, width * height * 6 ); - GLfloat * const lit = memarena_push_many( arena, GLfloat, width * height ); - - for( u32 i = 0; i < width * height; i++ ) { - lit[ i ] = 0; - } - - for( u32 y = 0; y < height; y++ ) { - lit[ y * width ] = -1; - - for( u32 x = 1; x < width; x++ ) { - const float h = hm->point( x, y ).z; - const float al = lit[ y * width + x - 1 ]; - float dh; - - if( al == -1 ) { - dh = hm->point( x - 1, y ).z - ( h + SLOPE ); - } - else { - dh = al - ( h + SLOPE ); - } - - if( dh > 0 ) { - lit[ y * width + x ] = h + dh; - } - else { - lit[ y * width + x ] = -1; - } - } - } - - for( u32 y = 0; y < height; y++ ) { - for( u32 x = 0; x < width; x++ ) { - const u32 i = y * width + x; - - lit[ i ] = lit[ i ] == -1 ? 1 : 0; - } - } - - for( u32 y = 0; y < height; y++ ) { - for( u32 x = 0; x < width; x++ ) { - const u32 base = 3 * ( y * width + x ); - const float height = hm->point( x, y ).z; - - vertices[ base ] = x + ox; - vertices[ base + 1 ] = y + oy; - vertices[ base + 2 ] = height; - - const glm::vec3 normal = hm->point_normal( x, y ); - - normals[ base ] = normal.x; - normals[ base + 1 ] = normal.y; - normals[ base + 2 ] = normal.z; - } - } - - for( u32 y = 0; y < height - 1; y++ ) { - for( u32 x = 0; x < width - 1; x++ ) { - const u32 base = 6 * ( y * width + x ); - - indices[ base + 0 ] = ( y + 0 ) * width + ( x + 0 ); - indices[ base + 1 ] = ( y + 1 ) * width + ( x + 0 ); - indices[ base + 2 ] = ( y + 0 ) * width + ( x + 1 ); - indices[ base + 3 ] = ( y + 1 ) * width + ( x + 1 ); - indices[ base + 4 ] = ( y + 0 ) * width + ( x + 1 ); - indices[ base + 5 ] = ( y + 1 ) * width + ( x + 0 ); - } - } - - glGenVertexArrays( 1, &hm->vao ); - glBindVertexArray( hm->vao ); - - glGenBuffers( 1, &hm->vbo_verts ); - glBindBuffer( GL_ARRAY_BUFFER, hm->vbo_verts ); - glBufferData( GL_ARRAY_BUFFER, width * height * sizeof( GLfloat ) * 3, vertices, GL_STATIC_DRAW ); - glEnableVertexAttribArray( at_pos ); - glVertexAttribPointer( at_pos, 3, GL_FLOAT, GL_FALSE, 0, 0 ); - - glGenBuffers( 1, &hm->vbo_normals ); - glBindBuffer( GL_ARRAY_BUFFER, hm->vbo_normals ); - glBufferData( GL_ARRAY_BUFFER, width * height * sizeof( GLfloat ) * 3, normals, GL_STATIC_DRAW ); - glEnableVertexAttribArray( at_normal ); - glVertexAttribPointer( at_normal, 3, GL_FLOAT, GL_FALSE, 0, 0 ); - - glGenBuffers( 1, &hm->vbo_lit ); - glBindBuffer( GL_ARRAY_BUFFER, hm->vbo_lit ); - glBufferData( GL_ARRAY_BUFFER, width * height * sizeof( GLfloat ), lit, GL_STATIC_DRAW ); - glEnableVertexAttribArray( at_lit ); - glVertexAttribPointer( at_lit, 1, GL_FLOAT, GL_FALSE, 0, 0 ); - - glGenBuffers( 1, &hm->ebo ); - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, hm->ebo ); - glBufferData( GL_ELEMENT_ARRAY_BUFFER, width * height * sizeof( GLuint ) * 6, indices, GL_STATIC_DRAW ); - - glBindVertexArray( 0 ); } void heightmap_destroy( Heightmap * const hm ) { if( hm->vbo_verts != 0 ) { - glDeleteBuffers( 1, &hm->vbo_verts ); - glDeleteBuffers( 1, &hm->vbo_normals ); - glDeleteBuffers( 1, &hm->vbo_lit ); - glDeleteBuffers( 1, &hm->ebo ); - glDeleteVertexArrays( 1, &hm->vao ); - free( hm->pixels ); hm->vbo_verts = 0; diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -11,56 +11,81 @@ #include "terrain_manager.h" #include "memory_arena.h" #include "work_queue.h" +#include "btt.h" +#include "gpubtt.h" #include "lz4.h" -static const GLchar * vert_src = GLSL( +static const GLchar * const vert_src = GLSL( in vec3 position; - in vec3 normal; - in float lit; - out vec3 n; out vec4 pos; - out float l; + out vec3 smooth_position; 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 ); + smooth_position = position; + pos = v * vec4( position, 1.0 ); } ); static const GLchar * frag_src = GLSL( - in vec3 n; in vec4 pos; - in float l; + in vec3 smooth_position; out vec4 colour; - uniform vec3 sun; + uniform float sun; + uniform sampler2D normals; + uniform sampler2D horizons; + uniform vec2 dimensions; void main() { + vec3 normal = normalize( texture( normals, smooth_position.xy / dimensions ).xyz ); + vec3 ground; - if( n.z > 0.9 ) { + if( normal.z > 0.9 ) { ground = vec3( 0.4, 1.0, 0.4 ); } else { ground = vec3( 0.7, 0.7, 0.5 ); } - float d = max( 0, -dot( n, sun ) ); + vec3 sunv = normalize( vec3( 1, 1, -sun ) ); + float horizon = texture( horizons, smooth_position.xy / dimensions ).x; + float l = sun > horizon ? 1.0 : 0.0; + + float d = max( 0, -dot( normal, sunv ) ); + // float light = max( 0.2, l * ( d * 0 + 1 ) ); float light = max( 0.2, l * d ); vec3 fog = vec3( 0.6, 0.6, 0.6 ); float depth = length( pos ); - float t = smoothstep( 200, 400, depth ); + float t = smoothstep( 900, 1200, depth ) * 0.6; colour = vec4( ( 1.0 - t ) * ground * light + t * fog, 1.0 ); + // colour *= 0; + // colour = vec4( horizon, horizon, horizon, 1.0 ); + + // if( smooth_position.y > 10.5 && smooth_position.y < 11.5 ) { + // colour.r = 1; + // } + // + // int a = int( smooth_position.x ) / 10; + // if( a % 2 == 0 ) colour.g = 1; + + // place a light + vec3 light_position = vec3( 500.0, 1500.0, 50.0 ); + vec3 light_colour = vec3( 15.0, 0.0, 15.0 ); + float light_sqdistance = dot( light_position - smooth_position, light_position - smooth_position ); + vec3 light_direction = normalize( light_position - smooth_position ); + float lambert_scale = dot( light_direction, normal ); + float distance_scale = 1.0 / sqrt( light_sqdistance ); + colour += vec4( light_colour * lambert_scale * distance_scale, 0.0 ); } ); @@ -70,8 +95,12 @@ static void terrain_load_tile( ) { Heightmap * hm = &tm->tiles[ vx ][ vy ]; - if( hm->vbo_verts != 0 ) heightmap_destroy( hm ); + if( hm->vbo_verts != 0 ) { + heightmap_destroy( hm ); + gpubtt_destroy( &tm->btt_tiles[ vx ][ vy ] ); + } + // if( tx >= WORLD_SIZE || ty >= WORLD_SIZE ) return; if( tx * TILE_SIZE >= tm->width || ty * TILE_SIZE >= tm->height ) return; CompressedTile ct = tm->compressed_tiles[ tx ][ ty ]; @@ -86,7 +115,11 @@ static void terrain_load_tile( heightmap_init( hm, tm->arena, pixels, width, height, tx * TILE_SIZE, ty * TILE_SIZE, - tm->at_pos, tm->at_normal, tm->at_lit ); + tm->at_position, tm->at_normal, tm->at_lit ); + MEMARENA_SCOPED_CHECKPOINT( tm->arena ); + BTTs btts = btt_from_heightmap( hm, tm->arena ); + const OffsetHeightmap ohm = { *hm, tx * TILE_SIZE, ty * TILE_SIZE }; + gpubtt_init( tm->arena, &tm->btt_tiles[ vx ][ vy ], &ohm, btts, tm->at_position ); } void terrain_init( @@ -118,13 +151,12 @@ void terrain_init( tm->shader = compile_shader( vert_src, frag_src, "colour" ); - tm->at_pos = glGetAttribLocation( tm->shader, "position" ); - 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->at_position = glGetAttribLocation( tm->shader, "position" ); + tm->un_V = glGetUniformLocation( tm->shader, "v" ); + tm->un_VP = glGetUniformLocation( tm->shader, "vp" ); tm->un_sun = glGetUniformLocation( tm->shader, "sun" ); + tm->un_normals = glGetUniformLocation( tm->shader, "normals" ); + tm->un_dimensions = glGetUniformLocation( tm->shader, "dimensions" ); tm->first_teleport = true; tm->view_left = tm->view_top = 0; @@ -263,13 +295,14 @@ void terrain_render( const TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float 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 ) ); + glUniformMatrix4fv( tm->un_V, 1, GL_FALSE, glm::value_ptr( V ) ); + glUniformMatrix4fv( tm->un_VP, 1, GL_FALSE, glm::value_ptr( VP ) ); + glUniform1f( tm->un_sun, 0.3 ); + glUniform2f( tm->un_dimensions, TILE_SIZE, TILE_SIZE ); for( u16 vy = 0; vy < VIEW_SIZE; vy++ ) { for( u16 vx = 0; vx < VIEW_SIZE; vx++ ) { - tm->tiles[ vx ][ vy ].render(); + gpubtt_render( &tm->btt_tiles[ vx ][ vy ], tm->un_normals ); } } diff --git a/terrain_manager.h b/terrain_manager.h @@ -7,6 +7,7 @@ #include "intrinsics.h" #include "memory_arena.h" #include "heightmap.h" +#include "gpubtt.h" static const u16 TILE_SIZE = 128; static const u16 WORLD_SIZE = 170; @@ -26,13 +27,16 @@ struct TerrainManager { GLuint shader; - GLint at_pos; + GLint at_position; + GLint at_colour; + GLint un_V; + GLint un_VP; + GLint at_normal; GLint at_lit; - - GLint un_v; - GLint un_vp; GLint un_sun; + GLint un_normals; + GLint un_dimensions; bool first_teleport; @@ -40,6 +44,7 @@ struct TerrainManager { // view_x and view_y are indices into tiles of the tile we are centered on CompressedTile compressed_tiles[ WORLD_SIZE ][ WORLD_SIZE ]; Heightmap tiles[ WORLD_SIZE ][ WORLD_SIZE ]; + GPUBTT btt_tiles[ WORLD_SIZE ][ WORLD_SIZE ]; u8 lods[ WORLD_SIZE ][ WORLD_SIZE ]; s16 tile_x, tile_y; s16 view_left, view_top;