TUTORIAL 1 - OpenGL Fundamentals

1  2  3  4  5  6  7  8  9  10  11  12  13

Distant Light

As a first example of lighting, let's take a look on how to realize a distant light, as it would be used in environmental scenes to imitate sunlight. Although we won't get shadows, we can imitate sunlight by reducing the ambient light component to a Minimum like 0.1. The dominant part has to be the diffuse light, so that we get bright areas facing the light source and dark areas in the shadow regions. If we take a tea pot as an object, it makes sense to use a high shininess of the material and a high specular light component. After we have defined the lighting and material we have to activate the defined light and the lighting itself. The entire lighting setup looks therefore as follows:

void __fastcall TFormMain::SetupLighting()
{
   GLfloat ambientLight[]={0.1,0.1,0.1,1.0};    	             // set ambient light parameters
   glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);

   GLfloat diffuseLight[]={0.8,0.8,0.8,1.0};    	             // set diffuse light parameters
   glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);

   GLfloat specularLight[]={0.5,0.5,0.5,1.0};  	               // set specular light parameters
   glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);

   GLfloat lightPos[]={0.0,30.0,60.0,0.0};      	              // set light position
   glLightfv(GL_LIGHT0,GL_POSITION,lightPos);

   GLfloat specularReflection[]={1.0,1.0,1.0,1.0};  	          // set specularity
   glMaterialfv(GL_FRONT, GL_SPECULAR, specularReflection);
   glMateriali(GL_FRONT,GL_SHININESS,128);

   glEnable(GL_LIGHT0);                         	              // activate light0
   glEnable(GL_LIGHTING);                       	              // enable lighting
   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight); 	     // set light model
   glEnable(GL_COLOR_MATERIAL);                 	              // activate material
   glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
   glEnable(GL_NORMALIZE);                      	              // normalize normal vectors
}

A very nice add-on to OpenGL is the glut library which simplifies OpenGL programming significantly. One of the nice commands provided by this library creates a three-dimensional model of the so called Utah tea pot:

void   glutSolidTeapot(GLfloat TeapotSize);

Put everything together the final result should look like the image below.

previous   next