Title: COMP471
1COMP471 Computer Graphics
- OpenGL Let There Be Light
2Whats Light?
- Light is energy
- A given surface can emit, reflect, etc this
energy. - Light bulb is emitting surface
- Some other surface reflecting base on properties
and material of that surface - White light contains all the color spectrum
3Light Sources
- Ambient
- Point
- Spotlight
- Distant
4Ambient Light
- Uniform illumination of the objects in a given
environment. - Coming from nowhere and going to nowhere.
- Its a diffuse light w/o a direction
- Good to highlight a little a dark scene
5Point Light
6Spotlight
?
7Distant Light Source
- Parallel
- The source is very-very far (like Sun)
- Directional vector determines such a parallel
light source
8Material
- Surfaces can have different material properties.
- These properties determine how a given surface
will reflect a light.
Diffuse
Secular (shiny)
Translucent
9OpenGL and Light
- Light sources
- Spotlight
- Distant
- Emissive
- Material
10Light Sources
- Enable Lighting (another OpenGL state)
- glEnable(GL_LIGHTING)
- Enable one of the light sources
- glEnable(GL_LIGHT0)
- Define light source atttibutes
- glLight(GL_LIGHT0, .)
11Material
- GLfloat ambient 0.2,0.2,0.2,1.0
- glMaterialfv(GL_FRONT,GL_AMBIENT, ambient)
12Light Positioning
- Fixed
- Moving around stationary objects
- Moving along with the viewpoint
- NOTE Position and direction of light are
affected by the current Model-View matrix.
13Fixed Light Position
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- glLightfv(, GL_POSITION, pos)
- other transformations
14Independently Moving Light
- Set viewpoint coordinates
- gluLookAt()
- glPushMatrix()
- glTranslate() glRotate()
- glLight()
- glPopMatrix()
- draw objects
These transformations will apply only to the
light source.
15Moving Light Source Along With the Viewpoint
- Set light source position pos
- glMatrixMode(GL_MODELVEIW)
- glLoadIdentity()
- glLightfv(..., GL_POSITION, pos)
-
- Change the viewpoint
16Normals
- For the light to highlight proper side of an
object, a normal has to be defined for that side. - Use glNormal() right before you draw the polygon
for it to be lit properly. - There are default normals however, they might
not be what you want them to be, so when dealing
with light and textures mapping explicitly state
the normals.
17Normals (2)
void Cubedraw() // Front
glNormal3f(0.0, 0.0, 1.0) drawFace(0, 3, 2,
1, 0) // Right glNormal3f(1.0, 0.0,
0.0) drawFace(2, 3, 7, 6, 1) // Bottom
glNormal3f(0.0, -1.0, 0.0) drawFace(0, 4,
7, 3, 2)
// Top glNormal3f(0.0, 1.0, 0.0)
drawFace(1, 2, 6, 5, 3) // Back
glNormal3f(0.0, 0.0, -1.0) drawFace(4, 5, 6,
7, 4) // Left glNormal3f(-1.0, 0.0,
0.0) drawFace(0, 1, 5, 4, 5)
18References
- Jianxiang Dong Slides from Fall 2001