GameProgrammer.org
tutorials for dummies bug source let me google it for you my blog all about me
Turn the lights on!

Introduction

In the real world you can't see anything if there is no light. In fact whatever you see is the illumination and reflection of lights. In this tutorial I'm going to talk about OpenGL lighting system.

Lights

OpenGL uses a very simple lighting system and actually for many reasons today's game engines don't use the default lighting model, actually OpenGL gives you the ability of overwriting the shading pipeline (by using the programmable pipeline) so you can define your own lighting model, but that's a huge and complex subject which we are going to discuss about later, but for now I just talk about OpenGL default lighting model.

To add light to your scene first you need to specify a light source, there are two kinds of light sources in OpenGL, point light and directional light. Using directional lights you can simulate the effect of a far light source like sun light for example, and point lights can be used to simulate the effect of a local light source like a lamp.

GLfloat light_position[] = { 1.0f, 1.0f, 0.0f, 0.0f };

glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glLightfv() function can be used to specify the attributes of a light stage, OpenGL supports up to 8 light stages and every light stage can be used to specify a light source, this means that you can't have more than 8 active lights simultaneously, so if you have more than 8 light sources in a scene, you need to choose the sources that make more effect on your object and assign them to the light stages.

GL_LIGHT0 indicates that we are using the first light stage (It could be GL_LIGHT1, GL_LIGHT2,... GL_LIGHT7 as well). Second parameter, GL_POSITION, indicates that we want to set the light source position. Depending on the light source type that you want to use, the third parameter can represents a point or a vector. Here I wanted to create a directional light so I specified a light source with a direction parallel to the vector (1.0f, 1.0f, 0.0f), fourth element of the array which is equal to 0.0f indicates that the array represents a vector, you should set it to 1.0f if you want to specify a point, OpenGL will automatically detects which light source type you want to use according to this value.

The next thing we want to do is to set the light color. In OpenGL every light source has three color components: ambient, diffuse, and specular light. Ambient light illuminates an object entirely at the same intensity, so if you only use ambient light in your scene your objects will look like 2D shapes. Ambient light can be used to simulate indirect lighting.

Diffuse light illuminates a surface according to the angle between its normal vector and the light direction. It simulates the effect of a light source on a matte material.

Specular light simulates the reflection of a light source on a shiny surface, so basically you need to use it when you want to create a metallic material.

GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };

glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

Here once again I've used glLightfv() function to set the diffuse color of the light. Here we simply set it to white color. You can also try GL_AMBIENT and GL_SPECULAR to set the other components.

At last we need to enable OpenGL lighting system:

glEnable(GL_LIGHTING);

And we need to activate the first stage as well:

glEnable(GL_LIGHT0);

Materials

We are done with lighting of our scene but you still need to define the materials for your objects, every material has 5 properties, ambient color, diffuse color, specular color, emission color and shininess value.

Ambient, diffuse and specular colors simply determine how much the corresponding light values will effect on the material.

GLfloat material_diffuse[] = { 0.0f, 0.5f, 0.0f, 1.0f };
GLfloat material_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, material_specular);

Emission color simulates the effect of an emissive object, so the object will illuminate independent of any light source. At last shininess value as its name says, indicates how much shiny the material is.

GLfloat material_shininess[] = { 10.0f };

glMaterialfv(GL_FRONT, GL_SHININESS, material_shininess);

Here we simply set the shininess to 10; it makes a pretty shiny material.

Normal Vectors

Now that we are done with materials there's one another thing that we need to consider, and that is the normal vectors. As I said before there are some calculations which depends on the normal value of the surface, so you need to specify the normal vectors of every polygon.

glNormal3f(0.0f,0.0f, 1.0f);

For example this line specifies the z axis as the normal vector of the following polygons. Calculating the normal vectors may be a little tricky but when you are using an external model file, you usually don't need to calculate them because they're usually stored in the 3D model file.

OK, now go compile the project and see the result; try to create multiple objects with different materials and multiple light sources. And if you have any question or comment let me know by sending an email.

Posted on : 12 Sep 2004
Vahid Kazemi

Copyright © 2003-2010 Vahid Kazemi, GameProgrammer.org

iPhone Games