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
## CHAPTER 11 Special Effects This chapter covers the following topics: • Bitmap transformations • Bitmap scaling • Bitmap rotation • Lighting tricks • Transition effects • Simple texture mapping • Star fields • Fire plasma effects Although interactive computer games are slowly gaining ground, it is probably safe to say that movies and television are the most popular forms of entertainment among the masses. A good film or movie with Oscar-winning actors and exotic, beautiful sets and locations can be very enjoyable. Indeed, classic films such as Ben Hur and Gone with the Wind are timeless in their ability to enthrall and entertain. Everyone enjoys a realistic, stirring performance by a good actor, and of course a good plot can make or break a movie (the same rule applies to games). Even so, when people watch movies, they want to be transported to another place and time, and it would be very difficult to achieve an effective illusion without the use of special effects. What would the Star Wars movies have been like without the massive space battles? Or Star Trek without the transporter? Special effects provide visualization of events or actions that could not take place in reality, and, in an ironic way, can add an air of “realism” to fantastic settings. The same holds true for computer games. It is easy to portray exotic locations and creatures using computer graphics. Even so, you can accomplish only so much by just relying on the blitting functions to copy graphics from one surface to another. As we’ve already examined, the Lock method gives us the ability to directly manipulate the contents of a surface. This gives us incredible power, limiting game world visualization only to our imaginations. This chapter examines how the Lock method can be used to produce effects that would be difficult to reproduce by using Blt or BltFast alone. While a comprehensive discussion of computer graphic special effects could probably fill several volumes, we’ll examine a few selected case studies to demonstrate the power of directly manipulating surface contents and to spark our imagination for further applications of this technique.
| Caution: Always remember to call the Unlock method on a surface before it is used with other DirectDraw methods, such as Flip, or your application will crash.
### Bitmap Manipulation Simply manipulating bitmaps in one way or another can produce many cool effects. The texture mapping that is performed in 3-D games could be considered a bitmap manipulation. Basically, by taking a plain, flat bitmap and drawing it in some way other than just blitting it to the screen in a plain, flat manner can give the bitmap an appearance of depth or texture. This can also stretch the art budget dollar, since producing visual effects programmatically will reduce the amount of artwork required. There are thousands of ways that bitmaps can be manipulated to produce all kinds of interesting effects. In the interest of space, we’ll examine only a small handful. Indeed, even the implementation of these examples can vary widely from programmer to programmer, but we’ll examine some simplistic ways to produce some interesting effects by manipulating bitmaps. Specifically, we’ll look at bitmap transformations, scaling, rotation, a cheap lighting effect, transition effects, and a very simple texture mapping routine to produce the illusion of a bitmap wrapped around a sphere. #### Bitmap Transformations This rather broad title addresses techniques for transforming bitmaps and drawing them in a non-linear or non-rectangular manner. You could classify every effect in this section as a bitmap transformation. However, our discussion deals with techniques for using mathematical equations to modify the location of pixels as they are drawn to a destination. Specifically, we’re going to look at using the Sin function to apply a sine wave to a bitmap of the Texas state flag in order to produce an animation of a flag blowing in the wind. Basic Theory At the heart of this technique is the Sin function. If you’ve ever taken a trigonometry class, you should be very familiar with this function. Without going into a lot of detail, the Sin function takes a number, more specifically a degree on a circle (i.e., 0º to 360º), and returns a value between 1 and 1. If you graphed out the result of the Sin function, it would look something like Figure 11-1. This produces what is known as a sine wave. It is a cyclic function that will return a result in the range of 1 to 1 for any value. If we scaled the resulting values from this function and used these as vertical offsets, we could easily produce a simple but effective wave animation with our bitmap. Figure 11-1: A graph of the Sin function results
| Tip: Remember that the trigonometry functions are notoriously slow, so you should probably use an optimization like a lookup table for trig function results in real-world applications.
We’ll need to draw our bitmap as vertical strips of pixels in order to make this work. Beginning with a starting value of 0, we use the Sin function with this value to determine the vertical offset for the first strip of pixels. We then draw the first vertical strip of pixels within the bitmap at some starting point within our destination, with the top of this strip offset vertically by the return value of the Sin function. Iterating horizontally through our bitmap, we’ll add 1 to the value passed to the Sin function as we draw each vertical strip of pixels, using this offset to modify the starting point of the top of the strip. This will produce an oscillating offset for the starting point of the top of each vertical line of pixels, as demonstrated in Figure 11-2. Figure 11-2: Using Sin to modify vertical starting points If we increment the starting point each time we draw the bitmap, it will make the wave appear to move, and we’ll produce an effective animation. Of course, we’ll need to scale the values returned from the Sin function by some constant in order to produce a more pronounced wave effect. We’ll also need to scale the values given to the Sin function in order to increase the frequency of the waves. The following listing demonstrates this concept.
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.