Depth problems in 2d

Hi, I have a list of polygons stored in a HashSet.
I start with int depth = 0;

I add the polygons in one at a time. Each time I add a polygon i increase the depth.


Set<Polygon> polygons = new HashSet<Polygon(); //NOT an awt polygon! my own implementation.
int depth = 0;

...

public void AddPolygon(Polygon p)
{
       p.setDepth(depth++);
       polygons.add(p);
}


Because I am using a hashset, the polygons are obviously unordered. I want it this way. I only want rendering to be ordered. (Smallest depth rendered first)

in my render method I:

  1. Enable depth testing
  2. Loop through and draw all polygons
  3. Disable depth testing

The problem is, some polygons are rendered out of order even though their depths are right.
Is there some setting I have missed, or something I am doing wrong?

Thanks,
roland

  • How do you set up the near and far-plane for your projection matrix (glOrtho())?
  • Have you requested a depth buffer?
  • Are you clearing it each frame?

Thanks for the reply, theagentd

  • GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -10000, 0);

  • ?

  • GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

Requesting a depth buffer: glEnable(GL_DEPTH_TEST);

In addition to enabling depth testing, you should also be requesting a depth buffer when creating the Display – i.e. by specifying a PixelFormat with a depth buffer, or by using the default PixelFormat (which uses 8 bits depth).

As to your problem; make sure your z values are within the near/far range you’ve set in glOrtho.

More reading on depth:
http://www.opengl.org/archives/resources/faq/technical/depthbuffer.htm

Why are you using depth sorting? For a 2D game it’s generally better to handle this yourself; i.e. sort for draw order.

Your valid depth range is therefor -10000 to 0. If the depth values you’re supplying is in the 0 to 10000 range they will all be culled and nothing will appear. Your glClear() code looks fine though.

No. glEnable(GL_MULTISAMPLE); does not magically give you multisampling. You have to supply a PixelFormat to Display.create() to get a depth buffer so the depth is actually stored when rendering like Davedes said. The standard is a 24 bit depth buffer.

The image links seem to be dead, but…

wut…when did I mention multisampling? O_o

glEnable(GL_MULTISAMPLE); does not magically give you a multisampled color buffer.
glEnable(GL_DEPTH_TEST); does not magically give you a depth buffer.

magic ALIENS!

Revelation 21:8

[quote]But the cowardly, the unbelieving, the vile, the murderers, the sexually immoral, those who practice magic arts, the idolaters and all liars —they will be consigned to the fiery lake of burning sulfur. This is the second death.
[/quote]

[quote]I guess IT is fucked…
[/quote]

[quote]Oh come on. Magic is all about weird clothes, secret societies, strange languages, darkened catacombs, and a system of mystical thinking that has little bearing on logic. IT is nothing like th-

Mother of god.
[/quote]
/. FTW! Always read the comments! =D

Mind = Blown.

I’m still confused in what I actually need to do. All these sites say “Create a depth buffer on initialisation” But how?!?!?
What do I do to magically get a depth buffer?

What is the advantage of manually sorting depths?

Thanks :slight_smile: