]> git.tdb.fi Git - libs/gl.git/blob - shaderlib/singlepass.glsl
Check the flat qualifier from the correct member
[libs/gl.git] / shaderlib / singlepass.glsl
1 import msp_interface;
2
3 const bool use_vertex_color = false;
4
5 const bool use_lighting = false;
6 const bool use_specular = false;
7 const bool use_sky = false;
8 const bool use_fog = false;
9
10 const bool use_diffuse_map = false;
11 const bool use_normal_map = false;
12
13 const bool use_shadow_map = false;
14
15 const bool use_environment_map = false;
16
17 #pragma MSP stage(vertex)
18 vec4 get_vertex_position()
19 {
20         return vertex;
21 }
22
23 vec3 get_vertex_normal()
24 {
25         return normal;
26 }
27
28 void singlepass_transform_and_lighting()
29 {
30         out vec4 eye_vertex = eye_obj_matrix*get_vertex_position();
31         gl_Position = projection_matrix*eye_vertex;
32
33         out vec3 eye_normal = eye_obj_normal_matrix*get_vertex_normal();
34         vec3 eye_tangent = eye_obj_normal_matrix*tangent;
35         vec3 eye_binormal = eye_obj_normal_matrix*binormal;
36         out mat3 eye_tbn_matrix = mat3(eye_tangent, eye_binormal, eye_normal);
37
38         out vec3 incident_dir = normalize(eye_vertex.xyz);
39         if(use_normal_map)
40                 incident_dir = incident_dir*eye_tbn_matrix;
41
42         vec3 ldir = normalize(light_sources[0].position.xyz-eye_vertex.xyz*light_sources[0].position.w);
43         if(use_normal_map)
44                 ldir = ldir*eye_tbn_matrix;
45         out vec3 light_dir = ldir;
46
47         out vec3 tbn_zenith_dir = eye_zenith_dir*eye_tbn_matrix;
48         out vec3 shadow_coord = (shd_eye_matrix*eye_vertex).xyz;
49         out float fog_coord = eye_vertex.z;
50
51         for(int i=0; i<max_clip_planes; ++i)
52                 gl_ClipDistance[i] = dot(eye_vertex, clip_planes[i].equation);
53 }
54
55 void main()
56 {
57         singlepass_transform_and_lighting();
58         passthrough;
59 }
60
61 #pragma MSP stage(fragment)
62 vec4 get_diffuse_sample()
63 {
64         return texture(diffuse_map, texcoord.xy);
65 }
66
67 vec3 get_normal_sample()
68 {
69         return texture(normal_map, texcoord.xy).xyz*2.0-1.0;
70 }
71
72 vec4 get_environment_sample(vec3 direction)
73 {
74         return texture(environment, direction);
75 }
76
77 vec3 normal;
78 vec4 diffuse_sample;
79
80 vec3 singlepass_lighting()
81 {
82         float shadow_sample = texture(shadow, shadow_coord);
83         float shadow_intensity = mix(1.0, shadow_sample, shadow_darkness);
84
85         vec3 ambient_light = ambient_color.rgb;
86         if(use_sky)
87         {
88                 vec3 zenith_dir;
89                 if(use_normal_map)
90                         zenith_dir = tbn_zenith_dir;
91                 else
92                         zenith_dir = eye_zenith_dir;
93                 float skylight_intensity = dot(normal, zenith_dir)*0.5+0.5;
94                 ambient_light += skylight_intensity*sky_color.rgb;
95         }
96
97         vec3 n_light_dir = normalize(light_dir);
98         float diffuse_intensity = max(dot(normal, n_light_dir), 0.0);
99         if(use_shadow_map)
100                 diffuse_intensity *= shadow_intensity;
101         vec3 diffuse_light = diffuse_intensity*light_sources[0].diffuse.rgb;
102
103         vec3 half_vec = normalize(light_dir-incident_dir);
104         float specular_intensity = pow(max(dot(half_vec, normal), 0.0), material.shininess);
105         if(use_shadow_map)
106                 specular_intensity *= shadow_intensity;
107         vec3 specular_light = specular_intensity*light_sources[0].specular.rgb;
108
109         vec3 result = material.ambient.rgb*ambient_light+material.diffuse.rgb*diffuse_light;
110         if(use_diffuse_map)
111                 result *= diffuse_sample.rgb;
112         if(use_specular)
113                 result += material.specular.rgb*specular_light;
114
115         return result;
116 }
117
118 float singlepass_transparency()
119 {
120         float alpha = material.diffuse.a;
121         if(use_diffuse_map)
122                 alpha *= diffuse_sample.a;
123         return alpha;
124 }
125
126 vec3 singlepass_reflection()
127 {
128         vec3 reflect_dir = reflect(incident_dir, normal);
129         if(use_normal_map)
130                 reflect_dir = eye_tbn_matrix*reflect_dir;
131
132         return get_environment_sample(env_eye_matrix*reflect_dir).rgb;
133 }
134
135 vec4 singlepass_color()
136 {
137         vec4 result = vec4(1.0);
138         if(use_vertex_color)
139                 result *= color;
140         if(use_diffuse_map)
141                 result *= get_diffuse_sample();
142         return result;
143 }
144
145 void main()
146 {
147         if(use_normal_map)
148                 normal = get_normal_sample();
149         else
150                 normal = normalize(eye_normal);
151
152         diffuse_sample = get_diffuse_sample();
153
154         vec4 final_color;
155         if(use_lighting)
156                 final_color = vec4(singlepass_lighting(), singlepass_transparency());
157         else
158                 final_color = singlepass_color();
159
160         if(use_environment_map)
161                 final_color += vec4(singlepass_reflection(), 0.0);
162         if(use_fog)
163         {
164                 float fog_value = exp(fog_coord*fog_density);
165                 final_color = vec4(mix(fog_color.rgb, final_color.rgb, fog_value), final_color.a);
166         }
167
168         frag_color = final_color;
169 }