You are on page 1of 21

Computer Graphics

Window to Viewport Transformations


Compositing Transformations

Does order matter?

Case 1: translate by (2, 0), scale by (2, 2)

Case 2: scale by (2, 2), translate by (-2, 0)

Begin: red, 1st transform: prple, 2nd: green


1
1
1
]
1

1 0 0
0 1 0
2 0 1
T
1
1
1
]
1

1 0 0
0 2 0
0 0 2
S
Y
X
1,1
!,1
2,!
Y
X
1,1
!,1
2,!
Case 1(translate t"en scale)
Case 2 (scale t"en translate)
Compositing Transformations

Does order matter?

Case 1: translate by (2, 0), sale by (2, 2)

Case 2: sale by (2, 2), translate by (!2, 0)

"e#in: red, 1st transform: p$rple, 2nd: #reen


1
1
1
]
1

1 0 0
0 1 0
2 0 1
T
1
1
1
]
1

1 0 0
0 2 0
0 0 2
S
Y
X
1,1
!,1
2,!
Y
X
1,1
!,1
2,!
Case 1(translate t"en scale)
Case 2 (scale t"en translate)
-1,1
0,3
-1,1
0,6
-2,2
2,2
2,2
6,2
4,6
0,2
4,2
2,6
Composition Example
STP P

TSP P

1
1
1
]
1

1 0 0
0 2 0
0 0 2
1
1
1
]
1

1
1
1
]
1


1 0 0
0 2 0
# 0 2
1 0 0
0 1 0
2 0 1
1
1
1
]
1


1 0 0
0 1 0
2 0 1
1
1
1
]
1

1
1
1
]
1

1 0 0
0 2 0
2 0 2
1 0 0
0 2 0
0 0 2
Scale(2.0,2.0);
Translate(-2.0,0.0);
drawTriangle();
Translate(-2.0, 0.0);
Scale(2.0,2.0);
drawTriangle();
$n general, transformations are not commtati%e
%eed to transform points from &world' (iew (window)
to t)e sreen (iew (viewport)

*aintain relati(e plaement of points ($s$ally)

Can be done wit) a translate!sale!translate se+$ene


Window-to-Viewport Transform
x
y
-3
-2
-1
0
1
2
3
-4 -3 -2 -1 1 2 3 4
u
v
0 10 20 30 40 50 60 70 80
10
20
!0
#0
&0
'0
0
(indo) (*)orld+) ,ie)port (screen)

&Window' refers to t)e area in &world spae' or &world


oordinates' t)at yo$ wis) to pro,et onto t)e sreen-

.oation, $nits, si/e, et- are all determined by t)e appliation,


and are on(enient for t)at appliation-

0nits o$ld be in)es, feet, meters, 1ilometers, li#)t years, et-

T)e window is often entered aro$nd t)e ori#in, b$t need not be

2peified as (3,y) oordinates


Window
x
y
-3
-2
-1
0
1
2
3
-4 -3 -2 -1 1 2 3 4
(indo) (*)orld+)
(-min, ymin)
(-ma-, yma-)

T)e area on t)e sreen t)at yo$ will map t)e window to

2peified in &sreen oordinates' ! ($,() oordinates

T)e (iewport an ta1e $p t)e entire sreen, or ,$st a


portion of it
Viewport
u
v
0 10 20 30 40 50 60 70 80
10
20
!0
#0
&0
'0
0
,ie)port (screen)
(min, %min)
(ma-, %ma-)

4o$ an )a(e m$ltiple (iewports

T)ey an ontain t)e same (iew of a window, different (iews of t)e same
window, or different (iews of different windows
Viewport (cont)
u
v
0 10 20 30 40 50 60 70 80
10
20
!0
#0
&0
'0
0
,ie)port (screen)

T)e window!to!(iewport transform is:


1- Translate lower!left orner of
window to ori#in
2- 2ale widt) and )ei#)t of
window to mat) (iewport5s
6- Translate orner at ori#in to
lower!left orner in (iewport
Window-to-Viewport Transform
(cont.)
x
y
-3
-2
-1
0
1
2
3
-4 -3 -2 -1 1 2 3 4
v
u
0 10 20 30 40 50 60 70 80
0
10
20
30
40
50
60
(indo) (*)orld+) ,ie)port (screen)
1
]
1

