Transcript ppt

2IV60 Computer graphics
set 5: Viewing
Jack van Wijk
TU/e
Viewing
• Transformation worldscreen
• Clipping: Removing parts outside screen
• 2D (chapter 8)
• 3D (chapter 10)
2D Viewing pipeline 1
World:
Screen:
Clipping window
Viewport
ywmax
yvmax
ywmin
yvmin
xwmin
xwmax
Clipping window:
What do we want to see?
xvmin
xvmax
Viewport:
Where do we want to see it?
H&B 8-1:258-259
2D Viewing pipeline 2
World:
Screen:
Clipping window
Viewport
ywmax
yvmax
ywmin
yvmin
xwmin
xwmax
xvmin
xvmax
Clipping window:
Panning…
H&B 8-2:259-261
2D Viewing pipeline 2
World:
Screen:
Clipping window
Viewport
ywmax
yvmax
ywmin
yvmin
xwmin
xwmax
xvmin
xvmax
Clipping window:
Panning…
H&B 8-2:259-261
2D Viewing pipeline 3
World:
Screen:
Viewport
ywmax
yvmax
ywmin
yvmin
xwmin
xwmax
xvmin
xvmax
Clipping window:
Zooming…
H&B 8-2:259-261
2D Viewing pipeline 3
World:
Screen:
ywmax
Viewport
yvmax
yvmin
ywmin
xwmin
xwmax
xvmin
xvmax
Clipping window:
Zooming…
H&B 8-2:259-261
2D Viewing pipeline 4
MC: Modeling Coordinates
Apply model transformations
WC: World Coordinates
Determine visible parts
VC: Viewing Coordinates
To standard coordinates
NC: Normalized Coordinates
Clip and determine pixels
DC: Device Coordinates
H&B 8-2:259-261
Clipping window
Clipping window
ywmax
ywmax  ywmin
ywmin
xwmin
xwmax
xwmax xwmin
- Clipping window usually an axis-aligned rectangle
- Sometimes rotation
- From world to view coordinates: T xwmin , ywmin 
possibly followed by rotation
- More complex in 3D
H&B 8-2:259-261
To normalized coordinates 1
ywmax -ywmin
yvmax -yvmin
xwmax-xwmin
Scale with :
xvmax-xvmin
 xv max  xv min yv max  yv min 

S
,
 xwmax  xwmin ywmax  ywmin 
If the two scale factors are unequal,
then the aspect - ratio changes : distortion!
H&B 8-3:261-265
To normalized coordinates 2
1
Viewport
yvmax
yvmax -yvmin
yvmin
xvmax-xvmin
xvmin
xvmax 1
T ranslate with :
T xv min , yv min 
H&B 8-3:261-265
To normalized coordinates 3
Clipping window
1
ywmax
yvmax
ywmin
yvmin
xwmin
xwmax
Viewport
xvmin
xvmax 1
All together :
 xvmax  xvmin yvmax  yvmin
T( xvmin , yvmin )S
,
 xwmax  xwmin ywmax  ywmin

