medfall

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

commit 5c84cb1cd2daa34d04ce52c62db7494ce1325786
parent e519c8953eed9f3ad4ac31f4c446650e3ea15c83
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri Dec  9 20:41:57 +0200

Make pp generate quadtrees

Diffstat:
pp.cc | 50++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 48 insertions(+), 2 deletions(-)
diff --git a/pp.cc b/pp.cc @@ -11,6 +11,7 @@ #include "array.h" #include "str.h" #include "stb_image.h" +#include "heightmap.h" #include "platform_io.h" #include "linear_algebra.h" @@ -105,6 +106,8 @@ static void write_compressed_file( const void * data, size_t len, const char * f size_t compressed_len = LZ4_compress_HC( ( const char * ) data, compressed, len, LZ4_compressBound( len ), COMPRESSION_LEVEL ); + printf( "compressed %zu to %zu (%.2f%%)\n", len, compressed_len, 100.0f * compressed_len / len ); + // write FILE * file = fopen( path.c_str(), "wb" ); ASSERT( file != NULL ); @@ -141,12 +144,46 @@ static void write_tile( } } - printf( "writing %d_%d%s\n", tx, ty, extension.c_str() ); + printf( "writing %d_%d%s... ", tx, ty, extension.c_str() ); write_compressed_file( tile.ptr(), tile.num_bytes(), "%s/%d_%d%s", dir.c_str(), tx, ty, extension.c_str() ); } -static u8 arena_memory[ megabytes( 16 ) ]; +static void write_tile_quadtree( + MemoryArena * arena, const std::string & dir, + const array2d< u8 > heightmap, int tx, int ty +) { + MEMARENA_SCOPED_CHECKPOINT( arena ); + array2d< u8 > tile_heightmap = memarena_push_array2d( arena, u8, OUT_SIZE, OUT_SIZE ); + + const u32 left = tx * TILE_SIZE; + const u32 top = ty * TILE_SIZE; + + for( int local_y = 0; local_y < OUT_SIZE; local_y++ ) { + for( int local_x = 0; local_x < OUT_SIZE; local_x++ ) { + int global_x = left + local_x; + int global_y = top + local_y; + + if( heightmap.in_range( global_x, global_y ) ) { + tile_heightmap( local_x, local_y ) = heightmap( global_x, global_y ); + } + else { + tile_heightmap( local_x, local_y ) = 0; + } + } + } + + Heightmap hm; + heightmap_init( &hm, ( u8 * ) tile_heightmap.ptr(), OUT_SIZE, OUT_SIZE ); + + size_t num_nodes = heightmap_quadtree_max_nodes( &hm ); + array< HeightmapQuadTreeNode > nodes = memarena_push_array( arena, HeightmapQuadTreeNode, num_nodes ); + HeightmapQuadTree qt = heightmap_build_quadtree( &hm, nodes ); + + printf( "writing %d_%d_quadtree.lz4... ", tx, ty ); + write_compressed_file( qt.nodes.ptr(), qt.nodes.num_bytes(), + "%s/%d_%d_quadtree.lz4", dir.c_str(), tx, ty ); +} #if PLATFORM_LINUX #include "autogdb.h" @@ -157,6 +194,7 @@ int main( int argc, char ** argv ) { install_debug_signal_handlers( true ); #endif + static u8 arena_memory[ megabytes( 64 ) ]; MemoryArena arena; memarena_init( &arena, arena_memory, ARRAY_COUNT( arena_memory ) ); @@ -215,6 +253,14 @@ int main( int argc, char ** argv ) { write_tile( &arena, dir, "_normals.lz4", normals, v3( 0.0f, 0.0f, 1.0f ), tx, ty ); } } + + printf( "generating collision quadtrees\n" ); + { + for( int ty = 0; ty < h / TILE_SIZE; ty++ ) { + for( int tx = 0; tx < w / TILE_SIZE; tx++ ) { + write_tile_quadtree( &arena, dir, heightmap, tx, ty ); + } + } } stbi_image_free( heightmap_memory );