Graphics Reference
In-Depth Information
Tutorial 9.5. Transforming Individual Primitives
Tutorial 9.5.
Project Name:
D3D _ PerPrimitiveTrans
Library Support:
UWB _ MFC _ Lib1
UWB _ D3D _ Lib8
Goals. Experience applying different transformations to different primi-
tives; understand that we can load and change the matrix processors during
each redraw of the application window.
Approach. Work with two primitives and allow the user to specify different
transforms for each primitive; examine how we support programming the
setting of different transformations.
Figure 9.16 is a screenshot of running Tutorial 9.5. From manipulating the slider
bars, we can verify that the translation slider bars control the positions of both of
the rectangles, whereas the scale/rotate slider bars control only one of the rectan-
gles. In this case, the vertices of the two rectangles, R 1 and R 2 ,are
Figure 9.16.
Tutorial
=(
,
) ,
=(
,
) ,
V a
0
0
V e
5
5
9.5.
V b =(
2
,
0
) ,
V f =(
8
,
5
) ,
R 1 vertices:
R 2 vertices:
V c =(
2
,
3
) ,
V g =(
8
,
6
) ,
V d =(
0
,
3
) ,
V h =(
5
,
6
) .
Notice that in both cases, the scaling and rotation is performed with respect to the
center of the rectangles. At this point, we can derive the two transformations for
the two rectangles to be
M 1 =
T
(
p 1
.
x
,−
p 1
.
y
)
S
(
s x ,
s y )
T
(
p 1
.
x
,
p 1
.
y
)
T
(
t x ,
t y ) ,
M 2 =
T
(
p 2
.
x
,−
p 2
.
y
)
R
( θ )
T
(
p 2
.
x
,
p 2
.
y
)
T
(
t x ,
t y ) ,
=(
,
.
)
=(
.
,
.
)
where p 1
are the center (pivot) positions of the
rectangles. We see that in this case, we must apply different transformation oper-
ators to each of the rectangles. The solution structure of this tutorial is identical
to the others from this chapter except for the CModel class. Listing 9.6 shows
the relevant definition of the CModel class. As in all previous tutorials from this
chapter, the slider bars' event service routines call the functions defined at label A
to set the parameters for transformation (defined at label C). The two rectangles
are defined at label B. Listing 9.7 shows the details of transformation setup and
drawing. At label A, we initialize the top of the matrix stack and save a copy of
the identity matrix. At label B, we concatenate the translation operator, T
1
1
5
and p 2
6
5
5
5
,
to the top of the matrix stack. Since both M 1 and M 2 need this operation, at la-
bel C1 we duplicate and push a copy of this matrix onto the stack. At this point,
there are three elements on the matrix stack: an initial identity matrix saved by
the Push() operation at label A and two copies of the TranslateLocal() ma-
trix resulting from the Push() operation at label C1. At label D, we proceed to
(
t x ,
t y )
Search WWH ::




Custom Search