]> git.tdb.fi Git - libs/gl.git/blobdiff - shaderlib/phong.glsl
Add a new, better structured Phong shader
[libs/gl.git] / shaderlib / phong.glsl
diff --git a/shaderlib/phong.glsl b/shaderlib/phong.glsl
new file mode 100644 (file)
index 0000000..e3b97f5
--- /dev/null
@@ -0,0 +1,120 @@
+import msp_interface;
+import common;
+import shadow;
+
+const bool use_diffuse_map = false;
+const bool use_specular = false;
+const bool use_specular_map = false;
+const bool use_shininess_map = false;
+const bool use_emission = false;
+const bool use_emission_map = false;
+const bool use_reflectivity = false;
+const bool use_reflectivity_map = false;
+const bool use_sky = false;
+const bool use_fog = false;
+
+#pragma MSP stage(fragment)
+vec4 get_diffuse_color()
+{
+       if(use_diffuse_map)
+               return texture(diffuse_map, texcoord.xy);
+       else
+               return basic_material.diffuse;
+}
+
+vec3 get_specular_color()
+{
+       if(use_specular_map)
+               return texture(specular_map, texcoord.xy).rgb;
+       else
+               return basic_material.specular.rgb;
+}
+
+float get_shininess_value()
+{
+       if(use_shininess_map)
+               return texture(shininess_map, texcoord.xy).r*255.0;
+       else
+               return basic_material.shininess;
+}
+
+vec3 get_emission_color()
+{
+       if(use_emission_map)
+               return texture(emission_map, texcoord.xy).rgb;
+       else
+               return basic_material.emission.rgb;
+}
+
+float get_reflectivity_value()
+{
+       if(use_reflectivity_map)
+               return texture(reflectivity_map, texcoord.xy).r;
+       else
+               return basic_material.reflectivity;
+}
+
+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)
+{
+       float diffuse_intensity = max(dot(light, normal), 0.0);
+       vec3 color = light_color*surface_diffuse*diffuse_intensity;
+       if(use_specular)
+       {
+               /* The light vector points towards the light, so reflected will point
+               towards the surface - but so does the look vector. */
+               vec3 reflected = reflect(light, normal);
+               float specular_intensity = pow(max(dot(reflected, look), 0.0), shininess);
+               color += light_color*surface_specular*specular_intensity;
+       }
+       return color;
+}
+
+vec3 phong_lighting(vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_specular, float shininess)
+{
+       vec3 light;
+       if(use_normal_map)
+               light = normalize(tbn_light_dir);
+       else
+               light = normalize(eye_light_dir);
+
+       vec3 color = phong_ambient(surface_diffuse);
+       float shadow = get_shadow_factor(0);
+       color += phong_one_light(light, normal, look, light_sources[0].diffuse.rgb, surface_diffuse, surface_specular, shininess)*shadow;
+
+       if(use_emission)
+               color += get_emission_color();
+
+       if(use_reflectivity)
+               color += get_reflection(normal, look)*get_reflectivity_value();
+
+       return color;
+}
+
+void main()
+{
+       vec3 normal;
+       vec3 look;
+       if(use_normal_map)
+       {
+               normal = get_normal_sample();
+               look = normalize(tbn_look_dir);
+       }
+       else
+       {
+               normal = normalize(eye_normal);
+               look = normalize(eye_look_dir);
+       }
+
+       vec4 surface_diffuse = get_diffuse_color();
+       vec3 surface_specular = get_specular_color();
+       float shininess = get_shininess_value();
+
+       vec3 lit_color = phong_lighting(normal, look, surface_diffuse.rgb, surface_specular, shininess);
+
+       frag_color = vec4(lit_color, surface_diffuse.a);
+}