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