]> git.tdb.fi Git - libs/gl.git/blobdiff - shaderlib/phong.glsl
Implement alpha cutoff for materials
[libs/gl.git] / shaderlib / phong.glsl
index 87eba5eadf38bce9ba1c6873a4dd18b2dcb2f841..234897116183a4997c625687447b65172ef8036d 100644 (file)
@@ -14,6 +14,7 @@ struct BasicMaterialParameters
 uniform BasicMaterial
 {
        BasicMaterialParameters basic_material;
+       float alpha_cutoff;
 };
 
 uniform sampler2D diffuse_map;
@@ -30,8 +31,7 @@ layout(constant_id=auto) const bool use_emission = false;
 layout(constant_id=auto) const bool use_emission_map = false;
 layout(constant_id=auto) const bool use_reflectivity = false;
 layout(constant_id=auto) const bool use_reflectivity_map = false;
-layout(constant_id=auto) const bool use_sky = false;
-layout(constant_id=auto) const bool use_fog = false;
+layout(constant_id=auto) const bool use_alpha_cutoff = false;
 
 #pragma MSP stage(fragment)
 virtual vec4 get_diffuse_color()
@@ -79,15 +79,15 @@ vec3 phong_ambient(vec3 surface_diffuse)
        return ambient_color.rgb*surface_diffuse;
 }
 
-vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 light_color, vec3 surface_diffuse, vec3 surface_specular, float shininess)
+vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_specular, float shininess)
 {
        float diffuse_intensity = max(dot(light, normal), 0.0);
-       vec3 color = light_color*surface_diffuse*diffuse_intensity;
+       vec3 color = surface_diffuse*diffuse_intensity;
        if(use_specular)
        {
                vec3 reflected = reflect(look, normal);
                float specular_intensity = pow(max(dot(reflected, light), 0.0), shininess);
-               color += light_color*surface_specular*specular_intensity;
+               color += surface_specular*specular_intensity;
        }
        return color;
 }
@@ -96,11 +96,11 @@ vec3 phong_lighting(vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_s
 {
        vec3 color = phong_ambient(surface_diffuse);
        for(int i=0; i<max_lights; ++i)
-               if(light_sources[i].enabled!=0)
+               if(light_sources[i].type!=0)
                {
-                       vec3 light = get_light_direction(i);
-                       float shadow = get_shadow_factor(i);
-                       color += phong_one_light(light, normal, look, light_sources[i].color, surface_diffuse, surface_specular, shininess)*shadow;
+                       IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
+                       float shadow = get_shadow_factor(i, world_vertex);
+                       color += phong_one_light(incoming.direction, normal, look, surface_diffuse, surface_specular, shininess)*incoming.color*shadow;
                }
 
        if(use_emission)
@@ -114,10 +114,13 @@ vec3 phong_lighting(vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_s
 
 void main()
 {
+       vec4 surface_diffuse = get_diffuse_color();
+       if(use_alpha_cutoff && surface_diffuse.a<alpha_cutoff)
+               discard;
+
        vec3 normal = get_fragment_normal();
        vec3 look = normalize(world_look_dir);
 
-       vec4 surface_diffuse = get_diffuse_color();
        vec3 surface_specular = get_specular_color();
        float shininess = get_shininess_value();