needs a lot of cleaning but lights are OK
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / common_world.glsl
index 043cf26898971b270032a2b5ff5bd78c4bacd202..43934098a427d5d5839a2736ed105961f2a136fc 100644 (file)
@@ -9,6 +9,7 @@ layout (std140) uniform ub_world_lighting
    vec4 g_water_plane;
    vec4 g_depth_bounds;
    float g_water_fog;
+   float g_time;
    int g_light_count;
    int g_light_preview;
    int g_shadow_samples;
@@ -57,7 +58,7 @@ vec3 newlight_compute_ambient()
    return g_ambient_colour.rgb;
 }
 
-float newlight_compute_sun_shadow()
+float newlight_compute_sun_shadow( vec3 dir )
 {
    if( g_shadow_samples == 0 )
    {
@@ -65,7 +66,7 @@ float newlight_compute_sun_shadow()
    }
 
    float fspread = g_light_colours[0].w;
-   vec3  vdir = g_light_directions[0].xyz;
+   vec3  vdir = dir;
    float flength = g_light_directions[0].w;
 
    float famt = 0.0;
@@ -107,8 +108,14 @@ vec3 newlight_compute_sun_spec( vec3 wnormal, vec3 halfview, float fintensity )
    return vcolour*spec*fintensity;
 }
 
+float newlight_specular( vec3 wnormal, vec3 dir, vec3 halfview, float exponent )
+{
+   vec3 specdir = reflect( -dir, wnormal );
+   return pow(max(dot( halfview, specdir ), 0.0), exponent);
+}
+
 vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview, 
-                                 vec3 light_pos, vec3 light_colour )
+                                 vec3 light_colour, vec3 light_pos )
 {
    vec3 light_delta = (light_pos-aWorldCo) * 10.0;
 
@@ -118,3 +125,21 @@ vec3 newlight_compute_quadratic( vec3 wnormal, vec3 halfview,
 
    return light_colour*attenuation;
 }
+
+vec3 newlight_compute_spot( vec3 wnormal, vec3 halfview, 
+                            vec3 light_colour, vec3 light_pos,
+                            vec4 light_dir )
+{
+   vec3 light_delta = (light_pos-aWorldCo) * 10.0;
+
+   float quadratic = dot(light_delta,light_delta);
+   float attenuation = 1.0f/( 1.0f + quadratic );
+
+   light_delta = normalize( light_delta );
+   attenuation *= max( 0.0, dot( light_delta, wnormal ) );
+
+   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*attenuation*falloff;
+}