commit 10e75b1c7ff4156a9a21c3a7f2050ccaad64873d parent 272b266fa67e3d3116ddf131f734a29f77342f69 Author: Michael Savage <mikejsavage@gmail.com> Date: Tue Feb 2 19:41:40 +0000 Add initial LOD code Diffstat:
terrain_manager.cc | | | 33 | +++++++++++++++++++++++++++++++++ |
diff --git a/terrain_manager.cc b/terrain_manager.cc @@ -156,6 +156,31 @@ 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 +) { + 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 +) { + 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 ); + } + } + + memcpy( tm->lods, new_lods, WORLD_SIZE * WORLD_SIZE ); +} + void terrain_update( TerrainManager * tm, glm::vec3 position ) { u32 player_tile_x = position.x / TILE_SIZE; u32 player_tile_y = position.y / TILE_SIZE; @@ -172,6 +197,8 @@ void terrain_update( TerrainManager * tm, glm::vec3 position ) { } 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 @@ -184,6 +211,8 @@ void terrain_update( TerrainManager * tm, glm::vec3 position ) { } tm->tile_x--; tm->view_left = view_sub( tm->view_left, 1 ); + + terrain_update_lods( tm, tm->tile_x, tm->tile_y ); } } @@ -199,6 +228,8 @@ void terrain_update( TerrainManager * tm, glm::vec3 position ) { } 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 @@ -211,6 +242,8 @@ void terrain_update( TerrainManager * tm, glm::vec3 position ) { } tm->tile_y--; tm->view_top = view_sub( tm->view_top, 1 ); + + terrain_update_lods( tm, tm->tile_x, tm->tile_y ); } } }