XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

Drawing 3D models over a 2D background.

Last post 2/9/2010 8:07 PM by Byron Nelson. 7 replies.
  • 2/9/2010 5:40 AM

    Drawing 3D models over a 2D background.

    I am making a 2D side scrolling game. I have started working on the Title Screen for the game. Since this is a space themed game, I thought it would be cool to have some 3D planets and space ships going across the screen--but just for the opening screen. I created a simple background with stars and an atmosphere for a planet, and I want to draw the planet right inside of the atmosphere circle. I was able to get the textured sphere in just fine, but then I could not get the background drawn behind the sphere. I did a bit of searching and was able to find this. Yet, that is just way over my head and is a bunch of gibberish at the moment. Can anyone spell it out for me?
  • 2/9/2010 5:47 AM In reply to

    Re: Drawing 3D models over a 2D background.

    Draw your background first.
    Draw you planet second.
    There really shouldn't be much else needed other than that.
  • 2/9/2010 5:56 AM In reply to

    Re: Drawing 3D models over a 2D background.

    You'd think it should be that easy.
  • 2/9/2010 8:02 AM In reply to

    Re: Drawing 3D models over a 2D background.

    The simplest (but not the fastest) way would be to simply do all your 2D drawing, end the spritebatch, and then do all your 3d drawing.  Don't try to do 3D drawing while the SpriteBatch is in between begin and end.
  • 2/9/2010 9:10 AM In reply to

    Re: Drawing 3D models over a 2D background.

    and don't forget to call the SpriteBatch.Begin() with SaveStateMode.SaveState, so that the mixed rendering does not cause any problems. the SpriteBatch often messes renderstates up :-)
  • 2/9/2010 3:26 PM In reply to

    Re: Drawing 3D models over a 2D background.

    Shawn's article has it spelled out pretty good, so I'll go the opposite direction and try to nutshell it.

    The spritebatch changes "renderstates," or the way that the gpu renders things.  In a nutshell, it changes them in ways that often make your 3d models render incorrectly.   Before you call the draw functions on your 3d object, set (what are probably) the relevant renderstates back to what they should be to get the 3d model to render the way you expect.

     GraphicsDevice.RenderState.DepthBufferEnable = true; 
    GraphicsDevice.RenderState.AlphaBlendEnable = false;
    GraphicsDevice.RenderState.AlphaTestEnable = false;

    Best,
    Byron
    ..shaders make you feel... powerful, or very very stupid.
    http://drjbn.spaces.live.com/
  • 2/9/2010 7:00 PM In reply to

    Re: Drawing 3D models over a 2D background.

    Byron Nelson:
    Shawn's article has it spelled out pretty good, so I'll go the opposite direction and try to nutshell it.

    The spritebatch changes "renderstates," or the way that the gpu renders things.  In a nutshell, it changes them in ways that often make your 3d models render incorrectly.   Before you call the draw functions on your 3d object, set (what are probably) the relevant renderstates back to what they should be to get the 3d model to render the way you expect.

     GraphicsDevice.RenderState.DepthBufferEnable = true; 
    GraphicsDevice.RenderState.AlphaBlendEnable = false;
    GraphicsDevice.RenderState.AlphaTestEnable = false;

    Best,
    Byron
    I don't think that is the problem. If you draw the background first then draw the planet while leaving those RenderStates unchanged, then the planet will draw infront of the background since the depth test will always pass (it'll probably look pretty crazy, but it should still appear in front).
    stupid question, but mikehall, can you get the background to draw when not drawing the sphere?

    Edit: Or perhaps it's the blend modes. If AlphaBlendEnable is false, but your blend modes are set to SourceAlpha and InverseSourceAlpha, does anybody know what happens?
  • 2/9/2010 8:07 PM In reply to

    Re: Drawing 3D models over a 2D background.

    thecray:
    Edit: Or perhaps it's the blend modes. If AlphaBlendEnable is false, but your blend modes are set to SourceAlpha and InverseSourceAlpha, does anybody know what happens?


    Yeah, you're right.  If he draws the background first then the planet should show up, unless there is the unlikely case he has his alpha in his texture/materials to zero. In that case not setting the renderstates to turn off the alpha test & blend would be an issue.

    If alphablend is not enabled,the blend states won't matter. 

    Best,
    Byron
    ..shaders make you feel... powerful, or very very stupid.
    http://drjbn.spaces.live.com/
Page 1 of 1 (8 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG