Vary texture intensity (alpha?)

(Maybe this is too simple - but I have looked in the Red Book. I’ve got my flame suit on just in case).

I want to vary the intensity of a texture, i.e. be able to “fade” it in and out. I’m guessing this is an alpha blending issue, but I can’t see how to set the alpha for a texture (on the fly in OpenGL). I have found out about glTexEnvf() - what next?

This is related to another thing I don’t understand. If I display a texture using
glTexEnvf(…,…,GL_BLEND) it is influenced by the last value glColor() was set to. I need to use GL_DECAL to keep its own color. Why does GL_BLEND change the texture color?



You don’t need glTexEnv (… GL_BLEND);
GL_MODULATE, as usual, does the job.
You don’t even need to have an alpha channel in your texture. To display your textured triangles with a given alpha, just call glMaterialfv for GL_AMBIENT, GL_DIFFUSE, and GL_SPECULAR components just before drawing your triangles. the vector you could pass to them could be :
float color [4] = {1.0, 1.0, 1.0, alpha};
where alpha is the alpha you want to give to your texture.

Of course, it will be necessary that blending, with convient blend func, is activated. This is done by :

glEnable (GL_BLEND);

Don’t forget to glDisable (GL_BLEND) whenever you have to draw an opaque object, that’ll be much faster.

Thanks for the tip about GL_MODULATE. This moves me forward, but I still have the funny color problem I mentioned. The code for my test is below. I paint the texture, which has yellow-green stuff on a black background (actually it is the NeHe logo wallpaper), then put a red square on it. I wait a bit (so I can see what happened) then call the function again.

The first time I call drawGLScene() I see what I expect, a red square on a yellow-green pattern. The next time, and thereafter, the yellow-green of the NeHe logo has been changed to red on black. If instead of red I draw a green square, the texture color changes to green on black. But if I draw a blue square the texture disappears.

Can you explain what is happening? I’m baffled.


int drawGLScene( GLvoid )
float acolor[4] = {1.0,1.0,1.0,1.0};


glLoadIdentity( );
glTranslatef( 0.0f, 0.0f, -2.0f );

glEnable( GL_TEXTURE_2D );
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, acolor);

glBindTexture( GL_TEXTURE_2D, texture[0] );
glBegin( GL_QUADS ); /* Start Drawing A Textured Quad */
  glTexCoord2f( 0.0f, 3.0f ); glVertex3f( -1.1f, -1.1f,  0.0f );
  glTexCoord2f( 3.0f, 3.0f ); glVertex3f(  1.1f, -1.1f,  0.0f );
  glTexCoord2f( 3.0f, 0.0f ); glVertex3f(  1.1f,  1.1f,  0.0f );
  glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.1f,  1.1f,  0.0f );
glEnd( );

glDisable( GL_TEXTURE_2D );
glEnable( GL_BLEND ); 

glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glBegin( GL_QUADS );
  glVertex3f( -0.5f, -0.5f, 0.0f );
  glVertex3f(  0.5f, -0.5f, 0.0f );
  glVertex3f(  0.5f,  0.5f, 0.0f );
  glVertex3f( -0.5f,  0.5f, 0.0f );
glEnd( );


SDL_GL_SwapBuffers( );


Ok sorry for having been so long to answer.

What’s happening is that the call to glColor4f for the red square remains effective for the yellow pattern.

Solution : after having drawn the red square, call glColor4f with white color.

The results you got are esay to explain. When rendering the pattern, each component (red, green, blue) of each pixel’s color is computed with the formula :

component = (component of texture, after lighting & shading) * (component passed by glColor).

Remember, yellow is RGB 1, 1, 0.
When multiplied by red = RGB 1, 0, 0, it gives 11, 10, 0*0 = 1,0,0 = red. Here’s why the NeHe logo gets red instead of yellow.

When yellow 1,1,0 is multiplied by green 0,1,0, same thing, you get green.

When yellow 1,1,0 is multiplied by blue 0,0,1, this time, you get 0,0,0 = black.

Got it ?

Thanks Morglum. I have it all working pretty well now. It is going to take a while to really get to grips with all the blending combinations that OpenGL makes available.


I don’t think it’s a priority : personnally I’ve never used another combination than GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA. In fact, as for today, most ordinary hardwares have no destination alpha, so all combinations involving a DEST_ALPHA are useless (unless working on a $5,000 workstation). Other combinations could allow you to render certain special effects, though.

Everything nvidia has produced since the tnt2 has destination alpha, and so does all ati cards since the radeon. Anything that has 32bit colour has destination alpha.

then how do you explain that GLUT_RGBA is nothig more than a synonym of GLUT_RGB ?

Could you tell me how to get a dest alpha with a tnt2 and , say, windows98 ?