Game Development Reference
In-Depth Information
Color Modulating Sprites
The current sprite code is quite comprehensive. It's easy to set the sprite texture,
U, V mapping, position, width, and height. The final operation that is quite
common with sprites is to alter the color. In many games, text can change color
and images may flash yellow to attract the attention of the user. This is normally
done through color modulation.
The basics of this technique have already been covered when the spinning tri-
angle was created. Each vertex is given a color. It's easier if the sprite has one
single color and then all vertices share it.
The color only needs to be set once, and then all the vertices will be set that
color.
float red = 1;
float green = 0;
float blue = 0;
float alpha = 1;
Gl.glBegin(Gl.GL_TRIANGLES);
{
Gl.glColor4f(red, green, blue, alpha);
This code will turn the sprite red. Altering the alpha will affect the entire sprite's
transparency. Using this code, it's very easy to imagine how to fade something in
by changing the alpha from 0 to 1 over time.
In certain cases it may be desirable to give a sprite a gradient rather than a solid
color. This can be achieved by setting the bottom vertices one color and the top
vertices a second color. The color will be interpolated by OpenGL and will create
a gradient automatically.
A Sprite and Renderer Class
The basics of a sprite have been demonstrated, but it's all quite untidy at the
moment, and very hard to reuse. All the code needs to be wrapped up into clas-
ses. The color, position, and U,V point data all need to be separated into their
own classes. It would be ideal to use pre-existing C# classes for these data struc-
tures, but it's better to make our own as they will need to be tailored to work
correctly with OpenGL.
 
Search WWH ::




Custom Search