checkin
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gen.h
index b6c2f15db41125191eb1b871332a06b43b318500..026701a1bf89148d574543fe9c4120b4d70438ec 100644 (file)
@@ -450,7 +450,7 @@ VG_STATIC float colour_luminance( v3f v )
 }
 
 VG_STATIC float calc_light_influence( world_instance *world, v3f position, 
-                                      v3f normal, int light )
+                                      int light )
 {
    struct world_light *world_light = &world->lights[ light ];
    struct classtype_world_light *inf = world_light->inf;
@@ -462,16 +462,15 @@ VG_STATIC float calc_light_influence( world_instance *world, v3f position,
    float quadratic = v3_dot( light_delta, light_delta ),
          attenuation = 1.0f/( 1.0f + quadratic );
 
-   v3_normalize( light_delta );
-   //attenuation *= vg_maxf( 0.0, v3_dot( light_delta, normal ) );
-
    float quadratic_light = attenuation * colour_luminance( inf->colour );
 
-   if( inf->type == k_light_type_point )
+   if( (inf->type == k_light_type_point) ||
+       (inf->type == k_light_type_point_nighttime_only) )
    {
       return quadratic_light;
    }
-   else if( inf->type == k_light_type_spot )
+   else if( (inf->type == k_light_type_spot) ||
+            (inf->type == k_light_type_spot_nighttime_only) )
    {
       v3f dir;
       q_mulv( world_light->node->q, (v3f){0.0f,1.0f,0.0f}, dir );
@@ -494,31 +493,23 @@ VG_STATIC void world_scene_compute_light_clusters( world_instance *world,
       vert->lights[0] = 0;
       vert->lights[1] = 1;
       vert->lights[2] = 2;
-      vert->lights[3] = 3;
-
-      float influences[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
-
-      v3f co, norm;
-      v3_copy( vert->co, co );
+      vert->lights[3] = 0;
 
-      norm[0] = vert->norm[0];
-      norm[1] = vert->norm[1];
-      norm[2] = vert->norm[2];
-
-      v3_muls( norm, 1.0f/127.0f, norm );
+      float influences[3] = { 0.0f, 0.0f, 0.0f };
+      const int N = vg_list_size( influences );
 
       for( int j=0; j<world->light_count; j ++ )
       {
-         float influence = calc_light_influence( world, co, norm, j );
+         float influence = calc_light_influence( world, vert->co, j );
 
-         int best_pos = 4;
+         int best_pos = N;
          for( int k=best_pos-1; k>=0; k -- )
             if( influence > influences[k] )
                best_pos = k;
 
-         if( best_pos < 4 )
+         if( best_pos < N )
          {
-            for( int k=3; k>best_pos; k -- )
+            for( int k=N-1; k>best_pos; k -- )
             {
                influences[k] = influences[k-1];
                vert->lights[k] = vert->lights[k-1];
@@ -528,6 +519,12 @@ VG_STATIC void world_scene_compute_light_clusters( world_instance *world,
             vert->lights[best_pos] = j;
          }
       }
+
+      for( int j=0; j<N; j++ )
+      {
+         if( influences[j] > 0.00000125f )
+            vert->lights[3] ++ ;
+      }
    }
 }
 
@@ -669,7 +666,17 @@ VG_STATIC void world_post_process( world_instance *world )
 
          /* colour  + night */
          v3_muls( inf->colour, inf->colour[3] * 2.0f, light_dst[i*3+0] );
-         light_dst[i*3+0][3] = 1.0f;
+         light_dst[i*3+0][3] = 0.0f;
+
+         if( (inf->type == k_light_type_spot_nighttime_only) ||
+             (inf->type == k_light_type_point_nighttime_only ) )
+         {
+            u32 hash = (i * 29986577) & 0xff;
+            float switch_on = hash;
+                  switch_on *= (1.0f/255.0f);
+
+            light_dst[i*3+0][3] = 0.44f + switch_on * 0.015f;
+         }
          
          /* position + nothing */
          v3_copy( light->node->co, light_dst[i*3+1] );