Multimedia Page 2 - Basic Lighting in OpenGL and SDL Game Programming |
In order to use the lighting API in OpenGL, certain steps need to be followed. These steps change according to the requirements. The requirements can include material, colors etc. However, for this discussion, the focus is on simple lighting, so the steps we will discuss are rudimentary. One point to keep in mind, however, is that the rudimentary steps remain unchanged even if the requirements change. Without further delay, here are the required steps: 1. Enable lighting. 2. Define normal vectors for each vertex. 3. Create one or more light sources. 4. Choosing a lighting model. Of these, the third and fourth steps have multiple sub-steps. Here are the details. Enable lighting OpenGL can be compared to a state machine. Using that analogy, lighting is also one of its states. Hence, lighting needs to be enabled. To enable lighting, the glEnable() function needs to be called with GL_LIGHTING as the parameter. Before performing any other operation regarding illumination, lighting should be enabled. In code the statement will be glEnable(GL_LIGHTING); Define normal vectors for each vertex It is necessary to define and determine the orientation of light with respect to an object, normal to the vertices of the object. OpenGL does not do normal vector calculations automatically. The calculation of normal vectors will be covered in the future. The calculated normal vectors need to be passed to OpenGL. To do so, any variation of glNormal function can be used. However, if no normal vectors are passed, then OpenGL takes (0, 0, 0, 0) as the normal vector. Create one or more light sources Creating one or more light sources can be sub-divided into the following two steps: 1. Defining a light source: In OpenGL one can define up to eight light sources. The names of these light sources are of the format GL_LIGHTn where n stands for numbers from 0 to 7. To define a light source, variants of glLight*() are used. Basically, it takes the light source number, the type of light and the values that will control the light (especially the color). The color is defined as RGBA i.e. Red, Green, Blue and the Alpha. For example, to define a light source with the source number as 0, the type as specular and the value as an array of floats, the statements will be GLfloat specular[] = {1.0f, 1.0f, 1.0f , 1.0f}; glLightfv(GL_LIGHT0, GL_SPECULAR, specular); 2. Enabling the light source: The next step is to enable the light source that has just been defined. To do so, one needs to use glEnable and pass the light source number as a parameter. By default, all of the light sources are disabled. Passing the light source number to glEnable(), enables the light source. For example, the following statement enables the light source that has 0 as its number: glEnable(GL_LIGHT0);
blog comments powered by Disqus |
|
|
|
|
|
|
|