tint a texture with openGL

Hello,

Now that I have managed to make a basic openGL rendering for my 2D tile based strategy game, I’m trying to impove it.

What I would like to do now is tint terrain in red when it’s in unit’s fire radius. The terrain is oubviously rendered with textures mapped on squares. So I would like to tint these textures with a transparent red color so that it looks like it is seen through a red glass. 'hope this is clear :-/

So how could I achieve this in an efficient way ? Playing with lights ? Create an already tinted texture beforehand ? Or another mean ?

Just one thing : I do not want to draw a transparent red circle since the fire radius does not mean a circle in my case. I really need a tile based processing.

Thanks for your help. :slight_smile:

The polygons that you’re using for your sprites and tiles can have colours assigned to each vertex using glColor3f() (just like glVertex2i). If you apply a colour to the verticies it will effect the texture (by tinting it)

Kev

Best way would probably be with vertex colours, then you get to control the exact colours instead of tricky tinkering with lights.

You’ll just need to add an extra colour value for each vertex, which means using glColor3f style calls if you’re using immediate mode, or glColorPointer for vertex arrays. Then you’ll want to glEnable with GL_COLOR_MATERIAL and map the colours to the diffuse colour with glColorMaterial.

A bit of a mouthful, but easy once you see it in code. Then your texture just gets multiplied with the vertex colours at every pixel which should give you the tinting you’re after.

Don’t listen to him, he’s mad :smiley:

Enable texture modulation with glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Then tint the vertices of your tiles with whatever colour you choose using glColor3f(). Obviously, glColor3f(1.0f, 1.0f, 1.0f) will keep be “untinted” as you’re not filtering any colour out at all by modulating with white.

Don’t bugger about with lights :wink: Far too complex.

Cas :slight_smile:

Thanks for your replies.

So I’ve tried glColor3f and it sort of do the trick. But now I know that it isn’t really the effect that I was looking for as green textures obviously go brown when applying a red filter whereas I wanted to have it in levels of red. So I guess the only thing to do is making a grey level texture from each texture and use it when original texture should be “tinted”.

So now my problem is : how do you make grey levels in a simple manner ? Is there some sort of GL functions to do this or will I have to average all the RGBs at loading time ?

And by the way Orangy Tang and Princec could you please explain in a few words what glColorMaterial and glTexEnvi are supposed to do ? I’m not sure to understand.

Thanks again for your help.

glColorMaterial is only used with lighting, which I don’t think you’re using, so don’t worry about it just yet!

glTexEnvi affects the way textures are applied to polygons. There are four modes, two of which are of usually of interest: GL_REPLACE and GL_MODULATE.

When you draw a polygon in GL, each vertex is given the colour last set by a call to glColor*. If it were untextured, it would draw that vertex in that colour.

If you enable texturing on the polygon, you’ve now got two colours for any pixel to choose from: the first, original colour you’re getting with glColor* calls; and the second colour, which is looked up from the texture (ie. it’s the texture pixels). These two are combined according to what glTexEnv is set to when the polygon is drawn.

If you use GL_REPLACE, the original glColor colours (known as the “fragment” colour) is simply ignored, and the texture colour is simply drawn.

If you use GL_MODULATE, the texture colour and the fragment colour are multiplied together, to give you a modulated or filtered colour. So setting glColor3f(1.0f, 0f, 0f) will completely filter out all the green and blue from the texture, giving it a blood red appearance.

Cas :slight_smile:

Oops, colour material is indeed only for when you’re using lighting as well, you can safely ignore it.

The modulate option is the default I think, but as you’ve found it can only darken an existing texture. Although if you use the EXT_texture_env_combine extension with glTexEnvi you can set a scale value so you can overbright your textures. With a scale of 2 you can then make a grey of (.5, .5, .5) the normal texture colour, and anything higher will brighten it.

http://www.berkelium.com/OpenGL/EXT/texture_env_combine.txt
Somewhere in the middle it talks about setting the scale value.

Sadly this extension is not included in GL bindings for SWT. Can someone explain me what is the theory bind additive coloring of textures so that I can make it ‘by hand’ ?
Could it just be averaging texture RGBs to get a grey value of the texel and then average again with wanted color ?

Thanks