]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/phong.glsl
Use forward references for entry point interfaces in SPIR-V
[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 layout(set=1) uniform BasicMaterial
15 {
16         BasicMaterialParameters basic_material;
17         AlphaCutoffParameters alpha_cutoff;
18 };
19
20 layout(set=1) uniform sampler2D diffuse_map;
21 layout(set=1) uniform sampler2D specular_map;
22 layout(set=1) uniform sampler2D shininess_map;
23 layout(set=1) uniform sampler2D emission_map;
24 layout(set=1) uniform sampler2D reflectivity_map;
25
26 layout(constant_id=auto) const bool use_diffuse_map = false;
27 layout(constant_id=auto) const bool use_specular = false;
28 layout(constant_id=auto) const bool use_specular_map = false;
29 layout(constant_id=auto) const bool use_shininess_map = false;
30 layout(constant_id=auto) const bool use_emission = false;
31 layout(constant_id=auto) const bool use_emission_map = false;
32 layout(constant_id=auto) const bool use_reflectivity = false;
33 layout(constant_id=auto) const bool use_reflectivity_map = false;
34
35 #pragma MSP stage(fragment)
36 virtual vec4 get_diffuse_color()
37 {
38         if(use_diffuse_map)
39                 return texture(diffuse_map, texcoord.xy);
40         else
41                 return basic_material.diffuse;
42 }
43
44 virtual vec3 get_specular_color()
45 {
46         if(use_specular_map)
47                 return texture(specular_map, texcoord.xy).rgb;
48         else
49                 return basic_material.specular.rgb;
50 }
51
52 virtual float get_shininess_value()
53 {
54         if(use_shininess_map)
55                 return texture(shininess_map, texcoord.xy).r*255.0;
56         else
57                 return basic_material.shininess;
58 }
59
60 virtual vec3 get_emission_color()
61 {
62         if(use_emission_map)
63                 return texture(emission_map, texcoord.xy).rgb;
64         else
65                 return basic_material.emission.rgb;
66 }
67
68 virtual float get_reflectivity_value()
69 {
70         if(use_reflectivity_map)
71                 return texture(reflectivity_map, texcoord.xy).r;
72         else
73                 return basic_material.reflectivity;
74 }
75
76 vec3 phong_ambient(vec3 surface_diffuse)
77 {
78         return ambient_color.rgb*surface_diffuse;
79 }
80
81 vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_specular, float shininess)
82 {
83         float diffuse_intensity = max(dot(light, normal), 0.0);
84         vec3 color = surface_diffuse*diffuse_intensity;
85         if(use_specular)
86         {
87                 vec3 reflected = reflect(look, normal);
88                 float specular_intensity = pow(max(dot(reflected, light), 0.0), shininess);
89                 color += surface_specular*specular_intensity;
90         }
91         return color;
92 }
93
94 vec3 phong_lighting(vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_specular, float shininess)
95 {
96         vec3 color = phong_ambient(surface_diffuse);
97         for(int i=0; i<max_lights; ++i)
98                 if(light_sources[i].type!=0)
99                 {
100                         IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
101                         float shadow = get_shadow_factor(i, world_vertex);
102                         color += phong_one_light(incoming.direction, normal, look, surface_diffuse, surface_specular, shininess)*incoming.color*shadow;
103                 }
104
105         if(use_emission)
106                 color += get_emission_color();
107
108         if(use_reflectivity)
109                 color += get_reflection(normal, look)*get_reflectivity_value();
110
111         return color;
112 }
113
114 void main()
115 {
116         vec4 surface_diffuse = get_diffuse_color();
117         float alpha = apply_alpha_cutoff(surface_diffuse.a, alpha_cutoff);
118
119         vec3 normal = get_fragment_normal();
120         vec3 look = normalize(world_look_dir);
121
122         vec3 surface_specular = get_specular_color();
123         float shininess = get_shininess_value();
124
125         vec3 lit_color = phong_lighting(normal, look, surface_diffuse.rgb, surface_specular, shininess);
126
127         frag_color = vec4(lit_color, alpha);
128 }