Author Topic: How to properly create PNG texture with Alpha in OpenGL.  (Read 1338 times)

Offline Wodzu

  • Imaging User
  • *
  • Posts: 5
    • View Profile
Hi guys.

I am problem with PNG files under OpenGL. The file is an PNG image (256x256) with many levels of transparency.

I am loading it like this:

Code: [Select]
var
  Textures: array[1..6] of GLuint;   

  Textures[4] := LoadGLTextureFromFile('Images\font_0.png');

And I am displaying it on the quad like this:

Code: [Select]
glBindTexture(GL_TEXTURE_2D, Textures[4]);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-2, 2, 1);

glTexCoord2f(1, 0);
glVertex3f(2, 2, 1);

glTexCoord2f(1, 1);
glVertex3f(2, -2, 1);

glTexCoord2f(0, 1);
glVertex3f(-2, -2, 1);
glEnd();   

However, the file is not displayed correctly. It looks disrupted. I suspect that I need to explictly state somewhere the file format to OpenGL?

Do I need to write my own texture loading routine in the case of PNG file?

Thanks for your time.

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 312
    • View Profile
    • Galfar's Homepage
Re: How to properly create PNG texture with Alpha in OpenGL.
« Reply #1 on: 8 April 2010, 23:15:47 »
Could you post a screenshot of how distorted it looks?

Quote
I suspect that I need to explictly state somewhere the file format to OpenGL?
No, OpenGL doesn't care how the file was stored on the disk.

Did you set texture filtering, wraping, etc. after the texture was created? Have you setup alpha blending in OpenGl correctly?

Offline Wodzu

  • Imaging User
  • *
  • Posts: 5
    • View Profile
Re: How to properly create PNG texture with Alpha in OpenGL.
« Reply #2 on: 8 April 2010, 23:34:10 »
Thank you Galfar for such quick replay.

By file format I've meant the format of data A8R8G8B8 or something like that.
I am quite new to the OpenGL so I am sure it is my fault;)

Take a look at the screenshot. On the left we have PNG texture on the right we have BMP texture. Both are on the size 256x256.
BMP texture is displayed correctly PNG is not.



To be honest I am not sure that I've set up the blending properly. I've set up only only Alpha and not blending (cause I do not know how yet). So I have 1 bit transperency now.

Code: [Select]
  glAlphaFunc( GL_GREATER, 0 );
  glEnable( GL_ALPHA_TEST );
                  

I've also attached a compiled example program with source code.

Thank you for your time.
« Last Edit: 8 April 2010, 23:42:25 by Wodzu »

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 312
    • View Profile
    • Galfar's Homepage
Re: How to properly create PNG texture with Alpha in OpenGL.
« Reply #3 on: 9 April 2010, 00:25:06 »
Instead of alpha testing:
Code: [Select]
glAlphaFunc( GL_GREATER, 0 );
glEnable( GL_ALPHA_TEST );

use alpha blending:
Code: [Select]
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
or glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)

You can look at OpenGL demo of Imaging in Demos folder.


Quote
By file format I've meant the format of data A8R8G8B8 or something like that.
Thar's setup for you in LoadGLTextureFromFile.

Offline Wodzu

  • Imaging User
  • *
  • Posts: 5
    • View Profile
Re: How to properly create PNG texture with Alpha in OpenGL.
« Reply #4 on: 9 April 2010, 18:33:38 »
Thank you.

I've managed to compile OpenGLDemo under Lazaurs. Blending works nicely!