Title: 3D Computer Game Design
1????????3D Computer Game Design
- Chapter 5
- Colors, Lighting, Blending, and Fog (Part II)
- ????? ???
2In this chapter, you will learn
- Colors in OpenGL
- Shading
- OpenGL lighting
- Light sources
- Materials
- Blending and transparency
- Fog
3Materials
- OpenGL approximates material properties based on
- Reflects red, green, and blue light
- Three color terms
- Ambient, diffuse, and specular
- A material with a high specular reflectance
- Appear shiny
4Defining Materials
- Setting a material is similar to creating a light
source
void glmaterialif(GLenum face, GLenum pname,
TYPE param) void glmaterialifv(GLenum face,
GLenum pname, const TYPE params) face how the
material will be applied to the objects
polygons. It can be one of three values
GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK. pname
tells OpenGL which material properties are being
set.
5Material Colors
- The ambient, diffuse, and specular components
- Specify how a material interacts with a light
source - Determine the color of the material
- OpenGL allows you to use GL_AMBIENT_AND_DIFFUSE
to specify them both together
6(No Transcript)
7Material Colors
- Set the ambient material color to red for the
front and back of polygons - Set both the ambient and diffuse materials to
white for the front of polygons
float red 1.0f, 0.0f, 0.0f,
1.0f glMaterialfv(GL_FRONT_AND_BACK,
GL_AMBIENT, red)
float white 1.0f, 1.0f, 1.0f,
1.0f glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFU
SE, white)
8Specifying a Material (1/5)
- Reflectivity properties of a material
- face
- GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
- name
- GL_DIFFUSE, GL_SPECLAR, GL_AMBIENT,
GL_AMBIENT_AND_DIFFUSE, GL_EMISSION, GL_SHININESS
glMaterialif(GLenum face, GLenum name, TYPE
value) glMaterialifv(GLenum face, GLenum name,
TYPE value)
9Specifying a Material (2/5)
10Specifying a Material (3/5)
11Specifying a Material (4/5)
12Specifying a Material (5/5)
13Brass Material (1/2)
14Brass Material (2/2)
15Red Plastic Material (1/2)
16Red Plastic Material (2/2)
17Shininess
- The illusion of shininess
- Caused by the bright spot, known as a specular
highlight - Set using GL_SHININESS
- Is range from 0 to 128
18Shininess
- The values of 128 representing an extremely shiny
material with a small specular highlight - 0 representing a material that is not shiny with
a very large specular highlight
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS,
128)
19Emissive Materials
- Simulate that emit light
- Such as an area light or anything that glows
- It wont illuminate nearby objects
- Cause the object to appear brighter
20Emissive Materials
- Use GL_EMISSION
- By default, the emissive term is (0.0, 0.0, 0.0,
1.0)
// use a dark gray color GLfloat emissiveColor
0.3, 0.3, 0.3, 1.0 // set the emissive term
for both the front and back face glMaterialfv(GL_F
RONT_AND_BACK, GL_EMISSION, emissiveColor)
21Color Tracking
- Another way to set material properties is by what
is called color tracking - Set material properties with calls to the
glColor()
22Color Tracking
- Passing the GL_COLOR_MATERIAL parameter to the
glEnable() function - Use glColorMaterial() function to specify which
material parameters will be affected by calls to
glColor
23Color Tracking
- A sample
- Set diffuse property of the fronts of polygons to
track the current color
glEnable(GL_COLOR_MATERIAL) glColorMaterial(GL_FR
ONT, GL_DIFFUSE) glColor3f(1.0f, 0.0f,
0.0f) glBegin(GL_TRIANGLES) // draw
triangles glEnd()
24Normals
- Normals are vectors that are perpendicular to a
surface - The orientation of that surface
- Specify on a per-vertex basis
25Normals
glNormal3f(nx, ny, nz) glNormal3fv(pointer_to_nor
mal)
26Working with Normals (1/2)
glShadeModel(GL_FLAT) glBegin(GL_TRIANGLES)
glNormal3fv(n) glVertex3fv(p1)
glVertex3fv(p2) glVertex3fv(p3) glEnd()
27Working with Normals (2/2)
glShadeModel(GL_SMOOTH) glBegin(GL_TRIANGLES)
glNormal3fv(n1) glVertex3fv(p1)
glNormal3fv(n2) glVertex3fv(p2)
glNormal3fv(n3) glVertex3fv(p3) glEnd()
28Blending
- Learn to use the a component in RGBA color for
- Blending for translucent surfaces
- Compositing images
29Opacity and Transparency
- Opaque surfaces permit no light to pass through
- Transparent surfaces permit all light to pass
- Translucent surfaces pass some light
- translucency 1 opacity (a)
opaque surface a 1
30OpenGL Blending and Compositing
- Must enable blending and pick source and
destination factors - glEnable(GL_BLEND)
- glBlendFunc(source_factor,
- destination_factor)
- Only certain factors supported
- GL_ZERO, GL_ONE
- GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
- GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA
- See Redbook for complete list
31OpenGL Blending and Compositing
- Transparency is implemented using GL_SRC_ALPHA
for the source and GL_ONE_MINUS_SRC_ALPHA for the
destination - glEnable(GL_BLEND)
- glColor4f(0.0f, 0.0f, 1.0f, 0.5f)
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
- Example
- First draw a red triangle (destination) and then
draw a blue triangle (source) on top of it - With an alpha value of 0.5, the blue triangle is
50 transparent
32Fog
33OpenGL Fog Functions
- GLfloat fcolor4
- glEnable(GL_FOG)
- glFogf(GL_FOG_MODE, GL_EXP)
- glFogf(GL_FOG_DENSITY, 0.5)
- glFogfv(GL_FOG, fcolor)
34Summary
- You have learned about
- how OpenGL handles colors
- How shading works
- What the elements of lighting model are
- How to control them
- How to use blending
- How to take advantage of OpenGLs built-in fog
35References
- Dave Astle and Kevin Hawkins, Beginning OpenGL
Game Programming, 1st Edition, Course Technology
PTR, 2004. - http//glbook.gamedev.net
- Edward Angel, Interactive Computer Graphics A
Top-Down Approach Using OpenGL, 4th Edition,
Addison-Wesley Pub. Co., March 2005. - http//www.cs.unm.edu/angel