Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Drawing with Direct3D, Part 1: Getting Started

Learn how to produce amazing, high-performance, three-dimensional pictures in Visual Basic or C# by using Direct3D.


advertisement
irect3D is Microsoft's high-performance, three-dimensional graphics library. Using Direct3D, you can build amazing scenes containing hundreds or even thousands of three-dimensional shapes, three-dimensional charts and graphs, surfaces that change in real time, and even arcade-style action games. In fact, Direct3D is the graphical part of XNA (which believe it or not stands for "XNA's Not Acronymed"), Microsoft's game platform development environment for Windows and Xbox 360. You can find more information on XNA at the XNA Developer Center. Direct3D is a huge topic, so this is the first in a series of articles. This article explains how to get started with Direct3D—how to download and install the Direct3D SDK, create a Direct3D device, and draw three-dimensional objects on the device. You'll see how to color the objects explicitly and how to remove surfaces that should be hidden. Later articles will explain such topics as textures, shading, and game-related topics.

So if you're ready to produce some remarkable three-dimensional graphics with amazing performance, grab some caffeine, pull up a high-speed Internet connection, and read on. Understanding Direct3D
The Direct3D library is designed to let you display three-dimensional graphics relatively easily, and optimized to use your computer's hardware where possible—so it gives pretty good performance and can display and animate fairly complicated scenes in real time.

Before you can draw three-dimensional objects, you need to understand how to define the data for those objects in three-dimensional space, and how Direct3D processes that data. (Strictly speaking, you can build a Direct3D device and display a blank window without understanding that stuff, but if you learn it now you can move quickly from creating the device to drawing something without any distractions. I'm going to start here.) Direct3D manipulates information about objects in three dimensions. Each point has X, Y, and Z coordinates that determine its position in the data space. That space is called world coordinate space.



To understand how coordinates are oriented, imagine that your computer's screen is a window into world coordinate space. As you look through the window, the X-axis increases to the right and the Y-axis increases upward. So far this is just how you would draw a graph in a mathematics class. In Direct3D, the Z-axis projects away from you into the monitor. Direct3D uses a "left-handed coordinate system." That means if you position your left hand so your fingers point along the X-axis (palm up with fingers pointed right), and then curl your fingers toward the Y-axis (upward), then your thumb points along the Z-axis as shown in Figure 1. (If you need to curl your fingers so they bend backwards, you have your hand upside down. Flip it over and you should be okay.)

 
Figure 1. Left-handed Coordinates: Using left-handed coordinates, if you relax your hand and point your fingers along the X-axis, and then curl them toward the Y-axis, your thumb points roughly along the Z-axis.
Keeping track of which directions the axes point can be important, particularly later when you learn how to draw scenes that contain hidden surfaces. It's often helpful to sketch out the points you are trying to draw on paper before you start trying to plug in their coordinates. Direct3D draws points, lines, and triangles. If you want to build something more complex, such as a pentagon or cube, you must build it out of more primitive objects. For example, you can make a square with two triangles that share an adjacent edge. As long as the two triangles that make up the square have the same color, they will merge seamlessly, so the user won't see an edge between them. After you know how to build a square, you can use six squares to build a cube.

There are several ways you can specify an object's color. For example, you can explicitly define the colors that a triangle should have at each of its corners. Direct3D will interpolate across the triangle's surface to generate colors for the interior. Another option is to specify a texture map to make Direct3D map pixels in an image onto the triangle. A third way to specify color is with a lighting model. Here you give Direct3D information about the "material" that makes up a triangle and you define light sources in three-dimensional space. Direct3D then uses some calculations to figure out what color the object should be based on its relationships with the light sources and with the current viewing position (basically where your "eye" or "camera" is sitting in space looking at the object). Assuming you know where your objects are and what colors they should be (don't worry, I'll provide more detail later), you should understand how Direct3D displays three-dimensional objects on a two-dimensional screen. To map the data from three to two dimensions, Direct3D uses three transformations.

The first transformation, called the world transformation, transforms the data in world coordinate space. The world transformation lets you modify the data before it is displayed. For example, a world transformation might rotate your objects around an axis or stretch the data in one or more directions. Of course, you can leave the data untransformed if you don't want to make any last-minute changes to the way it will appear. The second transformation, called the view transformation, adjusts the data for the particular viewing position that you want to use when displaying the data. You can think of the viewing position as a camera sitting in space looking at the objects. This transformation determines where the camera is and the direction in which it is looking. It also defines an "up" direction for the camera, because the camera might be oriented normally, rotated, or even upside down. In essence, this transformation flips your objects around so they are right-side up as determined by the camera's position. The view transformation converts the data from world coordinates into camera coordinates.

The final transformation, called the projection transformation, maps the data from three-dimensional space into two-dimensional coordinates suitable for display on your monitor. Typically, this perspective projection makes objects closer to the camera appear larger than those farther away, just as they do in a real photograph.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap