Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world...
Transcript of Computer Graphics 5 - View transformation and clippingOutline of procedure 1. Transform from world...
Computer Graphics 5 - View transformation andclipping
Tom Thorne
Slides courtesy of Taku Komurawww.inf.ed.ac.uk/teaching/courses/cg
Overview
View transformation
I Homogeneous coordinates recapI Parallel projectionI Persepctive projectionI Canonical view volume
Clipping
I Line and polygon clipping
Outline of procedure
1. Transform from world to camera coordinates2. Project into view volume or screen coordinates
3. Clip geometry that falls outside of the view volume
vg
g
c
Mg<-c
View projection
I Homogeneous transformationI Parallel projectionI Perspective projectionI Canonical view volume
Homogeneous transformations
v = Mproj
McΩg
MgΩl
vl
vg
g
c
Mg<-c vgv l
v l
Projection Global to camera Local to global
Homogeneous coordinates
I Allows us to represent translation, rotation and scaling bymatrix multiplication
I Coordinates (x , y , z , w) and (cx , cy , cz , cw) represent the samepoint
I Return to Cartesian coordinates using(x Õ, y
Õ, z
Õ) = (x/w , y/w , z/w)
Camera coordinate system
I As used by OpenGLI Camera is facing in the negative z direction
z
y
x
y
x
(1,1)(-1,1)
(-1,-1) (1,-1)
Parallel (orthographic) projection
I Specified by a direction of projectionI No change in size of objects
Parallel projection
Project coordinates onto plane at z = 0x
Õ = x , y
Õ = y , z
Õ = 0
M
orth
=
Q
ccca
1 0 0 00 1 0 00 0 0 00 0 0 1
R
dddb
Q
ccca
1 0 0 00 1 0 00 0 0 00 0 0 1
R
dddb
Q
ccca
x
y
z
1
R
dddb =
Q
ccca
x
y
01
R
dddb
Perspective projection
I Far away objects appear smallerI Specified by a center of projection and focal distance
Perspective projection
y
x
+z -z
d
(x,y,z)
(x ,y ,-d)p p
Perspective projection - simple case
From similar triangles:
I x
p
d
= x
≠z
, y
p
d
= y
≠z
Ix
p
= dx
≠z
= x
≠z/d
, y
p
= dy
≠z
= y
≠z/d
y
+z -z
d
(x,y,z)
(x ,y ,-d)p p
yp
y
Perspective projection
Q
ccca
x
Õ
y
Õ
≠d
1
R
dddb =
Q
ccca
≠d
x
z
≠d
y
z
≠d
1
R
dddb =
Q
ccca
x
y
z
≠z/d
R
dddb
Represented as a matrix multiplication:
M
proj
=
Q
ccca
1 0 0 00 1 0 00 0 1 00 0 ≠ 1
d
0
R
dddb
Perspective projection
V
p
= M
proj
V
c
=
Q
ccca
1 0 0 00 1 0 00 0 1 00 0 ≠ 1
d
0
R
dddb
Q
ccca
x
y
z
1
R
dddb =
Q
ccca
x
y
z
≠ z
d
R
dddb =
Q
ca
x
≠z/d
y
≠z/d
≠d
R
db
Alternative formulationCenter of projection at z = d , projection plane at z = 0
I x
p
d
= x
≠z+d
, y
p
d
= y
≠z+d
Ix
p
= dx
≠z+d
= x
(≠z/d)+1 , y
p
= dy
≠z
= y
(≠z/d)+1
y
+z -z
d
(x,y,z)
(x ,y ,0)p p
yp
y
This allows for d æ Œ
Alternative formulationy
+z -z
d
(x,y,z)
(x ,y ,0)p p
yp
y
Ix
p
= dx
≠z+d
= x
(≠z/d)+1 , y
p
= dy
≠z
= y
(≠z/d)+1
M
proj
=
Q
ccca
1 0 0 00 1 0 00 0 0 00 0 ≠1
d
1
R
dddb
Exercise
Where will the two points be projected?
(-1,-1,-1)
(-2,2,-2)
d=1
M
proj
=
Q
ccca
1 0 0 00 1 0 00 0 0 00 0 ≠ 1
d
1
R
dddb
Problems
I After projection depth information is lost (this is needed forhidden surface removal)
I Objects behind the camera are projected infront of the camera!
3D view volumeDefine a volume in which objects are visible
I For parallel projection, a boxI For perspective projection, a frustumI Surfaces outside this volume are clipped or removed and not
drawn
Canonical view volume
It can be computationally expensive to check if a point is inside afrustum
I Instead transform the frustum into a normalised canonical viewvolume
I Uses the same ideas a perspective projectionI Makes clipping and hidden surface calculation much easier
Transforming the view frustumThe frustum is defined by a set of parameters, l , r , b, t, n, f :
l Left x coordinate ofnear plane
r Right x coordinateof near plane
b Bottom ycoordinate of nearplane
t Top y coordinate ofnear plane
n Minus z coordinateof near plane
f Minus z coordinateof far plane
With 0 < n < f .
z=-n
z=-f
(l,b,-n) (r,t,-n)
Transforming the view frustum
We can transform the perspective canonical view volume to theparallel view volume using:
If z œ [≠n, ≠f ] and 0 < n < f then
M
can
=
Q
ccca
2n
r≠l
0 r+l
r≠l
00 2n
t≠b
t+b
t≠b
00 0 ≠ f +n
f ≠n
≠ 2fn
f ≠n
0 0 ≠1 0
R
dddb
Final steps
I After transformation to canonical view volume, divide by w toproduce 3D Cartesian coordinates
I Perform clipping in the canonical view volume:
≠1 Æ x Æ 1,≠1 Æ y Æ 1,≠1 Æ z Æ 1
Example
What does ABC look like after projection?
M
can
=Q
ccca
2n
r≠l
0 r+l
r≠l
00 2n
t≠b
t+b
t≠b
00 0 ≠ f +n
f ≠n
≠ 2fn
f ≠n
0 0 ≠1 0
R
dddb
ExampleM
can
=Q
ccca
2n
r≠l
0 r+l
r≠l
00 2n
t≠b
t+b
t≠b
00 0 ≠ f +n
f ≠n
≠ 2fn
f ≠n
0 0 ≠1 0
R
dddb =
Q
ccca
1 0 0 00 1 0 00 0 ≠2 ≠30 0 ≠1 0
R
dddb
n = 1,f = 3,r = 1,l = ≠1,t =1,b = ≠1
Q
ccca
1 0 0 00 1 0 00 0 ≠2 ≠30 0 ≠1 0
R
dddb
Q
ccca
30
≠21
R
dddb =
Q
ccca
3012
R
dddb =
Q
ccca
320121
R
dddb
Projection summaryI Parallel and perspective projectionI Projection matrices transform points to 2D coordinates on the
screenI Canonical view volumes can be used for clipping
z=-n
z=-f
(l,b,-n) (r,t,-n)
Overview
View transformation
I Homogeneous coordinates recapI Parallel projectionI Persepctive projectionI Canonical view volume
Clipping
I Line and polygon clipping
Projection of polygons and lines
I Lines in 3D become lines in 2DI Polygons in 3D become polygons in 2D
Clipping
They may intersect the canonical view volume, then we need toperform clipping:
I Clipping lines (Cohen-Sutherland algorithm)I Clipping polygons (Sutherland-Hodgman algorithm)
Cohen-Sutherland algorithm
Input Screen and 2D line segmentOutput Clipped 2D line segment
Cohen-Sutherland algorithm
0001
101010001001
0010
011001000101
0000
Outcodes:
I Space is split into nine regionsI The center corresponds to the area visible on the screenI Each region is encoded by four bits
Cohen-Sutherland algorithm
0001
101010001001
0010
011001000101
0000
4th bit
1st bit
2nd bit
3rd bit
Outcodes:
1st bit Above top of screen (y > y
max
)2nd bit Below bottom of screen (y < y
min
)3rd bit Right of right edge of screen (x > x
max
)4th bit Left of left edge of screen (x < x
min
)
Cohen-Sutherland algorithm
Data: Line from coordinate A to BResult: Clipped linewhile true do
Calculate outcodes of endpoints;if trivial accept or trivial reject then
return accepted line or reject;else
Find which endpoint is outside clipping rectangle;Clip outside endpoint to edge of first non-zero outcode bitand update endpoint coordinates;
end
end
Bitwise AND and OR
0 OR 0 = 00 OR 1 = 11 OR 0 = 11 OR 1 = 1
0 AND 0 = 00 AND 1 = 01 AND 0 = 01 AND 1 = 1
Trivial acceptAll line endpoints are inside the screen
I Apply an OR to outcodes of the line endpoints, test for equalto 0
Cohen-Sutherland algorithm
Data: Line from coordinate A to BResult: Clipped linewhile true do
Calculate outcodes of endpoints;if trivial accept or trivial reject then
return accepted line or reject;else
Find which endpoint is outside clipping rectangle;Clip outside endpoint to edge of first non-zero outcode bitand update endpoint coordinates;
end
end
Trivial rejectBoth line endpoints are outside of the screen on the same side
I Apply AND operation to the two endpoints and reject if not 0
Cohen-Sutherland algorithm
0001
101010001001
0010
011001000101
0000
0001
0000
1000
0000
Logical AND between codes for two endpoints. If non-zero, trivialrejection.
Cohen-Sutherland algorithm
0001
101010001001
0010
011001000101
0000
Logical AND between codes for two endpoints. If non-zero, trivialrejection.
Cohen-Sutherland algorithm
Data: Line from coordinate A to BResult: Clipped linewhile true do
Calculate outcodes of endpoints;if trivial accept or trivial reject then
return accepted line or reject;else
Find which endpoint is outside clipping rectangle;Clip outside endpoint to edge of first non-zero outcode bitand update endpoint coordinates;
end
end
Line intersectionI Need to clip a line to the edges of the rectangleI Find an endpoint which is outside the rectangleI Select edge to clip based on the outcode, split the line and
feedback into algorithm
A
B
A'A''
Cohen-Sutherland algorithm
Extension to 3D
I Clip lines to near and far planes of view volumeI Needs more bits for the outcode
Polygon clipping
Sutherland-Hodgman algorithm
Input 2D polygonOutput List of clipped vertices
Traverse the polygon and clip at each edge of the screen
Sutherland-Hodgman algorithm
For each edge of the clipping rectangle:
I For each polygon edge between v
i
and v
i+1
Edge of screenOutsideInside
Case 1
Output both vertices
Edge of screenOutsideInside
Case 2
Output intersection with screen
Edge of screenOutsideInside
Case 3
No output
Edge of screenOutsideInside
Case 4
Output intersection with screenand second vertex of edge
Example
Summary
Projection
I Parallel and perspectiveI Canonical view volumes
Clipping
I Cohen-Sutherland algorithmI Sutherland-Hodgman algorithm
References
View transformation
I Shirley, Chapter 7I Foley, Chapter 6
Clipping
I Foley Chapter 3.12, 3.14I http://www.cc.gatech.edu/grads/h/Hao-wei.Hsieh/
Haowei.Hsieh/mm.html