PDA

View Full Version : problem - texture with alpha channel



m3xican
11-09-2007, 08:49 AM
I have a problem drawing a PNG image with a transparent background, this screenshot shows the image I load as texture and what I get from the GL program:

http://img100.imageshack.us/img100/8518/problemrl2.th.png (http://img100.imageshack.us/img100/8518/problemrl2.png)

As you can see OpenGL doesn't draw the red circles properly.

This is the code I use to init the GL contest:

void GLInit(int w, int h)
{
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, w, h, 0.0, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glViewport(0, 0, w, h);

glEnable(GL_TEXTURE_2D);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

this is the code I use to load the texture:

GLuint LoadTexture(char * file)
{
GLuint id;
GLuint type;

SDL_Surface * surf = IMG_Load(file);

type = (surf->format->BitsPerPixel == ALPHA_BPP) ? GL_RGBA : GL_RGB;

glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, type, surf->w, surf->h, 0, type, GL_UNSIGNED_BYTE, surf->pixels);

SDL_FreeSurface(surf);

return id;
}

Any suggestions?

Relic
11-09-2007, 09:00 AM
Yup, you're either in 16 bit desktop color resolution. => Switch to true color. If you zoom in on the left image you notice that it's dithered, so that could be the culprit.
And/Or the texture has not been downloaded in its full precision.
Instead of the OpenGL 1.1 enums GL_RGB and GL_RGBA which are defined to mean "components" (3 and 4) you should use the enums with precision suffix which match the glTexImage2D internalFormat parameter better: GL_RGB8 and GL_RGBA8.
Those prevent that the OpenGL implementation possibly downgrades the texture color resolution to match the desktop.

BTW, in general you should always specify the GL_WRAP texture parameters. Most of the time GL_CLAMP_TO_EDGE is the correct choice. GL_CLAMP normally leads to unwanted colors at the edges when filtering accesses outside texel coordinates, because it filters in the default black texture border color.

m3xican
11-10-2007, 04:55 AM
Yep, I was in 16bit mode... :o stupid OpenSuse... :p

Changing the color resolution to 32bit improves the quality a lot, but I still notice a little difference between what I get now and what I got in the past (I'm using some old screenshots of a game I'm working on as comparison), maybe the problem is in the graphic driver and/or in the settings.

However thank you Relic!

P.S.
no GL_WRAP option affects the result, but I'll keep your suggestion in my mind.