I'm trying to create 2d sprites using quads and textures and blending.I've done a lot of experimenting with this, and I know the NeHe tutorial about the topic but I can't get it to work without creating separate masks (like in the NeHe tutorial, seems a bit awkward).I found that png, jpg and gif don't support an alpha channel (at least psp can't create one in those formats), I found that paletted images with a transparency color won't work and I tried to create an alpha channel when the image is loaded but couldn't get that to work either.

In short, I'm stuck

Is the 'NeHe-way' (creating a 2nd image for the mask) really the way to go?

If the texture's screwed it means that you've specified RGB format when the file is in RGBA format, or specified RGBA format when the file is in RGB format.

Either way you're doing something a bit wrong Check the file size of the image; it should be approximately 4 x width x height plus a teeny overhead. If it is, then you know you've saved it in RGBA format, in which case your problem is that your dest parameter to create the GL texture might not be GL.RGBA or your source parameter might not be GL.RGBA either.

Hmmm... Still I can't get masking to work. :-/I got an image with alpha channel, but I don't fully understand how glBlendFunc() works:What's the source and what's the destination exactly? (everytime think I got it, the results are different than what I expected).What blend function should I use for masking using an image with alpha channel?Any good resources that even I can understand?

The problem is the third parameter of gl.texImage2D() - 3. In OpenGL 1.0 the parameter means internal number of color components per pixel, 3 for RGB, 4 for RGBA... So your alpha channel simply disappears because OpenGL stores your texture internally as RGB.

OpenGL 1.1 and above uses internalFormat parameter instead of number of color components. I suggest you to use GL.RGB and GL.RGBA as the third parameter value.

Well, I don't know, I'm still an opengl noob, but I did masking it with a gl.enable(gl.AlphaTest) and something like gl.alphaFunc(GL.GREATER, 0.5) or so (I haven't code here). It works so far, but is only good for masking (single bit alpha) and I do not know if it is faster or slower than the other method mentioned in this thread. That was explained in the redbook in chapter 10 (I think).

I'm using the loadTexture from the Nehe-examples, only had to change all RGB to ARGB and one 3byte_rgb to 4byte_argb (well, I'm not really loading, I'm currently drawing textures with java2d at startup and then convert them ...)

The alpha test method ("cookie cutter") is considerably faster than blending as no pixel reads need to be done to blend the pixel (ie. GL_BLEND is disabled). It doesn't look as nice of course when you've got lovely alpha-antialised edges.

Well, the alpha blending method cas described seems to work after all. The problem is that it only works with the tunnel removed. :-/I think the tunnel looks too cool to sacrifice though.Even when I make the tunnel totally opaque, I still get strange glitches.

Yes, I am. When I disable depth testing for blended objects (as also suggested in the nehe tutorials), the strange thing is that *everything* becomes transparent. And I made sure enabled it again after drawing the translucent objects.

What I did was a gl.disable(GL.DEPTH_TEST) before drawMesh() and gl.enable(GL.DEPTH_TEST) after drawMesh() in case of transparency or masking.When I did that, everything became transparent even the player.

When I disable depth testing for blended objects (as also suggested in the nehe tutorials), the strange thing is that *everything* becomes transparent.

Oh, hang on, that's wrong isn't it?

If you're doing the easy blending hack, you want to render all the opaque objects as normal, then disable depth buffer *writing* (using gl.depthMask), but leave depth buffer *testing* on, while rendering all the transparent objects. Give that a try.

Ah, just found what's wrong.It was a very stupid mistake (I'm good at that. I managed to overlook that stupid mistake for at least a hundred times).In the draw ordering code, I didn't regard masked objects as using blending, so masked object were always drawn before translucent objects. (I do a depth sort on all objects, then 1st draw opaque objects, then blended ones).

But thanks anyway for the explanation about depth testing cfmdobbie, I think I understand now

Ah, heh, I do that a lot as well. My current problem is an app that works fine with the release library, but OpenGL seems to be throwing an "invalid operation" on a gl.end() call with the debug library... Arrrgh! There's gotta be something silly I've screwed up somewhere, but I can't for the life of me work out where... Between the gl.begin() and gl.end() I've only got gl.vertex3f() and gl.color4fv() calls...

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org