min
min

y
x
1
]
1

min
min

v
u
1
]
1

ma-
ma-

v
u
1
]
1

ma-
ma-

y
x
min min
and y t x t
y x

min ma-
min ma-
min ma-
min ma-
and
y y
v v
s
x x
u u
s
y x

min min
and v t u t
y x

Window-to-Viewport Transform
(cont.)

T)e final window!to!(iewport transform is:


( ) ( )
1
1
1
1
1
1
1
]
1

1
1
1
]
1

1
1
1
1
1
1
1
]
1

1
1
1
]
1

,
_


1 0 0
0
0
1 0 0
1 0
0 1
1 0 0
0 0
0 0
1 0 0
1 0
0 1
min
min ma-
min ma-
min
min ma-
min ma-
min
min ma-
min ma-
min
min ma-
min ma-
min
min
min ma-
min ma-
min ma-
min ma-
min
min
min min
min ma-
min ma-
min ma-
min ma-
min min
v
y y
v v
y
y y
v v
u
x x
u u
x
x x
u u
y
x
y y
v v
x x
u u
v
u

,-y -x T
-y y
-v v
,
x x
u u
S ,v u T M
WV
Window-to-Viewport Transform
(cont.)

*$ltiplyin# t)e matri3 Mwv by t)e point p #i(es:


( )
( )
1
1
1
1
1
1
1
1
1
1
1
]
1

1
.
min
min ma-
min ma-
min
min
min ma-
min ma-
min
v
y y
v v
y y
u
x x
u u
x x
p

%ow we need to pl$# t)e (al$es into t)e e+$ation:


Window-to-Viewport Example
( )
( )
1
1
1
1
1
1
1
1
1
1
1
]
1

1
.
min
min ma-
min ma-
min
min
min ma-
min ma-
min
v
y y
v v
y y
u
x x
u u
x x
p
x
y
-3
-2
-1
0
1
2
3
-4 -3 -2 -1 1 2 3 4
v
u
0 10 20 30 40 50 60 70 80
0
10
20
30
40
50
60
(indo) (*)orld+) ,ie)port (screen)
1
]
1

min
min

y
x
1
]
1

min
min

v
u
1
]
1

ma-
ma-

v
u
1
]
1

ma-
ma-

y
x

Window: (3min, ymin) 7 (!6, !6), (3ma3, yma3) 7 (2 , 1)

Viewport: ($min, (min) 7 (60, 10), ($ma3, (ma3) 7 (80,


60)
Window-to-Viewport Example
x
y
-3
-2
-1
0
1
2
3
-4 -3 -2 -1 1 2 3 4
v
u
0 10 20 30 40 50 60 70 80
0
10
20
30
40
50
60
(indo) (*)orld+) ,ie)port (screen)
1
]
1

min
min

y
x
1
]
1

min
min

v
u
1
]
1

ma-
ma-

v
u
1
]
1

ma-
ma-

y
x

9l$##in# t)e (al$es into t)e e+$ation:


Window-to-Viewport Example
( )
( )
1
1
1
1
1
1
1
1
1
1
1
]
1

1
.
min
min ma-
min ma-
min
min
min ma-
min ma-
min
v
y y
v v
y y
u
x x
u u
x x
p
( )
( )
1
1
1
1
1
1
1
1
1
1
1
]
1

1
10
) ! ( 1
10 !0
) ! (
!0
) ! ( 2
!0 /0
) ! (
. y
x
p
1
1
1
1
1
1
1
1
]
1

+ +
+ +

1
10
#
) ! (
!0
&
&0
) ! (
20
y
x
1
1
1
]
1

+ +
+ +

1
10 & ) ! (
!0 10 ) ! (
y
x
1
1
1
]
1

+
+

1
2& &
'0 10
y
x

2o: Tryin# some points:


Window-to-Viewport Example
1
1
1
]
1

+
+

