Interactive Game Loop Interactive Game Loop 3D Graphics

8/28/08
Interactive Game Loop
Lecture 4: 3D Rendering Pipeline (I)
Prof. Hsien-Hsin Sean Lee
School of Electrical and Computer
Engineering
Georgia Institute of Technology
2
Interactive Game Loop
Game Input
(Keyboard,
Mouse)
3D Graphics Rendering Pipeline
Physics
Strategy, AI
zy
y
Render
Triangles
(one frame)
y
z
x
y
z
z
y
y
x
x
z
x
Geometry
Processing
Rasterization
Processing
x
x
30 to 60 frames per second
y
•  Geometry Pipeline
Exit?
–  Processing Vertices
–  Mainly floating-point operations
x
•  Rasterization Pipeline
Cleanup
States
–  Processing Pixels
–  Mainly dealing with Integer operations
3
4
1
8/28/08
3D Graphics Rendering Pipeline
zy
y
z
y
x
y
y
z
x
Geometry
Processing
zy
y
x
x
z
y
z
3D Graphics Rendering Pipeline
y
Rasterization
Processing
z
x
x
y
x
z
y
z
y
x
x
z
x
Geometry
Processing
Rasterization
Processing
x
x
y
y
•  Geometry Pipeline
•  Geometry Pipeline
–  Processing Vertices
–  Mainly floating-point operations
–  Processing Vertices
–  Mainly floating-point operations
–  SSE/SSE2 were designed for this part
x
•  Rasterization Pipeline
x
•  Rasterization Pipeline
–  Processing Pixels
–  Mainly dealing with Integer operations
–  MMX was originally designed to accelerate
this type of functionality
–  Processing Pixels
–  Mainly dealing with Integer operations
–  MMX was originally designed to accelerate
this type of functionality
5
Fixed Function 3D Graphics Pipeline
zy
y
y
z
x
y
z
x
z
y
3D Coordinates
+y
y
+y
x
x
z
x
Geometry
Processing
Rasterization
Processing
+z
x
(away from the viewer)
Performed by (GP)GPU
+x
y
+x
•  Geometry Pipeline
–  Processing Vertices
–  Mainly floating-point operations
–  SSE/SSE2 were designed for this part
•  Rasterization Pipeline
6
+z (toward the viewer)
Left-Handed System
Right-Handed System
x
–  Processing Pixels
–  Mainly dealing with Integer operations
–  MMX was originally designed to accelerate
this functionality
•  LHS was more common for graphics viewing space
•  XNA uses RHS, however
•  Camera or viewer position can be set up using 3D API
7
8
2
8/28/08
Geometry Format ─ Vertex
Coordinates
+Y
Geometry Format ─ Vertex Normals
+Y
(X2, Y2, Z2)
(NX2, NY2, NZ2)
+Z
+Z
(X3, Y3, Z3)
(NX3, NY3, NZ3)
(X1, Y1, Z1)
(NX1, NY1, NZ1)
Y1
Z1
+X
+X
X1
9
Geometry Format ─ Vertex Colors
10
Triangle-based Geometry Representation
+Y
(R2, G2, B2, A2)
V1
V5
+Z
V4
V7
(R3, B3, B3, G3)
V6
V4
V2
V3
V4
V5
V2
V5
V3
V3
V9
V8
(R1, G1, B1, A1)
Triangle List
(note the vertex order)
V1
Triangle Strip
V2
Triangle Fan
+X
11
12
3
8/28/08
Specifying a 3D object
Specifying a 3D object
V7
Triangle list
{v1, v3, v2},
{v1, v5, v3},
{v5, v6, v3},
{v4, v3, v6},
{v1, v7, v6},
{v1, v6, v5}
V6
V1
V5
V6
V1
Triangle strip
{v5, v3, v1, v2},
{v5, v6, v3, v4},
{v7, v6, v1, v5}
V4
V2
V7
V8
V3
V2
•  Vertex ordering is critical when culling mode enabled
•  Direct3D and XNA “default”s are counter-clockwise culling
V4
Triangle list
{v1, v2, v7},
{v2, v8, v7},
{v2, v3, v4},
{v2, v4, v8},
{v4, v7, v8},
{v4, v6, v7}
Triangle strip
{v1, v2, v7, v8},
{v3, v4, v2, v8},
{v6, v7, v4, v8}
V3
•  Vertex ordering is critical when culling mode enabled
•  We will discuss normal computation later in this lecture
–  Weird default setting for an RHS
13
3D Rendering Pipeline
14
Transformation Pipeline
• World Transformation
• View Transformation
Rasterization
Viewport Transform
Perspective Divide
Clipping
Backface Culling
Projection Transform
Lighting
View Transform
World Transform
– Model coordinates  World coordinates
– World coordinates  Camera space
• Projection Transformation
– Camera space  View Plane
• These are a series of matrix multiplications
15
16
4
8/28/08
World Transformation
View Transformation
World Coordinates
World Coordinates
+y
+y
Local model coordinates
+z
+z
•  Translation
•  Rotation
•  Scaling
World origin
Local model coordinates
+z
•  Camera position
•  Look vector
+y
+x
World origin
+x
+x
17
18
Projection Transformation
Homogeneous Coordinates
• Set up camera internals
• Enable all transformations to be done by
“multiplication”
– Primarily for translation (See next few slides)
• Set up
– Field of View (FOV)
– View frustum
– View planes
• Add one coordinate (w) to a 3D vector
• Each vertex has [x, y, z, w]
– W will be useful for perspective projection
– W should be 1 in a Cartesian Coordinate System
• Will discuss later
19
20
5
8/28/08
Transformation 1: Translation (Offset)
+y
Translation Matrix
+y
(xt, yt, zt)
+z
+z
(x, y, z)
+x
+x
21
Transformation 2: Scaling
+y
22
Scaling Matrix
+y
+z
+z
+x
+x
23
24
6
8/28/08
Transformation 3: Rotation
2D Rotation
+y
+y
(x, y)
+x
+y
θ
+z
+x
+x
+y
+y
+x
+z
(x’, y’)
θ
(x, y)
ϕ
+x
Rotate along which axis?
25
3D Rotation Matrix
26
Non-Commutative Property (1)
+y
+y
Rotation along Z axis
+z
+z


Rotation along Y axis

+x
+x

1. 
2. 
Rotation along X axis
27
o
Counter-clockwise 90 along y
o
Clockwise 90 along x
1. 
2. 
o
Clockwise 90 along x
o
Counter-clockwise 90 along y
28
7
8/28/08
Non-Commutative Property (1)
+y

Non-Commutative Property (2)

+z
+y
+y

+x


+z
+z
(x’’, y’’, z’’) = (-z, -x, y)
+y



+z



+x
1. 
2. 
+x
Translation by (x, y, z)
Scale by 2 times
1. 
2. 

+x
Scale by 2 times
Translation by (x, y, z)
(x’’, y’’, z’’) = (-y, -z, x)
29
Non-Commutative Property (2)

+y

30
Non-Commutative Property
• Ordering matters !

+z

+x
• Be careful when performing matrix
multiplication
(x’’, y’’, z’’) = (x*Sx+Sx*Tx, y*Sy+Sy*Ty, z*Sz+Sz*Tz)
Offsets were scaled as well
+y


+z


+x
(x’’, y’’, z’’) = (x*Sx+Tx, y*Sy+Ty, z*Sz+Tz)
31
32
8