mpglecture04f09 3dbasics2 - georgia institute of...
TRANSCRIPT
8/26/09
1
3D Rendering Pipeline (II)
Prof. Hsien-Hsin Sean Lee School of Electrical and Computer
Engineering Georgia Institute of Technology
2
Projection from 3D space • Projection transform 3D geometry into a form that can be
rendered as a 2D image
Adapt from Joe Farrell’s article (http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__1/ )
x
y
z
3
Projection Basics • Projection transforms your geometry into a canonical view
volume (e.g., in D3D or XNA) • Only X- and Y-coordinates will be mapped onto the screen • Z will be almost useless but used for depth test
(-1, -1, 0)
(1, 1, 1)
x
y
z
(0, 0, 0)
Canonical view volume (LHS) (-1, -1, 0) to (1,1,1) used by D3D/XNA
4
Orthographic (or Parallel) Projection
Viewer’s position
View plane
• Project from 3D space to the viewer’s 2D space
8/26/09
2
5
Orthographic Projection
View Volume (an axis-aligned box)
(l, b, n)
(r, t, f)
(-1, -1, 0)
(1, 1, 1)
x
y
z
Canonical view volume 6
Orthographic Projection • Derive x’ and y’
7
Orthographic Projection • Derive z’ (slightly different for the range)
8
Orthographic Projection • Put all together
8/26/09
3
9
Orthographic Projection • In orthographic projection
– Z-axis passes through the center of your view volume – Field of view (FOV) extends equally far
• To the left as to the right (i.e., r = -l) • To the top as to the below (i.e., t=-b)
10
Orthographic Projection
• Same size in 2D and 3D • No distance feel • Parallel lines remain parallel • Good for tile-based games where camera is in fixed location
(e.g., Mahjong or 3D Tetris)
11
Perspective Projection
• The farther the object is, the smaller it appears • Some photo editing software allows you to perform
“Perspective Correction”
Viewer’s position
View plane
12
Viewing Frustum
Far plane
Viewer’s position
Near plane
Think about looking through a window in a dark room
8/26/09
4
13
Viewing Frustum
Near plane
Far plane
Viewer’s position
14
Perspective Projection
(-1, -1, 0)
(1, 1, 1)
x
y
z
Canonical view volume
(l, b, n)
(r, t, f)
View Frustum (a truncated pyramid)
15
Perspective Projection • Given a point (x,y,z) within the view frustum, project it onto
the near plane z=n – x ∈ [l, r] and y ∈ [b, t]
• We will map x from [l,r] to [-1,1] and y from [b,t] to [-1,1]
(-1, -1, 0)
(1, 1, 1)
x
y
z
Canonical view volume
(l, b, n)
(r, t, f)
View Frustum
16
Perspective Projection Math (x,y,z)
(x’,y’,n)
n x’
y’
x
y
z
(0,0,0) (0,0,n)
To calculate new coordinates of x’ and y’ f
8/26/09
5
17
Perspective Projection Math (x,y,z)
(x’,y’,n)
n x’
y’
x
y
z
(0,0,0) (0,0,n)
f
⇒ Apply the formula we derived in orthographic projection
Now let’s tackle at z’ component 18
Perspective Projection Math
• We know z (depth) transformation has nothing to do with x and y
• the desired z’z function is shown above
19
Perspective Projection Math
• We know for a fact that (boxed equations above) – z’ = 0 when z=n (near plane) – z’ = 1 when z=f (far plane)
⇒
20
Perspective Projection Math
8/26/09
6
21
Perspective Projection Math
• At the end, we will have to divide by z to obtain [x’, y’, z’, w’] • Similar to orthographic projection ⎯ we keep equal FOV
extension, thus replace l, r, t, b with w and h
22
Define Viewing Frustum
Viewer’s position
+z
Parameters: FOV: Field of View Aspect ratio = Width/Height Near z Far z
fov/2
+y
Far (f)
Near (n)
Height
XNA: Matrix.CreatePerspectiveFieldOfView
23
Perspective Projection
+z
+y
a h/2
h/2
n
Need to replace w and h with FOV and aspect ratio
24
Final Matrix of Perspective Projection
Don’t worry. XNA API did this for you: Matrix.CreatePerspectiveFieldOfView
8/26/09
7
25
Viewport Transformation
• The actual 2D projection to the viewer • Copy to your back buffer (frame buffer) • Can be programmed, scaled, ..
26
Other Geometry Optimizations • Backface Culling
• Clipping
• Hidden surface removal (Occlusion)
27
Backface Culling
(c1, c2, c3) (b1, b2, b3)
(a1, a2, a3)
Cross product
2 Vectors
28
Compute the Surface Normal for a Triangle
(0, 0, 0) (4, 0, 0)
(3, 3, 0)
Compute Normals (Clockwise vectors)
v2 x
z
y
8/26/09
8
29
Compute the Surface Normal for a Triangle
(0, 0, 0) (4, 0, 0)
(3, 3, 0)
Compute Normals
v1 x
z
y
30
Backface Culling Method
Surface vectors
Eye vector
31
Backface Culling Method (dot product)
θ
32
Backface Culling Method (dot product)
8/26/09
9
33
When to Perform Backface Culling?
Clipp
ing
Persp
ectiv
e Divi
de
View
port
Tran
sform
Raste
rizati
on
Proje
ction
Tran
sform
Wor
ld Tr
ansfo
rm
View
Tran
sform
Lighti
ng
Back
face C
ulling
Clipp
ing
Persp
ectiv
e Divi
de
View
port
Tran
sform
Raste
rizati
on
Proje
ction
Tran
sform
Wor
ld Tr
ansfo
rm
View
Tran
sform
Lighti
ng
Back
face C
ulling
How?
Transform your camera to the world space first! 34
3D Clipping
• Test 6 planes if a triangle is inside, outside, or partially inside the view frustum
• Clipping creates new triangles (triangulation) – Interpolate new vertices info
35
How to Clip Against a Plane? • Test each vertex of a triangle
– Outside – Inside – Partially inside
• Incurred computation overhead • Save unnecessary computation (and
bandwidth) later • Need to know how to determine a plane • Need to know how to determine a vertex is
inside or outside a plane 36
What it Takes to Determine a Plane?
• You need two things to specify a plane – A point on the plane (p0, p1, p2) – A vector (normal) perpendicular to the plane (a, b, c) – Plane a*(x – p0) + b*(y – p1) + c*(z - p2) = 0
V=(5, 6, 7)
P=(1, 2, 3) K=(x, y, z)
Plane equation
5*(x-1)+6*(y-2)+7*(z-3)=0
8/26/09
10
37
Distance Calculation from a Plane
• Given a point R, calculate the distance – Distance > 0 inside the plane – Distance = 0 on the plane – Distance < 0 outside the plane
Normal R υ →
d
d θP
38
Distance Calculation from a Plane
R
υ →
d θ
P 180-θ
39
Triangulation using Interpolation
(a2, b2, c2)
(a1, b1, c1)
(x, y, z)
d1 d2
40
Illumination Models
• It won’t look 3D without lighting • Part of geometry processing • Illumination Types
– Ambient – Diffuse – Specular – Emissive
8/26/09
11
41
Local vs. Global Illumination • Local Illumination
– Direct illumination: Light shines on all objects without blocking or reflection
– Used in most games
• Global Illumination – Indirect illumination: Light bounces from one
object to other objects – Add more realism (non real-time rendering) – Computationally much more expensive – Ray tracing, radiosity
42
Common Light Sources
Directional Light (Infinitely far away)
Point Light (Emit in all directions)
Spot Light (Emit within a cone)
43
Illumination: Ambient Lighting • Not created by any light source • A constant lighting from all directions • Contributed by scattered light in a surrounding
RGB multiplies separately Material Color Light Color
⊗
44
Illumination: Diffuse Lighting • Light sources are given • Assume light bounces in all directions
Refelcted Light will reach the eyes no matter where the camera is!
Diffuse surface
8/26/09
12
45
Reflected Light Intensity Calculation • Reflectivity ∝ the entry angle • Use Lambert’s cosine Law intensity
F(θ)=cos(θ)
Surface Normal
n →
Surface Normal
n → θ
Surface Normal
n → θ Dot product
Reflection Full
None
Partial
L →
46
Diffuse
Illumination: Diffuse Lighting
Material Color Light Color Ambient +
Diffuse
Ambient + Diffuse
⊗
47
Illumination: Specular Lighting • Create shining surface (surface perfectly reflects) • Viewpoint dependent
Ambient + Diffuse + Specular Material Color Light Color
Specular
48
Jim Blinn’s Specular Model
E →
L →
n →
R→ Half-way vector
H→
8/26/09
13
49
Brightness Effect (S)
L1
L2
n1
n2
H1
H2
More shining
E1
E2
50
Brightness Effect (S)
intensity
cosθ
cos2θ
cos8θ
cos64θ
cosθ > cosω
~0.65
~0.95
cos8θ >>> cos8ω
~0.62
n1
n2
H1 H2
θ
ω
S=1
n1
n2
H1 H2
θ
ω
S=8
~0.05 radian
51
Specular Lighting Effect
• A larger S shows more concentration of the reflection
52
Illumination: Emissive Lighting
• Color is emitted by the material only
Ambient + Diffuse +
Specular + Emissive
Material Emissive
Color
+
Ambient + Diffuse + Specular
8/26/09
14
53
Common Light Sources
Directional Light (Infinitely far away)
Point Light (Emit in all directions)
Spot Light (Emit within a cone)
54
Light Source Properties • Position • Range
– Specifying the visibility • Attenuation
– The farther the light source, the dimmer the color
55
Falloff effect
Spotlight Effect
• Similar idea to specular lighting • Falloff factor determines the fading effect of a spotlight • “f” exponentially decreases the cosα value
L → d → α