PDA

View Full Version : Vary texture intensity (alpha?)



gib
08-07-2002, 10:41 PM
(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?

thanks
Gib

thanks
Gib

Morglum
08-07-2002, 11:03 PM
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.

Morglum
08-07-2002, 11:05 PM
Of course, it will be necessary that blending, with convient blend func, is activated. This is done by :

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

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

gib
08-08-2002, 01:53 PM
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.

Gib


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

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

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

glEnable( GL_TEXTURE_2D );
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
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 );
glDisable(GL_DEPTH_TEST);
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

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( );

glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);

SDL_GL_SwapBuffers( );
}

Morglum
08-10-2002, 05:42 AM
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.

Morglum
08-10-2002, 05:49 AM
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 1*1, 1*0, 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 ?

gib
08-10-2002, 11:08 PM
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.

Gib

Morglum
08-11-2002, 12:14 AM
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.

harsman
08-11-2002, 06:06 AM
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.

Morglum
08-11-2002, 07:15 AM
then how do you explain that GLUT_RGBA is nothig more than a synonym of GLUT_RGB ?

Morglum
08-11-2002, 07:16 AM
Could you tell me how to get a dest alpha with a tnt2 and , say, windows98 ?