Anda di halaman 1dari 17

CG (Window to Viewport

Transformation)
Prof. Tathagata Ray
Associate Professor, BITS Pilani, Hyderabad Campus
BITS Pilani
Hyderabad Campus rayt@hyderabad.bits-pilani.ac.in
World Coordinates and
viewport
• As a designer or programmer, one may not want to think
of designing in terms of pixels.

• One may think of drawing a graph, where x varies from -


5 to 5 and y varies from -1 to 1.

• The coordinate space in which the objects of a scene are


described is called as world-coordinates.

• We define a rectangular window called world –window


in world coordinates to specify the part of the scene to
be drawn.

BITS Pilani, Hyderabad Campus


World coordinates and world-
window

World-window

World coodinates

BITS Pilani, Hyderabad Campus


Viewport
• We define a rectangular viewport in the screen window.
• World-window should map to the viewport.
• This window-viewport makes things like panning and
zooming easy.

Viewport
World-window

World Screen window


coodinates
BITS Pilani, Hyderabad Campus
Maintaining relative ratio

World-Window Viewport
ywmax
yvmax
 xw, yw
 xv, yv 
ywmin yvmin

0
xwmin xwmax xvmin xvmax
Maintain relative size and position between clipping window and viewport.
𝑥𝑣 − 𝑥𝑣𝑚𝑖𝑛 𝑥𝑤 − 𝑥𝑤𝑚𝑖𝑛
=
𝑥𝑣𝑚𝑎𝑥 − 𝑥𝑣𝑚𝑖𝑛 𝑥𝑤𝑚𝑎𝑥 − 𝑥𝑤𝑚𝑖𝑛
𝑦𝑣 − 𝑦𝑣𝑚𝑖𝑛 𝑦𝑤 − 𝑦𝑤𝑚𝑖𝑛
=
𝑦𝑣𝑚𝑎𝑥 − 𝑦𝑣𝑚𝑖𝑛 𝑦𝑤𝑚𝑎𝑥 − 𝑦𝑤𝑚𝑖𝑛
5
BITS Pilani, Hyderabad Campus
The transformation
Solving for  xv, yv  obtains:

xv  sx xw  t x , yv  s y yw  t y , where

xvmax  xvmin yvmax  yvmin


Scaling factors: sx  , sy 
xwmax  xwmin ywmax  ywmin

Translation factors:
xwmax xvmin  xwmin xvmax ywmax yvmin  ywmin yvmax
tx  , ty 
xwmax  xwmin ywmax  ywmin

6
BITS Pilani, Hyderabad Campus
Using Matrix notation

This can also be obtained by composing transformations:


M window, 
norm_viewport

 sx 0 tx 
T  xvmin , yvmin   S  sx , s y   T   xwmin ,  ywmin    0 sy t y 
 0 0 1 

7
BITS Pilani, Hyderabad Campus
Source:http://www.ocf.berkeley.edu/~fricke/projects/isr
ael/paeth/rotation_by_shearing.html

8
BITS Pilani, Hyderabad Campus
Affine Transformation

Affine transformation – transformed point P’ (x’,y’) is a


linear combination of the original point P (x,y), i.e.
x’ m11 m12 m13 x
y’ = m21 m22 m23 y
1 0 0 1 1

Translation, Scaling, Rotation, Shearing are all affine


transformation

9
BITS Pilani, Hyderabad Campus
The Inverse of an Affine Transformation

• Most affine transformations of interest are non


singular, which means that the determinant of M
which evaluates to
det M = m11 m22 –m12 m21
is non-zero.
• How to undo the effect of transformation?
• Let Q = M P then P = M-1 Q
• M-1 = 1/ det (M) x m22 -m12
-m21 m11
10
Elementary Inverse Transformation

11
Properties of 2D and 3D Affine
Transformations
Affine transformations preserve affine
combinations of points.
– W = a1P1 + a2P2 is an affine combination, where
a1+a2=1.
– MW = a1MP1 + a2MP2
Affine transformations preserve lines and planes.
– A line through A and B is L(t) = (1-t)A + tB, an affine
combination of points.
– A plane can also be written as an affine combination
of points: P(s, t) = sA + tB +(1 – s – t)C.

12
BITS Pilani, Hyderabad Campus
Properties of Affine transformation

• Any 2D affine transformation can be


decomposed into a rotation, followed by a
scaling, followed by a shearing, and followed
by a translation.
Affine matrix = translation x shearing x scaling x
rotation  a b
a b   1 0  R 0 
R
 c d    ac  bd 
1  0
ad  bc   R
b a
   R 2  R    
R R

Where R  a 2  b2

13
BITS Pilani, Hyderabad Campus
Properties of Affine
Transformation
Parallelism of lines and planes is preserved.
– Line A + bt having direction b transforms to the line
given in homogeneous coordinates by M(A + bt) =
MA + Mbt, which has direction vector Mb.
– Mb does not depend on point A. Thus two different
lines A1+ bt and A2 + bt that have the same direction
will transform into two lines both having the direction,
so they are parallel.
An important consequence of this property is that
parallelograms map into other parallelograms.

14
BITS Pilani, Hyderabad Campus
Properties of Affine Transformation

• Relative ratios are preserved: • The transformed point,


consider point P lying a T(P), lies the same
fraction t of the way between fraction t of the way
two given points, A and B (see between images T(A) and
figure). T(B).
• Apply affine transformation T
to A , B, and P.

15
BITS Pilani, Hyderabad Campus
Properties of Affine
Transformation
• How is the area of a figure affected by an affine
transformation?
• It is clear that neither translations nor rotations have any
effect on the area of a figure, but scalings certainly do,
and shearing might.
• The result is simple: When the 2D transformation with
matrix M is applied to an object, its area is multiplied by
the magnitude of the determinant of M:

area after transformation


 det M
area before transformation
16
BITS Pilani, Hyderabad Campus
Properties of Affine
Transformation
• In 2D the determinant of the matrix M is
(m11m22 – m12m21).

• For a pure scaling, the new area is SxSy times the


original area, whereas for a shear along one axis the
new area is the same as the original area.

17
BITS Pilani, Hyderabad Campus

Anda mungkin juga menyukai