grid based
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_scene.glsl
index c71f5e2dbb4dbbd3359be1afa933eaaf5ba6f1c3..8be69c1bfa16eb9f4f69009b240ea22f82bc1061 100644 (file)
@@ -4,10 +4,13 @@ in vec2 aUv;
 in vec4 aNorm;
 in vec3 aCo;
 in vec3 aWorldCo;
-flat in vec4 light_colours[3];
-flat in vec4 light_positions[3];
+flat in ivec4 light_indices;
+
+uniform samplerBuffer uLightsArray;
+uniform usampler3D uLightsIndex;
 
 #include "common_world.glsl"
+#include "light_clearskies.glsl"
 
 float sdLine( vec3 p, vec3 a, vec3 b )
 {
@@ -28,47 +31,124 @@ float compute_board_shadow()
    return 1.0 - player_shadow*0.8;
 }
 
+vec3 scene_apply_fog( vec3 vfrag, vec3 colour, float fdist )
+{
+   float dist = pow(fdist*0.0010,0.78);
+   return mix( vfrag, colour, min( 1.0, dist ) );
+}
+
+vec3 rand33(vec3 p3)
+{
+       p3 = fract(p3 * vec3(.1031, .1030, .0973));
+   p3 += dot(p3, p3.yxz+33.33);
+   return fract((p3.xxy + p3.yxx)*p3.zyx);
+}
+
+vec3 scene_calculate_light( int light_index, 
+                            vec3 halfview, vec3 co, vec3 normal )
+{
+   vec4 light_colour = texelFetch( uLightsArray, light_index+0 );
+   vec4 light_co     = texelFetch( uLightsArray, light_index+1 );
+   vec4 light_dir    = texelFetch( uLightsArray, light_index+2 );
+
+   vec3 light_delta = light_co.xyz-co;
+   float dist2 = dot(light_delta,light_delta);
+
+   light_delta = normalize( light_delta );
+
+   float quadratic = dist2*100.0;
+   float attenuation  = 1.0f/( 1.0f + quadratic );
+         attenuation *= max( dot( light_delta, normal ), 0.0 );
+
+   float falloff = max( 0.0, 1.0-(dist2*light_co.w) );
+
+   if( light_dir.w < 0.999999 )
+   {
+      float spot_theta = max( 0.0, dot( light_delta, -light_dir.xyz ) );
+      falloff *= max( 0.0, (spot_theta - light_dir.w) / (1.0-light_dir.w) );
+   }
+
+   return light_colour.rgb * attenuation * falloff;
+}
+
+vec3 scene_calculate_packed_light_patch( uint packed_index, 
+                                         vec3 halfview, vec3 co, vec3 normal )
+{
+   uint light_count = packed_index & 0x3u;
+
+   vec3 l = vec3(0.0);
+
+   if( light_count >= 1u )
+   {
+      int index_0 = int( ((packed_index >>  2u) & 0x3ffu) * 3u );
+      int index_1 = int( ((packed_index >> 12u) & 0x3ffu) * 3u );
+      int index_2 = int( ((packed_index >> 22u) & 0x3ffu) * 3u );
+
+      l += scene_calculate_light( index_0, halfview, co, normal );
+
+      if( light_count >= 2u )
+      {
+         l += scene_calculate_light( index_1, halfview, co, normal );
+
+         if( light_count >= 3u )
+         {
+            l += scene_calculate_light( index_2, halfview, co, normal );
+         }
+      }
+   }
+
+   return l;
+}
+
 vec3 scene_do_lighting( vec3 diffuse, vec3 wnormal )
 {
+   world_info world;
+   scene_state( g_time, world );
+
    // Lighting
    vec3 halfview = uCamera - aWorldCo;
    float fdist = length(halfview);
    halfview /= fdist;
 
-   vec3 total_light = newlight_compute_ambient();
+   vec3 total_light = vec3(0.0);
+   float world_shadow = newlight_compute_sun_shadow( world.sun_dir 
+                                    * (1.0/(max(world.sun_dir.y,0.0)+0.2)) );
+   float board_shadow = compute_board_shadow();
+
+   total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ), 
+                                  halfview, world );
+
+   vec3 cube_coord = (aWorldCo - g_cube_min.xyz) * g_cube_inv_range.xyz;
+        cube_coord = floor( cube_coord );
+
+   if( g_debug_indices == 1 )
+   {
+      return rand33(cube_coord);
+   }
+
+   if( g_debug_complexity == 1 )
+   {
+      ivec3 coord = ivec3( cube_coord );
+      uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
+
+      uint light_count = (index_sample.x & 0x3u) + (index_sample.y & 0x3u);
+      return vec3( float(light_count)*(1.0/6.0), 0.0, 0.5 );
+   }
+
+   // FIXME: this should absolutely must be clamped!
    
-   // Compute world lighting contribution and apply it according to the
-   // shadow map
-   //
-   vec3 world_light = newlight_compute_world_diffuse( wnormal );
-   world_light += newlight_compute_sun_spec( wnormal, halfview, 0.1 );
+   ivec3 coord = ivec3( cube_coord );
+   uvec4 index_sample = texelFetch( uLightsIndex, coord, 0 );
 
-   float world_shadow = newlight_compute_sun_shadow();
-   float board_shadow = compute_board_shadow();
+   total_light += 
+      scene_calculate_packed_light_patch( index_sample.x,
+                                          halfview, aWorldCo, wnormal ) 
+                                          * board_shadow;
+   total_light += 
+      scene_calculate_packed_light_patch( index_sample.y,
+                                          halfview, aWorldCo, wnormal )
+                                          * board_shadow;
 
-   total_light += world_light * min( board_shadow, world_shadow );
-
-   // Compute the other lights that exist in the map, not effected by the sun
-   // shadow
-
-   total_light += newlight_compute_quadratic
-                  ( 
-                     wnormal, halfview,
-                     light_positions[0].xyz,
-                     light_colours[0].rgb 
-                  ) * board_shadow;
-   total_light += newlight_compute_quadratic
-                  ( 
-                     wnormal, halfview,
-                     light_positions[1].xyz,
-                     light_colours[1].rgb 
-                  ) * board_shadow;
-   total_light += newlight_compute_quadratic
-                  ( 
-                     wnormal, halfview,
-                     light_positions[2].xyz,
-                     light_colours[2].rgb 
-                  ) * board_shadow;
-
-   return apply_fog( diffuse * total_light, fdist );
+   vec3 fog_colour = scene_sky( -halfview, world );
+   return scene_apply_fog( diffuse * total_light, fog_colour, fdist );
 }