Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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  
  Depth problems in 2d  (Read 2550 times)
0 Members and 1 Guest are viewing this topic.
Offline roland
« Posted 2012-05-23 05:36:44 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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


Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #1 - Posted 2012-05-23 06:49:43 »

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

Myomyomyo.
Offline roland
« Reply #2 - Posted 2012-05-23 06:54:33 »

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

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-05-23 19:18:28 »

Requesting a depth buffer: glEnable(GL_DEPTH_TEST);

Offline davedes
« Reply #4 - Posted 2012-05-23 19:49:06 »

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.

Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #5 - Posted 2012-05-23 23:07:13 »

- 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);   
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.

Requesting a depth buffer: glEnable(GL_DEPTH_TEST);
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.

Why are you using depth sorting? For a 2D game it's generally better to handle this yourself; i.e. sort for draw order.
The image links seem to be dead, but...
http://www.java-gaming.org/topics/heightmapping-sprites/25002/view.html

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-05-23 23:19:51 »

Requesting a depth buffer: glEnable(GL_DEPTH_TEST);
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.
wut....when did I mention multisampling? O_o

Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #7 - Posted 2012-05-23 23:37:50 »

Requesting a depth buffer: glEnable(GL_DEPTH_TEST);
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.
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.

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-05-23 23:53:17 »

*magic* ALIENS!

Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #9 - Posted 2012-05-24 00:18:09 »

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
I guess IT is f**ked....

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.

/. FTW! Always read the comments! =D

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

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2012-05-24 00:30:24 »

Mind = Blown.

Offline roland
« Reply #11 - Posted 2012-05-24 10:11:19 »

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

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #12 - Posted 2012-05-24 11:28:42 »

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

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

toopeicgaming1999 (73 views)
2014-11-26 15:22:04

toopeicgaming1999 (62 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (56 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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