Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
Alternatives/Enhancements There are hundreds, and probably thousands, of ways to map a texture onto a geometric shape. Texture mapping is an integral part of 3-D programming, unless you want your models to look like flat colors. Full 3-D texture mapping is beyond the scope of this book, but the idea behind it remains the same. Using the lighting techniques described earlier, the spinning planet could be made more realistic by modifying the palette and drawing part of the texture using darker colors, creating a “night” effect. Additionally, you could use a second texture for the clouds, and use transparency techniques to make the clouds appear to move around while the planet stays still, or at least moves at a different rate. Alternatively, a bitmap with text could be used to create an interesting marquee. ### Dynamic Images The ability to mold and shape a bitmap allows us to produce some pretty spectacular special effects. Creating an image on the fly can produce even more dramatic effects. Blitting sprite images on top of a background certainly creates a new image on the fly, but what we’re concerned with here is creating an image based on some sort of algorithm or mathematical function as opposed to just creating a composited layer of bitmaps. You could create dynamic images such as this in any manner imaginable. Fractal imaging comes to mind, which uses various mathematical equations to produce stunning graphics of everything from abstract forms to images that model natural objects, such as leaves or mountains, in an incredibly lifelike way. Full 3-D games use collections of points and polygons to produce realistic gaming environments. While this list could go on and on, we’ll look at two methods for creating some pretty cool special effects: 3-D star fields and fire. #### Star Fields Everyone has seen the Windows star field screen saver. At a fast enough velocity, it resembles something that you would see on the bridge of the Enterprise going at warp speed. Science fiction-based games are quite popular, and may benefit from the ability to produce a cool warp-speed star field animation that could be used when traveling from system to system. Basic Theory This technique makes use of some very basic 3-D concepts. Specifically, each star is tracked as a point in 3-D space. Before we discuss how the stars are moved and how they are subsequently drawn to the screen, we must first understand some very basic 3-D graphics principles. In a 2-D world, for which most of the examples in this book have been written, coordinates are typically based off of a flat grid. In mathematical terms, this can be considered a Cartesian grid, where the y-axis represents vertical placement and the x-axis represents horizontal placement. The origin, or where these two axes meet, is where the x-axis and y-axis are both at 0. The y-axis increases as you move upward and decreases as you move downward. Similarly, the x-axis increases to the right and decreases to the left. (For the moment, we’ll ignore the fact that on a computer screen, the origin is in the upper left-hand corner and y increases as you move downward.) The figure at the top of the following page demonstrates a typical Cartesian coordinate system. Using this system, simple 2-D sprites can be tracked by a simple x and y coordinate (as we’ve been doing throughout this book). They can move left and right or up and down, but this system has no method for tracking depth, or which sprites are on top of each other. Figure 11-11: A Cartesian coordinate system To track depth, we need to add another axis. This is known as the z-axis, and in concept, it is perpendicular to the x and y axes. You can think of it as another axis that runs straight into the computer screen. The z-axis increases as you go “into” the computer screen, and decreases as you come “out of” the computer screen. The following figure demonstrates this concept. Figure 11-12: A 3-D coordinate system A star can be effectively tracked as a single point, so one x, y, z coordinate will represent a single star. If we now initialize a star at a specific coordinate, and then modify its z coordinate, it will appear to move toward or away from the user as desired. The values for each axis in a system such as this are somewhat arbitrary. In our example, each axis can range from –1000 to 1000. Stars can be positioned anywhere within this square area. Now, even though we’ve got an acceptable method of tracking star positions, we still can’t draw them to the screen. How do we convert these arbitrary axis positions into actual screen coordinates that we can draw? This is where perspective projection comes in. The equations are very simple, and will produce a correct 2-D coordinate for a 3-D point that will accurately reflect the perspective in relation to its depth on the z-axis. Skipping the lengthy derivation and proofs for producing these equations, they are:
    Screen_X := XPos / ZPos * VIEWDISTANCE;

    Screen_Y := YPos / ZPos * VIEWDISTANCE;
Dividing the horizontal and vertical positions by the depth is what gives us the correct perspective. The viewing distance constant is required because without it, the origin of the z-axis is effectively the glass of the computer screen. By multiplying by a constant viewing distance, we effectively push the origin of the z-axis into the computer screen. By modifying this, we can make things appear closer or farther away, depending on the size of this value. To further enhance this illusion, we’ve dynamically created a grayscale palette and based the color of the pixel drawn to the screen on its z-axis position. This causes the stars to fade from black to white as they move toward the user, which further enhances the 3-D effect. The following listing demonstrates this effect.
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.