PDA

View Full Version : Alpha channel



DarkShadow44
02-16-2010, 12:14 PM
Hi there !

I' am using FreeImage and to load GIFs. I use GIF_PLAYBACK to "generate" the picture for every frame. But now my question is, how can I make the picture transparent ? There should be an alpha channel, but that doesn't work.



GLuint*textures;

FIMULTIBITMAP* gif=FreeImage_OpenMultiBitmap(FIF_GIF,"forgif.gif",false,true,true,GIF_PLAYBACK);

const int pageCount=FreeImage_GetPageCount(gif);
num=pageCount;

textures=new GLuint[pageCount];

glGenTextures(num, textures);

int width;
int height;

int i;



for (i=0;i<pageCount;i++)
{
FIBITMAP* page=FreeImage_LockPage(gif,i);



if(i==0)
{
width=FreeImage_GetWidth(page);
height=FreeImage_GetHeight(page);
}

int w=FreeImage_GetWidth(page); // Local Width
int h=FreeImage_GetHeight(page); // Local Height






glBindTexture(GL_TEXTURE_2D, textures[i]);


char* pixeles = (char*)FreeImage_GetBits(page);

glTexImage2D(GL_TEXTURE_2D,0,4, width, height, 0, GL_BGRA,GL_UNSIGNED_BYTE,(GLvoid*)pixeles);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);


GLenum Error = glGetError();





FreeImage_UnlockPage(gif,page,false);
}
FreeImage_CloseMultiBitmap(gif,GIF_PLAYBACK);

What should I do to get transparency to work ?

Please help :sorrow:

strattonbrazil
02-16-2010, 03:38 PM
Do you have blending turned on?

def
02-17-2010, 02:34 AM
If you are sure how to use blending maybe the gifs tranparency is not recognized correctly:

From the FreeImage docs:

Transparency is supported individually for all pages, the first entirely transparent index in the
table is used, the rest of the table will be entirely opaque.

DarkShadow44
02-17-2010, 10:08 AM
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);





glBindTexture(GL_TEXTURE_2D,textures[ix]);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-30);
glBegin(GL_QUADS);

glColor3f(1,1,1);
//glColor3f(1,0,0);
glTexCoord2f(0,1);
glVertex3f(-10,10,0);

glTexCoord2f(1,1);
glVertex3f(10,10,0);

glTexCoord2f(1,0);
glVertex3f(10,-10,0);

glTexCoord2f(0,0);
glVertex3f(-10,-10,0);


glEnd();


// TEST

//glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D,0);
glBegin(GL_TRIANGLES);
glColor3f(1,0,1);
glVertex3f(-15,0,-1);
glVertex3f(15,15,0);
glVertex3f(15,-15,0);

glEnd();

If I try differnt BlendFuncs, I get a black or white background on the textured quad...
What is wrong ?

DarkShadow44
02-18-2010, 02:37 PM
EDIT:
Sorry for that, I solved it. :o
But why isn't it possible to draw Objects BEHIND transparent objects ? :confused:

ZbuffeR
02-18-2010, 03:06 PM
because of depth test.
so draw transparent objects last.

Alfonse Reinheart
02-18-2010, 03:19 PM
why isn't it possible to draw Objects BEHIND transparent objects ?

Because objects don't exist. There are only pixels.

The reason that opaque objects can be drawn "behind" other objects is because you are using the depth test to cull fragments that are behind pixels that were previously drawn.

When you're doing transparency, you're still just rendering to pixels, just like with opaque rendering. The difference is that you create a pixel color by combining the transparent object color with whatever the pixel is currently in the framebuffer. This is called "blending". This creates a single framebuffer pixel color.

If you render something after doing this, it cannot magically know what the original color was, what the transparent color was, etc. It only knows what the color is currently in the framebuffer, and what the color is from the object being rendered. These are your only two inputs to the blending system.

DarkShadow44
02-18-2010, 03:26 PM
So this also affects mirrors,glas and other translucent objects ?

Alfonse Reinheart
02-18-2010, 04:25 PM
So this also affects mirrors,glas and other translucent objects ?

Again, there is no such thing as "mirrors", "glas" and so forth. There is only what is in the framebuffer, the current triangle you are rendering, and how to blend between the two. It is your job to use these tools to create something that looks like "mirrors" or "glas".