DirectDraw:
Gamma Control
By: Jack Hoxley
Written: May 2000
Download: DD_Gamma.Zip (566kb)
Gamma control, when used properly,
can allow you to achieve some amazing effects. The only serious problem with
using it is that it isn't well supported; only computers with new or nearly-new
graphics cards inside them will be able to support this effect. Because of this
downside it is advisable that you dont rely to heavily on it's use - as the fruitworld game example does.
In the fruitworld game example
it uses the control to achieve cinematic effects as it fades the credits in
with different amounts of colour; then, during the game, it uses it to create
lighting effects. It is possible to recreate this effect using several other,
more widely supported methods, such as these:
- Memory Access. A formula could
have been written to go through every pixel and fade it out slightly each
time. The only drawback being that it is very slow.
- Palette animation. This is
very fast, and extremely easy to use. The only drawback is that it will require
the display to be in 8bit paletised mode.
Gamma control is done through
an interface of ramps. Put simply it alters the amount of red green or blue
there is in every pixel. This is done between the time the primary surface is
updated and the time that the monitor DAC (Digital to Analogue Convertor) gets
the information; because it is done in this way it doesn't actually alter the
values stored in your surfaces.
The interface is called a ramp,
it can be visualised as a straight-line graph. Every colour component on the
primary buffer is represented by a value 0 to 65535. This is then converted
into a table of 256 different entries - which is basically the same as when
you set a colour using a number between 0 and 255. Directdraw then reads off
this graph what the output colour should be if the input colour is ??? If the
graph is a simple X=Y line, the value that goes in is the value that comes out;
by default this is how everything appears.
Using the access to the gamma
ramps we can modify the relationship between input colour and output colour;
depending on how you set the graph, you could make it so that half the amount
going in actually appears on the screen. Have a look at these graphs for a better
example:
These two graphs
represent what was just discussed above. "Ramp 1" represents a normal
gamma ramp; if you put in a value of 9620 you would get a value of 9620 appear
on the screen - what goes in comes out. "Ramp 2" represents a modified
gamma ramp, the value that comes out will be less than the value that went in,
although the image is not accurate it should be roughly half.
These graphs could
represent any of the three channels - Red, Green or Blue. Each channel can have
a different gamma ramp attached to it; because of this you can dynamically fade
in and fade out each colour. Using gamma correction has very little if any hardware
overhead, so using it will not bring the computer to a grinding halt. Which
is good.
I have written an
example project that shows you how to implement the use of gamma correction
in your games/programs. You can download it from the top of the page or from
the Downloads page.
|