You are on page 1of 13

1

2IV60 Computer graphics


set 5: Viewing
Jack van Wijk
TU/e
Viewing
Transformation worldscreen
Clipping: Removing parts outside screen
2D (chapter 8)
3D (chapter 10)
2D Viewing pipeline 1
Clipping window:
What do we want to see?
H&B 8-1:258-259
xw
min
xw
max
yw
max
yw
min
Viewport:
Where do we want to see it?
Clipping window
World:
xv
min
xv
max
yv
max
yv
min
Viewport
Screen:
2D Viewing pipeline 2
Clipping window:
Panning
H&B 8-2:259-261
yw
max
yw
min
xw
min
xw
max
Clipping window
World:
xv
min
xv
max
yv
max
yv
min
Viewport
Screen:
2D Viewing pipeline 2
Clipping window:
Panning
yw
max
yw
min
xw
min
xw
max
Clipping window
World:
xv
min
xv
max
yv
max
yv
min
Viewport
Screen:
H&B 8-2:259-261
2D Viewing pipeline 3
Clipping window:
Zooming
yw
max
yw
min
xw
min
xw
max
World:
xv
min
xv
max
yv
max
yv
min
Viewport
Screen:
H&B 8-2:259-261
2
2D Viewing pipeline 3
Clipping window:
Zooming
yw
max
yw
min
xw
min
xw
max
World:
xv
min
xv
max
yv
max
yv
min
Viewport
Screen:
H&B 8-2:259-261
2D Viewing pipeline 4
MC: Modeling Coordinates
WC: World Coordinates
VC: Viewing Coordinates
NC: Normalized Coordinates
DC: Device Coordinates
Apply model transformations
Determine visible parts
To standard coordinates
Clip and determine pixels
H&B 8-2:259-261
Clipping window
xw
min
xw
max
yw
max
yw
min
Clipping window
- Clipping window usually an axis-aligned rectangle
- Sometimes rotation
- From world to view coordinates:
possibly followed by rotation
- More complex in 3D
xw
max
xw
min
yw
max
yw
min
( )
min min
, yw xw T
H&B 8-2:259-261
To normalized coordinates 1
xv
max
-xv
min
yv
max
-yv
min
xw
max
-xw
min
yw
max
-yw
min
! distortion : changes ratio - aspect then the
unequal, are factors scale two the If
,
: with Scale
min max
min max
min max
min max

