PNG Transparency

Started by
17 comments, last by Prune 13 years, 8 months ago
Advertisement
your post seems to have a low alpha value ):
This is at least the third time he's posting this. I think he can't see it.
Quote:Original post by phresnel
This is at least the third time he's posting this. I think he can't see it.
You are so inclement.
Topic title couldn't be more appropriate...

Somebody said it before but that copy of the topic was removed, you should first load the texture by your own (e.g. using libpng or a library for loading images) and then make an OpenGL texture based off the data you get (you may need to reconstruct it to make it fit the texture format you want to use). And make sure that you're actually doing alpha blending when you try to use it, otherwise it'll be pointless.
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
I'm sorry guys about all the topics but when I was going to post the gamedev site wasn't answering.. So I tried again.. Well what I want is some code that could make this work! please, show some code!


[]'s, Ruy.
Make it work how? What are you trying to achieve? How far have you got with it? Have you managed to load a PNG? Have you managed to create a texture from it? Do you want to do alpha blending with it? Multitexturing? Or something else?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Oh, Ok. I thought that my post had show off in some other thread but here it goes: I'm trying to put a png on my scene. The image is very simple: a ball in a transparent background. The problem is that I couldn't take off the transparent background with any function that I've found on the internet. In the best of ocasions the figure blends entirely with the background. So what I want is very simple, a functional code to don't show the picture's background on my scene!

So, the code:
void display(void){    glPushMatrix();    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();    gluLookAt(0,0,45,0,0,0,0,1,0);    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glTranslatef(0,0,0);    glBindTexture(GL_TEXTURE_2D,Ttexturas[0]);    glEnable(GL_TEXTURE_2D);    glBegin(GL_QUADS);      glTexCoord2i(0,0);glVertex3f(-45,-45,0);      glTexCoord2i(1,0);glVertex3f(45,-45,0);      glTexCoord2i(1,1);glVertex3f(45,45,0);      glTexCoord2i(0,1);glVertex3f(-45,45,0);    glEnd();    glPopMatrix();    glutSwapBuffers();}.........int main(int argc, char *argv[]){    glutInit(&argc, argv);    glutInitWindowSize(800,600);    glutInitWindowPosition(0,0);    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);    glutCreateWindow("Corrida");    glColorMask(1,1,1,1);    ilutRenderer(ILUT_OPENGL);    ilInit();    glGenTextures(MAX_TEXTURAS,Ttexturas);    ilGenImages(MAX_TEXTURAS,Timagens);    for (_Ccounter = 0;_Ccounter<MAX_TEXTURAS;_Ccounter++) {       ilBindImage(Timagens[_Ccounter]);       ilLoadImage(TEXTURA_NOMES[_Ccounter]);       ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);       glBindTexture(GL_TEXTURE_2D,Ttexturas[_Ccounter]);       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);       glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);              glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());    }    ilDeleteImages(MAX_TEXTURAS,Timagens);    ilShutDown();    glBlendFunc(GL_ONE,GL_SRC_ALPHA);    glEnable(GL_BLEND);    glEnable(GL_ALPHA_TEST);    glAlphaFunc(GL_GREATER,0.01);    glutReshapeFunc(resize);    glutDisplayFunc(display);    glutSetKeyRepeat(1);     glutSpecialFunc(PressedKey);    glutSpecialUpFunc(ReleasedKey);    glutTimerFunc(10,Tempo,0);            glutMainLoop();    return 0;}

This is the important line:
glBlendFunc(GL_ONE, GL_SRC_ALPHA);
This configures how new pixels are blended onto the frame buffer. The first argument is what the new pixels are multiplied by, the second argument is what the old/existing pixels are multiplied by. The result of both these multiplications is added together.

You're setting it to:
result = newPixel * 1.0 + oldPixel * newPixel.alpha;

You probably want traditional alpha blending:
result = newPixel * newPixel.alpha + oldPixel * (1-newPixel.alpha);
...which is: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Or perhaps premultiplied alpha blending:
result = newPixel * 1.0 + oldPixel * (1-newPixel.alpha);
...which is: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Hi Hodgman! Thanks for your reply!

I don't think I made this clear... See, I already tried too these functionalities you mentioned but I got no success..
See, imagine a png of a ball and nothing else. The picture of the ball has no background, wich means that the alpha where there is no ball, is zero. What is happening is that when I draw it, the alpha channel of the png picture appears to be ignored, just draw black pixels when is supposed to draw nothing. Below I put the pictures:

This is my picture:
Click

This is how it been draw:
Click

This topic is closed to new replies.

Advertisement