checkin
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_scene.glsl
index 4a78fbcf01ca8a721a055dfb21a4b758b52dcb58..91dc6a19cf7116cae9b1a1e5a110418ab4dbc1ae 100644 (file)
 // :D
 
+in vec2 aUv;
+in vec4 aNorm;
+in vec3 aCo;
+in vec3 aWorldCo;
+flat in ivec4 light_indices;
+
+uniform samplerBuffer uLightsArray;
+
 #include "common_world.glsl"
+#include "light_clearskies.glsl"
+
+float sdLine( vec3 p, vec3 a, vec3 b )
+{
+  vec3 pa = p - a;
+  vec3 ba = b - a;
+
+  float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
+  return length( pa - ba*h );
+}
+
+float compute_board_shadow()
+{
+   // player shadow
+   float dist_to_player = max( 0.0, sdLine( aWorldCo, uBoard0, uBoard1 )-0.1 );
+   float player_shadow = max( 1.0-dist_to_player*2.7, 0.0 );
+   player_shadow *= player_shadow*player_shadow*player_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 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();
-   
-   // 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 );
+   vec3 total_light = vec3(0.0);
+
 
-   float world_shadow = newlight_compute_sun_shadow();
+   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 += world_light * world_shadow;
+   total_light += scene_lighting( wnormal, min( board_shadow, world_shadow ), 
+                                  halfview, world );
+
+   //total_light += scene_lighting_old( wnormal, world );
 
    // Compute the other lights that exist in the map, not effected by the sun
    // shadow
-   total_light += newlight_compute_quadratic
-                  ( 
-                     wnormal, halfview,
-                     g_point_light_positions[ aLights.x ].xyz,
-                     g_point_light_colours[ aLights.x ].rgb 
-                  );
-   total_light += newlight_compute_quadratic
-                  ( 
-                     wnormal, halfview,
-                     g_point_light_positions[ aLights.y ].xyz,
-                     g_point_light_colours[ aLights.y ].rgb 
-                  );
-   total_light += newlight_compute_quadratic
-                  ( 
-                     wnormal, halfview,
-                     g_point_light_positions[ aLights.z ].xyz,
-                     g_point_light_colours[ aLights.z ].rgb 
-                  );
-
-   return apply_fog( diffuse * total_light, fdist );
+
+   // read lights
+   vec4 light_colour_0 = texelFetch( uLightsArray, light_indices.x*3+0 );
+   vec4 light_colour_1 = texelFetch( uLightsArray, light_indices.y*3+0 );
+   vec4 light_colour_2 = texelFetch( uLightsArray, light_indices.z*3+0 );
+   vec4 light_co_0     = texelFetch( uLightsArray, light_indices.x*3+1 );
+   vec4 light_co_1     = texelFetch( uLightsArray, light_indices.y*3+1 );
+   vec4 light_co_2     = texelFetch( uLightsArray, light_indices.z*3+1 );
+   vec4 light_dir_0    = texelFetch( uLightsArray, light_indices.x*3+2 );
+   vec4 light_dir_1    = texelFetch( uLightsArray, light_indices.y*3+2 );
+   vec4 light_dir_2    = texelFetch( uLightsArray, light_indices.z*3+2 );
+
+   if( g_debug_indices == 1 )
+   {
+      float rings = min( fract(distance(light_co_0.xyz,aWorldCo)),
+                      min( fract(distance(light_co_1.xyz,aWorldCo)),
+                           fract(distance(light_co_2.xyz,aWorldCo)) ) 
+                        );
+               
+      return vec3(fract(light_indices.x * 0.125), fract(light_indices.y*0.125),
+                  fract(light_indices.z * 0.125 )) + (rings-0.5) * 0.25;
+   }
+
+   if( g_debug_complexity == 1 )
+   {
+      return vec3(1.0,0.0,0.0) * ( light_indices.w/3.0 );
+   }
+
+   if( light_indices.w >= 1 )
+   {
+      total_light += newlight_compute_spot
+                     ( 
+                        wnormal, halfview,
+                        light_colour_0.rgb,
+                        light_co_0.xyz,
+                        light_dir_0
+                     ) * board_shadow 
+                       * step( world.day_phase, light_colour_0.w );
+
+      if( light_indices.w >= 2 )
+      {
+         total_light += newlight_compute_spot
+                        ( 
+                           wnormal, halfview,
+                           light_colour_1.rgb,
+                           light_co_1.xyz,
+                           light_dir_1
+                        ) * board_shadow
+                          * step( world.day_phase, light_colour_1.w );
+
+         if( light_indices.w >= 3 )
+         {
+            total_light += newlight_compute_spot
+                           ( 
+                              wnormal, halfview,
+                              light_colour_2.rgb,
+                              light_co_2.xyz,
+                              light_dir_2
+                           ) * board_shadow
+                             * step( world.day_phase, light_colour_2.w );
+         }
+      }
+   }
+
+   vec3 fog_colour = scene_sky( -halfview, world );
+   
+   return scene_apply_fog( diffuse * total_light, fog_colour, fdist );
 }