LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: prich on October 11, 2009, 08:05:00

Title: no aplha of png file in lwjgl
Post by: prich on October 11, 2009, 08:05:00
hello there!

i have some problems concerning AngelCodeFont. Because in lwjgl there is no such thing i want to read a png file (see picture default.png).
But when i read the picture with:
BufferedImage bi = ImageIO.read(new File(img));
and cast it into byte[] with:
byte[] rawdata  = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
and upload it correctly i get a weired result:
(http://img246.imageshack.us/img246/391/testkr.png)

can somebody help?
maybe a wrong alpha test? i used also the code of slick2D but in slick it works :(

thx prich
Title: Re: no aplha of png file in lwjgl
Post by: kappa on October 11, 2009, 13:50:32
you have to switch on alpha blending in OpenGL, something like
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

Title: Re: no aplha of png file in lwjgl
Post by: prich on October 11, 2009, 18:03:08
thx alot!
can you also explain me why? because i would like to understand why i have to use blending!

thx prich
Title: Re: no aplha of png file in lwjgl
Post by: Evil-Devil on October 12, 2009, 07:02:56
Slick may activate blending by itself, when it discovers an image with transparency. OpenGL will only do the things you say you want it to do.
Title: Re: no aplha of png file in lwjgl
Post by: Ciardhubh on October 12, 2009, 08:05:39
Quote from: prich on October 11, 2009, 18:03:08
thx alot!
can you also explain me why? because i would like to understand why i have to use blending!

thx prich

Without active blending (GL11.glEnable(GL11.GL_BLEND);) OpenGL will just take the colour from the texture and overwrite what's currently in the framebuffer. The alpha channel is ignored. Every pixel of a character in your font is basically white. The smooth edges are white too, with a lower alpha value. So if there's no blending, you get these ugly non-smooth blobs. Plus you background is just black.

With GL11.glEnable(GL11.GL_BLEND); OpenGL takes the incoming texture colour and blends it with the existing framebuffer colour based on the function defined with GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);.

There's a lot more to blending (when to turn on/off, which order to draw objects, when to disable depth testing and so on), though.
Title: Re: no aplha of png file in lwjgl
Post by: prich on October 12, 2009, 16:06:26
thx alot, i really appreciate your help!

prich