T( xwmin , ywmin )

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
ywmax
ywmin
xwmin
xwmax
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);
vpHeight
vpWidth
(xvmin, yvmin)
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
Clipping window
ywmax
ywmin
xwmin
xwmax
- Clipping: removing parts outside clipping window
- Many algorithms: points, lines, fill-area, curves,…
H&B 8-5:274
2D Point Clipping
Clipping window
ywmax
ywmin
xwmin
xwmax
- Trivial: Save point P = (x,y) if it’s in the box:
xwmin  x  xwmax
ywmin  y  ywmax
H&B 8-5:274-275
2D Line Clipping 1
ywmax
ywmin
xwmin
xwmax
H&B 8-6:275-281
2D Line Clipping 2
ywmax
Q
u
ywmin
P
xwmin
Basic algorithm:
Determine interval (u0, u1) in
rectangle, by calculating
crossings with edges of window.
xwmax
Line segment:
X(u) = P + u (QP),
with 0 <= u <= 1
H&B 8-6:275-281
2D Line Clipping 3
ywmax
Q
u
ywmin
u1
P
xwmin
xwmax
Line segment:
X(u) = P + u (Q-P),
with 0 <= u <= 1
u0 := 0; u1 = 1;
(* Right side: *)
If Px=Qx then
If Px > xwmax then return empty
else
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
H&B 8-6:275-281
2D Line Clipping 4
ywmax
Q
u
ywmin
u1
P
xwmin
xwmax
Line segment:
X(u) = P + u (Q-P),
with 0 <= u <= 1
(* Left side: *)
If Px=Qx then
If Px < xwmin then return empty
else
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 *)
H&B 8-6:275-281
2D Line Clipping 5
ywmax
ywmin
xwmin
xwmax
- 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
2D Line Clipping 6
ywmax
ywmin
1001 1000 1010
0001 0000
0010
0101 0100
0110
xwmin
xwmax
bit 4 bit 3 bit 2 bit 1
Top
Right
Bottom
Left
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
2D Line Clipping 7
ywmax
ywmin
1001 1000 1010
0001 0000
0010
0101 0100
0110
xwmin
xwmax
bit 4 bit 3 bit 2 bit 1
Top
Right
Bottom
Left
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 aka
Viewing plane
• Pipeline has +/ same structure as in 2D
H&B 10-1:332-334
3D Viewing pipeline 2
MC: Modeling Coordinates
Apply model transformations
WC: World Coordinates
To camera coordinates
VC: Viewing Coordinates
Project
PC: Projection Coordinates
To standard coordinates
NC: Normalized Coordinates
Clip and convert to pixels
H&B 10-2:334-335
DC: Device Coordinates
3D viewing coordinates 1
Specification of projection:
yw
P0 : View or eye point
P0
N
Pref : Center or look-at point
V
Pref
V: View-up vector
xw
(projection along vertical
zw
axis)
zvp : positie view plane
P0, Pref , V: define viewing coordinate system
Several variants possible
H&B 10-3:336-338
3D viewing coordinates 2
xview
yview
yw
V
zw
P0
zview
N
Pref
xw
P0, Pref , V: define viewing coordinate system
Several variants possible
H&B 10-4:338-340
3D view coordinates 3
xview
yview
v
yw
V
zw
Derivation axis frame :
N  P0  Pref
u
n
P0
N
Pref
zview
N
n
 (nx , n y , n z )
N
u perpendicular to V and n :
xw
Vn
u
 (u x , u y , u z )
V
v perpendicular to n and u :
v  n  u  (v x , v y , v z )
H&B 10-4:338-340
3D viewing coördinaten 4
xview
yview
v
yw
V
zw
u
n
P0
N
Pref
xw
zview
T ransformation world view :
First, translate with T(P0 )
Next, rotate with R :
 ux

 vx
R 
n
 x
0

uy
vy
ny
0
uz
vz
nz
0
0

0
0

1 
Or :
MWC,VC  RT
H&B 10-4:338-340
Projection transformations
P2
View plane
P2
P’2
P1
P’1
Parallel projection
View plane
P’2
P1
P’1
Perspective projection
H&B 10-6:340-345
Orthogonal projections 1
P2
Parallell projection:
Projection lines are parallel
P’2
P1
P’1
Orthogonal projection:
Projection lines are parallel and
perpendicular to projection plane
P2
P’2
P1
P’1
z
y
x
Isometric projection:
Projection lines are parallel,
perpendicular to projection plane,
and have the same angle with axes.
H&B 10-6:340-345
Orthogonal projections 2
Orthogonal projection:
P2
P’2
P1
P’1
Projection of ( x, y, z )
(from view coordinates to
projection coordinates) :
xp  x
yp  y
zp  z
T rivial!
H&B 10-6:340-345
Orthogonal projections 3
View volume
Clipping
window
yview
xview
Far plane
zview
Near plane
H&B 10-6:340-345
Orthogonal projections 4
View volume
(xwmax, ywmax, zfar)
yview
xview
(xwmin, ywmin, znear)
zview
H&B 10-6:340-345
Orthogonal projections 4
(xwmax, ywmax, zfar)
(1,1,1)
yview
xview
(xwmin, ywmin, znear)
View volume
zview
znorm ynorm
xnorm
(-1,-1,-1)
Normalized View volume
Translation
Scaling
From right- to left handed
H&B 10-6:340-345
Perspective projection 1
P2
View plane: z = zvp
P’2
P1
P’1
Projection reference point
yview
xview
zview
View plane: orthogonal to zview axis.
H&B 10-8:351-364
Perspective projection 2
View plane: z = zvp
P = (x, y, z)
R: Projection reference point
(xp, yp, zp) (x , y , z )
r r r
yview
xview
To simplify,
Assume R in origin
zview
Question: What is the projection of P
on the view plane?
H&B 10-8:351-364
Perspective projection 3
Line from R (origin) to P :
View plane: z = zvp
P = (x, y, z)
(xp, yp, zp)
X'  uP, with 0  u  1,
yview
or x'  ux; y '  uy; and z '  uz.
xview
R= (0,0, 0) zview
At crossing with plane :
z '  zvp hence u 
zvp
z
.
Substitution gives
xp 
z vp
z
x and y p 
z vp
z
y
H&B 10-8:351-364
Perspective projection 4
P = (x, y, z)
View plane
We can see that :
y yp

