How to do texture masking in LWJGL?

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 ::slight_smile:

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

Thanks,
Erik

Im not even gonna attempt to answer the LWJGL/OGL side of this question :stuck_out_tongue:

All im gonna say, is…

psp can create png files with an alpha mask.

in the menus :-
Masks/new/from Image/

(point it at an image from which you want the mask creating - perhaps a greyscaled version of the original)

then…

Masks/save to Alpha Channel/

abu,

Took me a long time to figure out how to do this in photoshop. But yes, you can save png files with an alpha channel.

Ah, yes found it, thanks.
It seems to depend on the color format you choose whether it will save the alpha channel or not.

But… the trouble continues when you actually try to load the image with alpha channel in open-gl: The image will look totally screwed :-[

(I used the texture loading code from the examples, the same method used in the texture loading lib from chman).

Greetings,
Erik

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 :slight_smile: 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.

Cas :slight_smile:

Thanks Cas, I think you hit the spot.

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? ::slight_smile:

Erik

Here’s a good resource: the Answer :stuck_out_tongue:


gl.enable(GL.BLEND);
gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
gl.textureEnv(GL.TEXTURE_ENV, GL.TEXTURE_ENV_MODE, GL.MODULATE);
gl.color4ub((byte)255, (byte)255, (byte)255, (byte)255); // You can fade something out in its entirety by altering alpha here too
// Now draw your quads
gl.disable(GL.BLEND);

And of course make sure whatever textures you’ve bound to have been loaded as GL.RGBA and stored as GL.RGBA.

Cas :slight_smile:

I do the same and it work 8)

tip : if you got bleding probs on other objects it’s cause blending source was changed

I must be extremely stupid, cos it still doesn’t work… :’(
It’s all totally opaque now…

my code just before drawing:


      gl.enable(GL.BLEND);             gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA); 
      gl.texEnvf(GL.TEXTURE_ENV, GL.TEXTURE_ENV_MODE, GL.MODULATE); 
      gl.color4ub((byte)255, (byte)255, (byte)255, (byte)255); // You can fade something out in its entirety by altering alpha here too 

and loading and generating the texture


            /* Generate The Texture */
            gl.texImage2D(GL.TEXTURE_2D, 
                          0, 
                          3, 
                          textures[i].getWidth(), 
                          textures[i].getHeight(), 
                          0, 
                          textures[i].hasAlpha() ? GL.RGBA : GL.RGB, 
                          GL.UNSIGNED_BYTE, 
                          textures[i].getPtr());

I now textures[i].hasAlpha() == true, I checked.
It’s determined at:


        hasAlpha = bufferedImg.getAlphaRaster() != null;
        System.out.println("Has Alpha:" + hasAlpha());

When I look in the pic’s properties, I can also see it has an alpha channel as well as when I just look at the picture I can see the background on the parts where that show up black in the game…

:-/ :’( ??? :’( >:( :o :frowning: :stuck_out_tongue: :-[
(there’s no smiley for ‘banging his head violently against a concrete wall’)

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.

Aaargh, I am stupid ;D

Thanks a lot!

::slight_smile: :stuck_out_tongue: (recovering from major head-ache)

And you shouldn’t be using 3 or 4 there anyway - they’re OpenGL1.0 hacks - you should use GL.RGB or GL.RGBA (for 8/8/8 and 8/8/8/8 format anyway).

Cas :slight_smile:

don’t forget that :

gl.enable(GL.BLEND);
gl.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);

…sigh… :’(

Well, it’s a bit closer to do success, but still not quite ok.
It seems to be ok as long as the background is white. When it isn’t, the parts that should be fully masked become dark to black.

Look at http://www.mycgiserver.com/~movegaga/gltest1.jnlp
The stars you have to gather had in an earlier version black blocks around them, now they haven’t but only when the background is bright white (like in the middle of the screen).

I’m sorry for being such a drag but I still don’t get it even after all this help I already got… :-[

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.

Cas :slight_smile:

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.

Your using depth-testing aren’t you :slight_smile: ?

Cas :slight_smile:

Yes, I am. :slight_smile:
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.