]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/phong.glsl
Split the Light class into subclasses by light type
[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 color = phong_ambient(surface_diffuse);
98         for(int i=0; i<max_lights; ++i)
99                 if(light_sources[i].type!=0)
100                 {
101                         vec3 light = get_light_direction(i);
102                         float shadow = get_shadow_factor(i);
103                         color += phong_one_light(light, normal, look, light_sources[i].color, surface_diffuse, surface_specular, shininess)*shadow;
104                 }
105
106         if(use_emission)
107                 color += get_emission_color();
108
109         if(use_reflectivity)
110                 color += get_reflection(normal, look)*get_reflectivity_value();
111
112         return color;
113 }
114
115 void main()
116 {
117         vec3 normal = get_fragment_normal();
118         vec3 look = normalize(world_look_dir);
119
120         vec4 surface_diffuse = get_diffuse_color();
121         vec3 surface_specular = get_specular_color();
122         float shininess = get_shininess_value();
123
124         vec3 lit_color = phong_lighting(normal, look, surface_diffuse.rgb, surface_specular, shininess);
125
126         frag_color = vec4(lit_color, surface_diffuse.a);
127 }