1
2& &
'0 10
. y
x
p
(-min, ymin) 0 (-!, -!) > (!0, 10)
(-ma-, yma-) 0 (2, 1) >
1eft eye 0 (-1, -2/) >
3op of "ead 0 (-02&, 02&) >
x
y
-3
-2
-1
0
1
2
3
-4 -3 -2 -1 1 2 3 4
v
u
0 10 20 30 40 50 60 70 80
0
10
20
30
40
50
60
(indo) (*)orld+) ,ie)port (screen)
(&&, 242&)
(&0, 21)
(/0, !0)

W)at if t)e (iewport ori#in is top!left?


0se t)e same e+$ation, b$t s$btrat t)e res$lts from
((ma3 : (min)
Window-to-Viewport Transform
(cont.)
x
y
-3
-2
-1
0
1
2
3
-4 -3 -2 -1 1 2 3 4
u
v
0 10 20 30 40 50 60 70 80
60
50
40
30
20
10
0
(indo) (*)orld+) ,ie)port (screen)
1
]
1

min
min

y
x
1
]
1

min
min

v
u
1
]
1

ma-
ma-

v
u
1
]
1

ma-
ma-

y
x

;f (min 7 0, we re(erse t)e res$lts of o$r e+$ation by


s$btratin# it from (ma3, i-e-, (ma3 !< 0 and 0 !<
(ma3
Rationale
min
v
v
u
0 10 20 30 40 50 60 70 80
0
10
20
30
40
50
60
Viewport
ma-
v
) , (
point
arbitrary
v u p
u
v
0 10 20 30 40 50 60 70 80
60
50
40
30
20
10
0
,ie)port (screen)
v
ma-
v
v v
ma-
3"is gi%es s t"e correct offset from t"e ne) u a-is
v v
ma-
;f vmin was not 0, t)en we need
a) t)e offset inside t)e (iewport
b) t)e offset from t)e u a3is
Rationale (cont)
min
v
v
u
0 10 20 30 40 50 60 70 80
0
10
20
!0
#0
&0
'0
,ie)port
ma-
v
) , (
point
arbitrary
v u p
u
v
0 10 20 30 40 50 60 70 80
60
50
40
30
20
10
0
,ie)port
v v
ma-
5o t"e correct offset is vma- v 6 vmin 0 vma- 6 vmin v
v v
ma-
min
v
min
v
vma- - v
vmin

Window: (3min, ymin) 7 (!6, !6), (3ma3, yma3) 7 (2 , 1)

Viewport: ($min, (min) 7 (60, 10), ($ma3, (ma3) 7 (80,


60)
Window-to-Viewport Example -
downward pointing !-axis
x
y
-3
-2
-1
0
1
2
3
-4 -3 -2 -1 1 2 3 4
(indo) (*)orld+)
1
]
1

min
min

y
x
1
]
1

ma-
ma-

y
x
u
v
0 10 20 30 40 50 60 70 80
60
50
40
30
20
10
0
,ie)port (screen)
1
]
1

min
min

v
u
1
]
1

ma-
ma-

v
u

T)e e+$ation for t)is sit$ation is:


Window-to-Viewport Example -
downward pointing !-axis
( ) ( )
1
1
1
]
1

+
+

1
1
1
]
1

+ +
+

1
1
1
]
1

+ +
+

1
1& &
'0 10
1
2& & 10 !0
'0 10
1
2& &
'0 10
.
min ma-
y
x
y
x
y v v
x
p

2o: Tryin# some points:


Window-to-Viewport Example -
downward pointing !-axis
1
1
1
]
1

+
+

1
1& &
'0 10
. y
x
p
(-min, ymin) 0 (-!, -!) > (!0, !0)
(-ma-, yma-) 0 (2, 1) >
1eft eye 0 (-1, -2/) >
3op of "ead 0 (-02&, 02&) >
x
y
-3
-2
-1
0
1
2
3
-4 -3 -2 -1 1 2 3 4
(indo) (*)orld+) ,ie)port (screen)
(&&, 122&)
(&0, 17)
(/0, 10)
u
v
0 10 20 30 40 50 60 70 80
60
50
40
30
20
10
0