X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=world_gen.h;h=9ee1e6beb3329d7403f4602a8f3fe5d33d87271e;hb=166bd43e677bb4406197407a381b27058582c548;hp=16d64fdefdf8a53af2a1cbb98b28eed71d43702a;hpb=be6707a307bfeec1b45cca8b3fb647e81262be87;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/world_gen.h b/world_gen.h index 16d64fd..9ee1e6b 100644 --- a/world_gen.h +++ b/world_gen.h @@ -34,7 +34,7 @@ VG_STATIC void world_add_all_if_material( m4x3f transform, scene *pscene, } /* Sprinkle foliage models over the map on terrain material */ -VG_STATIC void world_apply_procedural_foliage(void) +VG_STATIC void world_apply_procedural_foliage( struct world_material *mat ) { vg_linear_clear( vg_mem.scratch ); @@ -61,8 +61,8 @@ VG_STATIC void world_apply_procedural_foliage(void) if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit )) { - if( (hit.normal[1] > 0.8f) && ray_hit_is_terrain(&hit) && - (hit.pos[1] > 0.0f+10.0f) ) + struct world_material *m1 = ray_hit_material( &hit ); + if((hit.normal[1] > 0.8f) && (m1 == mat) && (hit.pos[1] > 0.0f+10.0f)) { v4f qsurface, qrandom; v3f axis; @@ -173,22 +173,8 @@ VG_STATIC void world_pct_water( mdl_node *pnode ) return; } - mdl_submesh *sm = mdl_node_submesh( world.meta, pnode, 0 ); - - if( sm ) - { - vg_acquire_thread_sync(); - { - mdl_unpack_submesh( world.meta, &world.mesh_water, sm ); - world.water.enabled = 1; - water_set_surface( pnode->co[1] ); - } - vg_release_thread_sync(); - } - else - { - vg_warn( "Water entity has no submeshes!\n" ); - } + world.water.enabled = 1; + water_set_surface( pnode->co[1] ); } VG_STATIC void world_pct_audio( mdl_node *pnode ) @@ -322,37 +308,7 @@ VG_STATIC void world_generate(void) /* * Compile meshes into the world scenes */ - world.scene_geo = scene_init( world.dynamic_vgl, 350000, 1200000 ); - - /* - * TODO: System to dynamically allocate these - */ - u32 mat_surf = 0, - mat_surf_oob = 0, - mat_vertex_blend = 0, - mat_alphatest = 0, - mat_graffiti = 0, - mat_subworld = 0, - mat_terrain = 0; - - for( int i=1; iinfo.material_count; i++ ) - { - mdl_material *mat = &world.meta->material_buffer[ i ]; - const char *mat_name = mdl_pstr( world.meta, mat->pstr_name ); - - if( !strcmp( "surf", mat_name )) - mat_surf = i; - else if( !strcmp( "surf_oob", mat_name )) - mat_surf_oob = i; - else if( !strcmp( "vertex_blend", mat_name )) - mat_vertex_blend = i; - else if( !strcmp( "alphatest", mat_name )) - mat_alphatest = i; - else if( !strcmp( "graffitibox", mat_name )) - mat_graffiti = i; - else if( !strcmp( "terrain", mat_name ) ) - mat_terrain = i; - } + world.scene_geo = scene_init( world.dynamic_vgl, 320000, 1200000 ); m4x3f midentity; m4x3_identity( midentity ); @@ -363,36 +319,20 @@ VG_STATIC void world_generate(void) */ vg_info( "Generating collidable geometry\n" ); - vg_info( "terrain...\n" ); - /* terrain */ - if( mat_terrain ) - world_add_all_if_material( midentity, world.scene_geo, - world.meta, mat_terrain ); - scene_copy_slice( world.scene_geo, &world.sm_terrain ); - - /* oob */ - vg_info( "oob...\n" ); - if( mat_surf_oob ) - world_add_all_if_material( midentity, world.scene_geo, - world.meta, mat_surf_oob ); - else - vg_warn( "No OOB surface\n" ); - scene_copy_slice( world.scene_geo, &world.sm_geo_std_oob ); - - - /* surface */ - vg_info( "surface...\n" ); - if( mat_surf ) - world_add_all_if_material( midentity, world.scene_geo, - world.meta, mat_surf ); - scene_copy_slice( world.scene_geo, &world.sm_geo_std ); - - /* vertex_blend */ - vg_info( "vertex blend...\n" ); - if( mat_vertex_blend ) - world_add_all_if_material( midentity, world.scene_geo, - world.meta, mat_vertex_blend); - scene_copy_slice( world.scene_geo, &world.sm_geo_vb ); + + + for( int i=0; iinfo.flags & k_material_flag_collision ) + world_add_all_if_material( midentity, world.scene_geo, world.meta, i ); + + scene_copy_slice( world.scene_geo, &mat->sm_geo ); + } + + + /* compress that bad boy */ world.scene_geo = scene_fix( world.dynamic_vgl, world.scene_geo ); @@ -422,21 +362,28 @@ VG_STATIC void world_generate(void) world.scene_no_collide = scene_init( world.dynamic_vgl, 200000, 500000 ); +#if 0 vg_info( "Applying foliage\n" ); srand(0); world_apply_procedural_foliage(); scene_copy_slice( world.scene_no_collide, &world.sm_foliage_main ); +#endif + + for( int i=0; iinfo.flags & k_material_flag_collision) ) + { + world_add_all_if_material( midentity, world.scene_no_collide, + world.meta, i ); + } - vg_info( "graffiti...\n" ); - world_add_all_if_material( midentity, world.scene_no_collide, - world.meta, mat_graffiti ); - scene_copy_slice( world.scene_no_collide, &world.sm_graffiti ); + if( mat->info.flags & k_material_flag_grow_grass ) + world_apply_procedural_foliage( mat ); + scene_copy_slice( world.scene_no_collide, &mat->sm_no_collide ); + } /* upload and free that */ vg_acquire_thread_sync(); @@ -546,14 +493,71 @@ VG_STATIC void world_post_process(void) reset_player( 1, (const char *[]){"start"} ); } +VG_STATIC void world_process_resources(void) +{ + vg_info( "Loading textures\n" ); + world.texture_count = world.meta->info.texture_count; + world.textures = + vg_linear_alloc( world.dynamic_vgl, sizeof(GLuint)*world.texture_count ); + + vg_acquire_thread_sync(); + { + /* error texture */ + world.textures[0] = vg_tex2d_new(); + vg_tex2d_set_error(); + vg_tex2d_nearest(); + vg_tex2d_repeat(); + + for( int i=1; itexture_buffer[i]; + + if( !tex->pack_offset ) + { + vg_release_thread_sync(); + vg_fatal_exit_loop( "World models must have packed textures!" ); + } + + vg_linear_clear( vg_mem.scratch ); + world.textures[i] = vg_tex2d_new(); + vg_tex2d_set_error(); + vg_tex2d_qoi( world.meta->pack + tex->pack_offset, tex->pack_length, + mdl_pstr( world.meta, tex->pstr_name )); + vg_tex2d_nearest(); + vg_tex2d_repeat(); + } + } + vg_release_thread_sync(); + + vg_info( "Loading materials\n" ); + + u32 size = sizeof(struct world_material) * world.meta->info.material_count; + world.materials = vg_linear_alloc( world.dynamic_vgl, size ); + + world.material_count = world.meta->info.material_count; + memset( world.materials, 0, size ); + + for( int i=1; imaterial_buffer[i]; + + /* error material */ + struct world_material *errmat = &world.materials[0]; + v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour ); + v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour1 ); + errmat->info.flags = 0x00; + errmat->info.pstr_name = 0; /* useless? */ + errmat->info.shader = -1; + errmat->info.tex_decal = 0; + errmat->info.tex_diffuse = 0; + errmat->info.tex_normal = 0; +} VG_STATIC void world_unload(void) { /* free meshes */ + mesh_free( &world.mesh_route_lines ); mesh_free( &world.mesh_geo ); mesh_free( &world.mesh_no_collide ); - mesh_free( &world.mesh_route_lines ); - mesh_free( &world.mesh_water ); world.time = 0.0; world.rewind_from = 0.0; @@ -575,16 +579,24 @@ VG_STATIC void world_unload(void) uib->xpos = 0.0f; } + /* delete textures and meshes */ + glDeleteTextures( world.texture_count, world.textures ); + /* delete the entire block of memory */ vg_linear_clear( world.dynamic_vgl ); vg_linear_clear( world.audio_vgl ); /* clean dangling pointers */ world.meta = NULL; + + world.textures = NULL; + world.texture_count = 0; + world.materials = NULL; + world.material_count = 0; world.scene_geo = NULL; - world.scene_lines = NULL; world.scene_no_collide = NULL; + world.scene_lines = NULL; world.geo_bh = NULL; world.trigger_bh = NULL; @@ -627,6 +639,9 @@ VG_STATIC void world_load(void) world.meta = mdl_load_full( world.dynamic_vgl, world.world_name ); vg_info( "Loading world: %s\n", world.world_name ); + /* process resources from pack */ + world_process_resources(); + /* dynamic allocations */ world_ents_allocate(); world_routes_allocate();