CS559
Fall 2005

News
Calendar
Assignments

Lectures/Notes
Tutorials
Sample Code

Project 1
Project 2
Project 3

Basic Info
Policies

C++ hints


Lighting

Adapted from Stephen J. Chenney's Tutorial

Modified by Yu-Chi Lai at 2005





This tutorial will build on the previous tutorial, adding OpenGL colored lighting.
  1. EnableLighting
  2. Position the Light Source
  3. Setup Material Properties
  4. Add Normals
  5. Build and Run the Program
Lets get started.



Step 1:  Enable Lighting

We're going to be using OpenGL lighting so the first thing to do is tell OpenGL we need it to do the lighting calculations for the geometry we send it.  We do this by enabling lighting and enabling at least one OpenGL light source.  Our revised InitializeGL() method looks like this.


void MyWindow::InitializeGL()
{
	glClearColor(.1f, .1f, .1f, 1);
	glEnable(GL_DEPTH_TEST);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	float lightColor[4]= {1, 1, 1, 1};
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
}


We enable OpenGL lighting and enable the first light (GL_LIGHT0).  We then set the lights diffuse color to white.  We won't use any ambient or specular light for this tutorial.


Step 2:  Position the Light Source

We'll need to position our light source so it illuminates our cube.  We add this code to our draw() method.

. . .
gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
float lightPosition[4] = {5, 5, 5, 1};

glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); glRotatef(rotation, 0, 1, 0); . . .

We've placed our light at (5, 5, 5) so it should shine on the upper right corner of our cube.  Notice the one trailing the light position.  This is the homogeneous coordinate and tells OpenGL we want a point light.  If it was a zero the light would be a directional light and the (5, 5, 5) would specify the direction instead of a position.


Step 3:  Setup Material Properties

When using OpenGL lighting we no longer set colors for our geometry.  We need to set material properties.  However, for this simple demo we can use some OpenGL functionality to use the colors we've already specified as the material properties we need.  This will convert our glColor3f() calls to material property settings instead.  We'll add this to our InitializeGL() method.  We need to enable this functionality called color material and  set the diffuse material property to track the colors we've set with glColor3f().

. . .
	float lightColor[4] = {1, 1, 1, 1};
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
	glEnable(GL_COLOR_MATERIAL);
	
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
}



Step4:  Add Normals

The only thing left to do is add normal vectors to each of our quads that form the cube.  We'll modify DrawCube() to include normals.

void MyWindow::DrawCube()
{
	glBegin(GL_QUADS);
	// front
	glNormal3f(0, 0, 1);
	glColor3f(1, 0, 0);
	glVertex3f(-1, 1, 1);
	glVertex3f(-1, -1, 1);
	glVertex3f(1, -1, 1);
	glVertex3f(1, 1, 1);
	  
	// back
	glNormal3f(0, 0, -1);
	glColor3f(0, 1, 0);
	glVertex3f(-1, 1, -1);
	glVertex3f(1, 1, -1);
	glVertex3f(1, -1, -1);
	glVertex3f(-1, -1, -1);
	  
	// top
    glNormal3f(0, 1, 0);
	glColor3f(0, 0, 1);
	glVertex3f(-1, 1, -1);
	glVertex3f(-1, 1, 1);
	glVertex3f(1, 1, 1);
	glVertex3f(1, 1, -1);
	  
	// bottom
	glNormal3f(0, -1, 0);
	glColor3f(1, 1, 0);
	glVertex3f(-1, -1, -1);
	glVertex3f(1, -1, -1);
	glVertex3f(1, -1, 1);
	glVertex3f(-1, -1, 1);
	  
	// left
	glNormal3f(-1, 0, 0);
	glColor3f(0, 1, 1);
	glVertex3f(-1, 1, -1);
	glVertex3f(-1, -1, -1);
	glVertex3f(-1, -1, 1);
	glVertex3f(-1, 1, 1);
	  
	// right
	glNormal3f(1, 0, 0);
	glColor3f(1, 0, 1);
	glVertex3f(1, 1, 1);
	glVertex3f(1, -1, 1);
	glVertex3f(1, -1, -1);
	glVertex3f(1, 1, -1);
	glEnd();
}


Step 5:  Build and Run the Program

Ok we're ready to build and run our program.

Choose Build -> Build Solution to compile and link the program and Debug -> Start Without Debugging to run it.



Source code for this tutorial.