Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (428)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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 2316 times)
0 Members and 1 Guest are viewing this topic.
Offline jonjava
« Posted 2012-04-17 22:42:56 »

Hello.

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?


Thanks,
jon

Offline sproingie

JGO Kernel


Medals: 200



« Reply #1 - Posted 2012-04-17 23: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-18 00:16:17 »

You need to also clear the depth buffer along with the color buffer:
1  
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


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
Online princec

JGO Kernel


Medals: 284
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2012-04-18 00: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  
 
 
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.

xsi3rr4x (79 views)
2014-04-15 18:08:23

BurntPizza (71 views)
2014-04-15 03:46:01

UprightPath (82 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (82 views)
2014-04-14 15:48:38

tom_mai78101 (106 views)
2014-04-10 04:04:31

BurntPizza (166 views)
2014-04-08 23:06:04

tom_mai78101 (262 views)
2014-04-05 13:34:39

trollwarrior1 (212 views)
2014-04-04 12:06:45

CJLetsGame (221 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!