1 struct LightSourceParameters
8 struct MaterialParameters
21 uniform mat4 eye_obj_matrix;
22 uniform mat3 eye_obj_normal_matrix;
25 mat4 projection_matrix;
30 MaterialParameters material;
34 const bool use_vertex_color = false;
36 const bool use_lighting = false;
37 const bool use_specular = false;
38 const bool use_sky = false;
39 const bool use_fog = false;
42 // Declared as an array for compatibility reasons
43 LightSourceParameters light_sources[1];
52 const bool use_diffuse_map = false;
53 uniform sampler2D diffuse_map;
55 const bool use_normal_map = false;
56 uniform sampler2D normal_map;
58 const bool use_shadow_map = false;
59 uniform sampler2DShadow shadow;
62 float shadow_darkness;
66 const bool use_environment_map = false;
67 uniform samplerCube environment;
73 const int max_clip_planes = 0;
76 ClipPlane clip_planes[max_clip_planes];
80 layout(location=0) in vec4 vertex;
81 layout(location=8) in vec4 texcoord;
82 layout(location=3) in vec4 color;
83 layout(location=2) in vec3 normal;
84 layout(location=4) in vec3 tangent;
85 layout(location=5) in vec3 binormal;
87 vec4 get_vertex_position()
92 vec3 get_vertex_normal()
97 void singlepass_transform_and_lighting()
99 out vec4 eye_vertex = eye_obj_matrix*get_vertex_position();
100 gl_Position = projection_matrix*eye_vertex;
102 out vec3 eye_normal = eye_obj_normal_matrix*get_vertex_normal();
103 vec3 eye_tangent = eye_obj_normal_matrix*tangent;
104 vec3 eye_binormal = eye_obj_normal_matrix*binormal;
105 out mat3 eye_tbn_matrix = mat3(eye_tangent, eye_binormal, eye_normal);
107 out vec3 incident_dir = normalize(eye_vertex.xyz);
109 incident_dir = incident_dir*eye_tbn_matrix;
111 vec3 ldir = normalize(light_sources[0].position.xyz-eye_vertex.xyz*light_sources[0].position.w);
113 ldir = ldir*eye_tbn_matrix;
114 out vec3 light_dir = ldir;
116 out vec3 tbn_sky_dir = eye_sky_dir*eye_tbn_matrix;
117 out vec3 shadow_coord = (shd_eye_matrix*eye_vertex).xyz;
118 out float fog_coord = eye_vertex.z;
120 for(int i=0; i<max_clip_planes; ++i)
121 gl_ClipDistance[i] = dot(eye_vertex, clip_planes[i].equation);
126 singlepass_transform_and_lighting();
131 layout(location=0) out vec4 frag_color;
133 vec4 get_diffuse_sample()
135 return texture(diffuse_map, texcoord.xy);
138 vec3 get_normal_sample()
140 return texture(normal_map, texcoord.xy).xyz*2.0-1.0;
143 vec4 get_environment_sample(vec3 direction)
145 return texture(environment, direction);
151 vec3 singlepass_lighting()
153 float shadow_sample = texture(shadow, shadow_coord);
154 float shadow_intensity = mix(1.0, shadow_sample, shadow_darkness);
156 vec3 ambient_light = ambient_color.rgb;
161 sky_dir = tbn_sky_dir;
163 sky_dir = eye_sky_dir;
164 float skylight_intensity = dot(normal, sky_dir)*0.5+0.5;
165 ambient_light += skylight_intensity*sky_color.rgb;
168 vec3 n_light_dir = normalize(light_dir);
169 float diffuse_intensity = max(dot(normal, n_light_dir), 0.0);
171 diffuse_intensity *= shadow_intensity;
172 vec3 diffuse_light = diffuse_intensity*light_sources[0].diffuse.rgb;
174 vec3 half_vec = normalize(light_dir-incident_dir);
175 float specular_intensity = pow(max(dot(half_vec, normal), 0.0), material.shininess);
177 specular_intensity *= shadow_intensity;
178 vec3 specular_light = specular_intensity*light_sources[0].specular.rgb;
180 vec3 result = material.ambient.rgb*ambient_light+material.diffuse.rgb*diffuse_light;
182 result *= diffuse_sample.rgb;
184 result += material.specular.rgb*specular_light;
189 float singlepass_transparency()
191 float alpha = material.diffuse.a;
193 alpha *= diffuse_sample.a;
197 vec3 singlepass_reflection()
199 vec3 reflect_dir = reflect(incident_dir, normal);
201 reflect_dir = eye_tbn_matrix*reflect_dir;
203 return get_environment_sample(env_eye_matrix*reflect_dir).rgb;
206 vec4 singlepass_color()
208 vec4 result = vec4(1.0);
212 result *= get_diffuse_sample();
219 normal = get_normal_sample();
221 normal = normalize(eye_normal);
223 diffuse_sample = get_diffuse_sample();
227 final_color = vec4(singlepass_lighting(), singlepass_transparency());
229 final_color = singlepass_color();
231 if(use_environment_map)
232 final_color += vec4(singlepass_reflection(), 0.0);
235 float fog_value = exp(fog_coord*fog_density);
236 final_color = vec4(mix(fog_color.rgb, final_color.rgb, fog_value), final_color.a);
239 frag_color = final_color;