Weird transparency problem

Hello all. I’m doing a 2d-game with LWJGL. I’m drawing 64x45 tiles in the level and I wanted to draw only the ones that have changed. First I used pbuffer to achieve this but it was too slow and clumsy way to do it. So now i’m simply not using glClear(GL_COLOR_BUFFER_BIT) at all which works great… in windowed mode. In fullscreen mode with V-sync disabled I’m getting horrible flickering with black lines. In fullscreen mode with V-sync enabled the flickering goes away but the tiles are transparent and I see the main menu background image through the tiles - even though I do glClear(GL_COLOR_BUFFER_BIT) before changing to level’s rendering. So the tiles for some reason are about 50% translucent.

Because I’m not rendering the tiles all the tme i have to update the tiles which are under mouse cursor all the time. But in fullscreen mode it either doesn’t work correctly - if I move the mouse too quickly it leaves a trace (these ghost cursors are transparent too even though they shouldnt be).

The weirdest thing is that when my game was running and I got a notice from Windows Messenger saying that someone logged in (a pop-up window), all those problems did go away and it worked just like it was supposed to (until I restarted the game of course).

I’m not using depth-buffer at all (setting it to 0 bits size upon display creation).

I have Windows XP, Radeon 8500 (catalyst 4.5, newest) and LWJGL 0.9a.

Anyone have any ideas what might be happening here?

Always draw the whole scene every frame.
Always make sure you call Window.update() every frame, no matter what. Which means you must always render every frame, too, as Window.update() swaps the buffers, and if you’ve failed to draw anything in a buffer it’ll just appear with whatever was last in it.

Cas :slight_smile:

So even though I could get rid of the transparency effects/flickering on my comp, this method would be a ‘loose cannon/unsupported’ way?
I guess I’ll switch to constant rendering then, but I’m gonna use a buffer because it’s slow to draw all those 2880 images (they’re displayed all the time and most of them are static).
I’m new to opengl and pbuffers are actually the only way I know how this is done. What way would you suggest to render to an offscreen buffer or texture?

This was so easy with java2d and bufferedimages…

[quote]So the tiles for some reason are about 50% translucent
[/quote]
Sounds like you need to disable blending.

[quote]I’m not using depth-buffer at all (setting it to 0 bytes size upon display creation).
[/quote]
This is not the correct way of disabeling the z buffer. The docs says that you will get mininum 0 bits, you may get more. Instead use “glDisable(GL11.GL_DEPTH_TEST);” to disable the depth test.

[quote]but I’m gonna use a buffer because it’s slow to draw all those 2880 images
[/quote]
It depends on how you draw your images :wink: Can’t see why there would be a performance problem if you do it correctly.

OpenGL is not the same as java2d! Doing the same java2d optimizations in opengl is not the way to go. Try to rethink the problem and come up with another solution. I’m sure we can give you some suggestions if you give a good description of exactly what you are trying to accomplish.

Tried it already. All I got is black box around the cursor.

No luck here either… thanks for the info though.

I’m sure this ain’t the fastest way to do it but I thought it’d be fine if I jsut could render the entities i need to (most of the entities dont need to be rendered most of the time).
Now i’m getting ~80fps on my 1200mhz TB without sounds/multiplayer (rendering every tile evrey tick) and I want the game to run on older machines too. It’s actually a remake of an old dos game called Minebombers. Currently I’m storing all the game images in one texture and just drawing them as textured quads.

Entity:


      public final void render()
      {
            if(!changed)
                  return;
            if(!visible)
                  return;
            
            GL11.glPushMatrix();
                  GL11.glTranslatef(position.x,position.y,0);
                  GL11.glBegin(GL11.GL_QUADS);
                        GL11.glTexCoord2f(getTexture().OFFX, getTexture().OFFY);
                        GL11.glVertex3f(0, 0, 0);
                        GL11.glTexCoord2f(getTexture().OFFX+getTexture().OFFW, getTexture().OFFY);
                        GL11.glVertex3f(size.x, 0, 0);
                        GL11.glTexCoord2f(getTexture().OFFX+getTexture().OFFW, getTexture().OFFY+getTexture().OFFH);
                        GL11.glVertex3f(size.x, size.y, 0);
                        GL11.glTexCoord2f(getTexture().OFFX, getTexture().OFFY+getTexture().OFFH);
                        GL11.glVertex3f(0, size.y, 0);
                  GL11.glEnd();
            GL11.glPopMatrix();
            changed = false;
      }

