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:
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?