Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to draw a partly transparent texture a plane, but the problem is that it's incorrectly displaying what is behind that texture.

Pseudo code:

vertices1 
basiceffect1
// The vertices of vertices1 are located BEHIND vertices2

vertices2
basiceffect2
// The vertices of vertices2 are located IN FRONT vertices1

GraphicsDevice.Clear(Blue);
PrimitiveBatch.Begin();

//if I draw like this:
PrimitiveBatch.Draw(vertices1, trianglestrip, basiceffect1)
PrimitiveBatch.Draw(vertices2, trianglestrip, basiceffect2)
//Everything gets draw correctly, I can see the texture of vertices2 trough
//the transparent parts of vertices1

//but if I draw like this:
PrimitiveBatch.Draw(vertices2, trianglestrip, basiceffect2)
PrimitiveBatch.Draw(vertices1, trianglestrip, basiceffect1)
//I cannot see the texture of vertices1 in behind the texture of vertices2
//Instead, the texture vertices2 gets drawn, and the transparent parts are blue
//The clear color

PrimitiveBatch.Draw(vertice
PrimitiveBatch.End();

My question is, Why does the order in which I call draw matter?

Edit: Added screenshots

Background is drawn, then fighter

Fighter is drawn, then the background

With DepthStencilState.Default, image does behind another image but I can't see what's behind the texture, instead I see the background color

It's like there is no Z buffering at all. this is done with: GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead; I've tried other settings but they don't help

Edit: I think I have to somehow set the BlendState of the graphic card in some way so that the textures behind other textures get rendered.

share|improve this question
    
Can you provide a screenshot? –  Andrew Russell Sep 13 '12 at 3:50
    
@AndrewRussell I've added screenshots, I'll add all the code too if that's necacarry. –  ProgrammerAtWork Sep 13 '12 at 6:15
add comment

1 Answer 1

up vote 2 down vote accepted

The rendering depends mainly on two things:

  • spatial configuration (z-buffer configuration) - closest fragment is written to output pixel usualy
  • blending settings (there is a lot of possible blending configurations)

If you render your planes with same Z-depth, you can experience some z-fighting problems. GPU usualy performs some optimalization in rendering pipeline - for example early Z termination. This could sometimes imply unexpected results...

From what you've posted it's not clear, what shader code are you using and what are the depth buffer and blending states.

(For me it seems that one of the effects (shaders) used (basiceffect1, basiceffect2) is discartign fragments while the other does not...)

But in general: the rendering order MATTERS when handling transparency...

share|improve this answer
    
I've an answer that explains this: stackoverflow.com/questions/4683030/… –  ProgrammerAtWork Sep 13 '12 at 20:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.