Change texture opacity in OpenGL

This is hopefully a simple question: I have an OpenGL texture and would like to be able to change its opacity, how do I do that? The texture already has an alpha channel and blending works fine, but I want to be able to decrease the opacity of the whole texture, to fade it into the background. I have fiddled with glBlendFunc , but with no luck – it seems that I would need something like GL_SRC_ALPHA_MINUS_CONSTANT , which is not available. I am working on iPhone, with OpenGL ES.


I have no idea about OpenGL ES, but in standard OpenGL you would set the opacity by declaring a colour for the texture before you use it:

//          R,   G,   B,   A
glColor4f(1.0, 1.0, 1.0, 0.5);

The example would give you 50% alpha without affecting the colour of your texture. By adjusting the other values you can shift the texture colour too.


Use a texture combiner. Set the texture stage to do a GL_MODULATE operation between a texture and constant color. Then change the constant color from your code ( glTexEnv , GL_TEXTURE_ENV_COLOR ).

This should come as "free" in terms of performance. On most (if not all) graphics chips combiner operations take the same number of GPU cycles (usually 1), so just using a texture versus doing a modulate operation (or any other operation) is exactly the same cost.


Thank You all for the ideas. I've played both with glColor4f and glTexEnv and at last forced myself to read the glTexEnv manpage carefully. The manpage says that in the GL_MODULATE texturing mode, the resulting color is computed by multiplying the incoming fragment by the texturing color (C=Cf×Ct), same goes for the alpha. I tried glColor4f(1, 1, 1, opacity) and that did not work, but passing the desired opacity into all four arguments of the call did the trick. (Still not sure why though.)

链接地址: http://www.djcxy.com/p/33888.html

上一篇: 用OpenGL ES结合大量纹理图谱绘制调用

下一篇: 在OpenGL中更改纹理不透明度