clean boardshop idea
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gen.h
index 54857f59137ea40c3c34f84ad6004cb8ebcf49aa..79bb754aaf5b6f4d04388a6be663b67782874ec5 100644 (file)
 #ifndef WORLD_GEN_H
 #define WORLD_GEN_H
 
-/* 
- * FUTURE:
- *    If we have multiple levels, write an unloader
- */
-
 #include "world.h"
 
-static void world_add_all_if_material( m4x3f transform, scene *pscene, 
-                                       mdl_header *mdl, u32 id )
-{
-   for( int i=0; i<mdl->node_count; i++ )
-   {
-      mdl_node *pnode = mdl_node_from_id( mdl, i );
+VG_STATIC void world_load( u32 index, const char *path );
 
-      for( int j=0; j<pnode->submesh_count; j++ )
-      {
-         mdl_submesh *sm = mdl_node_submesh( mdl, pnode, j );
+VG_STATIC void world_add_all_if_material( m4x3f transform, scene_context *scene, 
+                                          mdl_context *mdl, u32 id )
+{
+   for( u32 i=0; i<mdl_arrcount(&mdl->meshs); i++ ){
+      mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
 
-         if( sm->material_id == id )
-         {
+      for( u32 j=0; j<mesh->submesh_count; j++ ){
+         mdl_submesh *sm = mdl_arritm( &mdl->submeshs, mesh->submesh_start+j );
+         if( sm->material_id == id ){
             m4x3f transform2;
-            mdl_node_transform( pnode, transform2 );
+            mdl_transform_m4x3( &mesh->transform, transform2 );
             m4x3_mul( transform, transform2, transform2 );
 
-            scene_add_submesh( pscene, mdl, sm, transform2 );
+            scene_add_mdl_submesh( scene, mdl, sm, transform2 );
          }
       }
+   }
+}
 
-      if( pnode->classtype == k_classtype_instance )
-      {
-         if( pnode->sub_uid )
-         {
-            u32 instance_id = pnode->sub_uid -1;
-            struct instance_cache *cache = &world.instance_cache[instance_id];
-            mdl_header *mdl2 = cache->mdl;
+VG_STATIC void world_add_blob( world_instance *world,
+                               scene_context *scene, ray_hit *hit )
+{
+   m4x3f transform;
+   v4f qsurface, qrandom;
+   v3f axis;
 
-            m4x3f transform2;
-            mdl_node_transform( pnode, transform2 );
-            m4x3_mul( transform, transform2, transform2 );
+   v3_cross( (v3f){0.0f,1.0f,0.0f}, hit->normal, axis );
 
-            world_add_all_if_material( transform2, pscene, mdl2, id );
-         }
-      }
+   float angle = v3_dot(hit->normal,(v3f){0.0f,1.0f,0.0f});
+   q_axis_angle( qsurface, axis, angle );
+   q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
+   q_mul( qsurface, qrandom, qsurface );
+   q_m3x3( qsurface, transform );
+   v3_copy( hit->pos, transform[3] );
+
+   scene_vert verts[] = 
+   {
+      { .co = { -1.00f, 0.0f, 0.0f } },
+      { .co = {  1.00f, 0.0f, 0.0f } },
+      { .co = { -1.00f, 1.2f, 0.0f } },
+      { .co = {  1.00f, 1.2f, 0.0f } },
+      { .co = { -0.25f, 2.0f, 0.0f } },
+      { .co = {  0.25f, 2.0f, 0.0f } }
+   };
+
+   const u32 indices[] = { 0,1,3, 0,3,2, 2,3,5, 2,5,4 };
+
+   if( scene->vertex_count + vg_list_size(verts) > scene->max_vertices )
+      vg_fatal_error( "Scene vertex buffer overflow" );
+
+   if( scene->indice_count + vg_list_size(indices) > scene->max_indices )
+      vg_fatal_error( "Scene index buffer overflow" );
+
+   scene_vert *dst_verts = &scene->arrvertices[ scene->vertex_count ];
+   u32 *dst_indices      = &scene->arrindices [ scene->indice_count ];
+
+   scene_vert *ref       = &world->scene_geo.arrvertices[ hit->tri[0] ];
+
+   for( u32 i=0; i<vg_list_size(verts); i++ )
+   {
+      scene_vert *pvert = &dst_verts[ i ],
+                 *src   = &verts[ i ];
+
+      m4x3_mulv( transform, src->co, pvert->co );
+      scene_vert_pack_norm( pvert, transform[1] );
+
+      v2_copy( ref->uv, pvert->uv );
    }
+
+   for( u32 i=0; i<vg_list_size(indices); i++ )
+      dst_indices[i] = indices[i] + scene->vertex_count;
+
+   scene->vertex_count += vg_list_size(verts);
+   scene->indice_count += vg_list_size(indices);
 }
 
-static void world_apply_procedural_foliage(void)
+/* Sprinkle foliage models over the map on terrain material */
+VG_STATIC void world_apply_procedural_foliage( world_instance *world,
+                                               scene_context *scene,
+                                               struct world_surface *mat )
 {
-   mdl_header *mfoliage = mdl_load("models/rs_foliage.mdl");
+   if( vg.quality_profile == k_quality_profile_low )
+      return;
+
+   vg_info( "Applying foliage (%u)\n", mat->info.pstr_name );
 
    v3f volume;
-   v3_sub( world.geo.bbx[1], world.geo.bbx[0], volume );
+   v3_sub( world->scene_geo.bbx[1], world->scene_geo.bbx[0], volume );
    volume[1] = 1.0f;
 
-   m4x3f transform;
-   mdl_node *mblob = mdl_node_from_name( mfoliage, "blob" );
-   mdl_submesh *sm_blob = mdl_node_submesh( mfoliage, mblob, 0 );
+   int count = 0;
 
-   for( int i=0;i<100000;i++ )
-   {
+   float area = volume[0]*volume[2];
+   u32 particles = 0.08f * area;
+
+   vg_info( "Map area: %f. Max particles: %u\n", area, particles );
+
+   for( u32 i=0; i<particles; i++ ){
       v3f pos;
       v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
       pos[1] = 1000.0f;
-      v3_add( pos, world.geo.bbx[0], pos );
+      v3_add( pos, world->scene_geo.bbx[0], pos );
       
       ray_hit hit;
       hit.dist = INFINITY;
 
-      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) )
-         {
-            v4f qsurface, qrandom;
-            v3f axis;
-
-            v3_cross( (v3f){0.0f,1.0f,0.0f}, hit.normal, axis );
-
-            float angle = v3_dot(hit.normal,(v3f){0.0f,1.0f,0.0f});
-            q_axis_angle( qsurface, axis, angle );
-            q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
-            q_mul( qsurface, qrandom, qsurface );
-            q_m3x3( qsurface, transform );
-
-            v3_copy( hit.pos, transform[3] );
-            scene_add_submesh( &world.foliage, mfoliage, sm_blob, transform);
+      if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &hit )){
+         struct world_surface *m1 = ray_hit_surface( world, &hit );
+         if((hit.normal[1] > 0.8f) && (m1 == mat) && (hit.pos[1] > 0.0f+10.0f)){
+            world_add_blob( world, scene, &hit );
+            count ++;
          }
       }
    }
 
-   vg_free( mfoliage );
+   vg_info( "%d foliage models added\n", count );
 }
 
-static void world_load(void)
+VG_STATIC void world_generate( world_instance *world )
 {
-   mdl_header *mworld = mdl_load( "models/mp_dev.mdl" );
-   vg_info( "Loading world: models/mp_dev.mdl\n" );
+   /* 
+    * Compile meshes into the world scenes
+    */
+   scene_init( &world->scene_geo, 320000, 1200000 );
+   u32 buf_size = scene_mem_required( &world->scene_geo );
+   u8 *buffer = vg_linear_alloc( world->heap, buf_size );
+   scene_supply_buffer( &world->scene_geo, buffer );
 
-   world.spawn_count = 0;
-   world.traffic_count = 0;
-   world.instance_cache = NULL;
+   m4x3f midentity;
+   m4x3_identity( midentity );
+
+   /*
+    * Generate scene: collidable geometry
+    * ----------------------------------------------------------------
+    */
+
+   vg_info( "Generating collidable geometry\n" );
    
+   for( u32 i=0; i<world->surface_count; i++ ){
+      struct world_surface *surf = &world->surfaces[ i ];
+
+      if( surf->info.flags & k_material_flag_collision )
+         world_add_all_if_material( midentity, &world->scene_geo, 
+                                    &world->meta, i );
+
+      scene_copy_slice( &world->scene_geo, &surf->sm_geo );
+   }
+
+   /* compress that bad boy */
+   u32 new_vert_max = world->scene_geo.vertex_count,
+       new_vert_size = vg_align8(new_vert_max*sizeof(scene_vert)),
+       new_indice_len = world->scene_geo.indice_count*sizeof(u32);
+
+   u32 *src_indices = world->scene_geo.arrindices,
+       *dst_indices = (u32 *)(buffer + new_vert_size);
+
+   memmove( dst_indices, src_indices, new_indice_len );
+
+   world->scene_geo.max_indices = world->scene_geo.indice_count;
+   world->scene_geo.max_vertices = world->scene_geo.vertex_count;
+   buf_size = scene_mem_required( &world->scene_geo );
+
+   buffer = vg_linear_resize( world->heap, buffer, buf_size );
+
+   world->scene_geo.arrvertices = (scene_vert *)(buffer);
+   world->scene_geo.arrindices = (u32 *)(buffer + new_vert_size);
+
+   scene_upload_async( &world->scene_geo, &world->mesh_geo );
+
+   /* need send off the memory to the gpu before we can create the bvh. */
+   vg_async_stall();
+   vg_info( "creating bvh\n" );
+
+   /* setup spacial mapping and rigidbody */
+   world->geo_bh = scene_bh_create( world->heap, &world->scene_geo );
+
+   v3_zero( world->rb_geo.rb.co );
+   v3_zero( world->rb_geo.rb.v );
+   q_identity( world->rb_geo.rb.q );
+   v3_zero( world->rb_geo.rb.w );
+
+   world->rb_geo.type = k_rb_shape_scene;
+   world->rb_geo.inf.scene.bh_scene = world->geo_bh;
+   rb_init_object( &world->rb_geo );
+
    /*
-    * Process entities
+    * Generate scene: non-collidable geometry
+    * ----------------------------------------------------------------
     */
-   for( int i=0; i<mworld->node_count; i++ )
-   {
-      mdl_node *pnode = mdl_node_from_id( mworld, i );
-      
-      if( pnode->classtype == k_classtype_none )
-      {}
-      else if( pnode->classtype == k_classtype_spawn )
-      {
-         struct respawn_point *rp = &world.spawns[ world.spawn_count ++ ];
-
-         v3_copy( pnode->co, rp->co );
-         v4_copy( pnode->q, rp->q );
-         strcpy( rp->name, mdl_pstr( mworld, pnode->pstr_name ) );
+   vg_info( "Generating non-collidable geometry\n" );
+
+   vg_async_item *call = scene_alloc_async( &world->scene_no_collide,
+                                            &world->mesh_no_collide,
+                                            200000, 500000 );
+
+   for( u32 i=0; i<world->surface_count; i++ ){
+      struct world_surface *surf = &world->surfaces[ i ];
+
+      if( !(surf->info.flags & k_material_flag_collision) ){
+         world_add_all_if_material( midentity, 
+                                    &world->scene_no_collide, &world->meta, i );
       }
-      else if( pnode->classtype == k_classtype_water )
-      {
-         if( wrender.enabled )
-         {
-            vg_warn( "Multiple water surfaces in level! ('%s')\n", 
-                  mdl_pstr( mworld, pnode->pstr_name ));
+
+      if( surf->info.flags & k_material_flag_grow_grass )
+         world_apply_procedural_foliage( world, 
+                                         &world->scene_no_collide, surf );
+
+      scene_copy_slice( &world->scene_no_collide, &surf->sm_no_collide );
+   }
+
+   for( u32 i=0; i<mdl_arrcount( &world->ent_traffic ); i++ ){
+      ent_traffic *vehc = mdl_arritm( &world->ent_traffic, i );
+
+      for( u32 j=0; j<vehc->submesh_count; j++ ){
+         mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, 
+                                       vehc->submesh_start+j );
+
+         if( sm->flags & k_submesh_flag_consumed ){
             continue;
          }
 
-         mdl_submesh *sm = mdl_node_submesh( mworld, pnode, 0 );
-         
-         if( sm )
-         {
-            vg_acquire_thread_sync();
-            {
-               glmesh surf;
-               mdl_unpack_submesh( mworld, &surf, sm );
-               water_set_surface( &surf, pnode->co[1] );
-            }
-            vg_release_thread_sync();
-         }
-      }
-      else if( pnode->classtype == k_classtype_car_path )
-      {
-         struct classtype_car_path *p = mdl_get_entdata( mworld, pnode );
-         traffic_node *tn = &world.traffic[ world.traffic_count ];
-         tn->mn_next = NULL;
-         tn->mn_next1 = NULL;
-         
-         if( p->target ) tn->mn_next = mdl_node_from_id( mworld, p->target );
-         if( p->target1 ) tn->mn_next1 = mdl_node_from_id( mworld, p->target1 );
-
-         m4x3f transform;
-         mdl_node_transform( pnode, transform );
-         m3x3_mulv( transform, (v3f){1.0f,0.0f,0.0f}, tn->h );
-         v3_copy( transform[3], tn->co );
-
-         pnode->sub_uid = world.traffic_count ++;
+         m4x3f identity;
+         m4x3_identity( identity );
+         scene_add_mdl_submesh( &world->scene_no_collide, 
+                                &world->meta, sm, identity );
+
+         scene_copy_slice( &world->scene_no_collide, sm );
+         sm->flags |= k_submesh_flag_consumed;
       }
-      else if( pnode->classtype == k_classtype_instance )
-      {
-         struct classtype_instance *inst = mdl_get_entdata( mworld, pnode );
-         pnode->sub_uid = 0;
-         
-         int cached = 0;
-         for( int i=0; i<world.instance_cache_count; i++ )
-         {
-            struct instance_cache *cache = &world.instance_cache[i];
-            if( inst->pstr_file == cache->pstr_file )
-            {
-               cached = 1;
-               pnode->sub_uid = i+1;
-               break;
-            }
-         }
+   }
+
+   vg_async_dispatch( call, async_scene_upload );
+}
+
+float fsd_cone_infinite( v3f p, v2f c )
+{
+   v2f q = { v2_length( (v2f){ p[0], p[2] } ), -p[1] };
+   float s = vg_maxf( 0.0f, v2_dot( q, c ) );
 
-         if( !cached )
-         {
-            world.instance_cache = buffer_reserve( 
-                            world.instance_cache, world.instance_cache_count,
-                            &world.instance_cache_cap, 1, 
-                            sizeof(struct instance_cache) );
+   v2f v0;
+   v2_muls( c, s, v0 );
+   v2_sub( q, v0, v0 );
 
-            struct instance_cache *cache = 
-               &world.instance_cache[world.instance_cache_count];
+   float d = v2_length( v0 );
+   return d * ((q[0]*c[1]-q[1]*c[0]<0.0f)?-1.0f:1.0f);
+}
 
-            const char *filename = mdl_pstr(mworld, inst->pstr_file);
+struct light_indices_upload_info{
+   world_instance *world;
+   v3i count;
 
-            cache->pstr_file = inst->pstr_file;
-            cache->mdl = mdl_load( filename );
+   void *data;
+};
 
-            if( cache->mdl )
-            {
-               world.instance_cache_count ++;
-               pnode->sub_uid = world.instance_cache_count;
-               mdl_link_materials( mworld, cache->mdl );
-               vg_success( "Cached %s\n", filename );
-            }
-            else
-            {
-               vg_warn( "Failed to cache %s\n", filename );
+VG_STATIC void async_upload_light_indices( void *payload, u32 size )
+{
+   struct light_indices_upload_info *info = payload;
+
+   glGenTextures( 1, &info->world->tex_light_cubes );
+   glBindTexture( GL_TEXTURE_3D, info->world->tex_light_cubes );
+   glTexImage3D( GL_TEXTURE_3D, 0, GL_RG32UI,
+                 info->count[0], info->count[1], info->count[2],
+                 0, GL_RG_INTEGER, GL_UNSIGNED_INT, info->data );
+   glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
+   glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
+}
+
+VG_STATIC void world_compute_light_indices( world_instance *world )
+{
+   /* light cubes */
+   v3f cubes_min, cubes_max;
+   v3_muls( world->scene_geo.bbx[0], 1.0f/k_light_cube_size, cubes_min );
+   v3_muls( world->scene_geo.bbx[1], 1.0f/k_light_cube_size, cubes_max );
+
+   v3_sub( cubes_min, (v3f){ 0.5f, 0.5f, 0.5f }, cubes_min );
+   v3_add( cubes_max, (v3f){ 0.5f, 0.5f, 0.5f }, cubes_max );
+
+   v3_floor( cubes_min, cubes_min );
+   v3_floor( cubes_max, cubes_max );
+
+   v3i icubes_min, icubes_max;
+
+   for( int i=0; i<3; i++ ){
+      icubes_min[i] = cubes_min[i];
+      icubes_max[i] = cubes_max[i];
+   }
+
+   v3f cube_size;
+
+   v3i icubes_count;
+   v3i_sub( icubes_max, icubes_min, icubes_count );
+   
+   for( int i=0; i<3; i++ ){
+      int clamped_count = VG_MIN( 128, icubes_count[i]+1 );
+      float clamped_max = icubes_min[i] + clamped_count,
+                    max = icubes_min[i] + icubes_count[i]+1;
+
+      icubes_count[i] = clamped_count;
+      cube_size[i] = (max / clamped_max) * k_light_cube_size;
+      cubes_max[i] = clamped_max;
+   }
+
+   v3_mul( cubes_min, cube_size, cubes_min );
+   v3_mul( cubes_max, cube_size, cubes_max );
+
+   for( int i=0; i<3; i++ ){
+      float range = cubes_max[i]-cubes_min[i];
+      world->ub_lighting.g_cube_inv_range[i] = 1.0f / range;
+      world->ub_lighting.g_cube_inv_range[i] *= (float)icubes_count[i];
+
+      vg_info( "cubes[%d]: %d\n", i, icubes_count[i] );
+   }
+
+   int total_cubes = icubes_count[0]*icubes_count[1]*icubes_count[2];
+
+   u32 data_size = vg_align8(total_cubes*sizeof(u32)*2),
+       hdr_size  = vg_align8(sizeof(struct light_indices_upload_info));
+
+   vg_async_item *call = vg_async_alloc( data_size + hdr_size );
+   struct light_indices_upload_info *info = call->payload;
+   info->data = ((u8*)call->payload) + hdr_size;
+   info->world = world;
+   u32 *cubes_index = info->data;
+
+   for( int i=0; i<3; i++ )
+      info->count[i] = icubes_count[i];
+                                       
+   vg_info( "Computing light cubes (%d) [%f %f %f] -> [%f %f %f]\n", 
+             total_cubes, cubes_min[0], -cubes_min[2], cubes_min[1],
+                          cubes_max[0], -cubes_max[2], cubes_max[1] );
+   v3_copy( cubes_min, world->ub_lighting.g_cube_min );
+
+   float bound_radius = v3_length( cube_size );
+
+   for( int iz = 0; iz<icubes_count[2]; iz ++ ){
+      for( int iy = 0; iy<icubes_count[1]; iy++ ){
+         for( int ix = 0; ix<icubes_count[0]; ix++ ){
+            boxf bbx;
+            v3_div( (v3f){ ix, iy, iz }, world->ub_lighting.g_cube_inv_range, 
+                  bbx[0] );
+            v3_div( (v3f){ ix+1, iy+1, iz+1 }, 
+                  world->ub_lighting.g_cube_inv_range, 
+                  bbx[1] );
+
+            v3_add( bbx[0], world->ub_lighting.g_cube_min, bbx[0] );
+            v3_add( bbx[1], world->ub_lighting.g_cube_min, bbx[1] );
+
+            v3f center;
+            v3_add( bbx[0], bbx[1], center );
+            v3_muls( center, 0.5f, center );
+            
+            u32 indices[6] = { 0, 0, 0, 0, 0, 0 };
+            u32 count = 0;
+
+            float influences[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
+            const int N = vg_list_size( influences );
+
+            for( u32 j=0; j<mdl_arrcount(&world->ent_light); j ++ ){
+               ent_light *light = mdl_arritm( &world->ent_light, j );
+               v3f closest;
+               closest_point_aabb( light->transform.co, bbx, closest );
+
+               float dist = v3_dist( closest, light->transform.co ),
+                     influence = 1.0f/(dist+1.0f);
+
+               if( dist > light->range )
+                  continue;
+
+               if( light->type == k_light_type_spot){
+                  v3f local;
+                  m4x3_mulv( light->inverse_world, center, local );
+
+                  float r = fsd_cone_infinite( local, light->angle_sin_cos );
+
+                  if( r > bound_radius )
+                     continue;
+               }
+
+               int best_pos = N;
+               for( int k=best_pos-1; k>=0; k -- )
+                  if( influence > influences[k] )
+                     best_pos = k;
+
+               if( best_pos < N ){
+                  for( int k=N-1; k>best_pos; k -- ){
+                     influences[k] = influences[k-1];
+                     indices[k] = indices[k-1];
+                  }
+
+                  influences[best_pos] = influence;
+                  indices[best_pos] = j;
+               }
             }
+
+            for( int j=0; j<N; j++ )
+               if( influences[j] > 0.0f )
+                  count ++;
+
+            int base_index = iz * (icubes_count[0]*icubes_count[1]) +
+                             iy * (icubes_count[0]) +
+                             ix;
+
+            int lower_count = VG_MIN( 3, count );
+            u32 packed_index_lower = lower_count;
+            packed_index_lower |= indices[0]<<2;
+            packed_index_lower |= indices[1]<<12;
+            packed_index_lower |= indices[2]<<22;
+
+            int upper_count = VG_MAX( 0, count - lower_count );
+            u32 packed_index_upper = upper_count;
+            packed_index_upper |= indices[3]<<2;
+            packed_index_upper |= indices[4]<<12;
+            packed_index_upper |= indices[5]<<22;
+
+            cubes_index[ base_index*2 + 0 ] = packed_index_lower;
+            cubes_index[ base_index*2 + 1 ] = packed_index_upper;
          }
       }
-      else if( pnode->classtype == k_classtype_achievement_box )
-      {
-         world.achievement_zones = 
-            buffer_reserve( world.achievement_zones,
-                            world.achievement_zones_count,
-                            &world.achievement_zones_cap, 1, 
-                            sizeof(struct achievement_zone) );
+   }
+
+   vg_async_dispatch( call, async_upload_light_indices );
+}
+
+VG_STATIC void async_world_postprocess_render( void *payload, u32 _size )
+{
+   /* create scene lighting buffer */
+   world_instance *world = payload;
+
+   u32 size = VG_MAX(mdl_arrcount(&world->ent_light),1) * sizeof(float)*12;
+   vg_info( "Upload %ubytes (lighting)\n", size );
+
+   glGenBuffers( 1, &world->tbo_light_entities );
+   glBindBuffer( GL_TEXTURE_BUFFER, world->tbo_light_entities );
+   glBufferData( GL_TEXTURE_BUFFER, size, NULL, GL_DYNAMIC_DRAW );
+   
+   /* buffer layout
+    *  
+    *  colour               position                direction (spots)
+    * | .   .   .   .     | .   .   .   .         | .   .   .   .  |
+    * | Re  Ge  Be  Night | Xco Yco Zco Range     | Dx  Dy  Dz  Da |
+    *
+    */
 
-         struct achievement_zone *zone = &world.achievement_zones[ 
-                                          world.achievement_zones_count ++ ];
+   v4f *light_dst = glMapBuffer( GL_TEXTURE_BUFFER, GL_WRITE_ONLY );
+   for( u32 i=0; i<mdl_arrcount(&world->ent_light); i++ ){
+      ent_light *light = mdl_arritm( &world->ent_light, i );
 
+      /* colour  + night */
+      v3_muls( light->colour, light->colour[3] * 2.0f, light_dst[i*3+0] );
+      light_dst[i*3+0][3] = 2.0f;
 
-         struct classtype_achievement_box *box = mdl_get_entdata(mworld,pnode);
+      if( !light->daytime ){
+         u32 hash = (i * 29986577u) & 0xffu;
+         float switch_on = hash;
+               switch_on *= (1.0f/255.0f);
 
-         mdl_node_transform( pnode, zone->transform );
-         m4x3_invert_full( zone->transform, zone->inv_transform );
-         vg_strncpy( mdl_pstr(mworld, box->pstr_name), zone->name, 31 );
-         zone->name[31] = 0x00;
-         zone->triggered = 0;
+         light_dst[i*3+0][3] = 0.44f + switch_on * 0.015f;
       }
+      
+      /* position + 1/range^2 */
+      v3_copy( light->transform.co, light_dst[i*3+1] );
+      light_dst[i*3+1][3] = 1.0f/(light->range*light->range);
+
+      /* direction + angle */
+      q_mulv( light->transform.q, (v3f){0.0f,-1.0f,0.0f}, light_dst[i*3+2]);
+      light_dst[i*3+2][3] = cosf( light->angle );
    }
 
-   world.instance_cache = buffer_fix( world.instance_cache, 
-                                      world.instance_cache_count,
-                                     &world.instance_cache_cap,
-                                     sizeof( struct instance_cache ) );
+   glUnmapBuffer( GL_TEXTURE_BUFFER );
 
-#if 0
-   traffic_finalize( world.traffic, world.traffic_count );
-   for( int i=0; i<vg_list_size(world.van_man); i++ )
-      world.van_man[i].current =&world.traffic[vg_randint(world.traffic_count)];
-#endif
+   glGenTextures( 1, &world->tex_light_entities );
+   glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
+   glTexBuffer( GL_TEXTURE_BUFFER, GL_RGBA32F, world->tbo_light_entities );
+
+   /* Upload lighting uniform buffer */
+   if( world->water.enabled )
+      v4_copy( world->water.plane, world->ub_lighting.g_water_plane );
+
+   v4f info_vec;
+   v3f *bounds = world->scene_geo.bbx;
+
+   info_vec[0] = bounds[0][0];
+   info_vec[1] = bounds[0][2];
+   info_vec[2] = 1.0f/ (bounds[1][0]-bounds[0][0]);
+   info_vec[3] = 1.0f/ (bounds[1][2]-bounds[0][2]);
+   v4_copy( info_vec, world->ub_lighting.g_depth_bounds );
 
    /* 
-    * Compile meshes into the world scenes
+    * Rendering the depth map
     */
-   scene_init( &world.geo );
+   camera ortho;
+
+   v3f extent;
+   v3_sub( world->scene_geo.bbx[1], world->scene_geo.bbx[0], extent );
+
+   float fl = world->scene_geo.bbx[0][0],
+         fr = world->scene_geo.bbx[1][0],
+         fb = world->scene_geo.bbx[0][2],
+         ft = world->scene_geo.bbx[1][2],
+         rl = 1.0f / (fr-fl),
+         tb = 1.0f / (ft-fb);
+
+   m4x4_zero( ortho.mtx.p );
+   ortho.mtx.p[0][0] = 2.0f * rl;
+   ortho.mtx.p[2][1] = 2.0f * tb;
+   ortho.mtx.p[3][0] = (fr + fl) * -rl;
+   ortho.mtx.p[3][1] = (ft + fb) * -tb;
+   ortho.mtx.p[3][3] = 1.0f;
+   m4x3_identity( ortho.transform );
+   camera_update_view( &ortho );
+   camera_finalize( &ortho );
+
+   glDisable(GL_DEPTH_TEST);
+   glDisable(GL_BLEND);
+   glDisable(GL_CULL_FACE);
+   render_fb_bind( &world->heightmap, 0 );
+   shader_blitcolour_use();
+   shader_blitcolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
+   render_fsquad();
+
+   glEnable(GL_BLEND);
+   glBlendFunc(GL_ONE, GL_ONE);
+   glBlendEquation(GL_MAX);
+
+   render_world_position( world, &ortho );
+   glDisable(GL_BLEND);
+   glEnable(GL_DEPTH_TEST);
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+
+   /* upload full buffer */
+   glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
+   glBufferSubData( GL_UNIFORM_BUFFER, 0, 
+                    sizeof(struct ub_world_lighting), &world->ub_lighting );
+}
 
-   u32 mat_surf = 0,
-       mat_surf_oob = 0,
-       mat_vertex_blend = 0,
-       mat_alphatest = 0,
-       mat_graffiti = 0,
-       mat_subworld = 0,
-       mat_terrain = 0;
+VG_STATIC int reset_player( int argc, char const *argv[] );
+VG_STATIC void world_post_process( world_instance *world )
+{
+   world_compute_light_indices( world );
+   vg_async_call( async_world_postprocess_render, world, 0 );
+}
 
-   for( int i=1; i<mworld->material_count; i++ )
-   {
-      mdl_material *mat = mdl_material_from_id( mworld, i );
-      const char *mat_name = mdl_pstr( mworld, 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;
-   }
+VG_STATIC void world_process_resources( world_instance *world )
+{
+   vg_info( "Loading textures\n" );
 
-   m4x3f midentity;
-   m4x3_identity( midentity );
+   world->texture_count = 0;
+
+   world->texture_count = world->meta.textures.count+1;
+   world->textures = vg_linear_alloc( world->heap,
+                              vg_align8(sizeof(GLuint)*world->texture_count) );
+
+   vg_tex2d_replace_with_error( &world->textures[0] );
+
+   for( u32 i=0; i<mdl_arrcount(&world->meta.textures); i++ ){
+      mdl_texture *tex = mdl_arritm( &world->meta.textures, i );
 
-   if( mat_terrain )
-      world_add_all_if_material( midentity, &world.geo, mworld, mat_terrain );
-   scene_copy_slice( &world.geo, &world.sm_terrain );
+      if( !tex->file.pack_size ){
+         vg_fatal_error( "World models must have packed textures!" );
+      }
+
+      vg_linear_clear( vg_mem.scratch );
+      void *src_data = vg_linear_alloc( vg_mem.scratch, 
+                                        tex->file.pack_size );
+      mdl_fread_pack_file( &world->meta, &tex->file, src_data );
+
+      vg_tex2d_load_qoi_async( src_data, tex->file.pack_size,
+                               VG_TEX2D_NEAREST|VG_TEX2D_REPEAT,
+                               &world->textures[i+1] );
+   }
 
-   if( mat_surf_oob )
-      world_add_all_if_material( midentity, &world.geo, mworld, mat_surf_oob );
-   else
-      vg_warn( "No OOB surface\n" );
-   scene_copy_slice( &world.geo, &world.sm_geo_std_oob );
+   vg_info( "Loading materials\n" );
 
-   if( mat_surf )
-      world_add_all_if_material( midentity, &world.geo, mworld, mat_surf );
-   scene_copy_slice( &world.geo, &world.sm_geo_std );
+   world->surface_count = world->meta.materials.count+1;
+   world->surfaces = vg_linear_alloc( world->heap,
+               vg_align8(sizeof(struct world_surface)*world->surface_count) );
 
-   if( mat_vertex_blend )
-      world_add_all_if_material( midentity, &world.geo,mworld,mat_vertex_blend);
-   scene_copy_slice( &world.geo, &world.sm_geo_vb );
+   /* error material */
+   struct world_surface *errmat = &world->surfaces[0];
+   memset( errmat, 0, sizeof(struct world_surface) );
+                       
+   for( u32 i=0; i<mdl_arrcount(&world->meta.materials); i++ ){
+      world->surfaces[i+1].info = 
+         *(mdl_material *)mdl_arritm( &world->meta.materials, i );
+   }
+}
 
+#if 0
+VG_STATIC void world_free( world_instance *world )
+{
    vg_acquire_thread_sync();
-   scene_upload( &world.geo );
+
+   /* free meshes */
+   mesh_free( &world->mesh_route_lines );
+   mesh_free( &world->mesh_geo );
+   mesh_free( &world->mesh_no_collide );
+   
+   /* glDeleteBuffers silently ignores 0's and names that do not correspond to 
+    * existing buffer objects. 
+    * */
+   glDeleteBuffers( 1, &world->tbo_light_entities );
+   glDeleteTextures( 1, &world->tex_light_entities );
+   glDeleteTextures( 1, &world->tex_light_cubes );
+
+   /* delete textures and meshes */
+   glDeleteTextures( world->texture_count, world->textures );
+
    vg_release_thread_sync();
 
-   scene_bh_create( &world.geo );
+   world->status = k_world_status_unloaded;
+}
+#endif
 
+VG_STATIC void world_init_blank( world_instance *world )
+{
+   memset( &world->meta, 0, sizeof(mdl_context) );
 
-   /* Foliage /nocollide layer.
-    * TODO: Probably should have material traits for this
-    */
-   scene_init( &world.foliage );
+   world->textures = NULL;
+   world->texture_count = 0;
+   world->surfaces = NULL;
+   world->surface_count = 0;
 
-   world_apply_procedural_foliage();
-   scene_copy_slice( &world.foliage, &world.sm_foliage_main );
+   world->geo_bh = NULL;
+   world->volume_bh = NULL;
+   world->audio_bh = NULL;
+   world->rendering_gate = NULL;
 
-   world_add_all_if_material( midentity, &world.foliage, mworld, mat_alphatest);
-   scene_copy_slice( &world.foliage, &world.sm_foliage_alphatest );
+   world->water.enabled = 0;
 
-   world_add_all_if_material( midentity, &world.foliage, mworld, mat_graffiti );
-   scene_copy_slice( &world.foliage, &world.sm_graffiti );
+   /* default lighting conditions 
+    * -------------------------------------------------------------*/
+   struct ub_world_lighting *state = &world->ub_lighting;
 
+   state->g_light_preview = 0;
+   state->g_shadow_samples = 8;
+   state->g_water_fog = 0.04f;
 
-   vg_acquire_thread_sync();
-   {
-      scene_upload( &world.foliage );
-
-      /* 
-       * Rendering the depth map
-       */
-      m4x4f ortho;
-      m4x3f camera;
-
-      v3f extent;
-      v3_sub( world.geo.bbx[1], world.geo.bbx[0], extent );
-
-      float fl = world.geo.bbx[0][0],
-            fr = world.geo.bbx[1][0],
-            fb = world.geo.bbx[0][2],
-            ft = world.geo.bbx[1][2],
-            rl = 1.0f / (fr-fl),
-            tb = 1.0f / (ft-fb);
-
-      m4x4_zero( ortho );
-      ortho[0][0] = 2.0f * rl;
-      ortho[2][1] = 2.0f * tb;
-      ortho[3][0] = (fr + fl) * -rl;
-      ortho[3][1] = (ft + fb) * -tb;
-      ortho[3][3] = 1.0f;
-      m4x3_identity( camera );
-
-      glViewport( 0, 0, 1024, 1024 );
-      glDisable(GL_DEPTH_TEST);
-      glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_depthmap );
-      shader_fscolour_use();
-      shader_fscolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
-      render_fsquad();
-
-      glEnable(GL_BLEND);
-      glBlendFunc(GL_ONE, GL_ONE);
-      glBlendEquation(GL_MAX);
-      render_world_depth( ortho, camera );
-      glDisable(GL_BLEND);
-      glEnable(GL_DEPTH_TEST);
-      glBindFramebuffer( GL_FRAMEBUFFER, 0 );
-
-
-      /* 
-       * TODO: World settings entity
-       */
-      struct ub_world_lighting *winfo = &gpipeline.ub_world_lighting;
-      v4_copy( wrender.plane, winfo->g_water_plane );
-
-      v4f bounds;
-      bounds[0] = world.geo.bbx[0][0];
-      bounds[1] = world.geo.bbx[0][2];
-      bounds[2] = 1.0f/ (world.geo.bbx[1][0]-world.geo.bbx[0][0]);
-      bounds[3] = 1.0f/ (world.geo.bbx[1][2]-world.geo.bbx[0][2]);
-      v4_copy( bounds, winfo->g_depth_bounds );
-
-      winfo->g_water_fog = 0.04f;
-      render_update_lighting_ub();
+   v4_zero( state->g_water_plane );
+   v4_zero( state->g_depth_bounds );
+
+   state->g_shadow_length = 9.50f;
+   state->g_shadow_spread = 0.65f;
+
+   v3_copy( (v3f){0.37f, 0.54f, 0.97f}, state->g_daysky_colour );
+   v3_copy( (v3f){0.03f, 0.05f, 0.20f}, state->g_nightsky_colour );
+   v3_copy( (v3f){1.00f, 0.32f, 0.01f}, state->g_sunset_colour );
+   v3_copy( (v3f){0.13f, 0.17f, 0.35f}, state->g_ambient_colour );
+   v3_copy( (v3f){0.25f, 0.17f, 0.51f}, state->g_sunset_ambient );
+   v3_copy( (v3f){1.10f, 0.89f, 0.35f}, state->g_sun_colour );
+}
+
+VG_STATIC void world_entities_init( u32 world_id )
+{
+   world_instance *world = &world_global.worlds[world_id];
+
+   /* lights */
+   for( u32 j=0; j<mdl_arrcount(&world->ent_light); j ++ ){
+      ent_light *light = mdl_arritm( &world->ent_light, j );
+
+      m4x3f to_world;
+      q_m3x3( light->transform.q, to_world );
+      v3_copy( light->transform.co, to_world[3] );
+      m4x3_invert_affine( to_world, light->inverse_world );
+
+      light->angle_sin_cos[0] = sinf( light->angle * 0.5f );
+      light->angle_sin_cos[1] = cosf( light->angle * 0.5f );
    }
 
-   vg_release_thread_sync();
+   /* gates */
+   for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
+      ent_gate *gate = mdl_arritm( &world->ent_gate, j );
+
+      if( gate->type == k_gate_type_teleport ){
+         gate_transform_update( gate );
+      }
+      else if( gate->type == k_gate_type_nonlocal_unlinked ){
+         const char *key = mdl_pstr( &world->meta, gate->target );
+         vg_info( "key: %s\n", key );
+
+         for( u32 i=0; i<vg_list_size(world_global.worlds); i++ ){
+            world_instance *other = &world_global.worlds[i];
+            if( other == world ) continue;
+            if( other->status != k_world_status_loaded ) continue;
+            vg_info( "Checking world %u for key matches\n", i );
+
+            for( u32 j=0; j<mdl_arrcount( &other->ent_gate ); j++ ){
+               ent_gate *gate2 = mdl_arritm( &other->ent_gate, j );
+               if( gate2->type != k_gate_type_nonlocal_unlinked ) continue;
+
+               const char *key2 = mdl_pstr( &other->meta, gate2->target );
+               vg_info( " key2: %s\n", key2 );
+
+               if( strcmp( key, key2 ) ) continue;
+
+               vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
+                            key, world_id, i );
+                  
+               gate->type = k_gate_type_nonlocel;
+               gate2->type = k_gate_type_nonlocel;
+               gate->target = i;
+               gate2->target = world_id;
+
+               v3_copy( gate->co[0], gate2->co[1] );
+               v3_copy( gate2->co[0], gate->co[1] );
+               v4_copy( gate->q[0], gate2->q[1] );
+               v4_copy( gate2->q[0], gate->q[1] );
+
+               v4f qflip;
+               q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
+               q_mul( gate->q[0], qflip, gate->q[0] );
+               q_mul( gate->q[1], qflip, gate->q[1] );
+
+               gate_transform_update( gate );
+               gate_transform_update( gate2 );
+
+               goto matched;
+            }
+         }
+matched:;
+      }
+   }
+
+   /* water */
+   for( u32 j=0; j<mdl_arrcount(&world->ent_water); j++ ){
+      ent_water *water = mdl_arritm( &world->ent_water, j );
+      if( world->water.enabled ){
+         vg_warn( "Multiple water surfaces in level!\n" );
+         break;
+      }
+
+      world->water.enabled = 1;
+      water_set_surface( world, water->transform.co[1] );
+   }
+   
+   /* volumes */
+   for( u32 j=0; j<mdl_arrcount(&world->ent_volume); j++ ){
+      ent_volume *volume = mdl_arritm( &world->ent_volume, j );
+      mdl_transform_m4x3( &volume->transform, volume->to_world );
+      m4x3_invert_full( volume->to_world, volume->to_local );
+   }
 
-   world_routes_loadfrom( mworld );
+   /* audio packs */
+   for( u32 j=0; j<mdl_arrcount(&world->ent_audio); j++ ){
+      ent_audio *audio = mdl_arritm( &world->ent_audio, j );
 
-   for( int i=0; i<world.instance_cache_count; i++ )
-      vg_free( world.instance_cache[i].mdl );
+      for( u32 k=0; k<audio->clip_count; k++ ){
+         ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,  
+                                             audio->clip_start+k );
 
-   vg_free( world.instance_cache );
-   vg_free( mworld );
-   scene_free_offline_buffers( &world.foliage );
+         if( clip->file.pack_size ){
+            u32 size = clip->file.pack_size,
+                offset = clip->file.pack_offset;
 
-   /*
-    * Setup scene collider 
-    */
-   v3_zero( world.rb_geo.co );
-   q_identity( world.rb_geo.q );
+            /* embedded files are fine to clear the scratch buffer, only
+             * external audio uses it */
+
+            vg_linear_clear( vg_mem.scratch );
+            void *data = vg_linear_alloc( vg_mem.scratch, 
+                                          clip->file.pack_size );
+
+            mdl_fread_pack_file( &world->meta, &clip->file, data );
+
+            clip->clip.path = NULL;
+            clip->clip.flags = audio->flags;
+            clip->clip.data = data;
+            clip->clip.size = size;
+         }
+         else{
+            clip->clip.path = mdl_pstr( &world->meta, clip->file.pstr_path );
+            clip->clip.flags = audio->flags;
+            clip->clip.data = NULL;
+            clip->clip.size = 0;
+         }
+
+         audio_clip_load( &clip->clip, world->heap );
+      }
+   }
+}
+
+VG_STATIC void world_load( u32 index, const char *path )
+{
+   world_instance *world = &world_global.worlds[index];
+   world_init_blank( world );
+   world->status = k_world_status_loading;
+
+   vg_info( "Loading world: %s\n", path );
+
+   void *allocator = NULL;
+   if( index == 0 ) allocator = world_global.heap;
+   else allocator = world_global.worlds[index-1].heap;
+
+   u32 heap_availible = vg_linear_remaining( allocator );
+   u32 min_overhead = sizeof(vg_linear_allocator);
+
+   if( heap_availible < (min_overhead+1024) ){
+      vg_fatal_error( "out of memory" );
+   }
+
+   u32 size = heap_availible - min_overhead;
+   void *heap = vg_create_linear_allocator( allocator, size, 
+                                            VG_MEMORY_SYSTEM );
+
+   world->heap = heap;
+   mdl_context *meta = &world->meta;
+
+   mdl_open( meta, path, world->heap );
+   mdl_load_metadata_block( meta, world->heap );
+   mdl_load_animation_block( meta, world->heap );
+   mdl_load_mesh_block( meta, world->heap );
+
+   mdl_load_array( meta, &world->ent_gate,      "ent_gate",       heap );
+   mdl_load_array( meta, &world->ent_camera,    "ent_camera",     heap );
+   mdl_load_array( meta, &world->ent_spawn,     "ent_spawn",      heap );
+   mdl_load_array( meta, &world->ent_light,     "ent_light",      heap );
+   mdl_load_array( meta, &world->ent_route_node,"ent_route_node", heap );
+   mdl_load_array( meta, &world->ent_path_index,"ent_path_index", heap );
+   mdl_load_array( meta, &world->ent_checkpoint,"ent_checkpoint", heap );
+   mdl_load_array( meta, &world->ent_route,     "ent_route",      heap );
+   mdl_load_array( meta, &world->ent_water,     "ent_water",      heap );
+   mdl_load_array( meta, &world->ent_audio_clip,"ent_audio_clip", heap );
+   mdl_load_array( meta, &world->ent_audio,     "ent_audio",      heap );
+   mdl_load_array( meta, &world->ent_volume,    "ent_volume",     heap );
+   mdl_load_array( meta, &world->ent_traffic,   "ent_traffic",    heap );
+   mdl_load_array( meta, &world->ent_marker,    "ent_marker",     heap );
+   mdl_load_array( meta, &world->ent_skateshop, "ent_skateshop",  heap );
+
+   /* process resources from pack */
+   world_process_resources( world );
+
+   world_routes_ent_init( world );
+   world_entities_init( index );
+   world->volume_bh = bh_create( heap, &bh_system_volumes, world,
+                                 mdl_arrcount( &world->ent_volume ), 1 );
+   
+   /* main bulk */
+   world_generate( world );
+   world_routes_generate( world );
+   world_post_process( world );
+
+   mdl_close( meta );
 
-   world.rb_geo.type = k_rb_shape_scene;
-   world.rb_geo.inf.scene.pscene = &world.geo;
-   world.rb_geo.is_world = 1;
-   rb_init( &world.rb_geo );
+   world->status = k_world_status_loaded;
 }
 
 #endif /* WORLD_GEN_H */