Do you activate depth buffer when doing 2D games and opengl es?

Member
Posts: 249
Joined: 2008.10
Post: #1
Hi dudes.

Do you activate depth buffer when doing 2D games and opengl es?
Or just create a scenegrapgh (a vector) where you have your sprites ordered from back to from, and render them from 0 to vector.size-1?

I ask you this question because I know depth buffer is expensive on the iphone.

Thanks for suggestions. Smile
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #2
glDisable(GL_DEPTH_TEST)

You then need to draw your stuff in order (closest stuff needs to be drawn last)

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #3
Najdorf Wrote:glDisable(GL_DEPTH_TEST)
You then need to draw your stuff in order (closest stuff needs to be drawn last)
I know how to (de)activate depth buffer.

I ask if you use it, or if it is useful (cost). And by the way, do you reorder this vector every frame? (using for instance std::sort). Do not forget I have to add and remove objects all the time (bullets for instance) and manage transparencies.

Thanks for suggestions.
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #4
I'd be interested in other people's ideas here too. I currently give all my game object classes a drawLayer:(int)layer method. Then in my drawing method I do this:

Code:
for (layer=0; layer<=5; layer++) {
    for (GameObject * object in game.objects) {
        [object drawLayer:layer];
    }
}

This means that every object can draw its shadow to layer 1 and its sprite to another layer.

Works for me but I can imagine there are better ways.
Quote this message in a reply
Member
Posts: 36
Joined: 2009.02
Post: #5
I deactivate the depth buffer and use layers in a similar but less object oriented way as maximile's example.

I'm currently working in a space shooter game in plain C and my draw function doesn't use layers explicitly, but does something similar by drawing in this order:

- Background
- Shots (bullets, etc.)
- Enemies
- Player
- Explosions
- Score board
- Ingame messages

In my case, I'm lucky I don't need to reorder stuff, so this method works fine for me.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
If everything you're drawing is perpendicular to the viewing angle, I'd generally think it's easier and less resource-hungry to not create a depth buffer and manage the ordering yourself.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #7
riruilo Wrote:by the way, do you reorder this vector every frame? (using for instance std::sort). Do not forget I have to add and remove objects all the time (bullets for instance) and manage transparencies.

Shmup-dev has a decent tutorial on bullet management, if you want to use STL.

Personally I think the simple "sparse array" solution works just fine, with no per-frame sorting at all:

1) decide how many bullets you will ever have active simultaneously.
2) allocate a static array for them. Each entry is the minimal bullet object info-- not the rendering info.
3) each frame, update bullet according to game logic. Dead bullets are marked dead, but not removed from the array. New bullets are added to the end of the array.
4) when array is full, compact array to remove all dead bullets. This is O(N), and is only done once every couple hundred frames, depending on your spawn/die rate.
5) when you draw, scan through the entire array and construct rendering info into vertex buffers, for each non-dead bullet. This is also O(N). Draw order is always maintained as FIFO this way, so overlapping transparencies work.
Quote this message in a reply
Member
Posts: 39
Joined: 2008.09
Post: #8
I think it works this way: when you sort yourself, you're using CPU power, when you use depth buffer, you drain GPU. So depending on what you need more, you can use either this or other method. I'm using standard sort() function - for me the bottleneck is video, not processing power. I've also optimized my routine to only resort once every 30 frames or by demand, which is what you can't do using depth buffer.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #9
I just draw the objects in general order, like background first, then an array of things above the background, then other stuff, then foreground stuff and so on. Probably only use like maybe half a dozen arrays.

I have no idea if any of this makes sense to anyone, but I'll try to describe what I do:

For adding and deleting from the arrays, I compact on the fly as I process each one, each tick. I do this with four total arrays for each set of objects:

- main array containing a generic struct for each object
- array containing indices of available objects from the main array used as a stack
- array of active object indices 1 (front buffer of objects, so to speak)
- array of active object indices 2 (back buffer of objects, so to speak)

The main array is the only relatively large one, since it has the actual structs. The other three arrays simply deal with indices into the main array, so they're relatively small, with just an int or unsigned int in each slot.

