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_reflection = false;
67 const bool use_environment_map = false;
68 uniform samplerCube environment;
74 const int max_clip_planes = 0;
77 ClipPlane clip_planes[max_clip_planes];
81 layout(location=0) in vec4 vertex;
82 layout(location=8) in vec4 texcoord;
83 layout(location=3) in vec4 color;
84 layout(location=2) in vec3 normal;
85 layout(location=4) in vec3 tangent;
86 layout(location=5) in vec3 binormal;
88 vec4 get_vertex_position()
93 vec3 get_vertex_normal()
98 void singlepass_transform_and_lighting()
100 out vec4 eye_vertex = eye_obj_matrix*get_vertex_position();
101 gl_Position = projection_matrix*eye_vertex;
103 out vec3 eye_normal = eye_obj_normal_matrix*get_vertex_normal();
104 vec3 eye_tangent = eye_obj_normal_matrix*tangent;
105 vec3 eye_binormal = eye_obj_normal_matrix*binormal;
106 out mat3 eye_tbn_matrix = mat3(eye_tangent, eye_binormal, eye_normal);
108 out vec3 incident_dir = normalize(eye_vertex.xyz);
110 incident_dir = incident_dir*eye_tbn_matrix;
112 vec3 ldir = normalize(light_sources[0].position.xyz-eye_vertex.xyz*light_sources[0].position.w);
114 ldir = ldir*eye_tbn_matrix;
115 out vec3 light_dir = ldir;
117 out vec3 tbn_sky_dir = eye_sky_dir*eye_tbn_matrix;
118 out vec3 shadow_coord = (shd_eye_matrix*eye_vertex).xyz;
119 out float fog_coord = eye_vertex.z;
121 for(int i=0; i<max_clip_planes; ++i)
122 gl_ClipDistance[i] = dot(eye_vertex, clip_planes[i].equation);
127 singlepass_transform_and_lighting();
132 layout(location=0) out vec4 frag_color;
134 vec4 get_diffuse_sample()
136 return texture(diffuse_map, texcoord.xy);
139 vec3 get_normal_sample()
141 return texture(normal_map, texcoord.xy).xyz*2.0-1.0;
147 vec3 singlepass_lighting()
149 float shadow_sample = texture(shadow, shadow_coord);
150 float shadow_intensity = mix(1.0, shadow_sample, shadow_darkness);
152 vec3 ambient_light = ambient_color.rgb;
157 sky_dir = tbn_sky_dir;
159 sky_dir = eye_sky_dir;
160 float skylight_intensity = dot(normal, sky_dir)*0.5+0.5;
161 ambient_light += skylight_intensity*sky_color.rgb;
164 vec3 n_light_dir = normalize(light_dir);
165 float diffuse_intensity = max(dot(normal, n_light_dir), 0.0);
167 diffuse_intensity *= shadow_intensity;
168 vec3 diffuse_light = diffuse_intensity*light_sources[0].diffuse.rgb;
170 vec3 half_vec = normalize(light_dir-incident_dir);
171 float specular_intensity = pow(max(dot(half_vec, normal), 0.0), material.shininess);
173 specular_intensity *= shadow_intensity;
174 vec3 specular_light = specular_intensity*light_sources[0].specular.rgb;
176 vec3 result = material.ambient.rgb*ambient_light+material.diffuse.rgb*diffuse_light;
178 result *= diffuse_sample.rgb;
180 result += material.specular.rgb*specular_light;
185 float singlepass_transparency()
187 float alpha = material.diffuse.a;
189 alpha *= diffuse_sample.a;
193 vec3 singlepass_reflection()
195 vec3 reflect_dir = reflect(incident_dir, normal);
197 reflect_dir = eye_tbn_matrix*reflect_dir;
198 vec3 result = vec3(0.0);
200 if(use_environment_map)
201 result += texture(environment, env_eye_matrix*reflect_dir).rgb*reflectivity;
203 if(use_sky && use_specular)
205 float reflect_altitude = clamp(dot(reflect_dir, eye_sky_dir)-horizon_limit, -1.0, 0.0);
206 float sky_specular_intensity = pow((1.0-reflect_altitude*reflect_altitude), material.shininess/2.0);
207 result += sky_specular_intensity*sky_color.rgb;
213 vec4 singlepass_color()
215 vec4 result = vec4(1.0);
219 result *= get_diffuse_sample();
226 normal = get_normal_sample();
228 normal = normalize(eye_normal);
230 diffuse_sample = get_diffuse_sample();
234 final_color = vec4(singlepass_lighting(), singlepass_transparency());
236 final_color = singlepass_color();
239 final_color += vec4(singlepass_reflection(), 0.0);
242 float fog_value = exp(fog_coord*fog_density);
243 final_color = vec4(mix(fog_color.rgb, final_color.rgb, fog_value), final_color.a);
246 frag_color = final_color;