Help those suffering in the Horn of Africa

Directional Lights II

Prev: Directional Lights I Next: Dir. Light per Pixel
 

Time for the specular component of the OpenGL directional light. The lighting model used is the Blinn-Phong model, which is a simplification of the Phong model. We shall take a peek at the Phong model since it makes it easier to understand the Blinn-Phong model.

The Phong model says that the specular component is proportional to the cosine between the light reflection vector and the eye vector. The following image shows this graphically:

L is the vector from the light to the vertex being shaded. N is the normal vector, and Eye is the vector from the vertex to the eye, or camera. R is the vector L mirror reflected on the surface. The specular component is proportional to the cosine of alpha.

If the eye vector coincides with the reflection vector then we get the maximum specular intensity. As the eye vector diverges from the reflection vector the specular intensity decays. The rate of decay is controlled by a shininess factor. The higher the shininess factor the faster the decay. This means that with a high shininess the bright spot caused by the specular component is smaller than with a low shininess value. Simply put, the shininess (a value between 0 and 128 in OpenGL) controls the size of the bright spot.

Shininess = 8 Shininess = 64 Shininess = 128

The formula for the reflection vector is as follows:

And the specular component in OpenGL using the Phong model would be:

Where the s exponent is the shininess value, Ls is the lights specular intensity, and Ms is the materials specular coefficient.

Blinn proposed a simpler and faster model, knows as the Blinn-Phong model that is based on the half-vector. The half-vector is a vector with a direction half-way between the eye vector and the light vector as shown in the following figure:

The intensity of the specular component is now based on the cosine of the angle between the half vector and the normal.

The formula for the half-vector is much simpler than for the reflection vector:

And the specular component in OpenGL using the Blinn-Phong model is:

This is the actual stuff as commonly used in the fixed pipeline of the graphics hardware. Since we want to emulate the OpenGL’s directional light, we’re going to use this last equation in our shader. There is a good news: OpenGL computes the half-vector for us! So the following snippet of code should do the trick:

	/* compute the specular term if NdotL is  larger than zero */
	if (NdotL > 0.0) {

		// normalize the half-vector, and then compute the
		// cosine (dot product) with the normal
		NdotHV = max(dot(normal, gl_LightSource[0].halfVector.xyz),0.0);
		specular = gl_FrontMaterial.specular * gl_LightSource[0].specular *
				pow(NdotHV,gl_FrontMaterial.shininess);
	}

The full source of the shaders, in a Shader Designer project can be found in here.

 

 

Prev: Directional Lights I Next: Dir. Light per Pixel
 

2 Responses to “Directional Lights II”

  1. The image after this sentence: “The half-vector is a vector with a direction half-way between the eye vector and the light vector as shown in the following figure” should probably be halfV.giv instead of eqhalfV.gif.

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2012 Lighthouse3d.com Suffusion theme by Sayontan Sinha