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