terrain_manager.h (1954B)
1 #pragma once 2 3 #include "intrinsics.h" 4 #include "array.h" 5 #include "memory_arena.h" 6 #include "heightmap.h" 7 #include "gpubtt.h" 8 #include "work_queue.h" 9 #include "linear_algebra.h" 10 #include "mpsc.h" 11 12 static const u16 TILE_SIZE = 512; 13 static const u16 WORLD_SIZE = 64; 14 15 static const u16 VIEW_SIZE = 11; 16 static const u16 VIEW_HALF = VIEW_SIZE / 2; 17 18 struct CompressedTile { 19 array< char > heightmap; 20 array< char > normalmap; 21 array< u8 > horizonmap; 22 array< char > quadtree; 23 }; 24 25 struct GPUTileData { 26 BTTs btt; 27 void * btt_memory; 28 u8 * horizonmap; 29 size_t horizonmap_size; 30 }; 31 32 struct DecompressedTile { 33 array2d< u16 > heightmap; 34 v3 * normalmap; 35 QuadTree quadtree; 36 }; 37 38 enum TileState { 39 TILE_EMPTY, 40 TILE_LOADING, 41 TILE_LOADED, 42 }; 43 44 struct ReadyTile { 45 s32 tx, ty; 46 }; 47 48 struct TerrainManager { 49 MemoryArena * arena; 50 WorkQueue * background_tasks; 51 52 u32 width, height; 53 54 TB point_light_origins; 55 TB point_light_colours; 56 57 bool first_teleport; 58 // tile_x and tile_y are the coordinates of the tile we are centered on 59 s32 tile_x, tile_y; 60 61 CompressedTile compressed_tiles[ WORLD_SIZE ][ WORLD_SIZE ]; 62 DecompressedTile decompressed_tiles[ WORLD_SIZE ][ WORLD_SIZE ]; 63 TileState tile_states[ WORLD_SIZE ][ WORLD_SIZE ]; 64 65 // holds tile data that needs to be moved to the gpu 66 GPUTileData gpu_tiles[ WORLD_SIZE ][ WORLD_SIZE ]; 67 68 // opengl handles 69 GPUBTT gpubtts[ WORLD_SIZE ][ WORLD_SIZE ]; 70 71 FixedMPSC< ReadyTile, 32 > ready_tiles; 72 }; 73 74 void terrain_init( TerrainManager * tm, const char * tiles_dir, MemoryArena * arena, WorkQueue * background_tasks ); 75 void terrain_teleport( TerrainManager * tm, v3 position ); 76 void terrain_update( TerrainManager * tm, v3 position ); 77 void terrain_render( TerrainManager * tm, const m4 & V, const m4 & P, float sun_angle, v3 sun_dir, double current_time ); 78 v3 terrain_normal( const TerrainManager * tm, v3 position ); 79 80 bool segment_vs_terrain( const TerrainManager * tm, v3 seg_origin, v3 seg_end, float * t, v3 * xnormal );