point maps (wip)
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
index e47ebe2739f6b0dabc6744fbd3864788b600d95a..62fe06913bb1e8da83347c9b00dd325c5c0e3e67 100644 (file)
@@ -9,6 +9,7 @@
 #include "world.h"
 #include "world_gate.h"
 #include "font.h"
+#include "pointcloud.h"
 
 #if 0
 #include "shaders/vblend.h"
@@ -84,13 +85,17 @@ VG_STATIC void world_routes_time_lap( world_instance *world, ent_route *route )
    u32 valid_count=0;
 
    for( u32 i=0; i<route->checkpoints_count; i++ ){
-      u32 cpid  = route->checkpoints_start+(i+route->active_checkpoint);
-          cpid  = cpid % route->checkpoints_count;
+      u32 cpid  = (i+route->active_checkpoint) % route->checkpoints_count;
+          cpid += route->checkpoints_start;
 
       ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, cpid );
       ent_gate *rg = mdl_arritm( &world->ent_gate, cp->gate_index );
                 rg = mdl_arritm( &world->ent_gate, rg->target );
 
+      if( i == 1 ){
+         route->timing_base = rg->timing_time;
+      }
+
       if( i == 0 )
          start_time = rg->timing_time;
       else{
@@ -102,7 +107,13 @@ VG_STATIC void world_routes_time_lap( world_instance *world, ent_route *route )
       vg_info( "%u %f\n", rg->timing_version, rg->timing_time );
    }
 
-   if( world_global.current_run_version == last_version+1 ) valid_count ++;
+   if( world_global.current_run_version == last_version+1 ){
+      valid_count ++;
+
+      if( route->checkpoints_count == 1 ){
+         route->timing_base = world_global.time;
+      }
+   }
    else valid_count = 0;
 
    vg_info( "%u %f\n", world_global.current_run_version, world_global.time );
@@ -113,7 +124,6 @@ VG_STATIC void world_routes_time_lap( world_instance *world, ent_route *route )
    }
 
    route->valid_checkpoints = valid_count+1;
-   route->timing_base = start_time;
 
    vg_info( "valid: %u\n", valid_count );
    vg_info( "----------------------------\n" );
@@ -125,10 +135,19 @@ VG_STATIC void world_routes_time_lap( world_instance *world, ent_route *route )
 VG_STATIC void world_routes_activate_entry_gate( world_instance *world, 
                                                  ent_gate *rg )
 {
-   ent_gate *dest = mdl_arritm( &world->ent_gate, rg->target );
-
    world_global.last_use = world_global.time;
 
+   /* disable all routes and leave the world */
+   if( rg->type == k_gate_type_nonlocel ){
+      for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
+         ent_route *route = mdl_arritm( &world->ent_route, i );
+         route->active_checkpoint = 0xffff;
+      }
+      return;
+   }
+
+   ent_gate *dest = mdl_arritm( &world->ent_gate, rg->target );
+
    for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
       ent_route *route = mdl_arritm( &world->ent_route, i );
 
@@ -210,8 +229,11 @@ VG_STATIC void world_routes_debug( world_instance *world )
    }
 }
 
