that produces pictures of a 3D scene .... 010. 0. 00. 0. 000 df fd df f h d h d. T pers.
Scaling (d/h in x and y). Truncated .... glVertex3f(pt[iv].x, pt[iv].y, pt[iv].z);.
CPSC 587/687
Graphics Pipeline : Geometric Operations
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 1
CPSC 587/687
Viewing transformation
•
Tools for creating and manipulating a “camera” that produces pictures of a 3D scene
•
Viewing transformations and projections
•
Perform culling or back-face elimination
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 2
Local coordinate space
Object definition Modeling transformation World coordinate space
Compose scene Define view reference Define lighting View transformation
View space
Projection
Cull Clip to 3D view volume 3D screen space Projection divide
Hidden surface removal Rasterization Shading Display space
3D Rendering Pipeline
CPSC 587/687
Modeling transformation Y
World coordinate space
( xw
w
yw
Compose scene Define view reference
X Z w
zw )
w
View space
( xv
yv
zv )
View transformation
⎛ xv ⎞ ⎛ xw ⎞ ⎜ ⎟ ⎜ ⎟ ⎜ yv ⎟ ⎜ yw ⎟ ⎜ z ⎟ = Tview ⎜ z ⎟ ⎜⎜ v ⎟⎟ ⎜⎜ w ⎟⎟ ⎝1⎠ ⎝1⎠ University of Calgary GraphicsJungle Project
CPSC 587 2005
page 4
Viewing transformation V View plane
U eye
CPSC 587/687
⎛ xv ⎞ ⎛ xw ⎞ ⎜ ⎟ ⎜ ⎟ ⎜ yv ⎟ ⎜ yw ⎟ ⎜ z ⎟ = Tview ⎜ z ⎟ ⎜⎜ v ⎟⎟ ⎜⎜ w ⎟⎟ ⎝1⎠ ⎝1⎠
-N
Tview = RT
⎛U x ⎜ ⎜V R=⎜ x N ⎜⎜ x ⎝ 0
Uy Vy
Uz Vz
Ny
Nz
0
0
⎛1 0 0⎞ ⎜ ⎟ 0⎟ ⎜0 1 T =⎜ 0 0 0⎟ ⎜ ⎟⎟ ⎜0 0 ⎝ 1⎠
0 − eVec ⋅ U ⎞ ⎟ 0 − eVec ⋅ V ⎟ 1 − eVec ⋅ N ⎟ ⎟⎟ 0 1 ⎠
eVec = (eye − (0 0 0))
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 5
Viewing Transforms ⎛U x ⎜ ⎜V R=⎜ x N ⎜⎜ x ⎝ 0
Uy Vy
Uz Vz
Ny
Nz
0
0
0⎞ ⎟ 0⎟ 0⎟ ⎟⎟ 1⎠
CPSC 587/687
N Lemma: Where does this matrix come from:
e = Eye Position g = gaze direction t = view up vector
g N= ||g|| U= t x N ||t x N||
V t
N
V= N x U e
g
U University of Calgary GraphicsJungle Project
CPSC 587 2005
page 6
Special Orthogonal Matrics
CPSC 587/687
See 453 slides
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 7
CPSC 587/687
Taxonomy of Projections
Parallel
University of Calgary GraphicsJungle Project
Perspective CPSC 587 2005
page 8
CPSC 587/687
Perspective Projections
X = xv Y = yv Z = zv
Tproj = Tpers
w = zv d University GraphicsJungle obs: d isofNCalgary in the pictureProject
⎛1 ⎜ ⎜0 =⎜ 0 ⎜⎜ ⎝0
CPSC 587 2005
0 1 0
0⎞ ⎟ 0⎟ 0⎟ ⎟⎟ 1⎠
0 0 1
0 1/ d page 9
Parallel Projections (orthographic)
Tproj
⎛1 ⎜ ⎜0 = Tort = ⎜ 0 ⎜⎜ ⎝0
University of Calgary GraphicsJungle Project
CPSC 587/687
0 0 0⎞ ⎟ 1 0 0⎟ 0 0 0⎟ ⎟⎟ 0 0 1⎠ CPSC 587 2005
page 10
CPSC 587/687
Perspective Divide Y
Modeling transformation
w
World coordinate space
( xw
X Z
w
w
yw
zw )
Compose scene Define view View spacereference
( xv
yv
zv )
View transformation
Projection 3D screen space
(X
Z W)
Y
Projection Divide
( xs
University of Calgary GraphicsJungle Project
ys
zs )
CPSC 587 2005
page 11
CPSC 587/687
Perspective Divide
X = xv
xs = X Perspective
Parallel (orthographic)
w
ys = Y
w
zs = Z
w
where
Y = yv Z = zv w = zv d
x s = xv y s = yv zs = 0
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 12
CPSC 587/687
Perspective Divide
See 453 slides for ramifications of divide by W
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 13
CPSC 587/687
View Volume
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 14
CPSC 587/687 We specify it!
V U View volume
eye -N
Yw
View plane window
What appears on the screen: projection of part of the scene contained within the view volume
Xw Zw University of Calgary GraphicsJungle Project
CPSC 587 2005
page 15
CPSC 587/687
eye
View volume University of Calgary GraphicsJungle Project
View plane window CPSC 587 2005
page 16
yvol =
hzv d
zv = d
h d
hzv xv = − d
hzv xv = d
f
hzv yv = − d
zv = f
CPSC 587/687
Perspective Matrix
Tpers
1 ⎛ ⎞ ⎛d ⎜ h 0 0 0 ⎟⎜ 0 ⎜ ⎟ ⎜ 0 d 0 0 ⎜ =⎜ h ⎟ 0 0 1 0 ⎟⎜ ⎜ 0 ⎜0 ⎟ ⎜ 0 0 0 1 ⎠⎜ ⎝ ⎝
Scaling (d/h in x and y) Truncated pyramid into a Regular pyramid University of Calgary GraphicsJungle Project
0
0
1 0
0 f
(f
0 − fd
−d)
(f
−d)
0⎞ ⎟ 0⎟ 1⎟ ⎟ ⎟ 0⎟ ⎠
Regular pyramid into a box
CPSC 587 2005
page 18
CPSC 587/687
Tpers
⎛d 0 ⎜ h 0 ⎜ 0 d 0 h ⎜ =⎜ f 0 0 (f −d) ⎜ ⎜⎜ 0 0 − fd (f −d) ⎝
0 ⎞⎟ 1⎟ ⎟ 1⎟ ⎟ 0 ⎟⎟ ⎠
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 19
CPSC 587/687
Tpers
⎛d 0 ⎜ h 0 ⎜ 0 d 0 h ⎜ =⎜ f 0 0 (f −d) ⎜ ⎜⎜ 0 0 − fd (f −d) ⎝
xs = X
w
ys = Y
w
zs = Z
w
0 ⎞⎟ 1⎟ ⎟ 1⎟ ⎟ 0 ⎟⎟ ⎠
where
University of Calgary GraphicsJungle Project
d X = xv h d Y = yv h fzv df Z= − (f −d) (f −d) w = zv
CPSC 587 2005
page 20
OpenGL: Viewing Process, Graphics Pipeline CPSC 587/687 Scales and shifts each vertex so that all of them that lie inside the view volume will lie inside a standard cube
Provides the CT Combines two effects: Modeling transformations applied to objects + Transformation that orients and positions the camera in space University of Calgary GraphicsJungle Project
CPSC 587 2005
page 21
CPSC 587/687
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 22
OpenGL: Positioning and Aiming the Camera V U
View plane eye
-N
glMatrixMode (GL_MODELVIEW) glLoadIdentity();
glLookAt (eye.x, eye.y, eye.z, look.x, look.y, look.z, up.x, up.y, up.z);
CPSC 587/687
OpenGL: Setting the Camera (Parallel Projection)
glMatrixMode (GL_PROJECTION) glLoadIdentity();
glOrtho (left, right, bottom, top, near, far);
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 25
CPSC 587/687
OpenGL: Setting the Camera (Perspective Projection)
glMatrixMode (GL_PROJECTION) glLoadIdentity();
glFrustum (left, right, bottom, top, near, far); more intuitive
or
gluPerspective (viewAngle, aspect, near, far);
University of Calgary GraphicsJungle Project
CPSC 587 2005
page 26
3D Rendering Pipeline
Local coordinate space
Object definition Modeling transformation
1.
INTRODUCTION
2.
IMAGING
3.
3D RENDERING OVERVIEW
4.
MODELING (1) shapes and transformations
5.
RENDERING (1, 2, 3, 4)
World coordinate space
Compose scene Define view reference Define lighting View transformation
View space
Projection
Cull Clip to 3D view volume 3D screen space Projection divide
Hidden surface removal Rasterization Shading Display space
1.
Graphics Pipeline : Geometric Operations
6.
MODELING (2) Representation and Modeling
7.
ANIMATION
8.
TOPICS
Modeling transformation
Yw
World coordinate space
( xw
yw
zw )
Compose scene Define view reference View space
Xw Zw
( xv
yv
zv )
View transformation
⎛ xv ⎞ ⎛ xw ⎞ ⎜ ⎟ ⎜ ⎟ ⎜ yv ⎟ ⎜ yw ⎟ T = view ⎜ ⎜z ⎟ ⎟ z ⎜⎜ v ⎟⎟ ⎜⎜ w ⎟⎟ ⎝1⎠ ⎝1⎠
V U
View plane
Culling (back-face removal)
eye
-N
F is back face if
(P − eye ) dot mF > 0
Dot Product
n
d = v ⋅ w = ∑ vi wi i =1
∧ ∧
Angle between Two Vectors
cos(θ ) = b⋅ c
Sign and Perpendicularity
“perpendicular” orthogonal normal
Normal to a Plane
Finding the Normal Vectors V1
Flat Face:
m
V3
V2
m = (V1 − V2 ) × (V3 − V2 )
Two Problems:
V1
V3
V2 Vectors nearly parallel, the cross product will be very small, Numerical inaccuracies may result
Polygon is not perfect planar
Finding the Normal Vectors V1 Robust Method (Newell):
m V2
V3
N −1
mx = ∑ ( yi − ynext (i ) )(zi + znext (i ) ) i =0
N −1
m y = ∑ (zi − znext (i ) )(xi + xnext (i ) ) i =0
N −1
mz = ∑ (xi − xnext (i ) )( yi + ynext (i ) ) i =0
Vertex and Face Table
Each face lists vertex references Shared vertices Still no topology information
( x3 , y 3 , z 3 )
(x1 , y1 , z1 )
(x5 , y5 , z5 )
VERTEX TABLE V1
F3
F1
V3
F2
( x2 , y 2 , z 2 )
V2
( x4 , y 4 , z 4 )
V4 V5
(x1 , y1 , z1 ) ( x2 , y 2 , z 2 ) ( x3 , y 3 , z 3 ) ( x4 , y 4 , z 4 ) (x5 , y5 , z5 )
FACE TABLE F1
V1 V2 V3
F2
V3 V2 V4
F2
V3 V4 V5
Mesh pt
numVerts
Point3 x, y, z
norm
numNormals
face
Face numFaces
vert
Vector3 dx, dy, dz
nVerts
VertexID vertIndex normIndex
V U
View plane
Culling (back-face removal)
eye
-N
F is back face if
(P − eye ) dot mF > 0
void Mesh :: draw () { for (int f = 0; f < numFaces; f++) { glBegin (GL_POLYGON) for (int v = 0; v < face[f].nVerts; v++) { int iv = face[f].vert[v].vertIndex; glVertex3f(pt[iv].x, pt[iv].y, pt[iv].z); } glEnd(); } }
void Mesh :: draw () { for (int f = 0; f < numFaces; f++) {
If ( isBackFace ( f, eye ) ) continue; glBegin (GL_POLYGON) for (int v = 0; v < face[f].nVerts; v++) { int iv = face[f].vert[v].vertIndex; glVertex3f(pt[iv].x, pt[iv].y, pt[iv].z); } glEnd(); } }