Title: 3D Computer Game Design
1????????3D Computer Game Design
- Chapter 5
- Colors, Lighting, Blending, and Fog (Part I)
- ????? ???
2In this chapter, you will learn
- Colors in OpenGL
- Shading
- OpenGL lighting
- Light sources
- Materials
- Blending and transparency
- Fog
3Using Colors in OpenGL
- Two methods
- Using lighting
- Using the current color
- If lighting is disabled, then the current color
is used instead - Primary and secondary color
- Red, green, blue, and alpha components
4Setting the Color
- RGBA mode
- Indicate the intensity of the red, green, and
blue - Alpha is used for transparency
- For example
- Black would be represented by setting the RGB
components to 0.0
5Setting the Color
- To specify the primary color
- The max and min integer values are mapped to 1.0
and 0.0 - Using glColor() with only three components
- The alpha value is set to 1.0
void glColor34bsifd ubusui(T
components) void glColor34bsifd ubusuiv(T
components)
6Setting the Color
- Examples
- Only the last change will have an effect
// using floats glColor3f(1.0, 1.0, 0.0) //
using unsigned bytes glColor3ui(255, 255, 0) //
using signed bytes in an array GLbyte
yellow127, 127, 0) glColor3iv(yellow)
7Shading
- Shading can either
- Flat or smooth
- Flat shading
- The entire primitive is drawn with a single color
- Smooth shading
- Based on the Gouraud shading model
- More realistic
- Use interpolation to determine the colors
8Shading
- Use flat shading on the sample line
- The line will be write because the last vertex
specified is white - Smooth shading is useful for simulating the
effect of a curved surface when lighting is
enabled
9Shading
- Specify the current shading model
- glShadeModel(GLenum mode)
- GL_SMOOTH or GL_FLAT as the mode parameter
- The default setting is GL_SMOOTH
10Shading
- Draw a smooth-shaded triangle
// use smooth shading glShadeModel(GL_SMOOTH) //
draw our smooth-shaded triangle glBegin(GL_TRIANG
LES) glColor3f(1.0f, 0.0f, 0.0f) // red
vertex glVertex3f(-10.0f, -10.0f, -5.0f)
glColor3f(0.0f, 1.0f, 0.0f) // green vertex
glVertex3f(20.0f, -10.0f, -5.0f)
glColor3f(0.0f, 0.0f, 1.0f) // blue vertex
glVertex3f(-10.0f, 20.0f, -5.0f) glEnd()
11A Colorful Example
- Top row uses flat shading, the bottom used smooth
12(No Transcript)
13Lighting in OpenGL
- The most important aspects of 3D graphics
- lighting
14OpenGL Lighting and the Real World
- Light sources
- Such as sun or a light bulb
- OpenGL calculates lighting by approximating the
light into RGB components
15OpenGL Lighting and the Real World
- Light is further broken down into four terms
- Ambient light
- Simulate light bouncing between surfaces
- Not affected by the position of either the light
or the viewer - Diffuse light
- Come from a certain direction
- Reflected equally in all directions
- Affected by the position or direction of the
light, but not the position of the viewer
16OpenGL Lighting and the Real World
- Specular light
- Directional and reflected off a surface in a
particular direction - Refer to as shininess
- Affected by the position of both the light and
the eye - Emissive light
- Does not illuminate surrounding objects
- Cause the emissive object to be more intensely
lit
17OpenGL Lighting and the Real World
- The final results of lighting depend on several
major factors - Position or direction or have terms that affect
attenuation - The orientation of surfaces in the scene
- The material each object is made of
- The lighting model
18OpenGL Lighting and the Real World
- OpenGL uses the material of the surface to
determine the percentage of red, green, and blue
light that should be reflected by the surface
19Light Sources
- The first thing is to enable the OpenGL lighting
- glEnable(GL_LIGHTING)
- glEnable(GL_LIGHTx)
- x takes on a numeric value ranging from 0 to a
max value
20Assigning Light Properties
- These properties are controlled through glLight()
glLightfi(GLenum light, GLenum pname, type
param) glLightfiv(GLenum light, GLenum pname,
const type params) light idebtifies which
lights properties you are modifing and uses
GL_LIGHTx
21(No Transcript)
22Position and Direction
- Each light have either a position or direction
- Lights with a position are often called
positional or point lights - Directional lights represent lights that are
infinitely far away - The sun is an excellent example of this
23Position and Direction
- Set a lights position using GL_POSITION
- Pass a four-element vector of the form (x, y, z,
w) - x, y, and z represent either the position or
direction - The w term is used to indicate whether this is a
directional or positional light - 0.0 directional
- 1.0 - positional
24Position and Direction
- Set up a directional light pointing down the z
- Set up a positional light located at (2,
4 , -3)
GLfloat lightDir 0.0, 1.0, 0.0,
0.0 glLightfv(GL_LIGHT0, GL_POSITION, lightDir)
GLfloat lightPos 2.0, 4.0, -3.0,
1.0 glLightfv(GL_LIGHT0, GL_POSITION, lightPos)
25Light Color
- Light sources are composed of three of the
lighting terms - Ambient, diffuse, and specular
- Set up a blue light with white specular
GLfloat white 1.0, 1.0, 1.0, 1.0 GLfloat
blue 0.0, 0.0, 1.0, 1.0 glLightfv(GL_LIGHT
0, GL_AMBIENT, blue) glLightfv(GL_LIGHT0,
GL_DIFFUSE, blue) glLightfv(GL_LIGHT0,
GL_SPECULAR, white)
26Light Color
- The default color for all terms for all lights is
black - Two exceptions
- Light zero have a default diffuse and specular
term of white (1.0, 1.0, 1.0, 1.0)
27Attenuation
- The intensity of the light dropping off away from
the lamp - Attenuation
- Based on the distance to the object
d is the distance from the light to the
vertex kc, kl, and kq are the constant, linear,
and quadratic attenuation factors, respectively
28Attenuation
- You can change them by passing GL_CONSTANT_ATTENUA
TION, GL_LINEAR_ATTENUATION, or
GL_QUADRATIC_ATTENUATION to glLight()
29Attenuation
- Set the attenuation factors to (4, 1, 0.25)
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION,
4.0f) glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION,
1.0f) glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATIO
N, 0.25f)
30Attenuation
- The attenuation factor affects only positional
light sources - One drawback
- Attenuation incurs as additional cost
31Spotlights
- Limit the effect of the light to a specific cone
- Spotlight
- Parameters
- The spotlight cutoff, direction, and focus
- Cutoff
- The angle between the edge of the cone
- GL_SPOT_CUTOFF
32Spotlights
- OpenGL accepts only values between 0.0 and 90.0
for the GL_SPOT_CUTOFF - The default value is 180.0 degrees
- Convert a spotlight back into a regular light
33Spotlight
- Specify a cone of light that spreads a total of
30.0 degree
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.0f) //
30 degrees light cone
34Spotlight
- Specify the direction that the spotlight is
facing - Done with GL_SPOT_DIRECTION
- The default direction is (0.0, 0.0, -1.0)
- Points the spotlight down the negative z axis
- Point the spotlight down the negative y axis
float spotlightDirection0.0, -1.0,
0.0 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,
spotlightDirection)
35Spotlight
- Define the concentration of the spotlight in the
center of the light cone - Use GL_SPOT_EXPONENT to control it
- A higher spot exponent results in a more focused
light source that drops off quickly
36Spotlight
- The spot exponent can range from 0 to 128
- The default value is 0
- Result in no attenuation
- The spotlight is evenly distributed
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 10.0f)
37Moving and Rotating Lights
- One way is to set the position of the object
after you translate or rotate it - The more general problem is having a ligh
position stay fixed relative to the eye, or
camera, position.
38Moving and Rotating Lights
- To achieve this effect
- You set the modelview matrix
- Define your light position at the origin
- Set up the camera transformation
glMatrixMode(GL_MODELVIEW) glLoadIdentity() //
position the light at the origin GLfloat
lightPos(0.0, 0.0, 0.0, 1.0) glLightfv(GL_LIGHT0,
GL_POSITION, lightPos) // set up the
camera glLookAt(eye.x, eye.y, eye.z, at.x, at.y,
at.z, up.x, up.y, up.z,)
39Reference
- Dave Astle and Kevin Hawkins, Beginning OpenGL
Game Programming, 1st Edition, Course Technology
PTR, 2004. - http//glbook.gamedev.net