PDA

View Full Version : alpha blending problem



ill bandit
06-25-2000, 07:47 PM
i'm having a problem with alpha blending. i'm trying to have
a triangle blend from one texture to another. i set the
alpha channel of each vertex to a unique value, yet in the
end result the triangle is rendered like a static value has
been used for each of the triangle's verticies' alphas. i
can see that both textures were used on the triangle, but
there is no variance of the amount of each texture that was
used. =(

i'm using win98, and running opengl in software mode...
here's the code=)

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_TEXTURE);
glScalef(1.0f,-1.0f,1.0f);
glTranslatef(0.0f,1.0f,0.0f);

...
// more setup code
...


glBindTexture(GL_TEXTURE_2D,texture1_id);
glBegin(GL_TRIANGLES);
{
// left vertex
glTexCoord2f(.5f,0.0f);
glVertex3f(0, top, zdepth);

// right vertex
glTexCoord2f(0.0f,1.0f);
glVertex3f(left, bottom, zdepth);

// apex vertex
glTexCoord2f(1.0f,1.0f);
glVertex3f(right, bottom, zdepth);
}
glEnd();

glBindTexture(GL_TEXTURE_2D,texture2_id);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glDepthFunc(GL_EQUAL);
glBegin(GL_TRIANGLES);
{
// left vertex
glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
glTexCoord2f(.5f,0.0f);
glVertex3f(0, top, zdepth);

// right vertex
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(left, bottom, zdepth);

// apex vertex
glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f,1.0f);
glVertex3f(right, bottom, zdepth);
}
glEnd();
glDepthFunc(GL_LESS);
glDisable(GL_BLEND);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

thx for any help=)

DFrey
06-26-2000, 03:25 AM
Looks like it should work. But you already knew that! http://www.opengl.org/discussion_boards/ubb/biggrin.gif The only one thing in the code that disagrees with me, is setting the blend function before blending is enabled. Just feels like bad voodoo to me. The other possibility is that the last texture you are attempting to blend, was loaded with an alpha channel and every pixel in the texture's alpha channel has a value of 0 or some small value in general.
You say you can tell both textures were used. What do you mean by this? How can you tell? Do you see both textures in the triangle? Or did you mean you stepped through the code and can see both triangles were being drawn?
Oh one other thing came to mind, the texture matrix, did you load it with the identity matrix (or some known matrix) before scaling and translating it?

BANE
06-26-2000, 05:31 AM
DFrey, I think he is trying to merge the 2 textures together, with a fading effect of one texture into another. Should look cool.

I think you will have to load/store your textures with varying alphas so that when you blend them, you get the different transparency effect.

Good luck,
Dan

DFrey
06-26-2000, 06:12 AM
BANE, I'm very well aware of that. I've done the effect many times. The textures do not need alpha components in GL_MODULATE mode. The fragment alpha should fade the second texture as it interpolates from 1 on one side of the triangle to 0 on the other. Simply doing this will produce not the best results. Since the linear interpolation often is not what you want. For this effect you typically want a nonlinear variation in alpha, that is texture dependent. By using an alpha component in the second texture that masks (more or less, literally) the second texture, this nonlinear effect can be achieved. On any card that supports GL_EXT_texture_env_combine this can even be done in just one pass using the INTERPOLATE_EXT combiner operation.

BANE
06-26-2000, 06:30 AM
I agree nearly everything you say. if you load the texture yourself and modulate the alpha with your own function you can probably achieve the effect you want providing you have a editor which lets position th texture. I think it is easier to store the texture with an alpha than to have to rely on an extension. At least you are garanteed than this will work with any card anytime.
Maybe the use of multi-texturing could be good here, using alpha textures and normal textures. I have never tried it though. Probably amounts to the same as storing an alpha in th texture.

Dan.

Kosta
06-28-2000, 12:32 AM
Originally posted by BANE:
I agree nearly everything you say. if you load the texture yourself and modulate the alpha with your own function you can probably achieve the effect you want providing you have a editor which lets position th texture. I think it is easier to store the texture with an alpha than to have to rely on an extension. At least you are garanteed than this will work with any card anytime.
Maybe the use of multi-texturing could be good here, using alpha textures and normal textures. I have never tried it though. Probably amounts to the same as storing an alpha in th texture.


Yes it is possible. I use 3 textures: 2 RGB-textures and 1 alpha-texture.
The alpha-texture specifies the the interpolation-factor for the 2 RGB-textures. A value of 0 in the alpha-texture means 100% texture1, a value of 255 means 100% texture2. Between these two values there will be linearly interpolated.
A nice feature of this approach is that I can modify the alpha-texture on-the-fly.
Have a look at http://wwwmath.uni-muenster.de/cs/u/mam/geothema.htm for a demo of this effect (section about texturing techniques).

Kosta