change shader properties to be vg_msg based
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gen.c
index 5f7b9a362e7b3fc294b4e6a5d13cd3ce00847f6c..3d88ecd72f87312d9f6e2ef389005c73b444675f 100644 (file)
@@ -4,21 +4,18 @@
  * World generation/population. Different to regular loading, since it needs to
  * create geometry, apply procedural stuff and save that image to files etc.
  */
-
-#ifndef WORLD_GEN_C
-#define WORLD_GEN_C
-
 #include "world.h"
 #include "world_gen.h"
 #include "world_load.h"
 #include "world_volumes.h"
 #include "world_gate.h"
+#include <string.h>
 
 /*
  * Add all triangles from the model, which match the material ID
  * applies affine transform to the model
  */
-VG_STATIC void world_add_all_if_material( m4x3f transform, scene_context *scene, 
+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++ ){
@@ -46,9 +43,9 @@ VG_STATIC void world_add_all_if_material( m4x3f transform, scene_context *scene,
  * |        |
  * |________|
  */
-VG_STATIC void world_gen_add_blob( scene_context *scene, ray_hit *hit )
+static void world_gen_add_blob( vg_rand *rand, world_instance *world,
+                                   scene_context *scene, ray_hit *hit )
 {
-   world_instance *world = world_loading_instance();
    m4x3f transform;
    v4f qsurface, qrandom;
    v3f axis;
@@ -57,7 +54,7 @@ VG_STATIC void world_gen_add_blob( scene_context *scene, ray_hit *hit )
 
    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_randf64()*VG_TAUf );
+   q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf64(rand)*VG_TAUf );
    q_mul( qsurface, qrandom, qsurface );
    q_m3x3( qsurface, transform );
    v3_copy( hit->pos, transform[3] );
@@ -85,13 +82,12 @@ VG_STATIC void world_gen_add_blob( scene_context *scene, ray_hit *hit )
 
    scene_vert *ref       = &world->scene_geo.arrvertices[ hit->tri[0] ];
 
-   for( u32 i=0; i<vg_list_size(verts); i++ )
-   {
+   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] );
+      scene_vert_pack_norm( pvert, transform[1], 0.0f );
 
       v2_copy( ref->uv, pvert->uv );
    }
@@ -106,13 +102,14 @@ VG_STATIC void world_gen_add_blob( scene_context *scene, ray_hit *hit )
 /* 
  * Sprinkle foliage models over the map on terrain material 
  */
-VG_STATIC void world_apply_procedural_foliage( scene_context *scene,
+static void world_apply_procedural_foliage( world_instance *world,
+                                               scene_context *scene,
                                                struct world_surface *mat )
 {
-   if( vg.quality_profile == k_quality_profile_low )
+   if( (vg.quality_profile == k_quality_profile_low) ||
+       (vg.quality_profile == k_quality_profile_min) )
       return;
 
-   world_instance *world = world_loading_instance();
    vg_info( "Applying foliage (%u)\n", mat->info.pstr_name );
 
    v3f volume;
@@ -124,9 +121,10 @@ VG_STATIC void world_apply_procedural_foliage( scene_context *scene,
    float area = volume[0]*volume[2];
    u32 particles = 0.08f * area;
 
-   /* TODO: Quasirandom? */
    vg_info( "Map area: %f. Max particles: %u\n", area, particles );
 
+   u64 t0 = SDL_GetPerformanceCounter();
+#if 0
    for( u32 i=0; i<particles; i++ ){
       v3f pos;
       v3_mul( volume, (v3f){ vg_randf64(), 1000.0f, vg_randf64() }, pos );
@@ -136,19 +134,64 @@ VG_STATIC void world_apply_procedural_foliage( scene_context *scene,
       ray_hit hit;
       hit.dist = INFINITY;
 
-      if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &hit )){
+      if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &hit, 
+                     k_material_flag_ghosts )){
          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_gen_add_blob( scene, &hit );
+            world_gen_add_blob( world, scene, &hit );
             count ++;
          }
       }
    }
