Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: problem - texture with alpha channel

  1. #1
    Intern Newbie m3xican's Avatar
    Join Date
    Nov 2007
    Location
    Italy -> Naples
    Posts
    36

    problem - texture with alpha channel

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

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,453

    Re: problem - texture with alpha channel

    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.

  3. #3
    Intern Newbie m3xican's Avatar
    Join Date
    Nov 2007
    Location
    Italy -> Naples
    Posts
    36

    Re: problem - texture with alpha channel

    Yep, I was in 16bit mode... stupid OpenSuse...

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •