now we're doing a bunch of them
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_world.glsl
index baffd66001af0c7dbd5b680f5dc1408b022b3223..043cf26898971b270032a2b5ff5bd78c4bacd202 100644 (file)
@@ -12,43 +12,26 @@ layout (std140) uniform ub_world_lighting
    int g_light_count;
    int g_light_preview;
    int g_shadow_samples;
-};
-
-uniform sampler2D g_world_depth;
-
-// Standard diffuse + spec models
-// ==============================
 
-vec3 do_light_diffuse( vec3 vfrag, vec3 wnormal )
-{
-   vec3 vtotal = g_ambient_colour.rgb;
+   // g_time ?
 
-   for( int i=0; i<g_light_count; i++ )
-   {
-      vec3 vcolour = g_light_colours[i].rgb;
-      vec3 vdir = g_light_directions[i].xyz;
-
-      float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);
-      vtotal += vcolour*flight;
-   }
+   //vec4 g_point_light_positions[32];
+   //vec4 g_point_light_colours[32];
+};
 
-   return vfrag * vtotal;
-}
+uniform sampler2D g_world_depth;
 
-vec3 do_light_spec( vec3 vfrag, vec3 wnormal, vec3 halfview, float fintensity )
+float world_depth_sample( vec3 pos )
 {
-   vec3 vcolour = g_light_colours[0].rgb;
-   vec3 vdir = g_light_directions[0].xyz;
-
-   vec3 specdir = reflect( -vdir, wnormal );
-   float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);
-   return vfrag + vcolour*spec*fintensity;
+   vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; 
+   return texture( g_world_depth, depth_coord ).r;
 }
 
-float world_depth_sample( vec3 pos )
+float world_water_depth( vec3 pos )
 {
    vec2 depth_coord = (pos.xz - g_depth_bounds.xy) * g_depth_bounds.zw; 
-   return texture( g_world_depth, depth_coord ).r;
+   float ref_depth = g_water_plane.y*g_water_plane.w;
+   return texture( g_world_depth, depth_coord ).g - ref_depth;
 }
 
 float shadow_sample( vec3 vdir )
@@ -60,34 +43,25 @@ float shadow_sample( vec3 vdir )
    return clamp( fdelta, 0.1, 0.2 )-0.1;
 }
 
-vec3 do_light_shadowing_old( vec3 vfrag )
+vec3 apply_fog( vec3 vfrag, float fdist )
 {
-   float faccum = 0.0;
-   faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 ));
-   faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 ));
-   faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 ));
-   faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 ));
-   faccum += shadow_sample( vec3( 0.0, 0.5, 0.0 )*1.5);
-   faccum += shadow_sample( vec3( 2.0, 0.3, 0.0 )*1.5);
-   faccum += shadow_sample( vec3( 3.0, 1.0, 0.0 )*1.5);
-   faccum += shadow_sample( vec3( 5.0, 1.0, 0.0 )*1.5);
-   return mix( vfrag, g_ambient_colour.rgb, faccum );
+   float dist = pow(fdist*0.0008,1.2);
+   return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );
 }
 
-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 );
+// New lighting model
+
+vec3 newlight_compute_ambient()
+{
+   return g_ambient_colour.rgb;
 }
 
-vec3 do_light_shadowing( vec3 vfrag )
+float newlight_compute_sun_shadow()
 {
    if( g_shadow_samples == 0 )
    {
-      return vfrag;
+      return 1.0;
    }
 
    float fspread = g_light_colours[0].w;
@@ -104,17 +78,43 @@ vec3 do_light_shadowing( vec3 vfrag )
    famt+=shadow_sample((vdir+vec3( 0.522,-0.379, 0.350)*fspread)*flength*0.7);
    famt+=shadow_sample((vdir+vec3( 0.483, 0.201, 0.306)*fspread)*flength*0.8);
 
-   // 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 - famt;
+}
+
+vec3 newlight_compute_world_diffuse( vec3 wnormal )
+{
+   vec3 vtotal = g_ambient_colour.rgb;
 
-   famt = max( player_shadow*0.8, famt );
-   return mix( vfrag, g_ambient_colour.rgb, famt );
+   for( int i=0; i<g_light_count; i++ )
+   {
+      vec3 vcolour = g_light_colours[i].rgb;
+      vec3 vdir = g_light_directions[i].xyz;
+
+      float flight = max(dot( vdir, wnormal )*0.75+0.25,0.0);
+      vtotal += vcolour*flight;
+   }
+
+   return vtotal;
 }
 
-vec3 apply_fog( vec3 vfrag, float fdist )
+vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )
 {
-   float dist = pow(fdist*0.0008,1.2);
-   return mix( vfrag, vec3(0.55,0.76,1.0), min( 1.0, dist ) );
+   vec3 vcolour = g_light_colours[0].rgb;
+   vec3 vdir = g_light_directions[0].xyz;
+
+   vec3 specdir = reflect( -vdir, wnormal );
+   float spec = pow(max(dot( halfview, specdir ), 0.0), 10.0);
+   return vcolour*spec*fintensity;
+}
+
+vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, 
+                                 vec3 light_pos, vec3 light_colour )
+{
+   vec3 light_delta = (light_pos-aWorldCo) * 10.0;
+
+   float quadratic = dot(light_delta,light_delta);
+   float attenuation = 1.0f/( 1.0f + quadratic );
+   attenuation *= max( 0.0, dot( normalize(light_delta), wnormal ) );
+
+   return light_colour*attenuation;
 }