+#else
+
+   vg_rand rand;
+   vg_rand_seed( &rand, 3030 );
+   
+   const f32 tile_scale = 16.0f;
+   v2i tiles = { volume[0]/tile_scale, volume[2]/tile_scale };
+
+   u32 per_tile = particles/(tiles[0]*tiles[1]);
+
+   for( i32 x=0; x<tiles[0]; x ++ ){
+      for( i32 z=0; z<tiles[1]; z ++ ){
+         for( u32 i=0; i<per_tile; i ++ ){
+            v3f co = { (f32)x+vg_randf64(&rand), 0, (f32)z+vg_randf64(&rand) };
+            v3_muls( co, tile_scale, co );
+            co[1] = 1000.0f;
+            v3_add( co, world->scene_geo.bbx[0], co );
+
+            ray_hit hit;
+            hit.dist = INFINITY;
+
+            if( ray_world( world, co, (v3f){0.0f,-1.0f,0.0f}, &hit, 
+                           k_material_flag_ghosts )){
+               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_gen_add_blob( &rand, world, scene, &hit );
+                  count ++;
+               }
+            }
+
+         }
+      }
+   }
+
+#endif
+
 
-   vg_info( "%d foliage models added\n", count );
+
+   u64 t1 = SDL_GetPerformanceCounter(),
+       utime_blobs = t1-t0,
+       ufreq = SDL_GetPerformanceFrequency();
+   f64 ftime_blobs = ((f64)utime_blobs / (f64)ufreq)*1000.0;
+
+   vg_info( "%d foliage models added. %f%% (%fms)\n", count, 
+            100.0*((f64)count/(f64)particles), ftime_blobs);
 }
 
-VG_STATIC 
+static 
 void world_unpack_submesh_dynamic( world_instance *world,
                                    scene_context *scene, mdl_submesh *sm ){
    if( sm->flags & k_submesh_flag_consumed ) return;
@@ -164,12 +207,11 @@ void world_unpack_submesh_dynamic( world_instance *world,
 /*
  * Create the main meshes for the world
  */
-VG_STATIC void world_gen_generate_meshes(void)
+void world_gen_generate_meshes( world_instance *world )
 {
    /* 
     * Compile meshes into the world scenes
     */
-   world_instance *world = world_loading_instance();
    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 );
@@ -193,6 +235,10 @@ VG_STATIC void world_gen_generate_meshes(void)
                                     &world->meta, i );
 
       scene_copy_slice( &world->scene_geo, &surf->sm_geo );
+      scene_set_vertex_flags( &world->scene_geo, 
+                              surf->sm_geo.vertex_start,
+                              surf->sm_geo.vertex_count, 
+                              (u16)(surf->info.flags & 0xffff) );
    }
 
    /* compress that bad boy */
@@ -219,19 +265,8 @@ VG_STATIC void world_gen_generate_meshes(void)
    /* 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 );
-
    /*
     * Generate scene: non-collidable geometry
     * ----------------------------------------------------------------
@@ -250,8 +285,10 @@ VG_STATIC void world_gen_generate_meshes(void)
                                     &world->scene_no_collide, &world->meta, i );
       }
 
-      if( surf->info.flags & k_material_flag_grow_grass )
-         world_apply_procedural_foliage( &world->scene_no_collide, surf );
+      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 );
    }
@@ -268,16 +305,28 @@ VG_STATIC void world_gen_generate_meshes(void)
          mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, 
                                        vehc->submesh_start+j );
          world_unpack_submesh_dynamic( world, &world->scene_no_collide, sm );
+         world->surfaces[ sm->material_id ].flags |= WORLD_SURFACE_HAS_TRAFFIC;
       }
    }
 
    /* unpack challenge models */
-   for( u32 i=0; i<mdl_arrcount( &world->ent_challenge ); i++ ){
-      ent_challenge *challenge = mdl_arritm( &world->ent_challenge, i );
+   for( u32 i=0; i<mdl_arrcount( &world->ent_objective ); i++ ){
+      ent_objective *objective = mdl_arritm( &world->ent_objective, i );
+
+      for( u32 j=0; j<objective->submesh_count; j ++ ){
+         mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, 
+                                       objective->submesh_start+j );
+         world_unpack_submesh_dynamic( world, &world->scene_no_collide, sm );
+      }
+   }
 
