Home arrow Multimedia arrow Page 2 - Basic Lighting in OpenGL and SDL Game Programming

Implementing Lighting, Step by Step - Multimedia

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.

  1. Basic Lighting in OpenGL and SDL Game Programming
  2. Implementing Lighting, Step by Step
  3. Lighting Model
  4. Lighting in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 3
December 09, 2008

print this article



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


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:


>>> More Multimedia Articles          >>> More By A.P.Rajshekhar

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Basic Lighting in OpenGL and SDL Game Progra...
- Working with Colors in OpenGL for Game Progr...
- Animation in OpenGL for Game Programming usi...
- Game Programming with SDL: Getting Started w...
- Using OpenGL with SDL for Game Programming
- Learning Sound for Game Programming using SDL
- Game Programming using SDL: Raw Graphics and...
- Game Programming using SDL: Getting Started
- Network Radio With Icecast
- Learning To SMILe

Developer Shed Affiliates


Dev Shed Tutorial Topics: