Now that you know how the eye can perceive three dimensions on a two-dimensional surface (the computer screen), let’s consider how to draw these objects on the screen. When you draw points, lines, or other shapes on the computer screen, you usually specify a position in terms of a row and column. For example, on a standard VGA screen there are 640 pixels from left to right, and 480 pixels from top to bottom. To specify a point in the middle of the screen, you specify that a point should be plotted at (320,240)—that is, 320 pixels from the left of the screen and 240 pixels down from the top of the screen.
In OpenGL, when you create a window to draw in, you must also specify the coordinate system you wish to use, and how to map the specified coordinates into physical screen pixels. Let’s first see how this applies to two-dimensional drawing, and then extend the principle to three dimensions.
The most common coordinate system for two-dimensional plotting is the Cartesian coordinate system. Cartesian coordinates are specified by an x coordinate and a y coordinate. The x coordinate is a measure of position in the horizontal direction and y is a measure of position in the vertical direction.
The origin of the Cartesian system is at x=0, y=0. Cartesian coordinates are written as coordinate pairs, in parentheses, with the x coordinate first and the y coordinate second, separated by a comma. For example, the origin would be written as (0,0). Figure 2-7 depicts the Cartesian coordinate system in two dimensions. The x and y lines with tick marks are called the axes and can extend from negative to positive infinity. Note that this figure represents the true Cartesian coordinate system pretty much as you used it in grade school. Today, differing Windows mapping modes can cause the coordinates you specify when drawing to be interpreted differently. Later in the book, you’ll see how to map this true coordinate space to window coordinates in different ways.
The x-axis and y-axis are perpendicular (intersecting at a right angle) and together define the xy plane. A plane is, most simply put, a flat surface. In any coordinate system, two axes that intersect at right angles define a plane. In a system with only two axes, there is naturally only one plane to draw on.
A window is measured physically in terms of pixels. Before you can start plotting points, lines, and shapes in a window, you must tell OpenGL how to translate specified coordinate pairs into screen coordinates. This is done by specifying the region of Cartesian space that occupies the window; this region is known as the clipping area. In two-dimensional space, the clipping area is the minimum and maximum x and y values that are inside the window. Another way of looking at this is specifying the origin’s location in relation to the window. Figure 2-8 shows two common clipping areas.
In the first example, on the left of Figure 2-8, x coordinates in the window range left to right from 0 to +150, and y coordinates range bottom to top from 0 to +100. A point in the middle of the screen would be represented as (75,50). The second example shows a clipping area with x coordinates ranging left to right from –75 to +75, and y coordinates ranging bottom to top from –50 to +50. In this example, a point in the middle of the screen would be at the origin (0,0). It is also possible using OpenGL functions (or ordinary Windows functions for GDI drawing) to turn the coordinate system upside-down or flip it right to left. In fact, the default mapping for Windows windows is for positive y to move down from the top to the bottom of the window. Although useful when drawing text from top to bottom, this default mapping is not as convenient for drawing graphics.
Rarely will your clipping area width and height exactly match the width and height of the window in pixels. The coordinate system must therefore be mapped from logical Cartesian coordinates to physical screen pixel coordinates. This mapping is specified by a setting known as the viewport. The viewport is the region within the window’s client area that will be used for drawing the clipping area . The viewport simply maps the clipping area to a region of the window. Usually the viewport is defined as the entire window, but this is not strictly necessary—for instance, you might only want to draw in the lower half of the window.
Figure 2-9 shows a large window measuring 300 x 200 pixels with the viewport defined as the entire client area. If the clipping area for this window were set to be 0 to 150 along the x-axis and 0 to 100 along the y-axis, then the logical coordinates would be mapped to a larger screen coordinate system in the viewing window. Each increment in the logical coordinate system would be matched by two increments in the physical coordinate system (pixels) of the window.
In contrast, Figure 2-10 shows a viewport that matches the clipping area. The viewing window is still 300 x 200 pixels, however, and this causes the viewing area to occupy the lower-left side of the window.
You can use viewports to shrink or enlarge the image inside the window, and to display only a portion of the clipping area by setting the viewport to be larger than the window’s client area.
In both 2D and 3D, when you draw an object you will actually compose it with several smaller shapes called primitives. Primitives are two-dimensional surfaces such as points, lines, and polygons (a flat, multisided shape) that are assembled in 3D space to create 3D objects. For example, a three-dimensional cube like the one in Figure 2-5 is made up of six two-dimensional squares, each placed on a separate face. Each corner of the square (or of any primitive) is called a vertex. These vertices are then specified to occupy a particular coordinate in 2D or 3D space. You’ll learn about all the OpenGL primitives and how to use them in Chapter 6.