Level:


            GLTextureManager.getManager().bind("gameimages");
            GL11.glPushMatrix();
                  for(int y=0; y < size.h; y++)
                   for(int x=0; x < size.w; x++)
                         entities[x][y].render();
            GL11.glPopMatrix();

Screenshot:
http://koti.mbnet.fi/mc3712/mb1.gif

Did you try a profiler to check what really is the most expensive operation? I’m guessing that either:

  1. the if(!visible) check doesn’t work and you draw a lot more than you think.
  2. the glPush/Pop and glBegin/End/Vertex/TexCoord is the culprit and you need to convert the rendering to something more effective. First try using absolute coordinates in the glVertex calls so you can avoid glPush/Pop/Translate. Like this:

glVertex3f(0, 0, 0)

becomes

glVertex3f(position.x, position.y, 0);

and so on. If that doesn’t help, and the coordinates are constant, I’d use vertex arrays to render the tiles. to avoid the explicit Begin/End/Vertex/TexCoord calls.

  • elias

Should definitely use vertex arrays.

Cas :slight_smile:

Depth testing comes disabled by default, so you shouldn’t have to worry about that. Also, is your ~80fps actually 85 and locked to your refresh rate perchance?

I would just use a text routine and alternate that a bit.

Display lists are ok… that automated translate (as part of the display list) is also great… and the print method will just spit out complete lines (instead of Strings). Since each line is put into a ByteBuffer the overhead will be ok-ish. Well, after all it’s just like writing 45 lines of text on the screen.

I don’t say that that would be the fastest way, but it’s of corse faster than the slowest way - intermediate rendering (the thing you do right now) and additionally it wouldn’t be much work. Oh and compatibility won’t be an issue :wink:

Actually it isn’t even used and I should’ve removed it.

[quote]Also, is your ~80fps actually 85 and locked to your refresh rate perchance?
[/quote]
Nope no vsync and getting ~1000 fps in menu.

Here’s a snip from -XProf:


         Stub + native   Method                        
 28.0%     0  +  4995    org.lwjgl.opengl.Window.swapBuffers
 27.9%     0  +  4976    org.lwjgl.opengl.GL11.glBegin
  6.6%     0  +  1184    org.lwjgl.opengl.GL11.glPushMatrix
  5.9%     0  +  1053    Music.FmodAPI.NativeFmodJNI.FSOUND_Update
  5.5%     0  +   972    org.lwjgl.opengl.GL11.glTranslatef
  5.2%     0  +   920    org.lwjgl.opengl.GL11.glVertex3f
  4.1%     0  +   733    org.lwjgl.opengl.GL11.glTexCoord2f
  1.8%     0  +   315    org.lwjgl.opengl.GL11.glPopMatrix
  1.2%     0  +   215    org.lwjgl.opengl.GL11.glBindTexture

I guess I’ll try vertex arrays then, thanks everyone.

Since the game does not scroll its screen, I’d recommend using the simple and compatible display lists. You should only update the list when the actual contents of the level change, e.g. when the player makes a hole in the wall, etc… Also, you should consider, if it would be wise to store the tiles in the display lists in rows or columns. That way, you’d have a single row/column of tiles in a single list -> less frequent updates of lists. Will this become MB2, BTW? :wink:

Nope, just a remake of original with network game (completely free). I haven’t actually asked yet if the Skitso guys like about the idea of making a remake (since they are making MB2 at the moment - http://koti.mbnet.fi/pacbros/skitsoproductions/MBII.html), but i doubt they’d have anything against it (especially since I have a big MB2 ad. as a quit screen :)).

And thanks for the display list suggestion I’ll try it too. Though i’ve been to lazy to try vertex arrays yet either.

I tried to use vertex arrays but for some reason nothing showed up. I probably screwed something up with ByteBuffers since I ain’t very used to them yet.

But then I removed glTranslate() and push/pop matrix from entity’s render code, tried display lists and now I’m getting ~500 fps. Now that was an improvement I really wasn’t expecting :o I guess i’ll still try vertex arrays just for the comparison anyways.

[quote]But then I removed glTranslate() and push/pop matrix from entity’s render code, tried display lists and now I’m getting ~500 fps. Now that was an improvement I really wasn’t expecting :o I guess i’ll still try vertex arrays just for the comparison anyways.
[/quote]
Nice. I bet the vertex arrays provide only a few extra fps…