Transformation From World To Viewing Coordinates

Download Report

Transcript Transformation From World To Viewing Coordinates

UBI 516
Advanced Computer Graphics
Three Dimensional Viewing
Aydın Öztürk
[email protected]
http://www.ube.ege.edu.tr/~ozturk
Overview
• Viewing a 3D scene
• Projections
– Parallel and perspective
Overview
• Depth cueing and hidden surfaces
• Identifying visible lines and surfaces
Overview
• Surface rendering
Overview
• Exploded and cutaway views
Overview
• 3D and stereoscopic viewing
3D Viewing Pipeline
MC
DC
Viewport
Transformation
Modeling
Transformation
NC
WC
Viewing
Transformation
VC
PC
Projectionn
Transformation
Normalization
Transformation
and Clipping
Viewing Coordinates
• Generating a view of an object in 3D is similar to
photographing the object.
• Whatever appears in the viewfinder is projected onto
the flat film surface.
• Depending on the position, orientation and aperture
size of the camera corresponding views of the scene
is obtained.
Specifying The View Coordinates
• For a particular view of a scene
first we establish viewingcoordinate system.
• A view-plane (or projection plane)
is set up perpendicular to the
viewing z-axis.
• World coordinates are transformed
to viewing coordinates, then
viewing coordinates are projected
onto the view plane.
yv
xv
yw
zv
P0=(x0 , y0 , z0)
xw
zw
Specifying The View Coordinates
• To establish the viewing reference frame, we
first pick a world coordinate position called the
view reference point.
• This point is the origin of our viewing
coordinate system. If we choose a point on an
object we can think of this point as the
position where we aim a camera to take a
picture of the object.
Specifying The View Coordinates
• Next, we select the positive
direction for the viewing z-axis,
and the orientation of the view
plane, by specifying the viewplane normal vector, N.
• We choose a world coordinate
position P and this point
establishes the direction for N.
• OpenGL establishes the direction
for N using the point P as a look at
point relative to the viewing
coordinate origin.
yv
xv
yw
zv
N
P0
P
xw
zw
Specifying The View Coordinates
• Finally, we choose the up direction
for the view by specifying view-up
vector V.
• This vector is used to establish the
positive direction for the yv axis.
• The vector V is perpendicular to N.
• Using N and V, we can compute a
third vector U, perpendicular to
both N and V, to define the
direction for the xv axis.
yv
xv
V
yw
N
zv
P0
P
xw
zw
Specifying The View Coordinates
To obtain a series of views of a
scene , we can keep the the
view reference point fixed and
change the direcion of N. This
corresponds to generating
views as we move around the
viewing coordinate origin.
V
P0
N
N
Transformation From World To Viewing
Coordinates
Conversion of object
descriptions from
world to viewing
coordinates is
equivalent to
transformation that
superimpoes the
viewing reference
frame onto the world
frame using the
translation and
rotation.
yv
yw
xv
zv
x
w
zw
Transformation From World To Viewing
Coordinates
First, we translate the
view reference point
to the origin of the
world coordinate
system
yv
yw
xv
zv
x
w
zw
Transformation From World To Viewing
Coordinates
Second, we apply
rotations to align the
xv,, yv and zv axes
with the world xw, yw
and zw axes,
respectively.
yw
yv
xv
zv
xv
zv
zw
x
w
Transformation From World To Viewing
Coordinates
If the view reference
point is specified at
word position (x0, y0,
z0), this point is
translated to the
world origin with the
translation matrix T.
1
0
T
0
0

0 0  x0 
1 0  y0 

0 1  z0 
0 0
1 
Transformation From World To Viewing
Coordinates
• The rotation
sequence requires
3 coordinate-axis
transformation
depending on the
direction of N.
• First we rotate
around xw-axis to
bring zv into the xw
-zw plane.
0
1 0
0 Cos  Sin
Rx  
Cos
0 Sin
0 0
0

0

0

0

1
Transformation From World To Viewing
Coordinates
Then, we rotate
around the
world yw axis to
align the zw and
zv axes.
 Cos
 0
Ry  
 Sin
 0

Sin
0
1
0
0

0 Cos 0 

