View Full Version : Blending - Make part of texture transparent?

11-10-2010, 05:30 PM
Hello all,

I've been banging my head against the wall trying to figure out how to do this. Basically I want to map a texture to a flat square. Then any part of the texture that is, say, cyan, needs to have its alpha value set to 0. I know how to map a texture to the square, and I can change the alpha of the ENTIRE texture, but I can't figure out how to only change the cyan part, while leaving the rest of the image completely opaque.

I've been messing around with blending modes, and none of them seem to be getting me any closer to what I want to do.

What setting of glBlendFunc do I need to use? Where do I set which color needs to be made transparent? Any and all help would be greatly appreciated.

11-10-2010, 06:34 PM
I'm not sure about setting a particular color to be used as an alpha value, maybe with a shader.

But what would work is using a texture format that supports an alpha channel(RBGA) such as PNG.

I believe you can also load a texture and create a texture mask with it, but you would likely be better off using an RBGA texture.

Alfonse Reinheart
11-10-2010, 06:38 PM
I can't figure out how to only change the cyan part, while leaving the rest of the image completely opaque.

That's something you need to do on the CPU before you upload the data. Just scan through the texture and set the alpha to 0 if it is the key color.

11-10-2010, 08:28 PM
How can I set the alpha values of the specific pixels though? I understand possibly scanning through it (though I'm not sure how to do that yet either) but how can I set each pixel individually? Can I just put a glColor statement before it? Does that actually work pixel by pixel in a texture?

Also, I was using .raw images, and they work fine. Png's are giving me colored static? Looking around it seems like you need to use lots of stuff to do that. I'm looking to do this with standard openGL stuff. (gl, glu, glut)

11-10-2010, 10:03 PM
Also, from the opengl tutorial stuff at http://www.opengl.org/resources/faq/technical/transparency.htm#blen0070

This question is EXACTLY what I want to do. Exactly. It just doesn't give a very good explanation. But this would imply that its possible to do what I want to do without mucking about pixel-by-pixel.

"15.080 How can I make part of my texture maps transparent or translucent?

It depends on the effect you're trying to achieve.

If you want blending to occur after the texture has been applied, then use the OpenGL blending feature. Try this:

glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ONE);

You might want to use the alpha values that result from texture mapping in the blend function. If so, (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) is always a good function to start with.

However, if you want blending to occur when the primitive is texture mapped (i.e., you want parts of the texture map to allow the underlying color of the primitive to show through), then don't use OpenGL blending. Instead, you'd use glTexEnv(), and set the texture environment mode to GL_BLEND. In this case, you'd want to leave the texture environment color to its default value of (0,0,0,0).

11-11-2010, 12:42 AM
Not really, it implies alpha values of the image must already be correct.

The only good way to do this, as said Alfonse, is to preprocess the cyan to have alpha=0 in your image, before using it with (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA).

Even better, go for premultiplied alpha, and convert RGBA cyan 0,1,1,1 to RGBA transparent black 0,0,0,0 with blend mode (GL_ONE,GL_ONE_MINUS_SRC_ALPHA) to avoid cyanish fringes caused by texture interpolation.

11-11-2010, 04:51 PM
Ok I figured out how to do the alphas. I go through and change each cyan pixel to be 0,0,0,0. But then it seems like every blend mode I use is incorrect.

Using (GL_ONE,GL_ONE_MINUS_SRC_ALPHA) seems to be giving me the exact opposite effect of what I want. It draws the window portion solid white with seemingly no transparency (fully opaque). Then it draws every other color semi-transparent. I'm having a really hard time trying to grasp which parameters should go where to help.

I feel like I'm so close!

11-11-2010, 04:53 PM
Wow. I had texenv on and it was messing it all up. Deleting the texenv statement made it work perfectly with (GL_ONE,GL_ONE_MINUS_SRC_ALPHA). I just got it. Thank you so much guys!