Sign in to follow this  
purple_moon

Change the global opacity of a texture efficiently

Recommended Posts

Hi, guys! When creating a texture, I usually use "glPixelTransferf" with "GL_ALPHA_SCALE" to set the global opacity of the texture image. But there is a problem. When I want to change the gobal opacity, I will have to (I havn't found any other ways T_T) destory and recreate the texture. So when I want to create a fade in/out effect with a large texture, the program performs terrible! How can I create a high-performance fade in/out effect? Thanks!

Share this post


Link to post
Share on other sites
Set texture combiner function to GL_MODULATE and fade factor with in primary color's alpha channel.

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColor4f(1, 1, 1, fade);

fade is a value in the range [0, 1], where 1 means the original texture alpha is unchanged, and 0 means totally transparent. Change the value of this variable slowly from 1 to 0 to fade the texture.

Share this post


Link to post
Share on other sites
Bob's suggestion is exactly right. I should be noted that GL_MODULATE is the default anyway. So as long as GL_COLOR_MATERIAL (I think??) is enabled, and blending's enabled, and you've chosen a suitable blend function, it should work.

You won't need to modify the texture at all. Have the texture fully opaque if you like (or with varying opacity if that's how you want it).

Mark

Share this post


Link to post
Share on other sites
GL_COLOR_MATERIAL doesn't have to be enabled. Unless you're using lighting of course, but then I suggest you use the texture combiner constant color to supply the fade factor instead of relying on the lighting equation to calculate it.

Share this post


Link to post
Share on other sites
glTexEnv affects the texture unit, so yes, if you need another texture combiner function form some other texture you need to change it every time. But as said, the function you need is the default one, so chances are you only need to restore the primary color.

Share this post


Link to post
Share on other sites
The function you need to use to change the opacity is glColor4f. And you can call that at ANY time, even between begin/end.

And if colour interpolation enabled, it will interpolate the alpha between verts (if you're in between verts when you call it).

Mark

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this