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 have gone through a few posts that talks about changing the GraphicsDevice.BlendState and GraphicsDevice.DepthStencilState (SpriteBatch & Render states). . however even after changing the states .. i cant see my 3D model on the screen.. I see the model for a second before i draw my video in the background. . If i dont play my video then i can see the 3D model ..Here is the code:

GraphicsDevice.Clear(Color.AliceBlue);
spriteBatch.Begin();

if (player.State != MediaState.Stopped)
{
    videoTexture = player.GetTexture();
}

Rectangle screen = new Rectangle(GraphicsDevice.Viewport.X, GraphicsDevice.Viewport.Y, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

// Draw the video, if we have a texture to draw.
if (videoTexture != null)
{
    spriteBatch.Draw(videoTexture, screen, Color.White);

    if (Selected_underwater == true)
    {
        spriteBatch.DrawString(font, "MaxX , MaxY" + maxWidth + "," + maxHeight, new Vector2(400, 10), Color.Red);
        spriteBatch.Draw(kinectRGBVideo, new Rectangle(0, 0, 100, 100), Color.White);
        spriteBatch.Draw(butterfly, handPosition, Color.White);

        foreach (AnimatedSprite a in aSprites)
        {
            a.Draw(spriteBatch);
        }                       
    }

    if(Selected_planet == true)
    {            
        spriteBatch.Draw(kinectRGBVideo, new Rectangle(0, 0, 100, 100), Color.White);
        spriteBatch.Draw(butterfly, handPosition, Color.White);
        spriteBatch.Draw(videoTexture,screen,Color.White);

        GraphicsDevice.BlendState = BlendState.Opaque;
        GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

        foreach (_3DModel m in Solar)
        {
            m.DrawModel();
        }
    }
    spriteBatch.End();
share|improve this question
1  
Your spriteBatch.Begin() and spriteBatch.End() do not occur in the same scope, that's a bad idea. You should really have your Begin() inside the 'if (videoTexture != null)' block. –  Jonathan Connell Oct 8 '12 at 17:41
    
Im using themes..thats why I have these bool statements.. If theme selected then do this . . or do that ..So i thought a general sprite.Begin() and sprite.End() should help –  Sweta Dwivedi Oct 8 '12 at 19:06
add comment

2 Answers

up vote 2 down vote accepted

The problem you are having is because SpriteBatch, by default[1], buffers all rendering until End is called.

It's not really a problem with render states. The states you specify in Begin (or the default states if you specify nothing) are only set during End.

What is happening is that, by calling End after drawing your model, you are rendering all your sprites over the top of your model.

Just split your rendering into multiple batches, or call End earlier (whichever suits) and it should work correctly. Be sure that Begin/End pairs always match up!


[1] : Except in SpriteSortMode.Immediate, which does everything, well, immediately. It sets states in Begin and draws a sprite for each Draw. Because of this it can't do batching and so is generally slower.

share|improve this answer
add comment

spriteBatch.Begin();

Defaults to SortMethod.Deferred. You can't change render states in between a begin/end pair unless its SortMethod.Immediate. So the below is not doing anything.

GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

The simple fix is to end the spritebatch then call the above code before you render your 3D stuff. Ex:

if(Selected_planet == true)
{            
    spriteBatch.Draw(kinectRGBVideo, new Rectangle(0, 0, 100, 100), Color.White);
    spriteBatch.Draw(butterfly, handPosition, Color.White);
    spriteBatch.Draw(videoTexture,screen,Color.White);
}

spriteBatch.End();

if(Selected_planet == true)
{ 
    GraphicsDevice.BlendState = BlendState.Opaque;
    GraphicsDevice.DepthStencilState = DepthStencilState.Default;
    GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

    foreach (_3DModel m in Solar)
    {
        m.DrawModel();
    }
}
share|improve this answer
    
Your solution will fix the problem, but your reasoning is incorrect. It's perfectly acceptable - and functional - to change states inside a Begin/End pair (it just doesn't affect the batch itself). See my answer. –  Andrew Russell Oct 9 '12 at 7:18
    
ClassicThunder.. im still awaiting ur reply for .3DS extension i have already added my links and models in tht thread –  Sweta Dwivedi Oct 9 '12 at 10:22
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.