2iv60 computer graphics set 5: viewing jack van wijk tu/e
TRANSCRIPT
2IV60 Computer graphicsset 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
xwmin xwmax
ywmax
ywmin
Viewport:
Where do we want to see it?
Clipping window
World:
xvmin xvmax
yvmax
yvmin
Viewport
Screen:
2D Viewing pipeline 2
Clipping window:
Panning…
H&B 8-2:259-261
ywmax
ywmin
xwmin xwmax
Clipping window
World:
xvmin xvmax
yvmax
yvmin
Viewport
Screen:
2D Viewing pipeline 2
Clipping window:
Panning…
ywmax
ywmin
xwmin xwmax
Clipping window
World:
xvmin xvmax
yvmax
yvmin
Viewport
Screen:
H&B 8-2:259-261
2D Viewing pipeline 3
Clipping window:
Zooming…
ywmax
ywmin
xwmin xwmax
World:
xvmin xvmax
yvmax
yvmin
Viewport
Screen:
H&B 8-2:259-261
2D Viewing pipeline 3
Clipping window:
Zooming…
ywmax
ywmin
xwmin xwmax
World:
xvmin xvmax
yvmax
yvmin
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
xwmin xwmax
ywmax
ywmin
Clipping window
- Clipping window usually an axis-aligned rectangle- Sometimes rotation- From world to view coordinates:
possibly followed by rotation- More complex in 3D
xwmax xwmin
ywmax ywmin
minmin , ywxw T
H&B 8-2:259-261
To normalized coordinates 1
xvmax-xvmin
yvmax -yvmin
xwmax-xwmin
ywmax -ywmin
!distortion :changes ratio-aspect then the
unequal, are factors scale two theIf
,
: withScale
minmax
minmax
minmax
minmax
ywyw
yvyv
xwxw
xvxvS
H&B 8-3:261-265
To normalized coordinates 2
xvmax-xvmin
yvmax -yvmin
xvmin xvmax
yvmax
yvmin
Viewport
1
1
minmin ,
: withTranslate
yvxvT
H&B 8-3:261-265
To normalized coordinates 3
xwmin xwmax
ywmax
ywmin
Clipping window
xvmin xvmax
yvmax
yvmin
Viewport
1
1
),(,),(
: togetherAll
minminminmax
minmax
minmax
minmaxminmin ywxw
ywyw
yvyv
xwxw
xvxvyvxv
TST
H&B 8-3:261-265
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
xwmin xwmax
ywmax
ywmin
Clipping window
- Clipping: removing parts outside clipping window- Many algorithms: points, lines, fill-area, curves,…
H&B 8-5:274
2D Point Clipping
xwmin xwmax
ywmax
ywmin
Clipping window
- Trivial: Save point P = (x,y) if it’s in the box:
maxmin
maxmin
ywyyw
xwxxw
H&B 8-5:274-275
2D Line Clipping 1
xwmin xwmax
ywmax
ywmin
H&B 8-6:275-281
2D Line Clipping 2
xwmin xwmax
ywmax
ywmin
P
Q Basic algorithm:Determine interval (u0, u1) inrectangle, by calculatingcrossings 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
xwmin xwmax
ywmax
ywmin
P
Q
u
u0 := 0; u1 = 1;(* Right side: *)If Px=Qx then If Px > xwmax then return emptyelse u := (xwmax – Px)/(Qx-Px) if Px < Qx then begin if u < u1 then u1 := u end else if u > u0 then u0 := u;If u0 > u1 then return empty
Line segment:X(u) = P + u (Q-P), with 0 <= u <= 1
u1
H&B 8-6:275-281
2D Line Clipping 4
xwmin xwmax
ywmax
ywmin
P
Q
u
(* Left side: *)If Px=Qx then If Px < xwmin then return emptyelse u := (xwmin – Px)/(Qx-Px) if Px < Qx then begin if u > u0 then u0 := u end else if u < u1 then u1 := u;If u0 > u1 then return empty
(* Lower and upper side idem *)
Line segment:X(u) = P + u (Q-P), with 0 <= u <= 1
u1
H&B 8-6:275-281
2D Line Clipping 5
xwmin xwmax
ywmax
ywmin
- 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
xwmin xwmax
ywmax
ywmin
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
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
xwmin xwmax
ywmax
ywmin
Fast test on status end points line with codes C0 en C1 : C0 bitwise-or C1 = 0000: then completely in; C0 bitwise-and C1 <> 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 akaViewing plane
• Pipeline has +/ same structure as in 2DH&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 pixelsH&B 10-2:334-335
3D viewing coordinates 1
Specification of projection:
P0 : View or eye point
Pref : Center or look-at point
V: View-up vector (projection along vertical axis)
zvp : positie view plane
P0
zw
yw
xw
Pref
NV
z vp
P0, Pref , V: define viewing coordinate system
Several variants possible H&B 10-3:336-338
3D viewing coordinates 2
P0
zw
yw
xw
Pref
NV
zview
yviewxview
P0, Pref , V: define viewing coordinate system
Several variants possible H&B 10-4:338-340
3D view coordinates 3
P0
zw
yw
xw
Pref
NV
zview
yviewxview
u
),,(
: and lar toperpendicu
zyx uuu
V
nVu
nVu
v
),,(
: and lar toperpendicu
zyx vvv unv
unv
),,(
:frame axis Derivation
ref0
zyx nnn
N
Nn
PPN
n
H&B 10-4:338-340
3D viewing coördinaten 4
P0
zw
yw
xw
Pref
NV
zview
yviewxview
n
uv
RTM
R
R
PT 0
WC,VC
zyx
zyx
zyx
nnn
vvv
uuu
:Or
1000
0
0
0
: with rotate Next,
)( with translateFirst,
:viewtion worldTransforma
H&B 10-4:338-340
Projection transformations
P1
P2
P’1
P’2
View plane
Parallel projection
P1
P2
P’1
P’2
View plane
Perspective projection
H&B 10-6:340-345
Orthogonal projections 1
P’1
P’2Parallell 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.
P1
P2
P’1
P’2P1
P2
xy
z
H&B 10-6:340-345
Orthogonal projections 2
Orthogonal projection:
P’1
P’2P1
P2
Trivial!
:s)coordinate projection
toscoordinate view(from
),,( of Projection
zz
yy
xx
zyx
p
p
p
H&B 10-6:340-345
Orthogonal projections 3
Clippingwindow
zview
xview
yview
Near plane
Far plane
View volume
H&B 10-6:340-345
Orthogonal projections 4
zview
xview
yview
View volume
(xwmin, ywmin, znear)
(xwmax, ywmax, zfar)
H&B 10-6:340-345
Orthogonal projections 4
zview
xview
yview
View volume
(xwmin, ywmin, znear)
(xwmax, ywmax, zfar)
znorm
xnorm
ynorm
Normalized View volume
(1,1,1)
(-1,-1,-1)
TranslationScaling
From right- to left handed H&B 10-6:340-345
Perspective projection 1
H&B 10-8:351-364
P1
P2
P’1
P’2
View plane: z = zvp
Projection reference point
zview
xview
yview
View plane: orthogonal to zview axis.
Perspective projection 2
P = (x, y, z) R: Projection reference point
zview
xview
yview
(xr, yr, zr)(xp, yp, zp)
To simplify, Assume R in origin
View plane: z = zvp
Question: What is the projection of Pon the view plane?
H&B 10-8:351-364
Perspective projection 3
P = (x, y, z)
zview
xview
yview(xp, yp, zp)
(0,0, 0)R=
.' and;';'or
,10 with ,'
: to(origin) from Line
uzzuyyuxx
uu
PX
PR
. hence '
:plane with crossingAt
z
zuzz vp
vp
yz
zyx
z
zx vp
pvp
p and
giveson Substituti
View plane: z = zvp
H&B 10-8:351-364
Perspective projection 4
P = (x, y, z) View plane
zview
yview
Viewed from the side
R
z
y
zvp
yp
yz
zy
z
y
z
y
vpp
vp
p
hence
: thatseecan We
H&B 10-8:351-364
Perspective projection 5
P = (x, y, z)Clipping window
in View plane
zview
yview
R
zvpW
wmax
wmin
Ratio between W=wmaxwmin and zvp
determines strenght perspective
Viewed from the side
H&B 10-8:351-364
Viewed from the side
Perspective projection 6
Clipping windowin View plane
zview
yview
R
Ratio between W=wmaxwmin and zvp
determines strenght perspective.
This ratio is 2tan(/2),with the view angle.
H&B 10-8:351-364
Perspective projection 7
zview
yview
R
H&B 10-8:351-364
Perspective projection 7
zview
yview
R
H&B 10-8:351-364
Perspective projection 7
R
H&B 10-8:351-364
Perspective projection 8
zview
yview
R
zvp
W
How to specify the ratio between W en zvp?How to specify ‘how much’ perspective I want to see?
Option 1: Specify view angle .
H&B 10-8:351-364
Perspective projection 9
zview
yview
R
zvp
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 zvp, such that W/zvp = 24/f.For x: W/zvp = 36/f.
f (mm)
24 mm
H&B 10-8:351-364
View volume orthogonal…
Clippingwindow
zview
xview
yview
Near clipping plane
Far clipping plane
View volume
H&B 10-8:351-364
View volume perspective
Clippingwindow
zview
xview
yview
Near clipping plane
Far clipping plane
View volume
R
H&B 10-8:351-364
To Normalized Coordinates…
zview
xview
yview
R
znorm
xnorm
ynorm
Normalized View volume
(1,1,1)
(1, 1, 1)
Rectangular frustumView Volume
H&B 10-8:351-364
Side view Front view
Perspective projection 10
yview
Rzview
znorm
ynorm
Perspective transformation:Distort space, such that perpendicular projection gives an image in perspective.
H&B 10-8:351-364
Perspective projection 10
yview
R2r
zn
zf
zview
znorm
ynorm
Simplest case:Square window, clipping plane coincides with view plane: zn=zvp
H&B 10-8:351-364
Perspective projection 11
yview
R
zn
zf
zview
znorm
ynorm
(-r, -r, zn)
((zf /zn)r, (zf /zn)r, zf )
2r
(-1,-1,-1)
(1,1,1)
H&B 10-8:351-364
(r, r, zn)
(rzf /zn, rzf /zn, zf )
Perspective projection 11
yview
Rzview
znorm
ynorm
(1, 1, 1)
(1,1,1)
How to put this transformation in the pipeline?How to process division by z?
Earlier: yz
zyx
z
zx vp
pvp
p ,
H&B 10-8:351-364
Homogeneous coordinates (reprise)
• Add extra coordinate:
P = (px , py , pz , ph) 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
.
/
/
/
:bygiven are scoordinate projectedsuch that
10100
:by described becan ation transformePerspectiv
hz
hy
hx
z
y
x
z
y
x
tsss
tsss
tsss
h
z
y
x
h
h
h
p
p
p
v
v
v
zzzzyzx
yyzyyyx
xxzxyxx
h
h
h
Homogeneous coordinates (reprise)
H&B 10-8:351-364
Perspective projection 12
xview
Rzview
znorm
ynorm
(r, r, zn)
(rzf /zn, rzf /zn, zf )
(1, 1, 1)
(1,1,1)(r, r, zn)
.0,/ :givesn Elaboratio
.1 also then , and / If
.1 then , and If
./)( is form Generic .First
xxzxynxx
pfnf
pn
xxzxyxxp
tssrzs
xzzzrzx
xzzrx
ztzsysxsxx
H&B 10-8:351-364
Perspective projection 13
yview
Rzview
znorm
ynorm
(r, r, zn)
(rzf /zn, rzf /zn, zf )
(1, 1, 1)
(1,1,1)
.0),/( :Or
./)/(
:gives , as Same . Next the
yyzyxnyy
np
tssrzs
zyrzy
xy
H&B 10-8:351-364
Perspective projection 14
yview
Rzview
znorm
ynorm
(r, r, zn)
(rzf /zn, rzf /zn, zf )
(1, 1, 1)
(1,1,1)
.0,2
,
givesn Elaboratio .1 then , If
.1 then , If
./)( :is form Generic . :Finally
zyzxfn
fnz
fn
fnzz
ff
pn
zzzzyzxp
sszz
zzt
zz
zzs
zzz
zzz
ztzsysxszz
H&B 10-8:351-364
Perspective projection 15
.
/
/
/
:from follow scoordinate projected thewhere
10100
200
00/0
000/
:by described be hencecan ation transformePerspectiv
hz
hy
hx
z
y
x
z
y
x
zz
zz
zz
zzrz
rz
h
z
y
x
v
v
v
p
p
p
v
v
v
fn
fn
fn
fn
n
n
h
h
h
H&B 10-8:351-364
3D Viewport coordinates 1
H&B 10-9:365-365
znorm
xnorm
ynorm
Normalized View volume
(1,1,1)
(1, 1, 1)
zv xv
yv
3D screen
(xvmin, yvmin, 0)
(xvmax, yvvmax, 1)
scalingtranslation
3D Viewport coordinaten 2
110002
1
2
100
002
0
0002
:scoordinatescreen 3D to
scoordinate viewnormalized fromtion Transforma
minmax
minmax
p
p
p
s
s
s
z
y
xyvyv
xvxv
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.
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: P0, viewpoint, location of camera;
xref,yref,zref: Pref, centerpoint;
Vx,Vy,Vz: V, view-up vector.
Default: P0 = (0, 0, 0); Pref = (0, 0, 1); V=(0, 1, 0).
OpenGL 3D Viewing 5
H&B 10-10:365-371
z
xy
Orthogonal projection:
MatrixMode(GL_PROJECTION); glOrtho(xwmin, xwmax, ywmin, ywmax, dnear, dfar);
xwmin
xwmaxywmin
ywmax
dnear
dfar
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);
xwminxwmaxywmin
ywmax
xwmin, xwmax, ywmin,ywmax:
specification window
dnear: distance to near clipping plane
dfar : distance to far clipping plane
z
xy
dnear
dfar
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
Next…
• We now know how to project objects.
• But how to model objects?