- // Lambert diffusion
- vec3 light_dir = normalize(v_light_center.xyz);
- float lambert = max(dot(light_dir, v_normal), 0.0);
-
- if (use_map_ambient)
- gl_FragColor = ambient_color * texture2D(map_ambient, v_tex_coord);
- else
- gl_FragColor = ambient_color;
-
- if (use_map_diffuse)
- gl_FragColor += lambert * diffuse_color * texture2D(map_diffuse, v_tex_coord);
- else
- gl_FragColor += lambert * diffuse_color;
-
+ // Normal
+ vec3 normal = v_normal;
+ if (use_map_bump) {
+ // TODO
+ vec3 bump = 2.0 * texture2D(map_bump, v_tex_coord).rgb - 1.0;
+ }
+
+ // Ambient light
+ vec4 ambient = ambient_color;
+ if (use_map_ambient) ambient *= texture2D(map_ambient, v_tex_coord);
+
+ // Diffuse Lambert light
+ vec3 to_light = v_to_light.xyz;
+ float lambert = clamp(dot(normal, to_light), 0.0, 1.0);
+
+ vec4 diffuse = lambert * diffuse_color;
+ if (use_map_diffuse) diffuse *= texture2D(map_diffuse, v_tex_coord);
+
+ // Specular Phong light
+ float s = 0.0;
+ if (lambert > 0.0) {
+ vec3 l = reflect(-to_light, normal);
+ s = clamp(dot(l, v_to_camera.xyz), 0.0, 1.0);
+ s = pow(s, 8.0); // TODO make this `shininess` a material attribute
+ }
+
+ vec4 specular = s * specular_color;
+ if (use_map_specular) specular *= texture2D(map_specular, v_tex_coord).x;
+
+ gl_FragColor = ambient + diffuse + specular;