Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (843)
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  
  [SOLVED] 2D z-culling?  (Read 3883 times)
0 Members and 1 Guest are viewing this topic.
Online jonjava
« Posted 2012-04-17 20:42:56 »


Getting into 3D I was wondering if there exists a similar efficient algorithm for drawing 2d sprites with z-culling. Ie. not drawing things that are behind other things unnecessarily.

Is this available in Java2D? What I've been doing previously is just drawing objects (and their images) based on their depth value that I store in an arrayList ( and sort as new objects are added to keep it in order ). This has worked fine.. to some extent.

What about when using an OpenGL library, is it as simple as turning some z-culling switch ON and giving Textures a depth value of some sorts?


Offline sproingie

JGO Kernel

Medals: 202

« Reply #1 - Posted 2012-04-17 21:35:39 »

The way you're doing it in Java2D is pretty much the right way -- draw back to front, don't bother drawing what's obscured by something in front.  With OpenGL, it is as easy as turning on depth testing and assigning z-values to the geometry (not the textures), and it'll all get sorted out in the z-buffer.  Complex scenes will still want to do their own occlusion culling, but that's usually not necessary for anything 2D.

Offline davedes
« Reply #2 - Posted 2012-04-17 22:16:17 »

You need to also clear the depth buffer along with the color buffer:

And make sure the z value is within near and far defined in glOrtho. You might also want to enable alpha testing depending on what you're rendering.

I personally just render in order with lists/sorting, as I haven't found it to be a CPU bottleneck in my simple games.

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

« JGO Spiffy Duke »

Medals: 1033
Projects: 3
Exp: 20 years

Eh? Who? What? ... Me?

« Reply #3 - Posted 2012-04-17 22:23:30 »

Mostly you don't want to use the depth buffer though, because you are likely going to be drawing transparent sprites with nice antialised edges. So although you can cheat if you're doing super retro (in which case you're probably wasting your time with this optimisation anyway!) and use something called the alpha test to draw the equivalent of cookie-cutter or "one bit" transparency with the depth buffer successfully, use any translucent sprites and it won't work.

tl;dr - don't use the depth buffer, sort your sprites manually back to front before rendering.

Cas Smiley

Pages: [1]
  ignore  |  Print  

EgonOlsen (37 views)
2018-06-10 19:43:48

EgonOlsen (20 views)
2018-06-10 19:43:44

EgonOlsen (39 views)
2018-06-10 19:43:20

DesertCoockie (186 views)
2018-05-13 18:23:11

nelsongames (122 views)
2018-04-24 18:15:36

nelsongames (121 views)
2018-04-24 18:14:32

ivj94 (861 views)
2018-03-24 14:47:39

ivj94 (122 views)
2018-03-24 14:46:31

ivj94 (758 views)
2018-03-24 14:43:53

Solater (139 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!