dont remember
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gen.h
index 74907c62fc87c29e029e0798c82558d133abedfa..030af6b8ac11628a1b9e26b3d66985ddc7eb2f59 100644 (file)
@@ -1,8 +1,12 @@
 #ifndef WORLD_GEN_H
 #define WORLD_GEN_H
 
-#include "world.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 )
@@ -86,12 +90,14 @@ static void world_apply_procedural_foliage(void)
          }
       }
    }
-   free( mfoliage );
+
+   vg_free( mfoliage );
 }
 
 static void world_load(void)
 {
    mdl_header *mworld = mdl_load( "models/mp_dev.mdl" );
+   vg_info( "Loading world: models/mp_dev.mdl\n" );
 
    world.spawn_count = 0;
    world.traffic_count = 0;
@@ -127,10 +133,13 @@ static void world_load(void)
          
          if( sm )
          {
-            glmesh surf;
-            mdl_unpack_submesh( mworld, &surf, sm );
-            world_water_init();
-            water_set_surface( &surf, pnode->co[1] );
+            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 )
@@ -261,7 +270,10 @@ static void world_load(void)
       world_add_all_if_material( midentity, &world.geo,mworld,mat_vertex_blend);
    scene_copy_slice( &world.geo, &world.sm_geo_vb );
 
+   vg_acquire_thread_sync();
    scene_upload( &world.geo );
+   vg_release_thread_sync();
+
    scene_bh_create( &world.geo );
 
 
@@ -279,76 +291,78 @@ static void world_load(void)
    world_add_all_if_material( midentity, &world.foliage, mworld, mat_graffiti );
    scene_copy_slice( &world.foliage, &world.sm_graffiti );
 
-   scene_upload( &world.foliage );
-   world_routes_loadfrom( mworld );
-
-   for( int i=0; i<world.instance_cache_count; i++ )
-      free( world.instance_cache[i].mdl );
 
-   free( world.instance_cache );
-   free( mworld );
+   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();
+   }
 
-   /* 
-    * 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);
+   vg_release_thread_sync();
 
-   /* 
-    * 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();
-   
+   world_routes_loadfrom( mworld );
 
-   world.mr_ball.type = k_rb_shape_sphere;
-   world.mr_ball.inf.sphere.radius = 2.0f;
-   v3_copy( (v3f){ 0.0f, 110.0f, 0.0f }, world.mr_ball.co );
+   for( int i=0; i<world.instance_cache_count; i++ )
+      vg_free( world.instance_cache[i].mdl );
 
-   q_identity(world.mr_ball.q);
-   rb_init( &world.mr_ball );
+   vg_free( world.instance_cache );
+   vg_free( mworld );
+   scene_free_offline_buffers( &world.foliage );
 
    /*
     * Setup scene collider