-      for( u32 j=0; j<challenge->submesh_count; j ++ ){
+   /* unpack region models */
+   for( u32 i=0; i<mdl_arrcount( &world->ent_region ); i++ ){
+      ent_region *region = mdl_arritm( &world->ent_region, i );
+
+      for( u32 j=0; j<region->submesh_count; j ++ ){
          mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, 
-                                       challenge->submesh_start+j );
+                                       region->submesh_start+j );
          world_unpack_submesh_dynamic( world, &world->scene_no_collide, sm );
       }
    }
@@ -295,12 +344,23 @@ VG_STATIC void world_gen_generate_meshes(void)
       }
    }
 
+   /* unpack prop models */
+   for( u32 i=0; i<mdl_arrcount( &world->ent_prop ); i++ ){
+      ent_prop *prop = mdl_arritm( &world->ent_prop, i );
+
+      for( u32 j=0; j<prop->submesh_count; j ++ ){
+         mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, 
+                                       prop->submesh_start+j );
+         world->surfaces[ sm->material_id ].flags |= WORLD_SURFACE_HAS_PROPS;
+         world_unpack_submesh_dynamic( world, &world->scene_no_collide, sm );
+      }
+   }
+
    vg_async_dispatch( call, async_scene_upload );
 }
 
 /* signed distance function for cone */
-static f32 fsd_cone_infinite( v3f p, v2f c )
-{
+static f32 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 ) );
 
@@ -322,8 +382,7 @@ struct light_indices_upload_info{
 /*
  * Async reciever to buffer light index data 
  */
-VG_STATIC void async_upload_light_indices( void *payload, u32 size )
-{
+static void async_upload_light_indices( void *payload, u32 size ){
    struct light_indices_upload_info *info = payload;
 
    glGenTextures( 1, &info->world->tex_light_cubes );
@@ -338,10 +397,9 @@ VG_STATIC void async_upload_light_indices( void *payload, u32 size )
 /*
  * Computes light indices for world
  */
-VG_STATIC void world_gen_compute_light_indices(void)
+void world_gen_compute_light_indices( world_instance *world )
 {
    /* light cubes */
-   world_instance *world = world_loading_instance();
    v3f cubes_min, cubes_max;
    v3_muls( world->scene_geo.bbx[0], 1.0f/k_world_light_cube_size, cubes_min );
    v3_muls( world->scene_geo.bbx[1], 1.0f/k_world_light_cube_size, cubes_max );
@@ -434,12 +492,14 @@ VG_STATIC void world_gen_compute_light_indices(void)
                v3f closest;
                closest_point_aabb( light->transform.co, bbx, closest );
 
-               float dist = v3_dist( closest, light->transform.co ),
-                     influence = 1.0f/(dist+1.0f);
+               f32 dist2 = v3_dist2( closest, light->transform.co );
 
-               if( dist > light->range )
+               if( dist2 > light->range*light->range )
                   continue;
 
+               f32 dist = sqrtf(dist2),
+                   influence = 1.0f/(dist+1.0f);
+
                if( light->type == k_light_type_spot){
                   v3f local;
                   m4x3_mulv( light->inverse_world, center, local );
@@ -498,10 +558,10 @@ VG_STATIC void world_gen_compute_light_indices(void)
 /*
  * Rendering pass needed to complete the world
  */
-VG_STATIC void async_world_postprocess_render( void *payload, u32 _size )
+void async_world_postprocess( void *payload, u32 _size )
 {
    /* create scene lighting buffer */
-   world_instance *world = world_loading_instance();
+   world_instance *world = payload;
 
    u32 size = VG_MAX(mdl_arrcount(&world->ent_light),1) * sizeof(float)*12;
    vg_info( "Upload %ubytes (lighting)\n", size );
@@ -565,7 +625,7 @@ VG_STATIC void async_world_postprocess_render( void *payload, u32 _size )
    /* 
     * Rendering the depth map
     */
-   camera ortho;
+   vg_camera ortho;
 
    v3f extent;
    v3_sub( world->scene_geo.bbx[1], world->scene_geo.bbx[0], extent );
@@ -584,8 +644,8 @@ VG_STATIC void async_world_postprocess_render( void *payload, u32 _size )
    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 );
+   vg_camera_update_view( &ortho );
+   vg_camera_finalize( &ortho );
 
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_BLEND);
@@ -609,37 +669,66 @@ VG_STATIC void async_world_postprocess_render( void *payload, u32 _size )
    glBufferSubData( GL_UNIFORM_BUFFER, 0, 
                     sizeof(struct ub_world_lighting), &world->ub_lighting );
 
-   /* yes we are using this as the entity begin thing. FIXME */
-   world->probabilities[ k_probability_curve_constant ] = 1.0f;
-   for( u32 i=0; i<mdl_arrcount(&world->ent_audio); i++ ){
-      ent_audio *audio = mdl_arritm(&world->ent_audio,i);
-      if( audio->flags & AUDIO_FLAG_AUTO_START ){
-         ent_call call;
-         call.data = NULL;
-         call.function = k_ent_function_trigger;
-         call.id = mdl_entity_id( k_ent_audio, i );
-         entity_call( world, &call );
+   /*
+    * Allocate cubemaps
+    */
+   for( u32 i=0; i<mdl_arrcount(&world->ent_cubemap); i++ ){
+      ent_cubemap *cm = mdl_arritm(&world->ent_cubemap,i);
+
+      glGenTextures( 1, &cm->texture_id );
+      glBindTexture( GL_TEXTURE_CUBE_MAP, cm->texture_id );
+      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+               glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+               glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+               glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+               glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
+
+               for( u32 j=0; j<6; j ++ ) {
+                       glTexImage2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, 0, GL_RGB, 
+                        WORLD_CUBEMAP_RES, WORLD_CUBEMAP_RES, 
+                        0, GL_RGB, GL_UNSIGNED_BYTE, NULL );
+               }
+
+               glGenFramebuffers( 1, &cm->framebuffer_id );
+               glBindFramebuffer( GL_FRAMEBUFFER, cm->framebuffer_id );
+               glGenRenderbuffers(1, &cm->renderbuffer_id );
+               glBindRenderbuffer( GL_RENDERBUFFER, cm->renderbuffer_id );
+               glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 
+                              WORLD_CUBEMAP_RES, WORLD_CUBEMAP_RES );
+
+               glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
+                       GL_TEXTURE_CUBE_MAP_POSITIVE_X, cm->texture_id, 0 );
+               glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 
+                                 GL_RENDERBUFFER, cm->renderbuffer_id );
+
+               glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 
+         GL_TEXTURE_CUBE_MAP_POSITIVE_X, cm->texture_id, 0 );
+
+               if( glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE ){
+         vg_error( "Cubemap framebuffer incomplete.\n" );
       }
    }
+
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
 }
 
 /* Loads textures from the pack file */
