]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/phong.glsl
Set array and matrix strides for default-block uniforms
[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 = normalize(world_light_dir);
98
99         vec3 color = phong_ambient(surface_diffuse);
100         float shadow = get_shadow_factor(0);
101         color += phong_one_light(light, normal, look, light_sources[0].color, surface_diffuse, surface_specular, shininess)*shadow;
102
103         if(use_emission)
104                 color += get_emission_color();
105
106         if(use_reflectivity)
107                 color += get_reflection(normal, look)*get_reflectivity_value();
108
109         return color;
110 }
111
112 void main()
113 {
114         vec3 normal = get_fragment_normal();
115         vec3 look = normalize(world_look_dir);
116
117         vec4 surface_diffuse = get_diffuse_color();
118         vec3 surface_specular = get_specular_color();
119         float shininess = get_shininess_value();
120
121         vec3 lit_color = phong_lighting(normal, look, surface_diffuse.rgb, surface_specular, shininess);
122
123         frag_color = vec4(lit_color, surface_diffuse.a);
124 }