0% found this document useful (0 votes)
8 views7 pages

3D Programming Basics Explained

The document introduces basic principles of 3D programming, focusing on two approaches: retained mode and immediate mode for rendering graphics. It explains coordinate systems, including 2D Cartesian coordinates and their extension to 3D, as well as the concept of projections for displaying 3D objects on 2D screens. Key topics include viewport mapping, coordinate clipping, and orthographic projections used in various applications.

Uploaded by

mare13136161
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

3D Programming Basics Explained

The document introduces basic principles of 3D programming, focusing on two approaches: retained mode and immediate mode for rendering graphics. It explains coordinate systems, including 2D Cartesian coordinates and their extension to 3D, as well as the concept of projections for displaying 3D objects on 2D screens. Key topics include viewport mapping, coordinate clipping, and orthographic projections used in various applications.

Uploaded by

mare13136161
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic 3D Programming Principles

Now, you have a pretty good idea of the basics of real-time 3D. We’ve covered some termi-
nology and some sample applications on the PC. How do you actually create these images
on your PC? Well, that’s what the rest of this book is about! You still need a little more
introduction to the basics, which we present here.

Immediate Mode and Retained Mode


There are two different approaches to low-level programming APIs for real-time 3D
graphics—both of which are well supported by OpenGL. The first approach is called
retained [Link] retained mode, you provide the API or toolkit with higher level geometric
descriptions of your objects in the scene. These blocks of geometry data can be transferred
quickly to the graphics hardware, or even stored directly in the hardware’s local memory
for faster access.

The second approach to 3D rendering is calledimmediate [Link] immediate mode, you


procedurally build up geometric objects one piece at a time. Although flexible, this suffers
performance-wise. We will discuss why this happens and ways to get around it in Chapter
11, “It’s All About the Pipeline: Faster Geometry Throughput.”
With both immediate mode and retained mode, new commands have no effect on render-
ing commands that have already been executed. This gives you a great deal of low-level
control. For example, you can render a series of textured unlit polygons to represent the
sky. Then you issue a command to turn off texturing, followed by a command to turn on
lighting. Thereafter, all geometry (probably drawn on the ground) that you render is
affected by the light but is not textured with the sky image.

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 posi-
tion 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 thecoordinate systemyou 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 coor-
dinate 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 originof the Cartesian system is at x=0, y=0. Cartesian coordinates are written as coor-
dinate pairs in parentheses, with the x coordinate first and the y coordinate second, sepa-
rated by a comma. For example, the origin is written as (0,0). Figure 1.21 depicts the
Cartesian coordinate system in two dimensions. The x and y lines with tick marks are
called theaxesand 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, differ-
ing 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. Aplaneis, 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.
25

1
FIGURE 1.21 The Cartesian plane.

Coordinate Clipping
A windowis 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 coordi-
nate 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.22 shows two commonclipping regions.

+y +y

– +
100 50
Window
– +
client
area Window client
–x +x –x –x +x
(0,0) –75 area +75
150
–50

–y –y

FIGURE 1.22 Two clipping regions.


26

In the first example, on the left of Figure 1.22, 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


Rarelywill 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 [Link] 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.23 shows a large window measuring 300 200 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 x-
axis 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.23 A viewport defined as twice the size of the clipping area.
27

Incontrast, Figure 1.24 shows a viewport that matches the clipping area. The viewing
window is still 300200 pixels, however, and this causes the viewing area to occupy the
lower-left side of the window.

1
FIGURE 1.24 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 both2D and 3D, when you draw an object, you actually compose it with several
smaller shapes called [Link] are one- or two-dimensional entities or 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 consists of six two-
dimensional squares, each placed on a separate face. Each corner of the square (or of any
primitive) is called [Link] vertices are then specified to occupy a particular coordi-
nate 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 connect-the-dots
of ! You’ll learn about all the
OpenGL primitives and how to use them in Chapter 3, “Drawing in Space: Geometric
Primitives and Buffers.”

3D Cartesian Coordinates
Now, we extend our two-dimensional coordinate system into the third dimension and add
a depth component. Figure 1.25 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 perpendic-
ularly from the center of the screen heading toward the viewer. (We have rotated our view
of the coordinate system from Figure 1.21 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.25 shows the point (–4,4,4) for clarification.
28

+y

(–4,4,4)
–z
–x

+x

+z
–y

FIGURE 1.25 Cartesian coordinates in three dimensions.

Projections: Getting 3D to 2D
You’veseen 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 explain-
ing this “simple” technique and lose most of our readers who didn’t take or don’t remem-
ber their linear algebra from college. You’ll learn more about it in Chapter 4, “Geometric
Transformations: The Pipeline,” and for a deeper discussion, you can check out the refer-
ences in Appendix A, “Further Reading/References.” 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 The 3D coordinates you
projection.
use to create geometry are flattenedprojected
or onto a 2D surface (the window back-
ground). 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.26, a house in the background is traced onto a
flat piece of glass. By specifying the projection, you specify viewing
the that you
volume
want displayed in your window and how it should be transformed.
Basic 3D Programming Principles 29

1
3D scene

2D image

FIGURE 1.26 A 3D image projected onto a 2D surface.

Orthographic Projections
Youare mostly concerned with two main types of projections in OpenGL. The first is
called anorthographic,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.27) is most often used
in architectural design, computer-aided design (CAD), or 2D graphs. Frequently, you will
also use an orthographic projection to add text or 2D overlays on top of your 3D graphic
scenes.

FIGURE 1.27 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.

You might also like