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?


1 Answer 1


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.

  • \$\begingroup\$ 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). \$\endgroup\$
    – user9219
    Aug 12, 2011 at 14:07
  • \$\begingroup\$ 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? \$\endgroup\$
    – user9219
    Aug 12, 2011 at 14:09
  • \$\begingroup\$ Sorry, reading a bit about alpha testing - it seems to come at the end of the pipeline, which introduces aliasing problems: blogs.msdn.com/b/shawnhar/archive/2011/05/06/…. As a final question, do you happen to know how to do alpha testing in XNA 4.0? \$\endgroup\$
    – user9219
    Aug 12, 2011 at 14:14
  • \$\begingroup\$ Hmm... it seems alpha testing was removed in DirectX 10. Do you know why this would be? gamedev.net/topic/462396-how-to-set-alpha-test-in-directx10 \$\endgroup\$
    – user9219
    Aug 12, 2011 at 14:20
  • \$\begingroup\$ @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. \$\endgroup\$ Aug 12, 2011 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .