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 making a small 3dgame which is made as a Silverlight and XNA app, but when I draw the sprites the graphics becomes all wierd.

All my primitive types are rendered correctly, but my 3d models are just wierd

My Draw is like this when silverlight is set to draw

private void OnDraw(object sender, GameTimerEventArgs e)
{
    // Render the Silverlight controls using the UIElementRenderer
    elementRenderer.Render();

    // Clear the screen to a solid color
    SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

    switch (gameState)
    {
        case GameState.ChooseStarter:
            TextBlockStatus.Text = "Find Starting Player";
            break;
        case GameState.PlaceBrick:
            TextBlockPlayer.Text = (playerTurn == PlayerTurn.PlayerOne) ? "Player One" : "Player Two";
            TextBlockState.Text = "Place Brick";
            foreach (IGraphicObject obj in _3dObjects)
            {
                obj.Draw(cameraPosition, e);
            }
            break;
        case GameState.GiveBrick:
            TextBlockState.Text = "Give Brick";
            break;
    }

    spriteBatch.Begin();
    // Using the texture from the UIElementRenderer, 
    // draw the Silverlight controls to the screen
    spriteBatch.Draw(elementRenderer.Texture, cameraProjection, Color.White);
    spriteBatch.End();
}

This gives me this outputenter image description here

If I comment the spritebatch lines out I get the correct output, except the silverlight text is of course not shown enter image description here

I am not entirely sure what to look for except that zero vector I am giving to the spritebatch, but if thats the source I have no idea what I am supposed to set it as epspecially when its a 2d vector

share|improve this question
    
That worked cool :) that line just isnt in the demo project from msdn :/ –  Mech0z Apr 7 '12 at 18:02

1 Answer 1

up vote 2 down vote accepted

Since the objects appear in the wrong order, this is most likely because the depth buffer is disabled at the time of drawing.

That's actually something that SpriteBatch does and you need to restore it manually. See this link for more information on the problem, as it also covers all the other device states that are also changed by SpriteBatch.

In this particular example, in order to reenable the depth buffer, just add the following line to the end of your OnDraw method:

GraphicsDevice.DepthStencilState = DepthStencilState.Default;
share|improve this answer
    
+1, plus you can also tell which render states to set back when calling SpriteBatch.Begin(..) and using the overload with all the render states. That makes the code more self documenting I think. –  Roy T. Apr 7 '12 at 18:34

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.