How it works...

The SpotLightInfo structure defines all of the configuration options for the spotlight. We declare a single uniform variable named Spot to store the data for our spotlight. The Position field defines the location of the spotlight in eye coordinates. The L field is the intensity (diffuse and specular) of the spotlight, and La is the ambient intensity. The Direction field will contain the direction that the spotlight is pointing, which defines the center axis of the spotlight's cone. This vector should be specified in camera coordinates. Within the OpenGL program, it should be transformed by the normal matrix in the same way that normal vectors would be transformed. We could do so within the shader; however, within the shader, the normal matrix would be specified for the object being rendered. This may not be the appropriate transform for the spotlight's direction.

The Exponent field defines the exponent that is used when calculating the angular attenuation of the spotlight. The intensity of the spotlight is decreased in proportion to the cosine of the angle between the vector from the light to the surface location (the negation of the variable s) and the direction of the spotlight. That cosine term is then raised to the power of the variable Exponent. The larger the value of this variable, the faster the intensity of the spotlight is decreased. This is similar to the exponent in the specular shading term.

The Cutoff field defines the angle between the central axis and the outer edge of the spotlight's cone of light. We specify this angle in radians.

The blinnPhongSpot function computes the Blinn-Phong reflection model, using a spotlight as the light source. The first line computes the ambient lighting component and stores it in the ambient variable.  The second line computes the vector from the surface location to the spotlight's position (s).  Next, we compute the dot product between the direction from the spotlight to the surface point (-s) and the direction of the spotlight and store the result in cosAng. The angle between them is then computed and stored in the variable angle.   The variable spotScale will be used to scale the value of the spotlight's diffuse/specular intensity.  It is initially set to zero.

We then compare the value of the angle variable with that of the Spot.Cutoff variable. If angle is greater than zero and less than Spot.Cutoff, then the surface point is within the spotlight's cone. Otherwise, the surface point only receives ambient light, so we skip the rest and return only the ambient component.

If angle is less than Spot.Cutoff, we compute the spotScale value by raising the dot product of -s and spotDir to the power of Spot.Exponent. The value of spotScale is used to scale the intensity of the light so that the light is maximal in the center of the cone, and decreases as you move toward the edges. Finally, the Blinn-Phong reflection model is computed as usual.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.15.1