PDA

View Full Version : PNG transparency



afra89
04-06-2010, 12:12 PM
So this is my main problem atm.
I've been searching all over the internet and nothing seems to work.
It's very basic, it's a PNG with transparent parts that i want to be transparent in my 2D game aswell, but those part is just black.

I've tried glBlendFunc() and stuff but nothing seems to give me the results that i want. Anyway here is my code:

A function that takes path to img and returns texture:


GLuint loadImage(char *path, GLuint tx,SDL_Surface* sur)
{
if ( (sur = IMG_Load(path)) )
{

// Check that the image's width is a power of 2
if ( (sur->w & (sur->w - 1)) != 0 ) {
printf("warning: image.bmp's width is not a power of 2\n");
}

// Also check if the height is a power of 2
if ( (sur->h & (sur->h - 1)) != 0 ) {
printf("warning: image.bmp's height is not a power of 2\n");
}

// get the number of channels in the SDL surface
nOfColors = sur->format->BytesPerPixel;
if (nOfColors == 4) // contains an alpha channel
{
if (sur->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
} else if (nOfColors == 3) // no alpha channel
{
if (sur->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
} else {
printf("warning: the image is not truecolor.. this will probably break\n");
// this error should not go unhandled
}

// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &tx );

// Bind the texture object
glBindTexture( GL_TEXTURE_2D, tx );

// Set the texture's stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// Edit the texture object's image data using the information SDL_Surface gives us
glTexImage2D( GL_TEXTURE_2D, 0, 3, sur->w, sur->h, 0,
texture_format, GL_UNSIGNED_BYTE, sur->pixels );

}
else
{
printf("SDL could not load image.bmp: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}

// Free the SDL_Surface only if it was successfully created
if ( sur )
{
SDL_FreeSurface( sur );
}

return tx;
}

The way i draw, don't mind the coord, just floats for animation:

void drawParam(GLuint tx,float x,float y,float d)
{
// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, tx );

glTranslatef(x,y,0.0f);
glRotatef(d,0.0f,0.0f,1.0f);
}

void drawChar()
{ glBegin( GL_QUADS ); /* Start drawing quads */
// Top-left vertex (corner)
glTexCoord2f( spriteCoordX-spriteCoordXincr, spriteCoordY-spriteCoordYincr );
glVertex2f( 0, 0);

// Bottom-left vertex (corner)
glTexCoord2f( spriteCoordX-spriteCoordXincr, spriteCoordY );
glVertex2f( 0, size);

// Bottom-right vertex (corner)
glTexCoord2f( spriteCoordX, spriteCoordY );
glVertex2f( size, size);

// Top-right vertex (corner)
glTexCoord2f( spriteCoordX, spriteCoordY-spriteCoordYincr );
glVertex2f( size, 0);
glEnd( );

And the way i call them:

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();

glPushMatrix();
drawParam(tx2,407.0f,252.0f,0.0f);
glCallList(bg); //draw background
drawParam(tx1,x_pos,y_pos,degree);
drawChar();
glPopMatrix();
SDL_GL_SwapBuffers();

ZbuffeR
04-06-2010, 01:03 PM
The internalFormat of glTexImage2D is wrong : try GL_RGBA8 instead of 3, otherwise there is no chance to use the alpha channel...

afra89
04-06-2010, 02:30 PM
once again, thank you ! :D

dorbie
04-06-2010, 04:04 PM
Don't forget: glEnable(GL_BLEND);