yw yw
yv yv
xw xw
xv xv
S
H&B 8-3:261-265
To normalized coordinates 2
xv
max
-xv
min
yv
max
-yv
min
xv
min
xv
max
yv
max
yv
min
Viewport
1
1
( )
min min
,
: with Translate
yv xv T
H&B 8-3:261-265
To normalized coordinates 3
xw
min
xw
max
yw
max
yw
min
Clipping window
xv
min
xv
max
yv
max
yv
min
Viewport
1
1
) , ( , ) , (
: together All
min min
min max
min max
min max
min max
min min
yw xw
yw yw
yv yv
xw xw
xv xv
yv xv

T S T
H&B 8-3:261-265
3
OpenGL 2D Viewing 1
Specification of 2D Viewing in OpenGL:
- Standard pattern, follows terminology.
First, this is about projection. Hence, select and the
Projection Matrix (instead of the ModelView
matrix) with:
glMatrixMode(GL_PROJECTION);
H&B 8-4:265-267
OpenGL 2D Viewing 2
Next, specify the 2D clipping window:
gluOrtho2D(xwmin, xwmax, ywmin, ywmax);
xwmin, xwmax: horizontal range, world coordinates
ywmin, ywmax: vertical range, world coordinates
xwmin xwmax
ywmin
ywmax
H&B 8-4:265-267
OpenGL 2D Viewing 3
Finally, specify the viewport:
glViewport(xvmin, yvmin, vpWidth, vpHeight);
xvmin, yvmin: coordinates lower left corner (in pixel coordinates);
vpWidth, vpHeight: width and height (in pixel coordinates);
(xvmin, yvmin)
vpWidth
vpHeight
H&B 8-4:265-267
OpenGL 2D Viewing 4
In short:
glMatrixMode(GL_PROJECTION);
gluOrtho2D(xwmin, xwmax, ywmin, ywmax);
glViewport(xvmin, yvmin, vpWidth, vpHeight);
To prevent distortion, make sure that:
(ywmax ywmin)/(xwmax xwmin) = vpWidth/vpHeight
H&B 8-4:265-267
Clipping
xw
min
xw
max
yw
max
yw
min
Clipping window
- Clipping: removing parts outside clipping window
- Many algorithms: points, lines, fill-area, curves,
H&B 8-5:274
2D Point Clipping
xw
min
xw
max
yw
max
yw
min
Clipping window
- Trivial: Save point P = (x,y) if its in the box:
max min
max min
yw y yw
xw x xw


H&B 8-5:274-275
4
2D Line Clipping 1
xw
min
xw
max
yw
max
yw
min
H&B 8-6:275-281
2D Line Clipping 2
xw
min
xw
max
yw
max
yw
min
P
Q
Basic algorithm:
Determine interval (u
0
, u
1
) in
rectangle, by calculating
crossings with edges of window.
u
Line segment:
X(u) = P + u (QP),
with 0 <= u <= 1
H&B 8-6:275-281
2D Line Clipping 3
xw
min
xw
max
yw
max
yw
min
P
Q
u
u
0
:= 0; u
1
= 1;
(* Right side: *)
If Px=Qx then
If Px > xw
max
then return empty
else
u := (xw
max
Px)/(Qx-Px)
if Px < Qx then
begin if u < u
1
then u
1
:= u end
else if u > u
0
then u
0
:= u;
If u
0
> u
1
then return empty
Line segment:
X(u) = P + u (Q-P),
with 0 <= u <= 1
u
1
H&B 8-6:275-281
2D Line Clipping 4
xw
min
xw
max
yw
max
yw
min
P
Q
u
(* Left side: *)
If Px=Qx then
If Px < xw
min
then return empty
else
u := (xw
min
Px)/(Qx-Px)
if Px < Qx then
begin if u > u
0
then u
0
:= u end
else if u < u
1
then u
1
:= u;
If u
0
> u
1
then return empty
(* Lower and upper side idem *)
Line segment:
X(u) = P + u (Q-P),
with 0 <= u <= 1
u
1
H&B 8-6:275-281
2D Line Clipping 5
xw
min
xw
max
yw
max
yw
min
- Expensive: calculation crossings
- Avoid this by using position end points
- For instance: If end points in window, then line in window
H&B 8-6:275-281
bit 4 bit 3 bit 2 bit 1
Top Right
Bottom Left
1001 1000 1010
0001 0000 0010
0101 0100 0110
2D Line Clipping 6
xw
min
xw
max
yw
max
yw
min
Cohen-Sutherland algorithm:
- Assign to each point a four-bit region code C;
- 1 is outside, 0 is inside
H&B 8-6:275-281
5
bit 4 bit 3 bit 2 bit 1
Top Right
Bottom Left
1001 1000 1010
0001 0000 0010
0101 0100 0110
2D Line Clipping 7
xw
min
xw
max
yw
max
yw
min
Fast test on status end points line with codes C
0
en C
1
:
C
0
bitwise-or C
1
= 0000: then completely in;
C
0
bitwise-and C
1
<> 0000: then completely out;
Else: fast intersection-calculation using codes.
H&B 8-6:275-281
3D Viewing
Viewing: virtual camera
Projection
Depth
Visible lines and surfaces
Surface rendering
H&B 10-1:332-334
3D Viewing pipeline 1
Similar to making a photo
Position and point virtuele camera, press button;
Projection plane aka
Viewing plane
Pipeline has +/ same structure as in 2D
H&B 10-1:332-334
3D Viewing pipeline 2
MC: Modeling Coordinates
WC: World Coordinates
VC: Viewing Coordinates
PC: Projection Coordinates
NC: Normalized Coordinates
DC: Device Coordinates
Apply model transformations
To camera coordinates
Project
To standard coordinates
Clip and convert to pixels
H&B 10-2:334-335
3D viewing coordinates 1
Specification of projection:
P
0
: View or eye point
P
ref
: Center or look-at point
V: View-up vector
(projection along vertical
axis)
z
vp
: positie view plane
P
0
z
w
y
w
x
w
P
ref
N
V
P
0
, P
ref
, V: define viewing coordinate system
Several variants possible
H&B 10-3:336-338
3D viewing coordinates 2
P
0
z
w
y
w
x
w
P
ref
N
V
z
view
y
view
x
view
P
0
, P
ref
, V: define viewing coordinate system
Several variants possible
H&B 10-4:338-340
6
3D view coordinates 3
P
0
z
w
y
w
x
w
P
ref
N
V
z
view
y
view
x
view
u
) , , (
: and lar to perpendicu
z y x
u u u =

=
V
n V
u
n V u
v
) , , (
: and lar to perpendicu
z y x
v v v = = u n v
u n v
) , , (
: frame axis Derivation
ref 0
z y x
n n n = =
=
N
N
n
P P N
n
H&B 10-4:338-340
3D viewing cordinaten 4
P
0
z
w
y
w
x
w
P
ref
N
V
z
view
y
view
x
view
n
u
v
RT M
R
R
P T
0
=

WC,VC
z y x
z y x
z y x
n n n
v v v
u u u
: Or
1 0 0 0
0
0
0
: with rotate Next,
) ( with translate First,
: view tion world Transforma
H&B 10-4:338-340
Projection transformations
P
1
P
2
P
1
P
2
View plane
Parallel projection
P
1
P
2
P
1
P
2
View plane
Perspective projection
H&B 10-6:340-345
Orthogonal projections 1
P
1
P
2
Parallell projection:
Projection lines are parallel
Orthogonal projection:
Projection lines are parallel and
perpendicular to projection plane
Isometric projection:
Projection lines are parallel,
perpendicular to projection plane,
and have the same angle with axes.
P
1
P
2
P
1
P
2
P
1
P
2
x
y
z
H&B 10-6:340-345
Orthogonal projections 2
Orthogonal projection:
P
1
P
2
P
1
P
2
Trivial!
: s) coordinate projection
to s coordinate view (from
) , , ( of Projection
z z
y y
x x
z y x
p
p
p
=
=
=
H&B 10-6:340-345
Orthogonal projections 3
Clipping
window
z
view
x
view
y
view
Near plane
Far plane
View volume
H&B 10-6:340-345
7
Orthogonal projections 4
z
view
x
view
y
view
View volume
(xw
min
, yw
min
, z
near
)
(xw
max
, yw
max
, z
far
)
H&B 10-6:340-345
Orthogonal projections 4
z
view
x
view
y
view
View volume
(xw
min
, yw
min
, z
near
)
(xw
max
, yw
max
, z
far
)
z
norm
x
norm
y
norm
Normalized View volume
(1,1,1)
(-1,-1,-1)
Translation
Scaling
From right- to left handed
H&B 10-6:340-345
Perspective projection 1
H&B 10-8:351-364
P
1
P
2
P
1
P
2
View plane: z = z
vp
Projection reference point
z
view
x
view
y
view
View plane: orthogonal to z
view
axis.
Perspective projection 2
P = (x, y, z) R: Projection reference point
z
view
x
view
y
view
(x
r
, y
r
, z
r
)
(x
p
, y
p
, z
p
)
To simplify,
Assume R in origin
View plane: z = z
vp
Question: What is the projection of P
on the view plane?
H&B 10-8:351-364
Perspective projection 3
P = (x, y, z)
z
view
x
view
y
view
(x
p
, y
p
, z
p
)
(0,0, 0) R=
. ' and ; ' ; ' or
, 1 0 with , '
: to (origin) from Line
uz z uy y ux x
u u
= = =
= P X
P R
. hence '
: plane with crossing At
z
z
u z z
vp
vp
= =
y
z
z
y x
z
z
x
vp
p
vp
p
= = and
gives on Substituti
View plane: z = z
vp
H&B 10-8:351-364
Perspective projection 4
P = (x, y, z) View plane
z
view
y
view
Viewed from the side
R
z
y
z
vp
y
p
y
z
z
y
z
y
z
y
vp
p
vp
p
=
=
hence
: that see can We
H&B 10-8:351-364
8
Perspective projection 5
P = (x, y, z)
Clipping window
in View plane
z
view
y
view
R
z
vp
W
w
max
w
min
Ratio between
W=w
max
w
min
and z
vp
determines strenght perspective
Viewed from the side
H&B 10-8:351-364
Viewed from the side
Perspective projection 6
Clipping window
in View plane
z
view
y
view
R

Ratio between
W=w
max
w
min
and z
vp
determines strenght perspective.
This ratio is 2tan(/2),
with the view angle.
H&B 10-8:351-364
Perspective projection 7
z
view
y
view
R
H&B 10-8:351-364
Perspective projection 7
z
view
y
view
R
H&B 10-8:351-364
Perspective projection 7
R
H&B 10-8:351-364
Perspective projection 8
z
view
y
view
R
z
vp
W
How to specify the ratio between W en z
vp
?
How to specify how much perspective I want to see?
Option 1: Specify view angle .

H&B 10-8:351-364
9
Perspective projection 9
z
view
y
view
R
z
vp
W
Use camera as metaphor. User specifies focal length f .
(20 mm wide angle, 50 mm normal, 100 mm tele).
Application adjusts W and/or z
vp
, such that W/z
vp
= 24/f.
For x: W/z
vp
= 36/f.
f (mm)
24 mm
H&B 10-8:351-364
View volume orthogonal
Clipping
window
z
view
x
view
y
view
Near
clipping plane
Far
clipping plane
View volume
H&B 10-8:351-364
View volume perspective
Clipping
window
z
view
x
view
y
view
Near
clipping plane
Far
clipping plane
View volume
R
H&B 10-8:351-364
To Normalized Coordinates
z
view
x
view
y
view
R
z
norm
x
norm
y
norm
Normalized View volume
(1,1,1)
(1, 1, 1)
Rectangular frustum
View Volume
H&B 10-8:351-364
Side view Front view
Perspective projection 10
y
view
R
z
view
z
norm
y
norm
Perspective transformation:
Distort space, such that perpendicular projection gives
an image in perspective.
H&B 10-8:351-364
Perspective projection 10
y
view
R 2r
z
n
z
f
z
view
z
norm
y
norm
Simplest case:
Square window,
clipping plane coincides with view plane: z
n
=z
vp
H&B 10-8:351-364
10
Perspective projection 11
y
view
R
z
n
z
f
z
view
z
norm
y
norm
(-r, -r, z
n
)
((z
f
/z
n
)r, (z
f
/z
n
)r, z
f
)
2r
(-1,-1,-1)
(1,1,1)
H&B 10-8:351-364
(r, r, z
n
)
(rz
f
/z
n
, rz
f
/z
n
, z
f
)
Perspective projection 11
y
view
R
z
view
z
norm
y
norm
(1, 1, 1)
(1,1,1)
How to put this transformation in the pipeline?
How to process division by z?
Earlier: y
z
z
y x
z
z
x
vp
p
vp
p
= = ,
H&B 10-8:351-364
Homogeneous coordinates (reprise)
Add extra coordinate:
P = (p
x
, p
y
, p
z
, p
h
) or
x = (x, y, z, h)
Cartesian coordinates: divide by h
x = (x/h, y/h, z/h)
Points: h = 1 (temporary) perspective: h = z !
H&B 10-8:351-364
.
/
/
/
: by given are s coordinate projected such that
1 0 1 0 0
: by described be can ation transform e Perspectiv

h z
h y
h x
z
y
x
z
y
x
t s s s
t s s s
t s s s
h
z
y
x
h
h
h
p
p
p
v
v
v
z zz zy zx
y yz yy yx
x xz xy xx
h
h
h
Homogeneous coordinates (reprise)
H&B 10-8:351-364
Perspective projection 12
x
view
R
z
view
z
norm
y
norm
(r, r, z
n
)
(rz
f
/z
n
, rz
f
/z
n
, z
f
)
(1, 1, 1)
(1,1,1)
(r, r, z
n
)
. 0 , / : gives n Elaboratio
. 1 also then , and / If
. 1 then , and If
. / ) ( is form Generic . First
= = = =
= = =
= = =
+ + + =
x xz xy n xx
p f n f
p n
x xz xy xx p
t s s r z s
x z z z rz x
x z z r x
z t z s y s x s x x
H&B 10-8:351-364
Perspective projection 13
y
view
R
z
view
z
norm
y
norm
(r, r, z
n
)
(rz
f
/z
n
, rz
f
/z
n
, z
f
)
(1, 1, 1)
(1,1,1)
. 0 ), / ( : Or
. / ) / (
: gives , as Same . Next the
= = = =
=
y yz yx n yy
n p
t s s r z s
z y r z y
x y
H&B 10-8:351-364
11
Perspective projection 14
y
view
R
z
view
z
norm
y
norm
(r, r, z
n
)
(rz
f
/z
n
, rz
f
/z
n
, z
f
)
(1, 1, 1)
(1,1,1)
. 0 ,
2
,
gives n Elaboratio . 1 then , If
. 1 then , If
. / ) ( : is form Generic . : Finally
= =

+
=
= =
= =
+ + + =
zy zx
f n
f n
z
f n
f n
zz
f f
p n
z zz zy zx p
s s
z z
z z
t
z z
z z
s
z z z
z z z
z t z s y s x s z z
H&B 10-8:351-364
Perspective projection 15
.
/
/
/
: from follow s coordinate projected the where
1
0 1 0 0
2
0 0
0 0 / 0
0 0 0 /
: by described be hence can ation transform e Perspectiv

h z
h y
h x
z
y
x
z
y
x
z z
z z
z z
z z
r z
r z
h
z
y
x
v
v
v
p
p
p
v
v
v
f n
f n
f n
f n
n
n
h
h
h
H&B 10-8:351-364
3D Viewport coordinates 1
H&B 10-9:365-365
z
norm
x
norm
y
norm
Normalized View volume
(1,1,1)
(1, 1, 1)
z
v
x
v
y
v
3D screen
(xv
min
, yv
min
, 0)
(xv
max
, yv
vmax
, 1)
scaling
translation
3D Viewport coordinaten 2

1
1 0 0 0
2
1
2
1
0 0
0 0
2
0
0 0 0
2
: s coordinate screen 3D to
s coordinate view normalized from tion Transforma
min max
min max
p
p
p
s
s
s
z
y
x
yv yv
xv xv
h
z
y
x
H&B 10-9:365-365
OpenGL 3D Viewing 1
H&B 10-10:365-371
3D Viewing in OpenGL:
- Position camera;
- Specify projection.
OpenGL 3D Viewing 2
z
x
y
View volume
H&B 10-10:365-371
Camera always in origin, in direction of negative z-axis.
Convenient for 2D, but not for 3D.
12
OpenGL 3D Viewing 3
H&B 10-10:365-371
Solution for view transform: Transform your model
such that you look at it in a convenient way.
Approach 1: Do it yourself. Apply rotations,
translations, scaling, etc., before rendering the
model. Surprisingly difficult and error-prone.
Approach 2: Use gluLookAt();
OpenGL 3D Viewing 4
H&B 10-10:365-371
MatrixMode(GL_MODELVIEW);
gluLookAt(x0,y0,z0, xref,yref,zref, Vx,Vy,Vz);
x0,y0,z0: P
0
, viewpoint, location of camera;
xref,yref,zref: P
ref
, centerpoint;
Vx,Vy,Vz: V, view-up vector.
Default: P
0
= (0, 0, 0); P
ref
= (0, 0, 1); V=(0, 1, 0).
OpenGL 3D Viewing 5
H&B 10-10:365-371
z
x
y
Orthogonal projection:
MatrixMode(GL_PROJECTION);
glOrtho(xwmin, xwmax, ywmin, ywmax, dnear, dfar);
xwmin
xwmax
ywmin
ywmax
xwmin, xwmax, ywmin,ywmax:
specification window
dnear: distance to near clipping plane
dfar : distance to far clipping plane
Select dnear and dfar right:
dnear < dfar,
model fits between clipping planes.
OpenGL 3D Viewing 6
H&B 10-10:365-371
Perspective projection:
MatrixMode(GL_PROJECTION);
glFrustrum(xwmin, xwmax, ywmin, ywmax, dnear, dfar);
xwmin
xwmax
ywmin
ywmax
xwmin, xwmax, ywmin,ywmax:
specification window
dnear: distance to near clipping plane
dfar : distance to far clipping plane
z
x
y
Standard projection: xwmin = -xwmax,
ywmin = -ywmax
Select dnear and dfar right:
0 < dnear < dfar,
model fits between clipping planes.
OpenGL 3D Viewing 7
Finally, specify the viewport (just like in 2D):
glViewport(xvmin, yvmin, vpWidth, vpHeight);
xvmin, yvmin: coordinates lower left corner (in pixel coordinates);
vpWidth, vpHeight: width and height (in pixel coordinates);
(xvmin, yvmin)
vpWidth
vpHeight
H&B 8-4:265-267
OpenGL 2D Viewing 8
In short:
glMatrixMode(GL_PROJECTION);
glFrustrum(xwmin, xwmax, ywmin, ywmax, dnear, dfar);
glViewport(xvmin, yvmin, vpWidth, vpHeight);
glMatrixMode(GL_MODELVIEW);
gluLookAt(x0,y0,z0, xref,yref,zref, Vx,Vy,Vz);
To prevent distortion, make sure that:
(ywmax ywmin)/(xwmax xwmin) = vpWidth/vpHeight
Make sure that you can deal with resize/reshape of the (OS) window.
H&B 8-4:265-267
13
Next
We now know how to project objects.
But how to model objects?

You might also like