Title: 3D Computer Game Design
1????????3D Computer Game Design
- Chapter 4
- Transformations and Matrices (Part I)
- ????? ???
2In this chapter, you will learn
- Coordinate transformations
- The camera and viewing transformations
- OpenGL matrices and matrix stacks
- Projections
- Using your own matrices with OpenGL
3Understanding Coordinate Transformations
- Local coordinate system
- Unique for every object
- Centered on the object
- Doesnt depend on any other objects
4Understanding Coordinate Transformations
- When rendering 3D scenes, vertices pass through
four types of transformations - Modeling transformation
- Move objects from local coordinates into world
coordinates - Viewing transformation
- Move objects form world coordinates into eye or
camera coordinates
5Understanding Coordinate Transformations
- Projection transformation
- Map objects from eye coordinates to clip
coordinate - Viewport transformation
- Map the clip coordinates into the 2D viewport, or
window, on your screen
6(No Transcript)
7Understanding Coordinate Transformations
- OpenGL includes and combines the modeling and
viewing transformation - Modelview transformation
- These transformations execute in a specific order
8Eye Coordinates
- Define the cameras position and orientation
- In OpenGL
- The default camera is always oriented to look
down the negative z axis
9(No Transcript)
10Viewing Transformations
- Used to position and aim the cameras
- Specify before any other modeling transformations
- Create the viewing transformation
void glLoadIdentity()
11Viewing Transformations
- After initializing the current matrix, you can
create the viewing matrix in several ways - Using the gluLookAt()
- Using the translation and rotation modeling
commands - Create your own routines
12Modeling Transformations
- Allow you to position and orient a model
- Translation
- Moving an object along a specified vector
- Rotation
- Rotate about a vector
- Scaling
- Increase or decrease the size of an object
13(No Transcript)
14Projection Transformations
- Define the viewing volume and clipping planes
- OpenGL offers two types of projections
- Perspective projection
- Show 3D worlds exactly as you see things in real
life - Orthographic projection
- Show objects on the screen in their true size,
regardless of their distance from the camera
15Viewport Transformations
- Map the clip coordinates onto your windows
rendering surface - The final images should be enlarged or shrunk
16OpenGL and Matrices
- Transformations in OpenGL rely on the matrix for
all math computations - Matrix stack
- Construct complicated models
17The Modelview Matrix
- Define the coordinate system
- To place and orient objects
- 4 x 4 matrix
- Modify matrix
- To modify the modelview matrix
- Use the argument GL_MODELVIEW
void glMatrixMode(Glenum mode)
18The Modelview Matrix
- Other arguments for glMatrixMode
- GL_PROJECTION, GL_COLOR, or GL_TEXTURE
- Reset the modelview matrix to the default
position (0, 0, 0) and orientation - Call the glLoadIdentity() function
glMatrixMode(GL_MODELVIEW) glLoadIdentity() //
do other transformations
19Translation
- Move an object from one position to another
position - The OpenGL function
- For example
void glTranslatefd(TYPE x, TYPE y, TYPE z)
glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTr
anslatef(5.0f, 5.0f, 5.0f) DrawCube()
20(No Transcript)
21Rotation
- Rotation
- Angle
- Measure in degrees in the counterclockwise
direction - For example
- Rotate clockwise around the y axis 135 degrees
void glRotatefd(TYPE angle, TYPE x, TYPE y,
TYPE z)
glRotatef(-135.0f, 0.0f, 1.0f, 0.0f)
22Scaling
- Increase or decrease the size of an object or
coordinates system
void glScalefd(GLfloat x, GLfloat y, GLfloat
z) For example glScalef(2.0f, 2.0f, 2.0f)
23Matrix Stacks
- There are four types of matrix stacks in OpenGL
- The modelview matrix stack
- The projection matrix stack
- The color matrix stack
- The texture matrix stack
24Matrix Stacks
- The modelview matrix stack allows you to save the
current state of transformation matrix - The functions to push and pop the matrix stacks
- glPushMatrix() and glPopMatrix()
25(No Transcript)
26The Robot Example
- RobotExample
- Show an animated walking robot around which the
camera rotate
27Reference
- Dave Astle and Kevin Hawkins, Beginning OpenGL
Game Programming, 1st Edition, Course Technology
PTR, 2004. - http//glbook.gamedev.net