Tricks of the Windows Game Programming Gurus:Advanced DirectDraw and Bitmapped Graphics

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

### Manual Color Transforms and Lookup Tables

At the very least, you can use the gamma correction system to perform filter operations on the entire image on the screen. Anyway, I’ll talk about lookup tables in RGB modes first, and then discuss the DirectX gamma correction system.

When dealing with pixels that are encoded as RGB `WORD`s, there’s really no way out of doing the work if you want to perform color animation. Not only must you write each pixel for which you want to change the color, but you may also have to read it. Hence, in the worst case you might have to perform a read, transform, and write cycle for each pixel you want to manipulate. There’s simply no way around this.

However, help is available. In most cases, performing mathematical transformation in RGB space is very computationally expensive. For example, let’s say that you want to simulate a square-shaped spotlight with a 16-bit graphics mode at location (x,y) with size (width, height). What would you do?

Well, you would begin by scanning out the rectangle of pixels that made up the spotlight area and storing them into a bitmap. Then, for each pixel in the bitmap, you would perform a color transform that looked something like this:

    I*pixel(r,g,b) = pixel(I*r, I*g, I*b)

Three multiplies to modulate the intensity. Not to mention that you would have to first extract out the RGB components of the 16-bit `WORD` and then put the 16-bit RGB `WORD` back together after the transform. The trick here is to use a lookup table.

Instead of using all 65,536 colors available in 16-bit mode, you only draw objects that can possibly be illuminated with, say, 1,024 colors that are equally distributed throughout the 64KB color space. Then you create a lookup table that contains a 2D array that’s 1,024 times however many levels of intensity you want, such as 64. Then you take the RGB level of each real color, compute 64 shades of it, and store each of them in the table. Then, when you create the spotlight, you use the 16-bit `WORD` as the index into the table, along with the light level as the second index, and the resulting 16-bit in the table is the RGB value premodulated! Hence, the lighting operation is a simple lookup.

This technique can be used for transparency, alpha-blending, lighting, darkening, and so on. I’m not going to show you a demo of it until the next chapter, but if you want to use lighting or color effects in 16-, 24-, or 32-bit color modes with any kind of speed, using lookup tables is the only way to go.

### The New DirectX Color and Gamma Controls Interface

In DirectX 5.0, two new interfaces were added to help game programmers and video programmers gain more control over the color properties of the screen image without resorting to complex software algorithms. For example, it would seem to be a simple thing to add a little red to the image on the screen, change the tint, etc. But operations like these, which require nothing more than a turn of a knob on a television, are rather complex to perform with digital data in software. Thankfully, the two new interfaces `IDirectDrawGammaControl` and `IDirectDrawColorControl` let programmers make these changes with some very simple calls.

`IDirectDrawColorControl` is very similar to a TV interface and gives you control over the brightness, contrast, hue, saturation, sharpness, and general gamma. To use the interface, you must query for it from the primary surface pointer with the `IID_IDirectDrawColorControl` identifier. Once you have the interface, set up a `DDCOLORCONTROL` structure, shown here:

    typedef struct _DDCOLORCONTROL

            {

            DWORD   dwSize;    // size of this struct

            DWORD   dwFlags;   // indicates which fields are valid

            LONG    lBrightness;

            LONG    lContrast;

            LONG    lHue;

            LONG    lSaturation;

            LONG    lSharpness;

            LONG    lGamma;

            LONG    lColorEnable;

            DWORD   dwReserved1;

            } DDCOLORCONTROL, FAR *LPDDCOLORCONTROL;

Next you make a call to `IDIRECTDRAWCOLORCONTROL::SetColorControl()`, and the primary surface will be immediately modified. The changes will remain in effect until you make another call.

    HRESULT SetColorControl(LPDDCOLORCONTROL lpColorControl);

The gamma control is a little different. Instead of you setting all the TV-like settings, the gamma correction control gives you control over the red, green, and blue color ramps of the primary surface. In essence, the shape of the ramps you define determines the color response of red, green, and blue. The setup of an `IDirectDrawGammaControl` is similar to the color control, so I’m not going to cover it (as if I really covered the color control). Take a look at the DirectX SDK for more information on these subjects, because they can make a number of effects very easy to do, such as underwater scenes, screen flashes, lightness, darkness, and so on. The only problem is that they only work with hardware that supports them, and very few cards do—not one of mine does, so I can’t make a demo!

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

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 in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.