So when I add an object, I'm actually adding its index to the front array by taking the next available index off the stack. Then when I process all the active objects in the front array, I copy the index of each object that will be alive next tick into the back buffer. If it was a deleted object then its index is simply not copied to the back buffer, which is how I automatically compact the array every update. Deleted object indices are pushed onto the stack of available objects. At the end of the update of the array, just swap the buffers. It probably sounds complicated, but it is very efficient and incredibly simple.

For depth sorting: I haven't tried it yet, but I've thought a bit about using a similar system, but as a binary tree to do depth sorting on the fly each tick too. Haven't needed that yet though...
Quote this message in a reply
Member
Posts: 87
Joined: 2006.08
Post: #10
Also, don't discount the depth buffer approach without measuring it.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #11
Frogblast Wrote:Also, don't discount the depth buffer approach without measuring it.
Did anyone do it?

On the other hand, do you usually use standard template library? (std::vector or std::list) or do you create your own structures/functions?

@maximile,miq01,AnotherJake So, you propose create several vectors, one for every "plane" and render the most depth planes firstly...
I think I will use this aproaching.

Thanks.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #12
> Did anyone do it?

Yeah, I've used the depth buffer for 2D. It works fine, but costs some precious iPhone RAM and I find that it's not needed for my 2D stuff, so I choose to skip it.

Have to use it for 2.5D though (3D mesh models represented in a 2D game). I still sort the objects by general grouping. I do give them an individual z value "slot" so they don't pass through each other, which can look weird. The z values are spread out so that they're roughly equal to the thickest object. Of course, things are somewhat limited in that there is only so much you can fit between the near and far planes before precision starts to be a factor, but on iPhone, there just isn't enough screen space to be drawing too many objects in the first place. With purely flat sprites, obviously you wouldn't need much spread, so I can fit those in pretty tight.

> On the other hand, do you usually use standard template library? (std::vector or std::list) or do you create your own structures/functions?

I just use my own structs and functions. I would imagine STL should work just fine for you though.

Hehe... I did try NSArray once for kicks, but it was pretty slow.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #13
AnotherJake Wrote:> Did anyone do it?

Yeah, I've used the depth buffer for 2D. It works fine, but costs some precious iPhone RAM and I find that it's not needed for my 2D stuff, so I choose to skip it.

Have to use it for 2.5D though (3D mesh models represented in a 2D game). I still sort the objects by general grouping. I do give them an individual z value "slot" so they don't pass through each other, which can look weird. The z values are spread out so that they're roughly equal to the thickest object. Of course, things are somewhat limited in that there is only so much you can fit between the near and far planes before precision starts to be a factor, but on iPhone, there just isn't enough screen space to be drawing too many objects in the first place. With purely flat sprites, obviously you wouldn't need much spread, so I can fit those in pretty tight.

> On the other hand, do you usually use standard template library? (std::vector or std::list) or do you create your own structures/functions?

I just use my own structs and functions. I would imagine STL should work just fine for you though.

Hehe... I did try NSArray once for kicks, but it was pretty slow.

Thanks a lot, very helpfull.
Quote this message in a reply
Member
Posts: 36
Joined: 2009.02
Post: #14
riruilo Wrote:@maximile,miq01,AnotherJake So, you propose create several vectors, one for every "plane" and render the most depth planes firstly...
I think I will use this aproaching.
Depends on the type of game, but if it's possible, I'd go for it. Also, the statically allocated arrays arekkusu mentions avoid having to reserve/release memory each time a new entity (enemy, bullet, ...) appears or disappears.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Depth Buffer woes in OpenGL ES 2.0 Macmenace 5 7,828 Mar 8, 2010 10:45 AM
Last Post: Macmenace
  opengl es 2.0 and the stencil buffer NelsonMandella 1 4,948 Sep 30, 2009 06:09 AM
Last Post: arekkusu
  For 2D (sprite) games, do I use OpenGL or something else? lindsay 24 12,160 May 9, 2009 12:06 PM
Last Post: Weltevrede
  Reading the depth buffer? Plus gluUnProject Bachus 7 7,247 Mar 5, 2009 04:29 AM
Last Post: ShadoFlameX
  Vertex buffer objects in OpenGL ES on iPhone ZooMan 5 7,533 Feb 4, 2009 07:00 PM
Last Post: ZooMan