46 CHAPTER 2: All That Math Jazz^
With these boundaries established, the one final transformation is that to the viewport,
OpenGL’s version of your screen. This is the point where OpenGL is fed the screen’s
dimensions, those of your display area, and the origin, which is likely the lower-left
corner of the screen. On small devices such as the iPhone or iPad, you will likely fill up
the entire screen and so will use the screen’s width. But should you want to place the
image into a subwindow of the main display, you could simply pass smaller values to the
viewport. The law of similar triangles plays out here.
In Figure 2-11 we want to find what the projected x’ is, given the x of an arbitrary vertex
on the model. Consider two triangles, one formed by the corners CBA and the other
smaller one by COA’ ( the O is for origin). The distance from C (where the eye is, to O is
d). The distance from C to B is d+z. So, just taking the ratio of those, as follows:
eye z deye
x
d
x
+
=
′
and
eye z deye
y
d
y
+
=
'
yields the following:
eye
eye
z d
xd
x
+
'= and
eye
eye
z d
yd
y
+
'= =
Figure 2-11. Mapping a vertex to the viewport using the Law of Similar Triangles
Figure 2-12 shows the final translations. Those can be added to x’ and y’:
x
eye
eye
T
z d
xd
x +
+
'= and y
eye
eye
T
z d
yd
y +
+
'=