Assignment #3 - sites.cs.ucsb.eduyfwang/courses/cs180/notes/opengltheory.pdf · Assignment #3 As a...
Transcript of Assignment #3 - sites.cs.ucsb.eduyfwang/courses/cs180/notes/opengltheory.pdf · Assignment #3 As a...
Assignment #3
As a graphics engineer, can you implement
a “skeletal” rendering pipeline?
Computer Graphics
graphics
primitives
modeling
transform
viewing
transformclipping
shading &
texture
projectionimages in
Internal buffer
transform
matrix
Eye, lookat,
headup
material,
lights,
surface color
Parallel or
Persepctive
volume
images on
screenviewport
transform
viewport
location
Computer Graphics
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(T1);
glMultiMatrixf(T2);
…
glMultiMatrixf(Tn);
draw_the_object(v);
v’ = IT1T2Tnv
Modeling Transform
As a global system
objects move but coordinates stay the same
apply in the reverse order
Tasks
Need a stack (CS16, 24)
Push & pop
Need to create matrices (4x4)
Need to multiply matrices (4x4)
Computer Graphics
Why 4x4 not 3x3?
Use of homogeneous coordinates
[x,y] -> [wx,wy,w]
[wx,wy,w] -> [wx/w, wy/w, w] -> [x,y]
[x,y,z] -> [wx, wy, wz, w]
[wx, wy, wz, w] -> [wx/w, wy/w, wz/w] -
>[x,y,z]
One dimension up (w!=0)
Computer Graphics
Reason #1
All operations (including translation) are
now matrix operations
Hierarchical transforms (multiple T, R, S)
are computed once (top matrix in the stack)
and applied
Computer Graphics
Computer Graphics
Euler Angle Rotation
X Y
Z
XY
Z
X
Y
Z
x
y
z
x
y
z
'
'
'
cos sin
sin cos
1
0 0
0 0
0 0 1 0
0 0 0 1 1
x
y
z
x
y
z
'
'
'
cos sin
sin cos
1
1 0 0 0
0 0
0 0
0 0 0 1 1
11000
0cos0sin
0010
0sin0cos
1
'
'
'
z
y
x
z
y
x
Transformations
Translation Scaling
X
Y
Zz
x
y
X
Y
Z 2x
11000
100
010
001
1
'
'
'
z
y
x
T
T
T
z
y
x
z
y
x
11000
000
000
000
1
'
'
'
z
y
x
S
S
S
z
y
x
z
y
x
Modeling Transform Your Way
Implement a stack with push and pop
Replace OpenGL codes with your own
codes
Computer Graphics
Computer Graphics
Viewing Transform
So the user specifies a lot of information
Eye
Center
Up
Near, far,
Left, right top, bottom, etc.
X
Y
Z
UP
EYE
rightleft
bottom
top
bf
CENTER
Computer Graphics
What does a system programmer do with those numbers?
Generate screen coordinates correctly and efficiently
Inside/outside test
Projection
Here comes the part which contains math which you may not like
But all you need to know is matrix operation
What does OpenGL do?
Computer Graphics
Arbitrary View Volume
X
Y
Z
UP
EYE
rightleft
bottom
top
bf
CENTER
Computer Graphics
Inside-Outside Test (Clipping)
Intersection of
A plane and
A Line
line of points end:),,(),,,(
10
)()()(
0)]([)]([)]([
)(
)(
)(
:
0:
222111
121212
111
121121121
121
121
121
zyxzyx
t
zzcyybxxa
dczbyaxt
dzztzcyytybxxtxa
zztz
yyty
xxtx
line
dczbyaxplane
EYE
Computer Graphics
Clipping in Canonical Volumes
Z
Y
Y=-Z
Y=Z
Z=-1
Z=Zmin
45^o
Z
Y Y=1
Y=-1
Z=-1
(A,B,C)(A,B,C)
far plane=film
near plane
Computer Graphics
Clipping with 6-bit outcode
Perspective
Above y>-z
Below y<z
Right x>-z
Left x<z
Behind z<-1
In front z >zmin
Parallel
Above y>1
Below y<-1
Right x>1
Left x<-1
Behind z<-1
In front z >0
Computer Graphics
Projection
Again, an intersection of
A plane and
A Line
EYE
Computer Graphics
Sidebar: Reason #2
A pin-hole model without inversion (f=1 in
OpenGL)
f (v)
Z
X
x fX
Z
y fY
Z
f (v) x
x
Computer Graphics
Canonical Volumes
Z
Y
Y=-Z
Y=Z
Z=-1
Z=Zmin
45^o
Z
Y Y=1
Y=-1
Z=-1
(A,B,C) (A,B) (A,B,C)(A/C,B/C)
far plane=film
near plane
Sidebar: Reason #2
Traditional Way Matrix Way
Computer Graphics
x fX
Z
y fY
Z
10100
1000
0010
0001
1
'
'
'
z
y
x
z
y
x
Computer Graphics
Problem
Both clipping and projection can be done
efficiently in a canonical volume
But we do not have a canonical volume in
general
Solution: Normalization transform
A single matrix operation to bring objects in
any arbitrary volume into a canonical volume
Cannot change what the user sees
Computer Graphics
Normalization Transform A transformation to facilitate clipping and
projection
X
Y
Z
An arbitrary view volume:
Expensive for clipping and projection
Computer Graphics
X
Y
Z
The canonical view volume:
Simple clipping (six-bit outcode)
Simple projection (x/z, y/z)
Y
Z
y z
y z
z 1
z z min
45o
Computer Graphics
OpenGL Terminology
X
Y
Z
UP
EYE
rightleft
bottom
top
bf
CENTER
Computer Graphics
Normalization TransformPerspective - OpenGL
External parameters
Translate EYE into origin
Rotate the EYE coordinate system such thatw (e-c) becomes z
u becomes x
v becomes y
Internal parameters
Shear to have centerline of the view volume
aligning with z
Scale into canonical truncated pyramid
Computer Graphics
Existing Rendering Pipeline
graphics
primitives
modeling
transform
viewing
transformclipping
shading &
texture
projectionimages in
Internal buffer
transform
matrix
Eye, lookat,
headup
material,
lights,
surface color
Parallel or
Perspective
volume
images on
screenviewport
transform
viewport
location
Computer Graphics
Rendering Pipeline with
Normalization Transform
graphics
primitives
modeling
transform
viewing
transformclipping
shading &
texture
projectionimages in
Internal buffer
transform
matrix
Eye, lookat,
headup
material,
lights,
surface color
Parallel or
Perspective
volume
images on
screenviewport
transform
viewport
location
normalization
transform
Computer Graphics
Changes
Modeling + Viewing + Normalization get
concatenated into ONE transform before
applying to any primitives
Confusion: normalization does not just push
the eye frame back to origin and line up
with world frame, it pushes objects away
too
Purpose: to make clipping and projection
much more efficient
Clipping?
Polygon clipping algorithm discussed
earlier
For inside/outside determination
What Else?
Start from polygon with vertices
End with polygon with vertices
Visible surface determination
Painter’s algorithm (sort by depth)
2D painting
Interior: Scan conversion
Exterior: Line drawing (Bresemheim)
That is!
Computer Graphics
Computer Graphics
Viewing Normalization
Line up (X-Y-Z) and (U-V-W)
Initially, (U-V-W) are specified in (X-Y-Z)
system (In fact, everything is specified in
X-Y-Z system)
Some point in time, want to specify things
in (U-V-W) system, or U becomes (1,0,0),
V becomes (0,1,0), W becomes (0,0,1)
Translation (easy) + Rotation (hard)
Hand-eye Coordination A common problem in graphics, robotics
and vision
The camera and effector are not co-located
Computer Graphics
From camera to robot
R and O are in camera’s frame
Rows are robot’s frame in
camera’s system
Computer Graphics
x
yz
o
P=(xc,yc,zc)
P’=(xr,yr,zr)?
)(
'
'
''
OP
z
y
x
P
Computer Graphics
X YZ
o
P=(xc,yc,zc)
P’=(xr,yr,zr)?
TPOPOPOP
z
y
x
P
|||
|||
|||
|||
|||
|||
)(
|||
|||
)(
'
'
'' ZYXZYXZYXZYX
T
From robot to camera
R and T are in robot’s frame
Cols are camera’s frame in robot’s system
Computer Graphics
Translate EYE into the origin
X
Y
Z
u
v
w
X
Y
Z
u
v
w
1000
100
010
001
1
z
y
x
EYE
EYE
EYE
T
Computer Graphics
Viewing Normalization
Three rotations
Rotate about Y
Rotate about X
Rotate about Z
Computer Graphics
X
Y
Z
X
Y
Z
X
Y
Z
X
Y
Z
Y rotation
X rotation
Z rotation
UP
E-C
Computer Graphics
Viewing Normalization
we c
e c
uup w
up w
v w u
| |
| |
u
v
w
u u u
v v v
w w w
x
y
z
x y z
x y z
x y z
1
0
0
0
0 0 0 1 1
Figuring out [u, v, w] in [x, y, z] system
Applying a rotation to transform [x, y, z]
coordinates into [u, v, w] coordinates
Computer Graphics
Rotate EYE coordinate to align w. world system
X
YZ
u
v
wX
Y
Z
u
v
w
Computer Graphics
Shear
X
Y
Z
u
v
wX
Y
Z
u
v
),2
,2
( nearbottomtoprightleft
),0,0( near
near
bottomtop
bnear
rightleft
a
b
a
SH
2,2
,
1000
0100
010
001
w
Computer Graphics
Scale into canonical volume
-z
Y2
bottomtop
near far
-z
Y -1
45o
)1,
2
,
2
(1 bottomtop
near
leftright
nearS
)1
,1
,1
(2farfarfar
S
• scale in x and y
• scale in z
2
bottomtop
Computer Graphics
Example
101
)0,20(),()0,20(),(
)0,1,0()0,0,0(
)10,10,10(
BF
bottomtopleftright
UPCENTEREYE
X
Y
Z
Computer Graphics
• Translate EYE into the origin
T1
1 0 0 10
0 1 0 10
0 0 1 10
0 0 0 1
Rotate EYE to align with the world system
R
1
20
1
20
1
6
2
6
1
60
1
3
1
3
1
30
0 0 0 1
)1,2,1(6
1)1,0,1()1,1,1(
6
12
)1,0,1(
|)1,1,1()0,1,0(|
)1,1,1()0,1,0(
||
3
)1,1,1(
||
uwv
wUP
wUPu
ce
cew
Computer Graphics
• Shear
102,102
,
1000
0100
01010
01001
near
bottomtop
bnear
rightleft
a
SH
Computer Graphics
• Scale into canonical volume
• scale in x and y
• scale in z
1000
0100
0010
10
00010
1
1S
1000
010
100
0010
10
00010
1
1S
Comparing Canonical Volumes
One additional division is needed in perspective
Z
Y
Y=-Z
Y=Z
Z=-1
Z=Zmin
45^o
Z
Y Y=1
Y=-1
Z=-1
(A,B,C) (A,B) (A,B,C)(A/C,B/C)
far plane=film
near plane
(1,-1)
(-1,-1)
(1,-1)
(-1,-1)
Matrix Magic
Try this:
Map perspective volume into parallel volume to
save the division (note Zmin is NEGATIVE)
Computer Graphics
1
0
1
0
0
0
1
0
0100
11
100
0010
0001
1
0
1
0
0
0
1
0
0100
11
100
0010
0001
0100
11
100
0010
0001
min
min
min
min
min
min
min
min
min
min
min
min
min
min
'
min
min
min
z
z
z
z
z
z
z
z
z
z
z
z
z
z
MNN
z
z
z
M
perper
Computer Graphics
Normalization TransformParallel (othographic) - OpenGL
External parameters
Translate EYE into origin
Even though eye is not really where the viewer is
Rotate the EYE coordinate system such that w (e-c) becomes z
u becomes x
v becomes y
Internal parameters
Translate to have centerline of the view volume
aligning with z, and near plane at z=0
Scale into canonical rectangular piped
Computer Graphics
Viewing Normalization
Line up (X-Y-Z) and (U-V-W)
Initially, (U-V-W) are specified in (X-Y-Z)
system (In fact, everything is specified in
X-Y-Z system)
Some point in time, want to specify things
in (U-V-W) system, or U becomes (1,0,0),
V becomes (0,1,0), W becomes (0,0,1)
Translation (easy) + Rotation (hard)
Computer Graphics
Translate EYE into the origin
X
Y
Z
u
v
w
X
Y
Z
u
v
w
1000
100
010
001
1
z
y
x
EYE
EYE
EYE
T
Computer Graphics
Viewing Normalization
Three rotations
Rotate about Y
Rotate about X
Rotate about Z
Computer Graphics
X
Y
Z
X
Y
Z
X
Y
Z
X
Y
Z
Y rotation
X rotation
Z rotation
UP
E-C
Computer Graphics
Viewing Normalization
we c
e c
uup w
up w
v w u
| |
| |
u
v
w
u u u
v v v
w w w
x
y
z
x y z
x y z
x y z
1
0
0
0
0 0 0 1 1
Figuring out [u, v, w] in [x, y, z] system
Applying a rotation to transform [x, y, z]
coordinates into [u, v, w] coordinates
Computer Graphics
Rotate EYE coordinate to align w. world system
X
YZ
u
v
wX
Y
Z
u
v
w
Computer Graphics
Translation
X
Y
Z
u
v
wX
Y
Z
u
v
),2
,2
( nearbottomtoprightleft
)0,0,0(
nearcbottomtop
brightleft
a
c
b
a
T
,2
,2
,
1000
100
010
001
w
Computer Graphics
Scale into canonical volume
-z
Y 2
bottomtop
near Far-near
)1
,
2
1,
2
1(
nearfarbottomtopleftrightS
• scale in x, y, and z
2
bottomtop
-z
Y 1
near -1
1
Computer Graphics
Example
101
)0,20(),()0,20(),(
)0,1,0()0,0,0(
)10,10,10(
BF
bottomtopleftright
UPCENTEREYE
X
Y
Z
Computer Graphics
• Translate EYE into the origin
T1
1 0 0 10
0 1 0 10
0 0 1 10
0 0 0 1
Rotate EYE to align with the world system
R
1
20
1
20
1
6
2
6
1
60
1
3
1
3
1
30
0 0 0 1
)1,2,1(6
1)1,0,1()1,1,1(
6
12
)1,0,1(
|)1,1,1()0,1,0(|
)1,1,1()0,1,0(
||
3
)1,1,1(
||
uwv
wUP
wUPu
ce
cew
Computer Graphics
• Translation
102,102
,
1000
1000
10010
10001
near
bottomtop
bnear
rightleft
a
SH
Computer Graphics
• Scale into canonical volume
• scale in x, y, and z
1000
09
100
0010
10
00010
1
1S