Portable Network Graphics transparency

Hi everyone. I am running into a little bit of trouble getting my Portable
Network Graphics to render with correct transparency. I am using OpenGL and
SDL together. I basically load the image with SDL-image and render
quadrilateral with OpenGL. This all works fine, but pixels that are
supposed to be transparent are rendered as white. Right now I am just using
a basic OpenGL setup with no special configuration. Is there anything I
need to enable in GL to get it to take the alpha values from the Portable
Network Graphics?

Thanks for help friends,
Adam Stevenson

Adam Stevenson wrote:

Hi everyone. I am running into a little bit of trouble getting my
Portable Network Graphics to render with correct transparency. I am
using OpenGL and SDL together. I basically load the image with
SDL-image and render quadrilateral with OpenGL. This all works fine,
but pixels that are supposed to be transparent are rendered as white.
Right now I am just using a basic OpenGL setup with no special
configuration. Is there anything I need to enable in GL to get it to
take the alpha values from the Portable Network Graphics?

First, the image format is typically just called a PNG. People are actually more likely to understand that then spelling out the acronym, as most graphics programs that export to PNG just list them as PNGs, not Portable Network Graphics.

Now, in order to make alpha blending working in OpenGL, you have to enable GL_BLEND, and then set the appropriate blending function. So you need:

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

Just so you know, you can insert a lot of different values in glBlendFunc. The first value is what the source color (the color you’re sending in) is multiplied by. The second value is what the destination color (the color already in the framebuffer) is multiplied by. After each color is multiplied, the results are added together. So the final color is srcColorsrcMultiplier + dstColordstMultiplier.

Check the OpenGL red book chapter on “Blending, Antialiasing, Fog and Polygon Offset” for more information.

Thanks Mike, much appreciated. I will remember the tip about PNG acronym.–
Adam Stevenson

Hey there,

Don’t forget to use glDisable(GL_BLEND);
This is very important! When you’re not working with transparent objects, turn off this step in the pipeline to speed things up a bit and to keep your code (like functions that draw transparent objects) compartmentalized.

Jonny DDate: Mon, 6 Aug 2007 22:44:33 -0500From: adamstevenson121 at gmail.comTo: sdl at lists.libsdl.orgSubject: Re: [SDL] Portable Network Graphics transparencyThanks Mike, much appreciated. I will remember the tip about PNG acronym.-- Adam Stevenson


Recharge–play some free games. Win cool prizes too!
http://club.live.com/home.aspx?icid=CLUB_wlmailtextlink