# GLSL Programming/Blender/Specular Highlights

This tutorial covers per-vertex lighting (also known as Gouraud shading) using the Phong reflection model.
It extends the shader code in the tutorial on diffuse reflection by two additional terms: ambient lighting and specular reflection. Together, the three terms constitute the Phong reflection model. If you haven't read the tutorial on diffuse reflection, this would be a very good opportunity to read it.

### Ambient Light

Consider the painting by Caravaggio to the left. While large parts of the white shirt are in shadows, no part of it is completely black. Apparently there is always some light being reflected from walls and other objects to illuminate everything in the scene — at least to a certain degree. In the Phong reflection model, this effect is taken into account by ambient lighting, which depends on a general ambient light intensity $I_{\text{ambient light}}$ and the material color $k_{\text{diffuse}}$ for diffuse reflection. In an equation for the intensity of ambient lighting $I_{\text{ambient}}$:
$I_{\text{ambient}}=I_{\text{ambient light}}\,k_{\text{diffuse}}$
Analogously to the equation for diffuse reflection in the tutorial on diffuse reflection, this equation can also be interpreted as a vector equation for the red, green, and blue components of light.
In Blender, the ambient light is specified in the World tab of a Properties window. For a specific material, this color is multiplied with Shading > Ambient from the Material tab of a Properties window such that the influence of the ambient color can be controlled for each material. In a GLSL shader in Blender, this product is available as gl_LightModel.ambient, which is one of the pre-defined uniforms of the OpenGL compatibility profile mentioned in the tutorial on shading in view space.

### Specular Highlights

If you have a closer look at Caravaggio's painting, you will see several specular highlights: on the nose, on the hair, on the lips, on the lute, on the violin, on the bow, on the fruits, etc. The Phong reflection model includes a specular reflection term that can simulate such highlights on shiny surfaces; it even includes a parameter $n_{\text{shininess}}$ to specify a shininess of the material. The shininess specifies how small the highlights are: the shinier, the smaller the highlights.
A perfectly shiny surface will reflect light from the light source only in the geometrically reflected direction R. For less than perfectly shiny surfaces, light is reflected to directions around R: the smaller the shininess, the wider the spreading. Mathematically, the normalized reflected direction R is defined by:
$\mathbf {R} =2\mathbf {N} (\mathbf {N} \cdot \mathbf {L} )-\mathbf {L}$
for a normalized surface normal vector N and a normalized direction to the light source L. In GLSL, the function vec3 reflect(vec3 I, vec3 N) (or vec4 reflect(vec4 I, vec4 N)) computes the same reflected vector but for the direction I from the light source to the point on the surface. Thus, we have to negate our direction L to use this function.
The specular reflection term computes the specular reflection in the direction of the viewer V. As discussed above, the intensity should be large if V is close to R, where “closeness” is parametrized by the shininess $n_{\text{shininess}}$. In the Phong reflection model, the cosine of the angle between R and V to the $n_{\text{shininess}}$-th power is used to generate highlights of different shininess. Similarly to the case of the diffuse reflection, we should clamp negative cosines to 0. Furthermore, the specular term requires a material color $k_{\text{specular}}$ for the specular reflection, which is usually just white such that all highlights have the color of the incoming light $I_{\text{incoming}}$. For example, all highlights in Caravaggio's painting are white. The specular term of the Phong reflection model is then:
$I_{\text{specular}}=I_{\text{incoming}}\,k_{\text{specular}}\max(0,\mathbf {R} \cdot \mathbf {V} )^{n_{\text{shininess}}}$
Analogously to the case of the diffuse reflection, the specular term should be ignored if the light source is on the “wrong” side of the surface; i.e., if the dot product N·L is negative.

### Shader Code

The shader code for the ambient lighting could be straightforward with a component-wise vector-vector product:
However, as mentioned in the tutorial on shading in view space, Blender doesn't set gl_FrontMaterial.diffuse (nor gl_FrontMaterial.ambient); thus, we use gl_FrontMaterial.emission:
For the implementation of the specular reflection, we require the direction to the viewer in view space, which we can compute as the difference between the camera position and the vertex position (both in view space). The camera position in view space is trivial because the view space is defined such that the camera position is at the origin $(0,0,0)$; see “Vertex Transformations”. The vertex position can be transformed to view space as discussed in the tutorial on diffuse reflection. The equation of the specular term in world space could then be implemented like this:
This code snippet uses the same variables as the shader code in the tutorial on diffuse reflection and additionally the built-in uniforms gl_FrontMaterial.specular and gl_FrontMaterial.shininess. (Which are specified by the user in Blender as mentioned in the tutorial on shading in view space.) pow(a, b) computes $a^{b}$.
If the ambient lighting and the specular reflection is added the full vertex shader of the tutorial on diffuse reflection, it looks like this:
The fragment shader is still:

### Summary

Congratulation, you just learned how to implement the Phong reflection model. In particular, we have seen:
* What the ambient lighting in the Phong reflection model is.
* What the specular reflection term in the Phong reflection model is.
* How these terms can be implemented in GLSL in Blender.

### Further Reading

If you still want to know more
* about the shader code, you should read the tutorial on diffuse reflection.
< GLSL Programming/Blender