0% found this document useful (0 votes)
44 views

Laboratory Work 4

This document describes a laboratory work on OpenGL lighting and materials. It explains that OpenGL lighting has four components - emissive, ambient, diffuse, and specular - which are computed independently and added together. Materials are defined by their ambient, diffuse, and specular colors. The tutorial then demonstrates how to enable lighting, define light and material properties, and animate a light source. It assigns extending the application by adding new materials, light sources, and modifying existing ones through keyboard inputs.

Uploaded by

Raluca Bolba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Laboratory Work 4

This document describes a laboratory work on OpenGL lighting and materials. It explains that OpenGL lighting has four components - emissive, ambient, diffuse, and specular - which are computed independently and added together. Materials are defined by their ambient, diffuse, and specular colors. The tutorial then demonstrates how to enable lighting, define light and material properties, and animate a light source. It assigns extending the application by adding new materials, light sources, and modifying existing ones through keyboard inputs.

Uploaded by

Raluca Bolba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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();
}

3.2 Define the light properties


Define a new light source using the ambient, diffuse and specular components.
GLfloat
GLfloat
GLfloat
GLfloat

mylight_ambient[] = {0.1, 0.1, 0.1, 0.1};


mylight_diffuse[] = {1.0, 1.0, 1.0, 1.0};
mylight_specular[] = {1.0, 1.0, 1.0, 1.0};
mylight_position[] = {0, 0, 0, 1};

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();
}

3.3 Define the material properties


Define, in the same manner, the materials that apply on the surface of the object.
GLfloat material_1_ambient[] = {0.1, 0.1, 0.1, 1.0};
GLfloat material_1_diffuse[] = {1.0, 0.0, 0.0, 1.0};
GLfloat material_1_specular[] = {0.0, 0.0, 0.0, 1.0};
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_1_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_1_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_1_specular);
glPushMatrix();
glTranslatef(-3, 3, 0);
glutSolidSphere(1, 20, 20);
glPopMatrix();
glutSwapBuffers();
}

3.4 Define a specular material


Change the specular component and see the effect.
GLfloat
GLfloat
GLfloat
GLfloat

material_2_ambient[] = {0.1, 0.1, 0.1, 1.0};


material_2_diffuse[] = {0.0, 1.0, 1.0, 1.0};
material_2_specular[] = {1.0, 1.0, 1.0, 1.0};
material_2_shininess[] = {60.0};

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();
}

3.5 Define an emissive material


Create an object that simulates the emission of light. Keep in mind that until you modify the value of
an attribute that value remains active.
GLfloat
GLfloat
GLfloat
GLfloat
GLfloat

material_3_ambient[] = {0.0, 0.0, 0.0, 1.0};


material_3_diffuse[] = {0.0, 0.0, 0.0, 1.0};
material_3_specular[] = {0.0, 0.0, 0.0, 1.0};
material_3_shininess[] = {128.0};
material_3_emission[] = {1.0, 1.0, 0.0, 1.0};

GLfloat material_no_emission[] = {0.0, 0.0, 0.0, 1.0};


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);
.......................................................
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_3_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_3_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_3_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, material_3_shininess);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_3_emission);
glPushMatrix();
glTranslatef(-3, -3, 0);
glutSolidSphere(1, 20, 20);
glPopMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_no_emission);
glutSwapBuffers();
}

3.6 Animate the light source


Create a small animation of the light source.
GLfloat angle;
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);
angle += 0.1;
glLightfv(GL_LIGHT0, GL_AMBIENT, mylight_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, mylight_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, mylight_specular);
glPushMatrix();

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.

You might also like