1

I am working on a 2D game with sprite sheets. I might be completely overlooking something here, but I can't figure out for the life of me how to use sprite sheets with z-buffer testing.

Here are the conflicting issues as I understand them:

  1. A sprite in a spritesheet will have transparent pixels all around it that define where the sprite doesn't exist (since each frame in the sprite sheet "grid" is a rectangle, but the sprite is contained within that rectangle).

  2. Transparency doesn't work with z-testing.

Therefore, it's impossible to use spritesheets with the z-buffer? I should just use painter's algorithm then?

Are both of those conclusions correct?

user9219
  • 13
  • 1
  • 4

1 Answers1

2

You can make things transparent while using depth testing. You just have to do it correctly.

One way is with alpha test. If your transparency is a simple binary on/off (not translucency, where something can be partially transparent), then alpha testing is all you need. You can set your renderer to simply ignore fragments that have an alpha value less than a given alpha. Or greater than, but generally, you're probably looking for less than.

However, if there is actual translucency, then you will have to draw things back-to-front. You can use depth testing on non-translucent objects, but you'll have to turn off depth writes on the translucent ones.

Nicol Bolas
  • 26,068
  • 3
  • 76
  • 104
  • Thanks for the response. So alpha testing seems the way to go for sprites that are opaque, but just use alpha to indicate the binary "off" (no sprite pixel). – user9219 Aug 12 '11 at 14:07
  • Just wanted to ask though, there is no way when creating spritesheets in a graphical program (say GIMP) to actually specify that the intervening space between sprites doesn't exist, so that there are no pixels between the sprite, rather than using fully transparent colorless pixels? I suppose picture formats don't allow it? – user9219 Aug 12 '11 at 14:09
  • Sorry, reading a bit about alpha testing - it seems to come at the end of the pipeline, which introduces aliasing problems: http://blogs.msdn.com/b/shawnhar/archive/2011/05/06/antialiasing-alpha-cutouts.aspx. As a final question, do you happen to know how to do alpha testing in XNA 4.0? – user9219 Aug 12 '11 at 14:14
  • Hmm... it seems alpha testing was removed in DirectX 10. Do you know why this would be? http://www.gamedev.net/topic/462396-how-to-set-alpha-test-in-directx10/ – user9219 Aug 12 '11 at 14:20
  • @user9219: If you're using D3D10, then you're using shaders. And shaders can discard fragments (in D3D terms, it's texkill or somesuch) based on arbitrary conditions. As for the "aliasing problems", you're making a 2D sprite-based game; aliasing isn't exactly your biggest concern. And any space between sprites can be marked as transparent. How much space there is between sprites is up to you, since you're building the sprite sheet. – Nicol Bolas Aug 12 '11 at 17:29
  • I see. Thanks for the help. The transparent space between sprites is the problem though, despite being transparent, because without alpha-testing, they will still write to the depth buffer. I was asking if it was possible to make that space truly null, but I don't think graphics formats support that. – user9219 Aug 12 '11 at 20:13
  • @user9219: I just told you: you use texkill. This discards the "pixel". The depth buffer will not be written to. – Nicol Bolas Aug 12 '11 at 20:24
  • I'm with you on that part. I just misinterpreted what you were saying - I thought you meant making the space transparent is a separate solution from using texkill in the shader. Now I get it and we are talking about the same thing - using transparency as a condition for texkill is exactly what I plan on doing. – user9219 Aug 12 '11 at 20:28