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 was trying to output some debug information using DrawString when I noticed my model suddenly was being rendered like it was inside-out (like the culling had been disabled or something) and the texture maps weren't applied

I commented out the DrawString method until I only had SpriteBatch.Begin() and .End() and that was enough to cause the model rendering corruption - when I commented those calls out the model rendered correctly

What could this be a symptom of? I've stripped it down to the barest of code to isolate the problem and this is what I noticed.

Draw code below (as stripped down as possible)

        GraphicsDevice.Clear(Color.LightGray);
        foreach (ModelMesh mesh in TIEAdvanced.Meshes)
        {
            foreach (Effect effect in mesh.Effects)
            {
                if (effect is BasicEffect)
                    ((BasicEffect)effect).EnableDefaultLighting();
                effect.CurrentTechnique.Passes[0].Apply();
            }
        }

        spriteBatch.Begin();
        spriteBatch.DrawString(spriteFont, "Camera Position: " + cameraPosition.ToString(), new Vector2(10, 10), Color.Blue);
        spriteBatch.End();

        GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        TIEAdvanced.Draw(Matrix.CreateScale(0.025f), viewMatrix, projectionMatrix);
share|improve this question
    
please add your drawing code into question –  Vodáček Jul 4 '12 at 6:54
    
I think I've had similar problems, when I generated lots of particles (sprites) with random Z-layers in my scene. Other sprites started blinking, phasing in and out rapidly. Maybe you're also doing something nasty with Z-layers? Let's see your code! –  Marton Jul 4 '12 at 7:02
    
Actually I JUST figured it out then - apparently the moment you use the SpriteBatch with .Begin / End it sets the DepthStencilState to None! Adding GraphicsDevice.DepthStencilState = DepthStencilState.Default; after the SpriteBatch call fixed it. This would seem like a very common scenario but I had a hard time finding any information on this subject –  manning18 Jul 4 '12 at 7:03
    
also recommended thing is to set blend state to opaque: GraphicsDevice.BlendState = BlendState.Opaque; –  Vodáček Jul 4 '12 at 7:07

2 Answers 2

up vote 9 down vote accepted

SpriteBatch will change the following render states. Even if you don't specify any in your Begin call - it will use its own defaults:

GraphicsDevice.BlendState = BlendState.AlphaBlend;
GraphicsDevice.DepthStencilState = DepthStencilState.None;
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;

When you want to draw 3D models, you will want to set:

GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;

And, if your 3D models have wrapping UV coordinates:

GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

This information comes from this blog post.

share|improve this answer
3  
+1 For XNA 3.x users, the blog post you're looking for is here –  doppelgreener Jul 4 '12 at 13:06
    
Unfortunate, but understandable. I ran into this issue since I made an omnipotent RenderLayer class that contained a list of IDrawable objects, but since it made no distinction between sprites and 3D (I had simply item.Draw() wedged between begin/end), I was hitting some issues. –  Danjen 2 days ago

I also had this problem, but the code

GraphicsDevice.DepthStencilState = DepthStencilState.Default;

resolved the problem.

share|improve this answer
    
Can't make any sense out of this. Please revise? –  Cameron Fredman Mar 9 '13 at 3:46

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.