z zvp
yview
y
yp
z
R
zvp
zview
hence
yp 
zvp
z
y
Viewed from the side
H&B 10-8:351-364
Perspective projection 5
Clipping window
P = (x, y, z) in View plane
wmax
yview
Ratio between
W=wmaxwmin and zvp
R
W
zvp
determines strenght perspective
zview
wmin
Viewed from the side
H&B 10-8:351-364
Perspective projection 6
Clipping window
in View plane

Ratio between
yview
W=wmaxwmin and zvp
R
determines strenght perspective.
zview
This ratio is 2tan(/2),
with  the view angle.
Viewed from the side
H&B 10-8:351-364
Perspective projection 7
yview
R
zview
H&B 10-8:351-364
Perspective projection 7
yview
R
zview
H&B 10-8:351-364
Perspective projection 7
R
H&B 10-8:351-364
Perspective projection 8
Option 1: Specify view angle .
yview

W
R
zview
zvp
How to specify the ratio between W en zvp?
How to specify ‘how much’ perspective I want to see?
H&B 10-8:351-364
Perspective projection 9
yview
R
24 mm
zview
W
zvp
f (mm)
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.
H&B 10-8:351-364
View volume orthogonal…
View volume
Clipping
window
yview
xview
Far
clipping plane
Near
clipping plane
zview
H&B 10-8:351-364
View volume perspective
View volume
Clipping
window
yview
xview
Far
clipping plane
Near
clipping plane
R
zview
H&B 10-8:351-364
To Normalized Coordinates…
(1,1,1)
yview
xview
R
Rectangular frustum
View Volume
zview
znorm ynorm
xnorm
(1, 1, 1)
Normalized View volume
H&B 10-8:351-364
Perspective projection 10
ynorm
yview
R
zview
znorm
Side view
Front view
Perspective transformation:
Distort space, such that perpendicular projection gives
an image in perspective.
H&B 10-8:351-364
Perspective projection 10
ynorm
yview
2r
R
zview
znorm
zn
zf
Simplest case:
Square window,
clipping plane coincides with view plane: zn=zvp
H&B 10-8:351-364
Perspective projection 11
((zf /zn)r, (zf /zn)r, zf )
(1,1,1) ynorm
yview
2r
R
zview
(-r, z-r, zn)
znorm
(-1,-1,-1)
n
zf
H&B 10-8:351-364
Perspective projection 11
(rzf /zn, rzf /zn, zf )
(1,1,1) ynorm
yview
R
zview
znorm
(1, 1, 1)
(r,  r, zn)
Earlier: x p 
zvp
z
x, y p 
zvp
z
y
How to put this transformation in the pipeline?
How to process division by z?
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
Homogeneous coordinates (reprise)
Perspective transform ation can be described by :
 xh   s xx s xy s xz t x  xv 
  
 
 yh   s yx s yy s yz t y  yv 
z  s
 z 
s
s
t
zy
zz
z  v 
 h   zx
h  0
 1 
0

1
0
  
 
such that projected coordinates are given by :
 x p   xh / h 
  

 y p    yh / h .
  

z
z
/
h
 p  h 
