2
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
3
Motivation We know how to rasterize
- Given a 3D triangle and a 3D viewpoint, we know which pixels represent the triangle
But what color should those pixels be?
4
Motivation – Why we need light?
Adds realism to our objects
Suppose we create a model of a sphere using many polygons and color it with glColor.
But our objective is
5
Why we need light? - Light example 1
Keyboard key l turns on and off the lights. Keyboard key d turns on and off depth test. The keys 1,2,3 allow turn on and of 3 light
sources. Without lights, it looks like a simple 2d red circle. When we use lights definitions, we can see its a
sphere.
6
Depth Test - Light Example 1
In this version of OpenGL, the sphere is drawn from the front to the back.
So if the depth test is cancelled, what we will see is the back of the sphere.
In another version of OpenGL the results can be different.
7
Motivation – (cont’d)
Light-material interactions affect vertices appearance
Due to this interaction, each vertex would have different color and shade
Need to consider - Light sources- Material properties - Location of viewer - Surface orientation
8
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
9
What is Color? Color is simply a
wavelength of light that is visible to the human eye
Reflection/absorption
Where is the black?
What about white?
Light Spectrum
glColor)…(
10
What is Color? – (cont’d)
glColor3f(red, green, blue)glColor4f(red, green, blue, alpha)
11
What determines vertex color in OpenGL?
Is OpenGL lighting enabled ?
Color determined by glColor3f(…)
Ignoring:• normals• lights• material
properties
Color determined by shades
Using:• normals• lights• material
properties
YESNO
12
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
13
Polygonal Shading
Shading is defined as a smooth transition from one color to another
Curved surfaces are approximated by polygons
Types of shading are:- Flat shading- Smooth Shading- Gouraud Shading
14
Flat Shading Enabled with glShadeModel(GL_FLAT) Shading is constant across polygon Color of last vertex determines interior
color Only suitable for very small polygons
v1v0
v2
15
Flat Shading – (cont’d)
Inexpensive to compute Appropriate for objects with flat faces Less suitable for smooth surfaces
16
Smooth Shading Enable with glShadeModel(GL_SMOOTH) Interpolate color in interior Computed during rasterization Much better than flat shading More expensive to calculate
v1v0
v2
17
Gouraud Shading Special case of smooth shading Need to calculate vertices normals, HOW? Average all adjacent face normals Requires knowledge about which faces
share a vertex
18
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
19
Enabling Light Lighting in general must be enabled
glEnable)GL_LIGHTING(
Each individual light must be enabledglEnable)GL_LIGHT0(
OpenGL supports at least 8 light sources
There are 3 types of effects of light source: Ambient Diffuse Specular
20
Light effects Ambient light :
Directionless Objects are evenly lit on all surfaces in all directions Objects are evenly shaded regardless of their viewing
angle
Diffuse light: Comes from a particular direction Reflected evenly off a surface
Specular light: Comes from a particular direction Reflected sharply and in a particular direction To make objects specular, both light source and object
must have specular property set
21
Light effects – (cont’d)
22
Defining light source Use vectors (r, g, b, a) for light properties Beware: light positions will be transformed by the
modelview matrix
GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0};GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_position[] = {-1.0, 1.0, -1.0, 0.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);glLightfv(GL_LIGHT0, GL_POSITION, light_position);
23
Point Source vs. Directional Source Directional light given by position vectorGLfloat light_position[] = {-1.0, 1.0, -1.0, 0.0};glLightfv)GL_LIGHT0, GL_POSITION, light_position(;
Point source given by position point
GLfloat light_position[] = {-1.0, 1.0, -1.0, 1.0};glLightfv)GL_LIGHT0, GL_POSITION, light_position(;
24
Moving Light Sources Light sources are geometric objects whose
positions or directions are affected by the modelview matrix
It is possible to: Move the light source(s) with the object(s) Move the light source(s) and object(s)
independently Fix the object(s) and move the light source(s) Fix the light source(s) and move the object(s)
25
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
26
Material Properties Material have certain reflective color properties Put a blue ball in a dark room with only yellow light,
how would it looks like? Material properties stay in effect until changed
GLfloat mat_a[] = {0.1, 0.5, 0.8, 1.0};GLfloat mat_d[] = {0.1, 0.5, 0.8, 1.0};GLfloat mat_s[] = {1.0, 1.0, 1.0, 1.0};GLfloat low_sh[] = {5.0};
glMaterialfv)GL_FRONT, GL_AMBIENT, mat_a(;glMaterialfv)GL_FRONT, GL_DIFFUSE, mat_d(;glMaterialfv)GL_FRONT, GL_SPECULAR, mat_s(;glMaterialfv)GL_FRONT, GL_SHININESS, low_sh(;
27
Material Properties – (cont’d)
Can shortcut material properties using glColor
Must be explicitly enabled and disabled
glEnable)GL_COLOR_MATERIAL(;
/* affect front face, diffuse reflection properties */glColorMaterial)GL_FRONT, GL_DIFFUSE(;
glColor3f)0.0, 0.0, 0.8(;/* draw some objects here in blue */
glColor3f)1.0, 0.0, 0.0(;/* draw some objects here in red */
glDisable)GL_COLOR_MATERIAL(;
28
Outline
Color in OpenGL Polygonal Shading Light Source in OpenGL Material Properties Normal Vectors
29
Defining normals Define unit normal before each vertex
glNormal3f(nx1, ny1, nz1);glVertex3f(x1, y1, z1);glNormal3f(nx2, ny2, nz2);glVertex3f(x2, y2, z2);glNormal3f(nx3, ny3, nz3);glVertex3f(x3, y3, z3);
different normals
glNormal3f(nx, ny, nz);glVertex3f(x1, y1, z1);glVertex3f(x2, y2, z2);glVertex3f(x3, y3, z3);
same normalfor all vertices
30
Maintain Normals Length of normals changes under some
modelview transformations (but not under translations and rotations)
Ask OpenGL to automatically re-normalize
glEnable)GL_NORMALIZE(;
31
Assignment 2 Gray scale image Using BMPLoader to create a terrain – height
map Objects in OFF files Locating objects somewhere on the terrain
32
Loading BMP files
By this class, you can load bmp images and access the cells directly
BMPClass bmp;BMPLoad)imgName,bmp(;
_width = bmp.width;_hight = bmp.height;