Masking with PNG

I’ve been looking at converting my app from Graphics2D to LWJGL, learning OpenGL as I go. I’ve run into a problem that I was hoping there might be a quick and dirty solution to.

I’ve been using PNG files for my sprites, using the alpha channel for simple bit-mask transparency (a bit of overkill, I know). So I’ve only used alpha values of 0 and 255.

Now, when I try and use these same graphics as textures, I get a sort of weird transparency when two images overlap, rather than the clean bit-masking I’m used to. I’ve looked through NeHe’s tutorials, and his solution is to use separate 2-bit mask files. I’d really hate to have to create 2-bit masks for all my graphics, especially when they have perfectly good alpha channels.

Is there a quick fix for this? Some combination of arguments passed to gl.blendFunc() that I haven’t tried yet? I’ve been attempting gl.blendFunc(GL.SRC_ALPHA, GL.ONE) and gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA). The former gives me the odd transparency I’ve mentioned, the latter no transparency whatsoever.

I hope to go grab myself a copy of the red book this weekend, as NeHe’s tutorials, while good, are just not enough. Perhaps someone has some insights to save me a bit of time though?

Have you tried gl.enable(GL.ALPHA_TEST)? Blending only makes an image tranlucent.

Switching from BLEND to ALPHA_TEST was definately a great idea. Thanks.

However, when that didn’t work, I continued to bang my head against it until I realized there was something very odd in my call to gl.texImage2D(). I had copied the code from an example (straight from lwjgl’s cvs!) that had the third parameter of that method a hard coded 3. I don’t even know which constant 3 is, but clearly it wasn’t GL.RGBA, which is what I wanted there.

Ah, not that that’s fixed, I can get into the serious work of transitioning this code. Thanks for pointer!

Oh yeah. That one.

Oh, Cas!!! Can you guys fix that one in future revisions before any more LWJGL newbies have trouble with this? Just use GIMP to convert the images to RGBA, then change the code to use a reasonable GL.RGBA instead of the 3. Everyone thinks the 3 is some sort of hardcoded value that can’t be changed. To be perfectly honest, I can’t even figure out what “3” is even supposed to represent. (scratches head) ???

“3” means “RGB” in OpenGL1.0
The constants 1, 2, 3, and 4 were replaced by named constants in 1.1. Using a 4 would have worked in this case.

Cas :slight_smile: