Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to do texture masking in LWJGL?  (Read 8643 times)
0 Members and 1 Guest are viewing this topic.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Posted 2003-01-27 17:29:02 »

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  Roll Eyes

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

Thanks,
Erik

Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #1 - Posted 2003-01-27 17:51:41 »

Im not even gonna attempt to answer the LWJGL/OGL side of this question 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,

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Jacko

Junior Member





« Reply #2 - Posted 2003-01-27 20:24:16 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #3 - Posted 2003-01-28 06:41:00 »

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  Embarrassed

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

Greetings,
Erik

Offline princec

JGO Kernel


Medals: 363
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2003-01-28 09:27:33 »

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 Smiley 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 Smiley

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #5 - Posted 2003-01-28 09:43:46 »

Thanks Cas, I think you hit the spot.

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #6 - Posted 2003-02-04 06:16:45 »

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?  Roll Eyes

Erik

Offline princec

JGO Kernel


Medals: 363
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2003-02-04 10:31:08 »

Here's a good resource: the Answer :p

1  
2  
3  
4  
5  
6  
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 Smiley

Offline vrm

Junior Member




where I should sign ?


« Reply #8 - Posted 2003-02-04 11:13:50 »

I do the same and it work  Cool

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

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #9 - Posted 2003-02-04 15:06:17 »

I must be extremely stupid, cos it still doesn't work..  Cry
It's all totally opaque now...

my code just before drawing:

1  
2  
3  
      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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
            /* 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.hasAlpha() == true, I checked.
It's determined at:

1  
2  
        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...

:-/ Cry Huh Cry Angry Shocked Sad Tongue Embarrassed
(there's no smiley for 'banging his head violently against a concrete wall')

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline alexz

Senior Newbie




Java games rock!


« Reply #10 - Posted 2003-02-04 19:24:56 »

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.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #11 - Posted 2003-02-04 19:50:05 »

Aaargh, I *am* stupid  Grin

Thanks a lot!

Roll Eyes Tongue (recovering from major head-ache)

Offline princec

JGO Kernel


Medals: 363
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #12 - Posted 2003-02-04 20:00:55 »

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 Smiley

Offline vrm

Junior Member




where I should sign ?


« Reply #13 - Posted 2003-02-05 05:15:57 »

don't forget that :

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

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #14 - Posted 2003-02-05 17:10:06 »

.......sigh............  Cry

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...  Embarrassed

Offline Themroc

Junior Member





« Reply #15 - Posted 2003-02-06 15:25:39 »

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 ...)
Offline princec

JGO Kernel


Medals: 363
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #16 - Posted 2003-02-06 16:22:48 »

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 Smiley

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #17 - Posted 2003-02-09 09:40:38 »

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.

Offline princec

JGO Kernel


Medals: 363
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #18 - Posted 2003-02-09 13:42:40 »

Your using depth-testing aren't you Smiley ?

Cas Smiley

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #19 - Posted 2003-02-09 15:48:43 »

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


Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #20 - Posted 2003-02-09 16:46:10 »

Hrm, there's something funky going on.  Would it be possible to post the relevant sections of your code so we can see what's going on?

Hellomynameis Charlie Dobbie.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #21 - Posted 2003-02-09 17:07:53 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
      /**
       * Draw the actor
       */

      public void draw() {
            if (alive) {

                  if (lightingEnabled) {
                        gl.enable(GL.LIGHTING);
                  }

                       // set the texture
                      if (currentTextureIndex >= 0) {
                             gl.enable(GL.TEXTURE_2D);
                      gl.bindTexture(GL.TEXTURE_2D, texPool.getTexBuf(textureIDs[currentTextureIndex]).get(0));
                       }

                       // set color
                      if (alpha >= 0) {
                        gl.color4ub((byte) red, (byte) green, (byte) blue, (byte) alpha);
                       } else {
                             gl.color4ub((byte) 255, (byte) 255, (byte) 255, (byte) 255);
                       }

                  // Blending Function For Translucency Based On Source Alpha Value
                 if (translucent) {
                        gl.blendFunc(blendSrc, blendTgt);
                        gl.enable(GL.BLEND);
                  } else if (masked) {
                        //System.out.println("masked");
                             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
                 }

                       
                       // translate to location
                      gl.translatef(x, y, z);
                       
                       // rotate
                      gl.rotatef(rot, rx, ry, rz);
                                 
                     drawMesh();

                       // rotate back
                      gl.rotatef(-rot, rx, ry, rz);

                       // translate back to origin
                      gl.translatef(-x, -y, -z);
                       
                       if (translucent || masked) {
                             gl.disable(GL.BLEND);
                       }

                       if (currentTextureIndex >= 0) {
                             gl.disable(GL.TEXTURE_2D);
                       }

                        if (lightingEnabled){
                              gl.disable(GL.LIGHTING);
                  }
                                                        }                  
      }
     
      protected void drawMesh() {
        // draw the quad
       // TO DO: non-square sprites
       
        float ts = this.textureSizeLocked ? scale : 1f;
        ts /= textureScale;
       
          gl.begin(GL.QUADS);
                gl.normal3f(0,0,1);
             
              gl.texCoord2f(0.0f + this.textureXOffset, 0.0f + this.textureYOffset);
              gl.vertex3f(-0.5f * scale, -0.5f * scale, 0f);
             
              gl.texCoord2f(ts + this.textureXOffset, 0.0f + this.textureYOffset);
              gl.vertex3f(+0.5f * scale, -0.5f * scale, 0f);
             
              gl.texCoord2f(ts + this.textureXOffset, ts + this.textureYOffset);
              gl.vertex3f(+0.5f * scale, +0.5f * scale, 0f);
                 
                  gl.texCoord2f(0.0f + this.textureXOffset, ts + this.textureYOffset);
                  gl.vertex3f(-0.5f * scale, +0.5f * scale, 0f);
          gl.end();
      }            


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.

Greetings,
Erik

Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #22 - Posted 2003-02-09 18:01:31 »

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


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.

Hellomynameis Charlie Dobbie.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #23 - Posted 2003-02-09 20:13:20 »

Ah, just found what's wrong.
It was a very stupid mistake (I'm good at that.  Tongue 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  Smiley

Greetings,
Erik

Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #24 - Posted 2003-02-09 20:40:10 »

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...  Angry  Angry  Angry

Hellomynameis Charlie Dobbie.
Offline princec

JGO Kernel


Medals: 363
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #25 - Posted 2003-02-09 20:52:04 »

oooops, that sounds like our problem to me.
I bet the CHECK_ERROR macro has sneaked into one of those calls (you can't check GL errors between begin and end)...

2 mins later: Whoops, you're dead right, it was buried in some of the glColor calls. Fixed in CVS; expect a new release out maybe... Monday? Elias? Brian?

Cas Smiley

Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #26 - Posted 2003-02-09 22:05:43 »

Hurrah!  Not my problem! Grin

Phew, I'll stop fiddling with it for tonight then.  I've got an interview tomorrow but just couldn't go to bed while this problem persisted... Wink  I'll go now...

Hellomynameis Charlie Dobbie.
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #27 - Posted 2003-02-10 03:48:02 »

Quote
2 mins later: Whoops, you're dead right, it was buried in some of the glColor calls. Fixed in CVS; expect a new release out maybe... Monday? Elias? Brian?

That all depends on some of the many recent checkins. However it will be soon. There are some things- feature wise -  we need to clarify first though.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!