]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/phong.glsl
Support automatic generation of interface blocks
[libs/gl.git] / shaderlib / phong.glsl
1 import msp_interface;
2 import common;
3 import shadow;
4
5 const bool use_diffuse_map = false;
6 const bool use_specular = false;
7 const bool use_specular_map = false;
8 const bool use_shininess_map = false;
9 const bool use_emission = false;
10 const bool use_emission_map = false;
11 const bool use_reflectivity = false;
12 const bool use_reflectivity_map = false;
13 const bool use_sky = false;
14 const bool use_fog = false;
15
16 #pragma MSP stage(fragment)
17 vec4 get_diffuse_color()
18 {
19         if(use_diffuse_map)
20                 return texture(diffuse_map, texcoord.xy);
21         else
22                 return basic_material.diffuse;
23 }
24
25 vec3 get_specular_color()
26 {
27         if(use_specular_map)
28                 return texture(specular_map, texcoord.xy).rgb;
29         else
30                 return basic_material.specular.rgb;
31 }
32
33 float get_shininess_value()
34 {
35         if(use_shininess_map)
36                 return texture(shininess_map, texcoord.xy).r*255.0;
37         else
38                 return basic_material.shininess;
39 }
40
41 vec3 get_emission_color()
42 {
43         if(use_emission_map)
44                 return texture(emission_map, texcoord.xy).rgb;
45         else
46                 return basic_material.emission.rgb;
47 }
48
49 float get_reflectivity_value()
50 {
51         if(use_reflectivity_map)
52                 return texture(reflectivity_map, texcoord.xy).r;
53         else
54                 return basic_material.reflectivity;
55 }
56
57 vec3 phong_ambient(vec3 surface_diffuse)
58 {
59         return ambient_color.rgb*surface_diffuse;
60 }
61
62 vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 light_color, vec3 surface_diffuse, vec3 surface_specular, float shininess)
63 {
64         float diffuse_intensity = max(dot(light, normal), 0.0);
65         vec3 color = light_color*surface_diffuse*diffuse_intensity;
66         if(use_specular)
67         {
68                 /* The light vector points towards the light, so reflected will point
69                 towards the surface - but so does the look vector. */
70                 vec3 reflected = reflect(light, normal);
71                 float specular_intensity = pow(max(dot(reflected, look), 0.0), shininess);
72                 color += light_color*surface_specular*specular_intensity;
73         }
74         return color;
75 }
76
77 vec3 phong_lighting(vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_specular, float shininess)
78 {
79         vec3 light;
80         if(use_normal_map)
81                 light = normalize(tbn_light_dir);
82         else
83                 light = normalize(eye_light_dir);
84
85         vec3 color = phong_ambient(surface_diffuse);
86         float shadow = get_shadow_factor(0);
87         color += phong_one_light(light, normal, look, light_sources[0].diffuse.rgb, surface_diffuse, surface_specular, shininess)*shadow;
88
89         if(use_emission)
90                 color += get_emission_color();
91
92         if(use_reflectivity)
93                 color += get_reflection(normal, look)*get_reflectivity_value();
94
95         return color;
96 }
97
98 void main()
99 {
100         vec3 normal;
101         vec3 look;
102         if(use_normal_map)
103         {
104                 normal = get_fragment_normal();
105                 look = normalize(tbn_look_dir);
106         }
107         else
108         {
109                 normal = normalize(eye_normal);
110                 look = normalize(eye_look_dir);
111         }
112
113         vec4 surface_diffuse = get_diffuse_color();
114         vec3 surface_specular = get_specular_color();
115         float shininess = get_shininess_value();
116
117         vec3 lit_color = phong_lighting(normal, look, surface_diffuse.rgb, surface_specular, shininess);
118
119         frag_color = vec4(lit_color, surface_diffuse.a);
120 }