3D 坐标系统

Coordinate Systems


Let’s consider now how we describe objects in three dimensions. Before you can specify an
object’s location and size, you need a frame of reference to measure and locate against.
When you draw lines or plot points on a simple flat computer screen, you specify a position in terms of a row and column. For example, a standard VGA screen has 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, or almost any 3D API, when you create a window to draw in, you must also
specify the coordinate system you want 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.


2D Cartesian Coordinates
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 is written as (0,0). Figure 1.17 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. This figure represents the
true Cartesian coordinate system pretty much as you used it in grade school. Today, differing window 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 (or two lines) that intersect at right angles define a plane. In a system with only two
axes, there is naturally only one plane to draw on.
                                         

FIGURE 1.17 The Cartesian plane.


Coordinate Clipping
 

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. You do this by specifying the region of Cartesian space
that occupies the window; this region is known as the clipping region. In two-dimensional
space, the clipping region 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 1.18 shows two common clipping regions.

 

                                       

 

FIGURE 1.18 Two clipping regions.
 

In the first example, on the left of Figure 1.18, x coordinates in the window range left to
right from 0 to +150, and the 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 bottom of the window. Although useful when drawing text from
top to bottom, this default mapping is not as convenient for drawing graphics

 

Viewports: Mapping Drawing Coordinates to Window Coordinates
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 is
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 want to draw only in the lower half of the
window.
Figure 1.19 shows a large window measuring 300x200 pixels with the viewport defined as
the entire client area. If the clipping area for this window were set to 0 to 150 along the xaxis and 0 to 100 along the y-axis, 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.
                                  

 

FIGURE 1.19 A viewport defined as twice the size of the clipping area.
 

In contrast, Figure 1.20 shows a viewport that matches the clipping area. The viewing
window is still 300x200 pixels, however, and this causes the viewing area to occupy the
lower-left side of the window.

                                

 

FIGURE 1.20 A viewport defined as the same dimensions as the clipping area.
 

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.
The Vertex—A Position in Space
In both 2D and 3D, when you draw an object, you actually compose it with several
smaller shapes called primitives. Primitives are one- or two-dimensional entities or surfaces
such as points, lines, and triangles that are assembled in 3D space to create 3D objects. For
example, a three-dimensional cube consists of six two-dimensional squares made of two
triangles each, 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
3D space. A vertex is nothing more than a coordinate in 2D or 3D space. Creating solid 3D
geometry is little more than a game of connect-the-dots! You learn about all the OpenGL
primitives and how to use them in Chapter 3, “Basic Rendering.”
3D Cartesian Coordinates
Now, we extend our two-dimensional coordinate system into the third dimension and add
a depth component. Figure 1.21 shows the Cartesian coordinate system with a new axis, z.
The z-axis is perpendicular to both the x- and y-axes. It represents a line drawn perpendicularly from the center of the screen heading toward the viewer. (We have rotated our view
of the coordinate system from Figure 1.17 to the left with respect to the y-axis and down
and back with respect to the x-axis. If we hadn’t, the z-axis would come straight out at
you, and you wouldn’t see it.) Now, we specify a position in three-dimensional space with
three coordinates: x, y, and z. Figure 1.21 shows the point (–4,4,4) for clarification
                                         

FIGURE 1.21 Cartesian coordinates in three dimensions.
Projections: Getting 3D to 2D
You’ve seen how to specify a position in 3D space using Cartesian coordinates. No matter
how we might convince your eye, however, pixels on a screen have only two dimensions.
How does OpenGL translate these Cartesian coordinates into two-dimensional coordinates
that can be plotted on a screen? The short answer is “trigonometry and simple matrix
manipulation.” Simple? Well, not really; we could actually go on for many pages explaining this “simple” technique and lose most of our readers who didn’t take or don’t remember their linear algebra from college. You learn more about it in Chapter 4, and for a
deeper discussion, you can check out the references in Appendix A, “Further Reading”
Fortunately, you don’t need a deep understanding of the math to use OpenGL to create
graphics. You might, however, discover that the deeper your understanding goes, the more
powerful a tool OpenGL becomes!
The first concept you really need to understand is called projection. The 3D coordinates you
use to create geometry are flattened or projected onto a 2D surface (the window background). It’s like tracing the outlines of some object behind a piece of glass with a black
marker. When the object is gone or you move the glass, you can still see the outline of the
object with its angular edges. In Figure 1.22, a house in the background is traced onto a
flat piece of glass. By specifying the projection, you specify the viewing volume that you
want displayed in your window and how it should be transformed.
                           

 

FIGURE 1.22 A 3D image projected onto a 2D surface.
Orthographic Projections
You are mostly concerned with two main types of projections in OpenGL. The first is
called an orthographic, or parallel, projection. You use this projection by specifying a square
or rectangular viewing volume. Anything outside this volume is not drawn. Furthermore,
all objects that have the same dimensions appear the same size, regardless of whether they
are far away or nearby. This type of projection (shown in Figure 1.23) is most often used
in architectural design, computer-aided design (CAD), or 2D graphs. Frequently, you also
use an orthographic projection to add text or 2D overlays on top of your 3D graphic
scenes.
                               

FIGURE 1.23 The clipping volume for an orthographic projection.


You specify the viewing volume in an orthographic projection by specifying the far, near,
left, right, top, and bottom clipping planes. Objects and figures that you place within this
viewing volume are then projected (taking into account their orientation) to a 2D image
that appears on your screen.
 

Perspective Projections
The second and more common projection is the perspective projection. This projection adds
the effect that distant objects appear smaller than nearby objects. The viewing volume (see
Figure 1.24) is something like a pyramid with the top shaved off. The remaining shape is
called the frustum. Objects nearer to the front of the viewing volume appear close to their
original size, but objects near the back of the volume shrink as they are projected to the
front of the volume. This type of projection gives the most realism for simulation and 3D
animation.
                               

FIGURE 1.24 The clipping volume (frustum) for a perspective projection.
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值