-VG_STATIC void world_routes_place_curve( world_instance *world,
-                                         v4f h[3], v3f n0, v3f n2 )
+VG_STATIC 
+void world_routes_place_curve( world_instance *world,
+                               v4f h[3], v3f n0, v3f n2, scene_context *scene,
+                               struct pointcloud_vert *points, 
+                               u32 *point_count, v4f colour )
 {
    float t;
    v3f p, pd;
@@ -271,13 +293,44 @@ VG_STATIC void world_routes_place_curve( world_instance *world,
 
       int resa = ray_world( world, sa, down, &ha ),
           resb = ray_world( world, sb, down, &hb );
+      
+      for( u32 j=0; j<10; j++ ){
+      if( *point_count < POINTCLOUD_POINTS ){
+         struct pointcloud_vert *vert = &points[*point_count];
+         *point_count = (*point_count) + 1;
+
+         v3f sample, jitter, pcpoint;
+         vg_rand_sphere( jitter );
+         v3_muladds( ha.pos, jitter, 8.0f, sample );
+
+         if( bh_closest_point( world->geo_bh, sample, pcpoint, 10.0f ) == -1 ){
+            v3_copy( sample, pcpoint );
+         }
+
+         v3f pos, vol;
+         v3_sub( world->scene_geo.bbx[1], world->scene_geo.bbx[0], vol );
+         v3_sub( pcpoint, world->scene_geo.bbx[0], pos );
+         v3_div( pos, vol, pos );
+
+         for( u32 i=0; i<3; i++ ){
+            vert->pos[i] = vg_clampf(pos[i], 0.0f,1.0f) * 65535.0f;
+            vert->norm[i] = ha.normal[i] * 127.0f;
+         }
+
+         float dist = 1.0f-(v3_length(jitter));
+
+         for( u32 i=0; i<4; i++ ){
+            vert->colour[i] = colour[i] * 255.0f * dist*dist;
+         }
+      }
+      }
 
       if( resa && resb ){
          struct world_surface *surfa = ray_hit_surface( world, &ha ),
                               *surfb = ray_hit_surface( world, &hb );
 
-         if( (surfa->info.flags & k_material_flag_skate_surface) &&
-             (surfb->info.flags & k_material_flag_skate_surface) )
+         if( (surfa->info.flags & k_material_flag_skate_target) &&
+             (surfb->info.flags & k_material_flag_skate_target) )
          {
             scene_vert va, vb;
 
@@ -295,18 +348,18 @@ VG_STATIC void world_routes_place_curve( world_instance *world,
             vb.uv[0] = t1;
             vb.uv[1] = 1.0f;
 
-            scene_push_vert( world->scene_lines, &va );
-            scene_push_vert( world->scene_lines, &vb );
+            scene_push_vert( scene, &va );
+            scene_push_vert( scene, &vb );
 
             if( last_valid ){
                /* Connect them with triangles */
-               scene_push_tri( world->scene_lines, (u32[3]){ 
+               scene_push_tri( scene, (u32[3]){ 
                      last_valid+0-2, last_valid+1-2, last_valid+2-2} );
-               scene_push_tri( world->scene_lines, (u32[3]){ 
+               scene_push_tri( scene, (u32[3]){ 
                      last_valid+1-2, last_valid+3-2, last_valid+2-2} );
             }
             
-            last_valid = world->scene_lines->vertex_count;
+            last_valid = scene->vertex_count;
          }
          else
             last_valid = 0;
@@ -325,9 +378,19 @@ VG_STATIC void world_routes_place_curve( world_instance *world,
    }
 }
 
-VG_STATIC void world_routes_create_mesh( world_instance *world, u32 route_id )
+VG_STATIC 
+void world_routes_create_mesh( world_instance *world, u32 route_id, 
+                               scene_context *sc,
+                               struct pointcloud_vert *points, 
+                               u32 *point_count )
 {
    ent_route *route = mdl_arritm( &world->ent_route, route_id );
+   u8 colour[4];
+   colour[0] = route->colour[0] * 255.0f;
+   colour[1] = route->colour[1] * 255.0f;
+   colour[2] = route->colour[2] * 255.0f;
+   colour[3] = route->colour[3] * 255.0f;
+
    u32 last_valid = 0;
 
    for( int i=0; i<route->checkpoints_count; i++ ){
@@ -430,23 +493,36 @@ VG_STATIC void world_routes_create_mesh( world_instance *world, u32 route_id )
          v3_normalize( n0 );
          v3_normalize( n2 );
 
-         world_routes_place_curve( world, p, n0, n2 );
+         world_routes_place_curve( world, p, n0, n2, sc, points, point_count,
+                                   route->colour );
 
          /* --- */
          v4_copy( p[2], p[0] );
       }
    }
 
-   scene_copy_slice( world->scene_lines, &route->sm );
+   scene_copy_slice( sc, &route->sm );
 }
 
+VG_STATIC 
+struct world_surface *world_tri_index_surface( world_instance *world, 
+                                                 u32 index );
 /* 
  * Create the strips of colour that run through the world along course paths
  */
 VG_STATIC void world_routes_generate( world_instance *world )
 {
    vg_info( "Generating route meshes\n" );
-   world->scene_lines = scene_init( world_global.generic_heap, 200000, 300000 );
+   vg_async_stall();
+
+   vg_rand_seed( 2000 );
+   vg_async_item *call = scene_alloc_async( &world->scene_lines, 
+                                            &world->mesh_route_lines,
+                                            200000, 300000 );
+   vg_async_item *call1 = 
+      vg_async_alloc( sizeof(struct pointcloud_vert)*POINTCLOUD_POINTS );
+   u32 generated_points = 0;
+   struct pointcloud_vert *cloud_data = call1->payload;
 
    for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
       ent_gate *gate = mdl_arritm( &world->ent_gate, i );
@@ -487,15 +563,117 @@ VG_STATIC void world_routes_generate( world_instance *world )
       }
    }
 
-   for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ )
-      world_routes_create_mesh( world, i );
+   for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
+      world_routes_create_mesh( world, i, &world->scene_lines,
+                                cloud_data, &generated_points );
+   }
+
 
-   vg_acquire_thread_sync();
    {
-      scene_upload( world->scene_lines, &world->mesh_route_lines );
+   vg_info( "Executing awesome algorithm!\n" );
+
+   f32 accum = 0.0f;
+   
+   v3f vol;
+   v3_sub( world->scene_geo.bbx[1], world->scene_geo.bbx[0], vol );
+   v3_div( (v3f){1.0f,1.0f,1.0f}, vol, vol );
+
+   u8 colour[] = { 80,80,80,255 };
+   v3f light_dir = {0.3f,0.8f,0.1f};
+   v3_normalize( light_dir );
+
+   for( u32 k=1; k<=10; k++ ){
+      f32 rate = 50.0f * (1.0f/(float)k);
+
+      for( u32 i=0; i<world->scene_geo.indice_count/3; i++ ){
+         u32 *tri = &world->scene_geo.arrindices[i*3];
+         struct world_surface *surf = world_tri_index_surface( world, tri[0] );
+
+         if( surf->info.shader == k_shader_boundary ||
+             surf->info.shader == k_shader_invisible ) continue;
+
+         if( !(surf->info.flags & k_material_flag_preview_visibile) ) continue;
+
+         scene_vert *va = &world->scene_geo.arrvertices[tri[0]],
+                    *vb = &world->scene_geo.arrvertices[tri[1]],
+                    *vc = &world->scene_geo.arrvertices[tri[2]];
+
+         v3f v0, v1, vn;
+         v3_sub( vb->co, va->co, v0 );
+         v3_sub( vc->co, va->co, v1 );
+         v3_cross( v0, v1, vn );
+         if( vn[1] < 0.0f ) continue;
+
+         accum += v3_length(vn)*0.5f;
+
+         v3_normalize( vn );
+
+         while( accum > rate ){
+            accum -= rate;
+
+            if( generated_points >= POINTCLOUD_POINTS ){
+               goto too_many_points;
+            }
+
+            v2f co = { vg_randf64(), vg_randf64() };
+            if( v2_length2(co) > 0.5f ){
+               co[0] = 1.0f-co[0];
+               co[1] = 1.0f-co[1];
+            }
+
+            v3f pt;
+            v3_muls( v0, co[0], pt );
+            v3_muladds( pt, v1, co[1], pt );
+            v3_add( va->co, pt, pt );
+
+            if( pt[1] < world->water.height ) continue;
+            struct pointcloud_vert *vert = &cloud_data[generated_points ++];
+
+            v3f pos;
+            v3_sub( pt, world->scene_geo.bbx[0], pos );
+            v3_mul( pos, vol, pos );
+
+            for( u32 j=0; j<3; j++ ){
+               vert->pos[j] = vg_clampf(pos[j],0.0f,1.0f) * 65535.0f;
+               vert->norm[j] = vg_clampf(vn[j],-1.0f,1.0f) * 127.0f;
+            }
+
+            f32 brightness = vg_clampf( v3_dot( vn, light_dir ), 0.0f, 1.0f );
+
+            v4f col = {0.0f,0.0f,0.0f,0.0f};
+            if( surf->info.surface_prop == k_surface_prop_wood ){
+               v4_copy( (v4f){0.66f,0.6f,0.5f}, col );
+            }
+            else if( surf->info.surface_prop == k_surface_prop_grass ){
+               v4_copy( (v4f){0.42f,0.5f,0.33f}, col );
+            }
+            else if( surf->info.surface_prop == k_surface_prop_concrete ){
+               v4_copy( (v4f){0.57f,0.57f,0.55f}, col );
+            }
+            else if( surf->info.surface_prop == k_surface_prop_metal ){
+               v4_copy( (v4f){0.76f,0.76f,0.75f}, col );
+            }
+            else{
+               v4_copy( (v4f){0.2f,0.2f,0.2f}, col );
+            }
+
+            v4_copy( (v4f){0.2f,0.2f,0.26f}, col );
+            v3_muls( col, brightness*0.25f, col );
+
+            for( u32 j=0; j<4; j++ ){
+               vert->colour[j] = col[j] * 255.0f;
+            }
+         }
+      }
    }
-   vg_release_thread_sync();
-   vg_linear_del( world_global.generic_heap, world->scene_lines );
+
+too_many_points:
+   vg_info( "finished awesome algorithm! yipee (%u)!\n", generated_points );
+   }
+
+   call1->size = generated_points;
+   vg_async_dispatch( call, async_scene_upload );
+   vg_async_dispatch( call1, async_pointcloud_sub );
 
    world_routes_clear( world );
 }
@@ -556,11 +734,6 @@ VG_STATIC void world_routes_ent_init( world_instance *world )
 
    for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
       ent_gate *gate = mdl_arritm( &world->ent_gate, i );
-
-      vg_info( "ROUTES :: %hu %hu %hu %hu\n", gate->routes[0],
-                                              gate->routes[1],
-                                              gate->routes[2],
-                                              gate->routes[3] );
    }
 
    world_routes_clear( world );
@@ -575,7 +748,7 @@ VG_STATIC void world_routes_ent_init( world_instance *world )
 VG_STATIC void world_routes_init(void)
 {
    world_global.current_run_version = 200;
-   world_global.time = RESET_MAX_TIME*2.0;
+   world_global.time = 300.0;
    world_global.last_use = 0.0;
 
    shader_scene_route_register();
@@ -625,7 +798,7 @@ VG_STATIC void world_routes_fixedupdate( world_instance *world )
    rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
 
    for( int i=0; i<rb_contact_count; i++ ){
-      rb_contact_restitution( rb_contact_buffer+i, vg_randf() );
+      rb_contact_restitution( rb_contact_buffer+i, vg_randf64() );
    }
 
    for( int i=0; i<6; i++ ){
@@ -807,13 +980,13 @@ VG_STATIC void world_routes_fracture( world_instance *world, ent_gate *gate,
             v3_muls( origin, -1.0f, particle->mlocal[3] );
 
             v3_copy( world_co, particle->obj.rb.co );
-            v3_muls( imp_v, 1.0f+vg_randf(), particle->obj.rb.v );
+            v3_muls( imp_v, 1.0f+vg_randf64(), particle->obj.rb.v );
             particle->obj.rb.v[1] += 2.0f;
 
             v4_copy( q, particle->obj.rb.q );
-            particle->obj.rb.w[0] = vg_randf()*2.0f-1.0f;
-            particle->obj.rb.w[1] = vg_randf()*2.0f-1.0f;
-            particle->obj.rb.w[2] = vg_randf()*2.0f-1.0f;
+            particle->obj.rb.w[0] = vg_randf64()*2.0f-1.0f;
+            particle->obj.rb.w[1] = vg_randf64()*2.0f-1.0f;
+            particle->obj.rb.w[2] = vg_randf64()*2.0f-1.0f;
 
             particle->obj.type = k_rb_shape_sphere;
             particle->obj.inf.sphere.radius = r*0.6f;
@@ -846,8 +1019,6 @@ VG_STATIC void render_world_routes( world_instance *world, camera *cam,
    shader_scene_route_uPvmPrev( cam->mtx_prev.pv );
    shader_scene_route_uMdl( identity_matrix );
    shader_scene_route_uCamera( cam->transform[3] );
-   shader_scene_route_uBoard0( TEMP_BOARD_0 );
-   shader_scene_route_uBoard1( TEMP_BOARD_1 );
 
    mesh_bind( &world->mesh_route_lines );
 
@@ -880,7 +1051,7 @@ VG_STATIC void render_world_routes( world_instance *world, camera *cam,
                              cam, text->transform );
       }
 
-      shader_model_font_uOffset( (v3f){0.0f,0.0f,0.0f} );
+      shader_model_font_uOffset( (v4f){0.0f,0.0f,0.0f,1.0f} );
 
       for( u32 i=0; i<world_global.text_particle_count; i++ ){
          struct text_particle *particle = &world_global.text_particles[i];