OpenGL Super Bible! Page (Best web hosting) 335 Spotlights So far,
OpenGL Super Bible! Page 335 Spotlights So far, we have been specifying a light s position with glLight as follows: // Array to specify position GLfloat lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f }; // Set the light position glLightfv(GL_LIGHT0,GL_POSITION,lightPos); The array lightPos[] contains the x, y, and z values that specify either the light s actual position in the scene, or the direction from which the light is coming. The last value, a 1.0 in this case, indicates that the light is actually present at this location. By default, the light will radiate equally in all directions from this location but this can be changed to make a spotlight effect. To make a light source an infinite distance away and coming from the direction specified by this vector, you would place a 0.0 in this last lightPos[] array element. A directional light source, as this is called, strikes the surface of your objects evenly. That is, all the light rays are parallel. In a positional light source on the other hand, the light rays diverge from the light source. The specular highlights achieved in the SHINYJET example would not be possible with a directional light source. Rather than the glistening spot, the entire face of the triangles that make up the jet would be white when they faced the light source dead on (the light rays strike the surface at a 90 angle). Creating a Spotlight Creating a spotlight is no different from creating any other directional light source. The code in Listing 9-8 shows the SetupRC() function from the SPOT example program. This program places a blue sphere in the center of the window. A spotlight is created that can be moved vertically with the up and down arrow keys, and horizontally with the left and right arrow keys. As the spotlight moves over the surface of the sphere, a specular highlight follows it on the surface. Listing 9-8 Lighting setup for the SPOT sample program // Light values and coordinates GLfloat lightPos[] = { 0.0f, 0.0f, 75.0f, 1.0f }; GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f}; GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat ambientLight[] = { 0.5f, 0.5f, 0.5f, 1.0f}; GLfloat spotDir[] = { 0.0f, 0.0f, -1.0f }; // This function does any needed initialization on the rendering // context. Here it sets up and initializes the lighting for
In case you need quality webspace to host and run your web applications, try our personal web hosting services.