0
0
1
0
Transformation From World To Viewing
Coordinates
The final rotation
is about the
world zw axis to
align the yw and
yv axes.
Cos
 Sin
Rz  
 0
 0

 Sin 0 0 
Cos 0 0 

0
1 0

0
0 1
Transformation From World To Viewing
Coordinates
The complete transformation from world to viewing
coordinate transformation matrix is obtaine as the matrix
product
M wc ,vc  R z  R y  R x  T
Transformation From World To Viewing
Coordinates
Another method for generating the rotation-transformation
matrix is to calculate uvn vectors and obtain the composite
rotation matrix directly. Given the vectors N and V ,
these unit vectors are calculated as
N
n   (n1 , n2 , n3 )
N
VN
u
 (u1 , u2 , u3 )
VN
v  n  u  (v1 , v2 , v3 )
Transformation From World To Viewing
Coordinates
This method also automatically adjusts the direction for V
so that v is perpendicular to n . The rotation matrix for the
viewing transformation is then
u1

v
1

R
n1

0
u2
u3
v2
v3
n2
n3
0
0
0

0
0

1 
Transformation From World To Viewing
Coordinates
The matrix for translating the viewing origin to the world
origin is
1 0 0
0 1 0
R
0 0 1

0
0
 x0 

 y0 
 z0 

1 
Transformation From World To Viewing
Coordinates
The composite matrix for the viewing transformation is then
MW C,VC
u1
v
1


n1

0
u2
u3
v2
v3
n2
n3
0
0
t1 

t2 
t3 

1 
where
t1  u  P0   x0u x  y0u y  z 0u z
t 2   v  P0   x0 v x  y0 v y  z 0 v z
t3  n  P0   x0 n x  y0 n y  z 0 n z
Transformation From World To Viewing
Coordinates: An Example For 2d System
6
y
4
P=(5,5)
Θ=300
0
2
P0=(4,3)
0
2
4
6
x
Transformation From World To Viewing
Coordinates: An Example For 2d System
Translation:
4
2
P
0
1 0  4


T  0 1 3


0 0 1 
6
y
P0
Θ=300
2
4
6
x
Transformation From World To Viewing
Coordinates: An Example For 2d System
Rotation
y′
2
0
0

1
P
0
 0.866 0.5
R    0.5
0.866

0
 0
4
6
y
P0
2
4
6
x
Transformation From World To Viewing
Coordinates: An Example For 2d System
New coordinates
M wc.vc
0  1 0  4
 0.866 0.5
  0.5 0.866 0  0 1  3 

 

 0
0
1  0 0 1 
 x '   0.866 0.500
 y '    0.500 0.866
  
0
1   0
 4.964 5 1.866 
 0.598 5  1.232 
  

1
1 1

Transformation From World To Viewing
Coordinates: An Example For 2d System
Alternative Method
n  (0.866
0.500 )
y
2
3
v  (0.500 0.866)
P
1
0
0

1
n
v
Θ=300
0
 0.866 0.500
R   0.500 0.866

0
0

P0
x'  R  T  x
1
2
3
x
Projections
• Once WC description of the objects in a scene are
converted to VC we can project the 3D objects onto 2D
view-plane.
• Two types of projections:
-Parallel Projection
-Perspective Projection
Classical Viewings
• Hand drawings : Determined by a specific
relationship between the object and the viewer.
Parallel Projections
Coordinate Positions are transformed to the view plane
along parallel lines.
View
Plane
P2
P1
P′2
P′1
Parallel Projections
• Orthographic parallel projection
The projection is perpendicular to the view
plane.
• Oblique parallel projecion
The parallel projection is not perpendicular to
the view plane.
Orthographic Parallel Projection
The orthographic transformation
 x '  1 0 0 0  x 
 y '  0 1 0 0  y 
 
 
 z '   0 0 0 0 z 
1   0 0 0 1 1 
 
  
Orthographic Parallel Projection
Oblique Parallel Projection
– The projectors are still ortogonal to the projection plane
– But the projection plane can have any orientation with
respect to the object.
– It is used extensively in architectural and mechanical design.
Oblique Parallel Projection
• Preserve parallel lines but not angles
– Isometric view : Projection plane is placed
symmetrically with respect to the three principal
faces that meet at a corner of object.
– Dimetric view : Symmetric with two faces.
– Trimetric view : General case.
Oblique Parallel Projection
• Preserve parallel lines but not angles
– Isometric view : Projection plane is placed
symmetrically with respect to the three principal
faces that meet at a corner of object.
– Dimetric view : Symmetric with two faces.
– Trimetric view : General case.
Oblique Parallel Projection
x xp x xLLcos

