Laboratory Work 4
Laboratory Work 4
1 Objectives
The objective of this laboratory is to present notions about OpenGL lighting and materials.
2 Theoretical background
In OpenGL, light sources are characterized by the amount of red, green, and blue components,
emitted by this source. The same principle could be apply regarding the OpenGL materials. Often,
the material from the surface of an object is characterized by the percentage of the incoming red,
green, and blue components that are reflected in various directions.
The OpenGL lighting model considers the lighting divided into four independent components:
emissive, ambient, diffuse, and specular. All four components are computed independently and then
added together in order to generate the properties of the final light source.
The ambient component defines the light that has been scattered by the environment is such a way
that its direction cannot be determined.
The diffuse component defines the light that comes from one direction. After it hits the surface of the
objects, it is scattered equally in all directions.
The specular component defines the light that comes from a particular direction and bounces off the
surface in a preferred direction (compared with the diffuse light).
The materials color in OpenGL depends on the percentage of the incoming red, green, and blue light
that is reflected by this material. The materials are defined by the ambient, diffuse and specular
colors. The ambient component of the material is combined with the ambient component of the
light. The diffuse and specular components are combined in the same way. For instance, if the
diffuse component specifies that the light emits a yellow color and the material reflects all the green
light, then the color of the object will be red (the yellow color contains the red and green channels,
and if the material reflects all the green light, then only the red chanel remains on the surface of the
object).
The materials have also an emissive color, which can be used to simulate light originating from an
object. Even if you define an emissive object, it will not create a new light source in the scene.
3 Tutorial
3.1 Enable lighting and draw a sample object
Before defining light sources we need to enable the light computation in OpenGL. Modify in the
initOpenGL function as follows:
void initOpenGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glShadeModel(GL_SMOOTH);
glViewport(0, 0, screen_width, screen_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)screen_width/(GLfloat)screen_height, 1.0f, 1000.0f);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 11.0, 0.0, 0.0, -10.0, 0.0, 1.0, 0.0);
glPushMatrix();
glTranslatef(-3, 3, 0);
glutSolidSphere(1, 20, 20);
glPopMatrix();
glutSwapBuffers();
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 11.0, 0.0, 0.0, -10.0, 0.0, 1.0, 0.0);
glLightfv(GL_LIGHT0, GL_AMBIENT, mylight_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, mylight_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, mylight_specular);
glPushMatrix();
glTranslatef(-3, 3, 0);
glutSolidSphere(1, 20, 20);
glPopMatrix();
glutSwapBuffers();
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 11.0, 0.0, 0.0, -10.0, 0.0, 1.0, 0.0);
glLightfv(GL_LIGHT0, GL_AMBIENT, mylight_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, mylight_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, mylight_specular);
.......................................................
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_2_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_2_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_2_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, material_2_shininess);
glPushMatrix();
glTranslatef(3, 3, 0);
glutSolidSphere(1, 20, 20);
glPopMatrix();
glutSwapBuffers();
}
glRotatef(angle, 0, 0, 1);
glTranslatef(10.0, 0.0, 0.0);
glLightfv(GL_LIGHT0, GL_POSITION, mylight_position);
glPopMatrix();
.......................................................
glutSwapBuffers();
}
4 Assignment
Extend the application with the following functionality:
Define new materials and add a new light source. Modify the parameters of the existing
materials and light sources, using the keyboard inputs.