Can't see behind transparent object with another transparent object?

Okay, so my problem is that I’ve got two enemy sprites on screen, and they each show whats behind them, like if I have a wall behind them then it shows them, then the wall, but if one enemy is half behind the other it gets cut off. Any idea why this is happening?

This is how I got openGL set up:


private void initGL() {

		/* OpenGL */
		int width = Display.getDisplayMode().getWidth();
		int height = Display.getDisplayMode().getHeight();

		GL11.glViewport(0, 0, width, height); // Reset The Current Viewport
		GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
		GL11.glLoadIdentity(); // Reset The Projection Matrix
		GLU.gluPerspective(45.0f, ((float) width / (float) height), 0.1f, 1000.0f); // Calculate The Aspect Ratio Of The Window
		GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
		GL11.glLoadIdentity(); // Reset The Modelview Matrix

		GL11.glShadeModel(GL11.GL_SMOOTH); // Enables Smooth Shading
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
		GL11.glClearDepth(1.0f); // Depth Buffer Setup
		GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
		GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Test To Do
		GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST); // Really Nice Perspective Calculations
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
   	}

In which order do you draw them? Using blending you always need to draw the objects furthest away first (it’s quite annoying, I know ;))

Mike

Oh my! is there a way around that? Maybe using something else?

Are you sure about the alpha channel in the texture?

Well, I can see behind it when it’s behind a wall, so yeah.

Problem is the depth buffer. The easiest way around it is to sort by distance, as said. It’s not as hard as it sounds.

Cas :slight_smile:

If you only use alpha values of 255 and 0 (nothing in-between) you can use something like


GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_GREATER, 0.9f);

Then you don’t have to sort it. But if you ever want to add something semi transparent you better build the engine correctly now already :slight_smile:

Mike

That worked, thank you ;D

You should read up on how the depth buffer (AKA z-buffer) works, or in this case, doesn’t. It doesn’t work for transparent surfaces unless you hack around it with sorting and disabling depth writes or alpha testing.