-VG_STATIC void world_gen_load_surfaces(void)
+void world_gen_load_surfaces( world_instance *world )
 {
-   world_instance *world = world_loading_instance();
    vg_info( "Loading textures\n" );
    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) );
+   world->textures[0] = vg.tex_missing;
 
-   vg_tex2d_replace_with_error( &world->textures[0] );
-
-   for( u32 i=0; i<mdl_arrcount(&world->meta.textures); i++ ){
+   for( u32 i=0; i<mdl_arrcount(&world->meta.textures); i++ )
+   {
       mdl_texture *tex = mdl_arritm( &world->meta.textures, i );
 
-      if( !tex->file.pack_size ){
+      if( !tex->file.pack_size )
+      {
          vg_fatal_error( "World models must have packed textures!" );
       }
 
@@ -663,10 +752,19 @@ VG_STATIC void world_gen_load_surfaces(void)
    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 );
+   for( u32 i=0; i<mdl_arrcount(&world->meta.materials); i++ )
+   {
+      struct world_surface *surf = &world->surfaces[i+1];
+      surf->info = *(mdl_material *)mdl_arritm( &world->meta.materials, i );
+      surf->flags = 0;
+
+      if( surf->info.shader == k_shader_standard_cutout ||
+          surf->info.shader == k_shader_foliage )
+      {
+         struct shader_props_standard *props = surf->info.props.compiled;
+         surf->alpha_tex = props->tex_diffuse;
+      }
+      else
+         surf->alpha_tex = 0;
    }
 }
-
-#endif /* WORLD_GEN_C */