Karl
Karl

Reputation: 5733

iPhone OpenGL ES: Applying a Depth Test on Textures that have transparent pixels for 2D game

Currently, I have blending and depth testing turn on for a 2D game. When I draw my textures, the "upper" texture remove some portion of the lower textures if they intersect. Clearly, transparent pixels of the textures are taken into account of the depth test, and it clear out all the colors of the drawn lower textures if they intersect. Moreover, alpha blendings are incorrectly rendered. Are there any sort of functions that can tell OpenGL to not include transparent pixels into depth testing?

Upvotes: 2

Views: 2721

Answers (4)

chunkyguy
chunkyguy

Reputation: 3679

if you're using shaders, can try disabling blending and discard the pixels with alpha 0

if(texColor.w == 0.0)
    discard;

Upvotes: 2

OptimisticMonkey
OptimisticMonkey

Reputation: 518

What type of blending are you using?

glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Should prevent any fragments with alpha of 0 from writing to the depth buffer.

Upvotes: 0

Goz
Goz

Reputation: 62323

glEnable( GL_ALPHA_TEST );
glAlphaFunc( GL_EQUAL, 1.0f );

This will discard all pixels with an alpha of anything other than fully opaque. These pixels will, then, not be rendered to the Z-Buffer. This does, however, affect various Z-Buffer pipeline optimisations so it may cause some serious slowdowns. Only use it if you really have too.

Upvotes: 3

olliej
olliej

Reputation: 36783

No it's not possible. This is true of all hardware depth testing.

GL (full or ES -- and D3D) all have the same model -- they paint in the order you specify polygons. If you draw polygon A in before polygon B, and logically polygon A should be in front on polygon B, polygon B won't be painted (courtesy of the depth test).

The solution is to draw you polygons in order from farthest to nearest the current view origin. Happily in a 2D game this should just be a simple sort (one you probably won't even need to do very often).

In 3D games BSPs are the basic solution to this issue.

Upvotes: 2

Related Questions