PDA

View Full Version : Change texture color



08-18-2005, 08:55 AM
I have a grayscale texture which is a rather intense to compute. If I draw a polygon with this texture I get a picture that goes from black to white in its colors (naturally).
However, I would like draw this texture in another "color scale", perhaps ranging from green to blue. That is, OpenGL would show a green color when the texture is black (or zero), blue color when the texture is white (or one), and somewhere between green and blue if the texture is gray.

This must be done without rebuilding the texture from scratch, as the scale can change between frames.

Is this possible?

Thanks in advance,
David

Aeluned
08-18-2005, 09:21 AM
The only way I can think of doing this would be using a fragment shader.

You would look up the grayscale value in the texture and your fragment color would be computed using something like:

gl_FragColor = color1*sample + color2*(vec4(1,1,1,1)-sample);

where color1 and color2 are green and blue in your example.

08-18-2005, 09:49 AM
Thanks for your reply. Would it make it easier if

1) there were only two colors to choose from, say either always green or always blue?

2) one of the colors is either black or white?

Anyway, I was thinking of drawing the polygon twice, first in green, and then use some alpha stuff to draw it half-blue. But I can't really figure out how to do it correctly.

Aeluned
08-18-2005, 10:14 AM
The colors are of no consequence.
You would pass the two colors you want to use as vec4's to the shader.

Your grayscale texture is acting sort of as a 'key' for how much of each color you use. A grayscale texture is uniform in all its channels - if you had a texture going from, say, red to white, you'd be filtering out all but the red channel from color1 and gradating to color2.

I don't know how much of this makes sense, you should just try and write the shader and experiment - I think you'll find that you can achieve what you're trying to do.

Drawing the polygon twice seems like much less an elegant solution. But it may be your only option for older cards not supporting fragment shaders (I don't know if you're targeting low end machines like that).

There are also register combiners...
I've never used them, I've always had access to shaders so I do my custom texture sampling using them.
You may or may not be able to use those on older graphics boards to achieve what you want, but someone else would have to help you with that.

08-18-2005, 06:49 PM
Thanks for your help. Actually, the code will have to run on very old and low-end machines, (picture a computer from 1999, with NT4 or Win98 as OS, and a noname graphics card). Therefore I'm only using OpenGL 1.0 as well.

Since the numbers are the ones that are expensive to compute, I think a possible way would be to compute an RGB texture from the grayscale texture every frame, and then use the RGB texture in the call to glTexImage2d.

Anyway, having read some more of this, it think glTexEnv could hold the key to success. If the texture function is set to GL_BLEND, the texture environment color (which I totally missed earlier) as blue, and the fragment color (which must be the same as the call to glColor3f?) to green, and supplying the grayscale as an alpha/luminance texture, it might achieve the correct results. IIRC, GL_BLEND should not be enabled although the texture function is set to GL_BLEND. I'll get back when I've tried it out.

// David

08-18-2005, 09:46 PM
Hey, it worked! :)