H&B 10-8:351-364
Perspective projection 12
(rzf /zn, rzf /zn, zf )
(r, r, zn)
xview
R
zview
(1,1,1) ynorm
znorm
(r, r, zn)
(1, 1, 1)
First x. Generic form is x p  ( s xx x  s xy y  s xz z  t x ) /  z.
If x  r and z  z n , then x p  1.
If x  rz f / z n and z  z f , then also x p  1.
Elaboration gives : s xx   z n / r , s xy  s xz  t x  0.
H&B 10-8:351-364
Perspective projection 13
(rzf /zn, rzf /zn, zf )
(1,1,1) ynorm
yview
R
zview
znorm
(r, r, zn)
(1, 1, 1)
Next the y. Same as x, gives :
y p  ( zn / r ) y /  z.
Or : s yy  ( zn / r ), s yx  s yz  t y  0.
H&B 10-8:351-364
Perspective projection 14
(rzf /zn, rzf /zn, zf )
(1,1,1) ynorm
yview
R
zview
znorm
(1, 1, 1)
(r, r, zn)
Finally: z. Generic form is : z p  ( s zx x  s zy y  s zz z  t z ) /  z.
If z  z n , then z p  1.
If z  z f , then z f  1. Elaboration gives
s zz 
zn  z f
zn  z f
,tz 
 2 zn z f
zn  z f
, s zx  s zy  0.
H&B 10-8:351-364
Perspective projection 15
Perspective transform ation can hence be described by :
0
0
0
 z / r
 xh   n
   0
 zn / r
0
0
y
 h 
zn  z f  2 zn z f
z  0
0
h
  
zn  z f
zn  z f
h
   0
0
1
0

where the projected coordinates follow from :
 x
 v 
y 
 v
  zv 
  
 1 

 x p   xv / h 
  

 y p    yv / h .
  

z
z
/
h
p
v

  
H&B 10-8:351-364
3D Viewport coordinates 1
(xvmax, yvvmax, 1)
(1,1,1)
znorm ynorm
xnorm
(1, 1, 1)
yv
zv
xv
scaling
translation
Normalized View volume
(xvmin, yvmin, 0)
3D screen
H&B 10-9:365-365
3D Viewport coordinaten 2
T ransformation from normalized view coordinates
to 3D screen coordinates :
 xv max

 xs  
 
 ys  
z 
 s 
h 
  

 xv min
2
0
0
0
yv max  yv min
2
0
0
0
0
0
1
2
0

0
  x p 
0   yp 
 
1   zp 
 1 
2 
1
H&B 10-9:365-365
OpenGL 3D Viewing 1
3D Viewing in OpenGL:
- Position camera;
- Specify projection.
H&B 10-10:365-371
OpenGL 3D Viewing 2
View volume
y
x
z
Camera always in origin, in direction of negative z-axis.
Convenient for 2D, but not for 3D.
H&B 10-10:365-371
OpenGL 3D Viewing 3
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();
H&B 10-10:365-371
OpenGL 3D Viewing 4
MatrixMode(GL_MODELVIEW);
gluLookAt(x0,y0,z0, xref,yref,zref, Vx,Vy,Vz);
x0,y0,z0:
xref,yref,zref:
Vx,Vy,Vz:
P0, viewpoint, location of camera;
Pref, centerpoint;
V, view-up vector.
Default: P0 = (0, 0, 0); Pref = (0, 0, 1); V=(0, 1, 0).
H&B 10-10:365-371
OpenGL 3D Viewing 5
Orthogonal projection:
MatrixMode(GL_PROJECTION);
glOrtho(xwmin, xwmax, ywmin, ywmax, dnear, dfar);
xwmin, xwmax, ywmin,ywmax:
specification window
dnear: distance to near clipping plane
dfar : distance to far clipping plane
y
ywmax
x
xwmax
ywmin
xwmin
z
Select dnear and dfar right:
dnear < dfar,
model fits between clipping planes.
H&B 10-10:365-371
OpenGL 3D Viewing 6
Perspective projection:
MatrixMode(GL_PROJECTION);
glFrustrum(xwmin, xwmax, ywmin, ywmax, dnear, dfar);
xwmin, xwmax, ywmin,ywmax:
specification window
dnear: distance to near clipping plane
dfar : distance to far clipping plane
y
ywmax
ywmin
x
xwmax
xwmin
Standard projection:
z
xwmin = -xwmax,
ywmin = -ywmax
Select dnear and dfar right:
0 < dnear < dfar,
model fits between clipping planes.
H&B 10-10:365-371
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);
vpHeight
vpWidth
(xvmin, yvmin)
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?