commit 73fec546f676da35c5e409f7ce754c0464929c8f parent 90145b4ac5063d7d4013fe94fffa764ae8cfe35e Author: Michael Savage <mikejsavage@gmail.com> Date: Tue Sep 6 21:45:05 -0700 Remove the ring buffer mess from TerrainManager Diffstat:
terrain_manager.cc | | | 118 | +++++++++++++++++++++++++------------------------------------------------------ |
terrain_manager.h | | | 6 | ++---- |
diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -97,19 +97,17 @@ static const GLchar * frag_src = GLSL( } ); -static void terrain_load_tile( - TerrainManager * tm, u32 tx, u32 ty, - u32 vx, u32 vy -) { - Heightmap * hm = &tm->tiles[ vx ][ vy ]; +static void terrain_load_tile( TerrainManager * tm, s32 tx, s32 ty ) { + Heightmap * hm = &tm->tiles[ tx ][ ty ]; if( hm->vbo_verts != 0 ) { heightmap_destroy( hm ); - gpubtt_destroy( &tm->btt_tiles[ vx ][ vy ] ); + gpubtt_destroy( &tm->btt_tiles[ tx ][ ty ] ); } // if( tx >= WORLD_SIZE || ty >= WORLD_SIZE ) return; - if( tx * TILE_SIZE >= tm->width || ty * TILE_SIZE >= tm->height ) return; + if( tx < 0 || ty < 0 ) return; + if( ( u32 ) tx * TILE_SIZE >= tm->width || ( u32 ) ty * TILE_SIZE >= tm->height ) return; CompressedTile ct = tm->compressed_tiles[ tx ][ ty ]; int width, height; @@ -127,7 +125,7 @@ static void terrain_load_tile( 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 ); + gpubtt_init( tm->arena, &tm->btt_tiles[ tx ][ ty ], &ohm, btts, tm->at_position ); } void terrain_init( @@ -181,22 +179,15 @@ void terrain_init( glGenTextures( 1, &tm->tex_point_light_colours ); tm->first_teleport = true; - tm->view_left = tm->view_top = 0; } void terrain_teleport( TerrainManager * tm, glm::vec3 position ) { - if( !tm->first_teleport ) { - for( u32 vy = 0; vy < VIEW_SIZE; vy++ ) { - for( u32 vx = 0; vx < VIEW_SIZE; vx++ ) { - heightmap_destroy( &tm->tiles[ vx ][ vy ] ); - } - } - } + assert( tm->first_teleport ); tm->first_teleport = false; - u32 player_tile_x = position.x / TILE_SIZE; - u32 player_tile_y = position.y / TILE_SIZE; + s32 player_tile_x = position.x / TILE_SIZE; + s32 player_tile_y = position.y / TILE_SIZE; tm->tile_x = player_tile_x; tm->tile_y = player_tile_y; @@ -205,40 +196,27 @@ void terrain_teleport( TerrainManager * tm, glm::vec3 position ) { for( u32 vx = 0; vx < VIEW_SIZE; vx++ ) { terrain_load_tile( tm, vx + player_tile_x - VIEW_HALF, - vy + player_tile_y - VIEW_HALF, - vx, vy ); + vy + player_tile_y - VIEW_HALF ); } } } -static u32 view_sub( u32 v, u32 o ) { - if( v >= o ) return v - o; - return VIEW_SIZE - ( o - v ); -} - -static u32 view_add( u32 v, u32 o ) { - return ( v + o ) % VIEW_SIZE; -} - static u8 terrain_tile_lod( - s16 player_tile_x, s16 player_tile_y, - s16 tx, s16 ty + s32 player_tile_x, s32 player_tile_y, + s32 tx, s32 ty ) { u16 manhattan_distance = abs( player_tile_x - tx ) + abs( player_tile_y - ty ); return min( ( u16 ) 8, manhattan_distance ); } -static void terrain_update_lods( - TerrainManager * tm, - s16 player_tile_x, s16 player_tile_y -) { +static void terrain_update_lods( TerrainManager * tm ) { u8 new_lods[ WORLD_SIZE ][ WORLD_SIZE ]; for( u16 tx = 0; tx < WORLD_SIZE; tx++ ) { for( u16 ty = 0; ty < WORLD_SIZE; ty++ ) { new_lods[ tx ][ ty ] = terrain_tile_lod( - player_tile_x, player_tile_y, tx, ty ); + tm->tile_x, tm->tile_y, tx, ty ); } } @@ -246,8 +224,8 @@ static void terrain_update_lods( } void terrain_update( TerrainManager * tm, glm::vec3 position ) { - s16 player_tile_x = position.x / TILE_SIZE; - s16 player_tile_y = position.y / TILE_SIZE; + s32 player_tile_x = position.x / TILE_SIZE; + s32 player_tile_y = position.y / TILE_SIZE; if( player_tile_x != tm->tile_x ) { if( player_tile_x > tm->tile_x ) { @@ -255,28 +233,19 @@ void terrain_update( TerrainManager * tm, glm::vec3 position ) { for( u32 vy = 0; vy < VIEW_SIZE; vy++ ) { terrain_load_tile( tm, tm->tile_x + VIEW_HALF + 1, - tm->tile_y + vy - VIEW_HALF, - view_add( tm->view_left, VIEW_SIZE ), - view_add( tm->view_top, vy ) ); + tm->tile_y + vy - VIEW_HALF ); } tm->tile_x++; - tm->view_left = view_add( tm->view_left, 1 ); - - terrain_update_lods( tm, tm->tile_x, tm->tile_y ); } else { // -x boundary for( u32 vy = 0; vy < VIEW_SIZE; vy++ ) { terrain_load_tile( tm, tm->tile_x - VIEW_HALF - 1, - tm->tile_y + vy - VIEW_HALF, - view_sub( tm->view_left, 1 ), - view_add( tm->view_top, vy ) ); + tm->tile_y + vy - VIEW_HALF ); } tm->tile_x--; - tm->view_left = view_sub( tm->view_left, 1 ); - terrain_update_lods( tm, tm->tile_x, tm->tile_y ); } } @@ -286,29 +255,21 @@ void terrain_update( TerrainManager * tm, glm::vec3 position ) { for( u32 vx = 0; vx < VIEW_SIZE; vx++ ) { terrain_load_tile( tm, tm->tile_x + vx - VIEW_HALF, - tm->tile_y + VIEW_HALF + 1, - view_add( tm->view_left, vx ), - view_add( tm->view_top, VIEW_SIZE ) ); + tm->tile_y + VIEW_HALF + 1 ); } tm->tile_y++; - tm->view_top = view_add( tm->view_top, 1 ); - - terrain_update_lods( tm, tm->tile_x, tm->tile_y ); } else { // -y boundary for( u32 vx = 0; vx < VIEW_SIZE; vx++ ) { terrain_load_tile( tm, tm->tile_x + vx - VIEW_HALF, - tm->tile_y - VIEW_HALF - 1, - view_add( tm->view_left, vx ), - view_sub( tm->view_top, 1 ) ); + tm->tile_y - VIEW_HALF - 1 ); } tm->tile_y--; - tm->view_top = view_sub( tm->view_top, 1 ); - - terrain_update_lods( tm, tm->tile_x, tm->tile_y ); } + + terrain_update_lods( tm ); } } @@ -322,6 +283,7 @@ void terrain_render( const TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float glUniform1f( tm->un_sun, 0.3 ); glUniform2f( tm->un_dimensions, TILE_SIZE, TILE_SIZE ); + /* start lighting */ float tbo1[ 15 ] = { 500, 1500 + 500 * sin( glfwGetTime() + 3.14 * 1 / 5 ), 50, 600, 1500 + 500 * sin( glfwGetTime() + 3.14 * 2 / 5 ), 50, @@ -351,37 +313,31 @@ void terrain_render( const TerrainManager * tm, glm::mat4 V, glm::mat4 VP, float glBindTexture( GL_TEXTURE_BUFFER, tm->tex_point_light_colours ); glTexBuffer( GL_TEXTURE_BUFFER, GL_RGB32F, tm->tbo_point_light_colours ); glUniform1i( tm->un_point_light_colours, 2 ); + /* end lighting */ for( u16 vy = 0; vy < VIEW_SIZE; vy++ ) { for( u16 vx = 0; vx < VIEW_SIZE; vx++ ) { - gpubtt_render( &tm->btt_tiles[ vx ][ vy ], tm->un_normals ); + s32 tx = vx + tm->tile_x - VIEW_HALF; + s32 ty = vy + tm->tile_y - VIEW_HALF; + if( tx < 0 || ty < 0 ) continue; + gpubtt_render( &tm->btt_tiles[ tx ][ ty ], tm->un_normals ); } } glUseProgram( 0 ); } -// TODO: this is horrible -// it's probably worth using view_x/y instead of view_left/top -// and using signed arithmetic to make this less annoying -// also implement the bounded arithmetic class float terrain_height( const TerrainManager * tm, glm::vec3 position ) { - s16 player_tile_x = position.x / TILE_SIZE; - s16 player_tile_y = position.y / TILE_SIZE; - - if( player_tile_x < tm->tile_x ) assert( tm->tile_x - player_tile_x <= VIEW_HALF ); - else assert( player_tile_x - tm->tile_x <= VIEW_HALF ); - if( player_tile_y < tm->tile_y ) assert( tm->tile_y - player_tile_y <= VIEW_HALF ); - else assert( player_tile_y - tm->tile_y <= VIEW_HALF ); - - s16 dx = player_tile_x - tm->tile_x; - s16 dy = player_tile_y - tm->tile_y; + assert( position.x >= 0 ); + assert( position.y >= 0 ); + assert( position.x < tm->width ); + assert( position.y < tm->height ); - s16 vx = view_add( tm->view_left, VIEW_HALF + dx ); - s16 vy = view_add( tm->view_top, VIEW_HALF + dy ); + s32 tx = position.x / TILE_SIZE; + s32 ty = position.y / TILE_SIZE; - const Heightmap * hm = &tm->tiles[ vx ][ vy ]; + const Heightmap * hm = &tm->tiles[ tx ][ ty ]; - return hm->bilerp_height( position.x - player_tile_x * TILE_SIZE, - position.y - player_tile_y * TILE_SIZE ); + return hm->bilerp_height( position.x - tx * TILE_SIZE, + position.y - ty * TILE_SIZE ); } diff --git a/terrain_manager.h b/terrain_manager.h @@ -54,14 +54,12 @@ struct TerrainManager { bool first_teleport; - // tile_x and tile_y are the coordinates of the tile we are centered on - // 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; + // tile_x and tile_y are the coordinates of the tile we are centered on + s32 tile_x, tile_y; }; void terrain_init( TerrainManager * tm, const char * tiles_dir, MemoryArena * arena );