Lighting is one of the components of a game that can provide an immersive reality to the player -- or be so distracting that playing becomes difficult. To ease working with lighting, OpenGL provides simplified API. The focus of this discussion will be on the basics of using lighting API provided by OpenGL. This article is the latest part in a multi-part series on game programming with OpenGL and SDL.
The first section will be about the whys and wherefores of lighting. The second and third sections will focus on using the basic lighting API in a game. The last section will delve into an application that will make use of the concepts and API introduced in the first two sections. That is the outline for this discussion.
Lighting in OpenGL: The Whys and Wherefores
Similar to all other aspects of a game, lighting has a close relationship with the mechanics of illumination in real world. Hence, the first step in understanding how lighting works in OpenGL, and in any given game, is to know how lighting works in real life. One has to know the types of lighting or illumination one encounters in day-to-day life.
The lighting encountered in the real world can be broadly categorized into ambient light, diffuse light, and specular light. The categorization is based upon the direction of the source of light. Here are the details:
When the direction of light cannot be determined, giving an illusion that the light is coming from all directions, such a light or illumination is known as ambient light or ambient illumination. The reason for the illusion is the scattering of the light by the environment. Examples of ambient light are sunlight, the overhead lighting of a room, and so forth.
The backlighting of a room also has a large component of ambient light. The reason for this is that the light is bounced off many surfaces before it reaches one’s eyes, thus seemingly coming from all directions. One of the main aspects of ambient light is that when it comes in contact with any surface, it is scattered in all directions equally.
When the light comes from a particular direction, it provides diffuse illumination. Since it comes from one direction, if it hits a surface squarely, it is brighter; if it just touches the surface, the illumination will be less. However, once the light hits the surface, it is scattered equally in all directions. Due to this, a diffuse light appears bright from whichever angle it is seen.
When a light comes from a particular direction, strikes a surface and bounces off in a preferred direction, such a light or illumination is known as specular light or specular illumination. In other words, light reflecting off the surface of smooth material is specular. The shininess of an object can be related to the specularity. One thing to keep in mind about specular light is that its angle of incidence is the same as that of its angle of reflection.
Now that the types of lighting have been discussed, let us move onto the steps required to implement basic lighting in OpenGL.