PDA

View Full Version : transparent pixels



t3jem
06-21-2005, 08:34 AM
Hi,

I have a program that has fire, but so far to show the fire correctly I must use

glBlendFunc(GL_SRC_ALPHA,GL_ONE);

This workes ok, but when it is very hard to see against a white background so I thought maybe it would be easier just to pick the pixels in the bmp file that I want to be transparent. Here is a thread that is on the topic http://www.opengl.org/discussion_boards/cgi_directory/ultimatebb.cgi?ubb=get_topic;f=2;t=018804 , but I can't figure out how to convert the code they have into my bitmap loader below.


void text(UINT textureArray[], LPSTR strFileName, int ID)
{
if(!strFileName) return;

AUX_RGBImageRec *pBitMap = auxDIBImageLoad(strFileName);

if(pBitMap == NULL) exit(0);

glGenTextures(1, &textureArray[ID]);
glBindTexture(GL_TEXTURE_2D, textureArray[ID]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitMap->sizeX, pBitMap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitMap->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);

if (pBitMap)
{
if (pBitMap->data)
{
free(pBitMap->data);
}
free(pBitMap);
}
}all replies are greatly appreciated.

dorbie
06-21-2005, 11:48 AM
for (x = 0; x < pBitMap->sizeX; x++)
{
for y = 0, y < pBitMap->sizeY; y++)
{
byte_ptr = pBitMap->data + x + y*pBitMap->sizeX;
red = *byte_ptr;
green = *(byte_ptr+1);
blue = *(byte_ptr+2);
alpha = *(byte_ptr+2);

// premultiply rgb by alpha here and write back
// remember these are unsigned chars so you need to convert 0-255 to 0.0-1.0 and back.
// scale alpha down to modulate dst less with the new glBlendFunction
}
}

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);This is not as straightforward as you think, you need to change your blend equation to use source alpha for destination and premultiply the source rgb by alpha then replace alpha with a lower value. This will retain a partial glow and show you some color withot the dulling effects of a full src_a, 1-src_a blendfunc.

So do this and change your blendfunc to glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) and you'll get decent results. The key is alpha used for destination modulation is less than the source alpha you premultiplied by.

With some content it may already be premultiplied and you're doing a double modulate so bear this in mind before premultiplying.

t3jem
06-21-2005, 01:58 PM
I'm sorry I must be dim witted or something I can't understand what you want me to do. I put that code into my program and set the variables, but I still can't get it to work.