User Tools

Site Tools


Old-School OpenGL Lighting

We are starting out learning an older OpenGL standard to get our feet wet. This is mainly to get you used to some of the basic ways OpenGL works before jumping into the more advanced topics. As such, we are going to look at a somewhat old-school lighting method.

First things first, we will need to tell OpenGL that we want to light our scene. This is pretty straightforward and probably looks a little familiar.

        glEnable(GL_LIGHTING) #Turns on lighting

Usually, we want to have this enabled for everything we will be rendered in a scene. This means that you will likely want to put this in your __init__() function. The lighting functions built into the fixed-function version of OpenGL allow you to place up to eight lights in your scene. Newer implementations do not have this limitation. Just like everything else in OpenGL, you have to enable your light sources individually. How? You probably guessed it…by calling glEnable:


Your first light source will be GL_LIGHT0 and your eighth light source will be GL_LIGHT7. Next, you will need to specify the lighting properties of the materials in your scene:

        mat_specular = [ 0.5, 0.5, 0.0, 1.0 ] #color of specular highlights
        mat_diffuse =[ 1.0, 0.0, 0.0, 1.0 ] #color of diffuse shading
        mat_ambient= [ 1.0, 1.0, 1.0, 1.0 ] #color of ambient light
        mat_shininess = [ 0.2 ] #the "shininess" of the specular highlight
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular)
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse)
        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient)
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess)

You will also need to specify the position of the light in your scene. Per usual, your light's position is going to be a 3D position, but you'll notice in the example below that the light position has four numbers! The first three are the X, Y, Z components of the light's position, so what then is that last number? The fourth value is used to specify the type of light being used. Setting this parameter to 0.0 specifies that the light source is directional while 1.0 specifies that the light is a point source.

        light_position = [ 1.0, 1.0, 1.0, 1.0 ]
        glLightfv(GL_LIGHT0, GL_POSITION, light_position)

There is a final component that you may want to address. You can specify one of two shading models for your program to follow. The first is flat shading. This shading method computes a single color for each polygon in your scene. This gives your geometry a somewhat blocky look when lite. The more common shading approach is smooth shading which specifies a color of each vertex of each polygon and then interpolates between them across the surface of the polygon. These should be specified in the same place you set up your lighting parameters. This can be done as follows:

glEnable(GL_FLAT) #Enables Flat Shading
glEnable(GL_SMOOTH) #Enables Smooth Shading

tutorials/ffopengl_lighting.txt · Last modified: 2021/08/01 00:50 by jones