TNM046: Datorgrafik Perspective Projection, Clipping, Hidden surface
removal
Sasan Gooran VT 2014
2
Pinhole camera
Film plane Pinhole
z y
The data on the film plane need to be flipped later to obtain the desired photo. Mathematically we can avoid this step if we instead model the pinhole camera with the film plane in front of the pinhole
X-axis is pointing towards us
3
Pinhole camera
Film plane at z=z
vpz y
Once the picture is taken if we hold up the photograph at z=z
vpplane and observe it with our own eye placed at the origin it will
look like the original scene.
4
Perspective Projection
View plane, z=z
vpz y
P : x y z
!
"
#
#
#
$
%
&
&
& P' :
x ' y' z'
!
"
#
#
#
$
%
&
&
&
x ' x = z
vpy' z y = z
vpz' = z
vpz
x ' = x z
vpz y' = y z
vpz' = z
vpz
x ' = x d
−z y' = y d z' = −d −z
à à
Assume the distance from the eye to the view plane is d, (d=-zvp)
5
Perspective Projection
x ' = x d
−z y' = y d
−z z' = −d
How can we write this as a matrix multiplication using homogenous coordinate representation?
Notice the division by –z! So multiply everything with –z and write it as a matrix multiplication and try to get –z from the last row.
x
hy
hz
hh
!
"
#
#
#
# #
$
%
&
&
&
&
&
=
d 0 0 0
0 d 0 0
0 0 d 0
0 0 −1 0
!
"
#
# #
#
$
%
&
&
&
&
x y z 1
!
"
#
#
#
#
$
%
&
&
&
&
6
Perspective Projection
x
h= d ⋅ x y
h= d ⋅ y z
h= d ⋅ z h = −z
This is what we get from the matrix multiplication on previous page.
So far we always had the last element, h, equal to 1. From now on whenever it is not 1, then divide the three xyz coordinates by h. If we do so we get what we want, which is:
x ' = x
hh = d ⋅ x
−z y' = y
hh = d ⋅ y
−z z' = z
hh = −d
Exercise 34 a, b
7
Clipping
No objects closer than the near clip and farther than the far clip should be shown.
Clipping must separate the visible from the invisible objects (or parts of objects).
z y
View plane Near clip plane
Far clip plane View volume
8
Perspective Projection - Clipping OpenGL
z y
View plane Near clip plane
Far clip plane View volume
-Znear
-Zfar
In OpenGL we should arrange the matrix such that z=-z
faris transformed to z=1 and z=-z
nearis transformed to z=-1.
Notice that z is independent of x and y and therefore only column 3 and 4 are affected. See next page:
9
Perspective Projection – Clipping OpenGL
x
hy
hz
hh
!
"
#
#
#
# #
$
%
&
&
&
&
&
=
d 0 0 0
0 d 0 0
0 0 A B
0 0 −1 0
!
"
#
# #
#
$
%
&
&
&
&
x y z 1
!
"
#
#
# #
$
%
&
&
&
&
z' = z
hh = Az + B
−z
−1 = −Az
near+ B z
near→ −Az
near+ B = −z
near1 = −Az
far+ B
z
far→ −Az
far+ B = z
farA = − z
far+ z
nearz
far− z
nearB = − 2z
nearz
farz
far− z
nearWhich gives:
and
10
Perspective Projection – Clipping OpenGL
x
hy
hz
hh
!
"
#
# #
# #
$
%
&
&
&
&
&
=
d ? 0 0 0
0 d ? 0 0
0 0 − z
far+ z
nearz
far− z
near− 2z
nearz
farz
far− z
near0 0 −1 0
!
"
#
#
# #
# #
$
%
&
&
&
&
&
&
x y z 1
!
"
# #
#
#
$
%
&
&
&
&
In OpenGL we also want the x and y coordinates to be between -1 and 1. Assume that the view plane is limited to (-s, s) in both x and y directions. Thus we want (-s, s) to be transformed to (-1, 1), which means we just need to divide the x and y values with s.
11
Perspective Projection – Clipping OpenGL
xh yh
zh h
!
"
##
#
##
$
%
&
&
&
&
&
=
d / s 0 0 0
0 d / s 0 0
0 0 −zfar+ znear
zfar− znear
−2znearzfar
zfar− znear
0 0 −1 0
!
"
#
##
#
##
$
%
&
&
&
&
&
&
x y z 1
!
"
#
#
###
$
%
&
&
&
&&
z y
View plane s
d
This angle is called vertical field of view, vfov, and we have:
cot( vfov
2 ) = d
s
This is why the perspective matrix in the lab material (Section 2.4.3) looks like as it does.
12
Clipping
z y
clip plane
Objects can consist of millions of triangles, testing each triangle against the clip planes is expensive.
We use instead bounding boxes or spheres. The bounding object, rather than the individual triangles, can now be tested against the clip planes.
Example: We wrap around the object with a sphere with radius R and the center C. If the distance from C to the upper clip plane (CP) is bigger than R then the upper clip plane does not intersect the bounding sphere, thus, any object triangle doesn’t either.
clip plane P
C
13
Clipping- View Coordinate-system
Point clipping: Assume that the point P has the coordinate (x,y) in the view system. It is saved for display if:
xv
min≤ x ≤ xv
maxyv
min≤ y ≤ yv
max"
# $
%$
Notice that here we have xvmin= yvmin= 0
Line clipping: First, test whether it lies completely inside the clipping window (a line with both endpoints inside all clipping boundaries lies inside the window, ex: P1P2).
If not, determine whether it lies completely outside the window (a line with both endpoints outside any one of the clipping boundaries lies outside the window, ex:
P3P4). Notice that for line P7P8, P7 is outside the bottom boundary but P8 is not outside that boundary. On the other hand, P8 is outside the right boundary but P7 is not. Therefore we cannot say that this line is outside the window.
Finally if we cannot determine either, we must perform intersection calculations with one or more clipping boundaries.
x y
View plane xvmax xvmin
yvmin yvmax
P3 P2
P4
P1
P7 P8
P5 P6
14
Clipping
x y
View plane xvmax xvmin
yvmin
yvmax
For a line segment with endpoints (x1, y1) and (x2, y2) and one or both endpoints outside the clipping rectangle we can use the parametric representation of the line segment.
x = (1− u)x
1+ ux
2= x
1+ u(x
2− x
1) y = (1− u)y
1+ uy
2= y
1+ u(y
2− y
1)
"
# $
%$
with 0 ≤ u ≤ 1
If the value of u for an intersection with a boundary edge is outside the range 0 to 1, the line does not enter the window at that boundary.
If the value of u is within this range, the line segment crosses into the clipping area.
This method can be applied to each clipping boundary edge P7
P8
P5 P6
15
Clipping
x y
View plane xvmax xvmin
yvmin
yvmax
Example: Assume that
xvmax=4, yvmax=3, P7:(3, -1) and P8: (5, 2)
P7 P8
Since 5>xvmax then P8 is outside the right boundary but 3<xvmax which means P7 is not outside that boundary. P7 is outside the bottom boundary (-1<0) but P8 is not (2>0). Then we must perform intersection calculations.
x = 3+ u(5 − 3) = 3+ 2u y = −1+ u(2 − (−1)) = −1+ 3u
"
# $
%$
with 0 ≤ u ≤ 1
The parametric representation of P
7P
8:
16
Clipping
x y
View plane xvmax
xvmin
yvmin
yvmax
P7 P8
x = 3+ u(5 − 3) = 3+ 2u y = −1+ u(2 − (−1)) = −1+ 3u
"
# $
%$
with 0 ≤ u ≤ 1
Left boundary (x=0):
0 = 3+ 2u → u = −3 / 2 < 0, does not enter at this boundary
Top boundary (y=3):3 = −1+ 3u → u = 4 / 3 > 1, does not enter at this boundary
Bottom boundary (y=0):0 = −1+ 3u → 0< u = 1 / 3 < 1, does enter at this boundary
Intersection point is (11/3, 0) and this point is inside the boundaryRight boundary (x=4):
Intersection point is (4, 1/2) and this point is inside the boundary
4 = 3+ 2u → 0< u = 1 / 2 < 1, does enter at this boundary
Exercise 36 b, c
17
Clipping
x y
View plane
P3
P2 P1
I2 I1
Example: Clipping of a triangle against the view plane.
Assumption: The triangle has been projected into the view plane.
Given: The points P
1, P
2, P
3(triangle list) and the vertex list.
We calculate the intersection points I
1and I
2between the triangle edges and the view plane boundary.
A temporary vertex list and triangle list take points P
1, I
1and I
2. P
2and P
3are discarded. The clipped triangle is plotted.
18
Hidden Surface Removal - Culling
How do we determine whether or not a triangle is visible?
View direction
The normal of triangle j is N
jThe triangle is visible if its normal points towards us (the eye):
Triangle j is visible if, V •
N
j≤ 0 and not visible if V •
N
j> 0
V
19
Culling
Culling works fine if we have one convex object, but for two objects it may fail.
View direction Invisible Visible
Invisible Visible
Culling will remove the backside of both objects.
The triangles with normals N
1and N
2are both ‘visible’
à Culling cannot remove the front side of the left object.
V
N1 N220
Culling
Culling can fail even if we only have one object. (concave)
This object (torus) from the side can appear as two objects:
Culling fails.
If we look it from above, it is a ring (one object):
Culling works.
21
Hidden Surface Removal – Painters Algorithm
Assume XYZ is the view coordinate system and the view direction is Z and we use parallel projection. We make use of the distances.
x y
z
P1 P2
T1 T2
We have two triangles, T1 and T2. Select points P1 and P2. Take the Z-values of these points, Z1 and Z2. Since the projection is parallel then Z1 and Z2 are distances from triangle T1 and T2 to the view plane, respectively.
Generate a list and store the z-values of the points.
22
Painters Algorithm
Assume that we have 5 triangles, which are ordered randomly according to their distance to the view plane (Left table).
We sort the triangles according to their distance (right table).
Draw the triangles from the top to the bottom in the list (right table).
If there is an overlap between triangles, the closer one over-plots the farther one.
Triangle Distance
T1 11
T2 6
T3 3
T4 9
T5 14
Sorting à
Triangle Distance
T5 14
T1 11
T4 9
T2 6
T3 3
23
Painters Algorithm
Triangle Distance
T5 14
T1 11
T4 9
T2 6
T3 3
x
y T
5T
1T
4T
2T
3The painters algorithm works if the typical distances are large compared to the triangle sizes, then the ordering is simple.
24
Painters Algorithm
Problem: Consider triangles that are close compared to their size.
x y
z
P2P1 T1 T2
The point P1 determines the distance of T1.
If we use the point P2 to determine the distance of T2, then T2 is farther away.
If we use the point P3 to determine the distance of T2, then T2 is closer.
The problem gets worse if the triangles intersect.
P3