cos
p
y yp y yLLsin
sin
yv
(xp, yp)
p
zz
LL
zLzL
11
tan
tan
α
(x, y, z)
L
φ
xv
x xp x xz (zL
cos
) )
1
(
L
cos
p
1
y yp y yz (zL
( 1Lsin
sin) )
p
1
(x, y)
zv
Oblique Parallel Projection
The oblique transformation
 x p  1 0
  
y
0
1
p
 
 z p  0 0
  
1   0 0
L1 cos 
L1 sin
0
0
0  x 



0 y
 
0 z 



1 1 
Oblique Parallel Projection
Perspective Projections
• First discovered by Donatello, Brunelleschi,
and DaVinci during Renaissance
• Objects closer to viewer look larger
• Parallel lines appear to converge to single
point
Perspective Projections
In perspective projection object positions are
transformed to the view plane along lines that
converge to a point called the projection
reference point (or center of projection)
Perspective Projections
• In the real world, objects exhibit perspective
foreshortening: distant objects appear smaller
• The basic situation:
Perspective Projections
When we do 3-D graphics, we think of the
screen as a 2-D window onto the 3-D world:
How tall should
this bunny be?
Perspective Projections
The geometry of the situation is that of similar
triangles. View from above:
View plane
P (x, y, z)
X
(xp, yp)
x′ = ?
(0,0,0)
Z
d
Perspective Projections
Desired result for a point [x, y, z, 1]T projected
onto the view plane:
x' x y ' y
 ,

d z d z
dx
x
dy
y
x' 

, y' 

, z'  d
z
zd
z
zd
Perspective Projections
1
0
Mperspectiv e  
0
0

0
0
1
0
0 1
0 1d
0
0

0
1
Perspective Projections
 xh  1
 y  0
 h  
 z h  0
  
 h  0
0
0
1
0
0
1
0 1d
0

0
0

0
 x
 y
 
z
 
1 
h  z/d
x p  xh / h,
y p  yh / h
Projection Matrix
• We talked about geometric transforms,
focusing on modeling transforms
– Ex: translation, rotation, scale,
gluLookAt()
– These are encapsulated in the OpenGL
modelview matrix
• Can also express projection as a matrix
– These are encapsulated in the OpenGL
projection matrix
View Volumes
• When a camera used to take a picture, the
type of lens used determines how much of
the scene is caught on the film.
• In 3D viewing, a rectangular view window in
the view plane is used to the same effect.
Edges of the view window are parallel to the
xv-yv axes and window boundary positions are
specified in viewing coordinates.
View Volumes
View volume
View volume
(frustum)
zv
Back Plane
Front Plane
Back Plane
Front Plane
Parallel Projection
Projection
Reference Point
Perspective Projection
Clipping
• An algorithm for 3D clipping identifies and
saves all surface segments within the view
volume for display.
• All parts of object that are outside the view
volume are discarded.
Clipping Lines
• To clip a line against the view volume, we need to test
the relative position of the line using the view volume’s
boundary plane equation.
• An end point (x,y,z) of a line segment is outside a
boundary plane if
Ax  By  Cz  D  0
where A, B, C and D are the plane parameters for that
boundary.
Clipping Polygon Surface
• To clip a polygon surface, we can clip the individual
polygon edges.
• First we test the coordinate extends against each
boundary of the view volume to determine whether
the object is completely inside or completely outside
of that boundary.
• If the object has intersection with the boundary then
we apply intersection calculations.
Clipping Polygon Surface
• The projection operation can take place before the
view- volume clipping or after clipping.
• All objects within the view volume map to the interior
of the specified projection window.
• The last step is to transform the window contents to a
2D view port.
Clipping Polygon Surface
Viev volume
Steps For Normalized View Volumes
• A scene is constructed by transforming object
descriptions from modeling coordinates to wc.
• The world descriptions are converted to viewing
coordinates.
• The viewing coordinates are transformed to projection
coordinates which effectively converts the view volume
into a rectangular parallelepiped.
• The parallelepiped is mapped into the unit cube called
normalized projection coordinate system.
• A 3D viewport within the unit cube is constructed.
• Normalized projection coordinates are converted to
device coordinates for display.
Normalized View Volumes
y
x
(Xwmax, ywmax, zback)
(Xvmax, yvmax, zvmax)
z
(Xwmin, ywmin, zfront)
Parallelepiped
View Volume
(Xvmin, yvmin, zwmin)
Unit Cube
Orthogonal Projection Normalization
xmax  xmin y max  y min z max  z min
T( 
,
,
)
2
2
2
2
2
2
S(
,
,
)
xmax  xmin y max  y min z max  z min
2

x  x
 max min

0
PST


0


0
0
0
2
y max  y min
0
0
0
2
z max  z min
0
xmax  xmin 


2

y max  y min 


2
z max  z min 

2


1
Oblique Projection Normalization
• Angles of projection
–  for x axis
–  for y axis
tan  


z
x  xp
x p  x  z cot
y p  y  z cot 
zp  0
• Shearing matrix H(, )
1
0
H ( ,  )  
0

0
0  cot 
1  cot 
0
1
0
0
0
0
0

1
Oblique Projection Normalization
1
0
P  Porth H ( ,  )  
0

0
0 0 0 1
1 0 0 0
0 0 0  0

0 0 1  0
Finished ? No, this is a sheared view volume, so we have to apply
orthogonal transformation :
P=Porth STH
0  cot
1  cot 
0
1
0
0
0
0
0

1
Perspective Projection Normalization
Perspective Normalization is Trickier
Perspective Projection Normalization
Consider N =
1
0

0

0
After multiplying:
p’ = Np
0 0
1 0
0 
0 1
0
0 


0
Perspective Projection Normalization
After dividing by w’, p’ -> p’’
Perspective Projection Normalization
Quick Check
• If x = z
– x’’ = -1
• If x = -z
– x’’ = 1
Perspective Projection Normalization
What about z?
• if z = zmax
• if z = zmin
• Solve for  and  such that zmin -> -1 and zmax ->1
• Resulting z’’ is nonlinear, but preserves ordering of
points
– If z1 < z2 … z’’1 < z’’2
Perspective Projection Normalization
We did it. Using matrix, N
• Perspective viewing frustum transformed to cube
• Orthographic rendering of cube produces same
image as
perspective rendering of original frustum
OpenGL Projection Commands
OpenGL Look-At Function
• OpenGL utility function
gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz);
look-at positioning
– VRP: eyePoint (eyex, eyey, eyez)
– VPN: – ( atPoint – eyePoint ) (atx, aty, atz) – (eyex, eyey, eyez)
– VUP: upPoint – eyePoint (upx, upy, upz)
Projections in OpenGL
• Angle of view, field of view :
– Only objects that fit within
the angle of view of the
camera appear in the
image
• View volume, view frustum :
– Be clipped out of scene
– Frustum – truncated
pyramid
Projections in OpenGL
Perspective in OpenGL
Specification of a frustum
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
glFrustum(xmin, xmax, ymin, ymax, near, far);
– near, far: positive number !!
 zmax = – far
 zmin = – near
Perspective in OpenGL
Specification using the field of view
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
gluPerspective(fovy, aspect, near, far);
– fov: angle between top and
bottom planes
– fovy: the angle of view in the
up (y) direction
– aspect ratio: width / height
Parallel Viewing in OpenGL
Orthographic viewing function
glMatrixMode(GL_PROJECTION);
glLoadIdentity( );
glOrtho(xmin, xmax, ymin, ymax, near, far);
– OpenGL provides only this parallel-viewing
function
– near < far !!
 no restriction on the sign
 zmax = – far
 zmin = – near
Optional Clipping Planes
– glClipPlane(id, PlaneParameters);
glEnable(id);
// id = GL_CLIP_PLANE0, GL_CLIP_PLANE1, ...
// PlaneParameters = A,B,C and D of the plane
...
glDisable(id);