locked
SpriteBatch + Model = Transparent Model RRS feed

  • Question

  • I'm confused about why this is happening -

    I used the code from tutorial1 - Displaying a 3D Model on the Screen

    And then I added a background as recommended in the Ideas to Expand section using a call to spritebatch.draw.

    Problem is that when I call SrpiteBatch.Begin is causes my model to become semi-transparent (I can see the model, the background through the model and all the sub mesh pieces that should be hidden behind the outer most portions of the model. If I comment out the spritebatch.begin and end methods the model is no longer transparent.

    Heres the code: If anyone knows why this is happening please explain - Thanks much.

    using System;

    using System.Collections.Generic;

    using Microsoft.Xna.Framework;

    using Microsoft.Xna.Framework.Audio;

    using Microsoft.Xna.Framework.Content;

    using Microsoft.Xna.Framework.Graphics;

    using Microsoft.Xna.Framework.Input;

    using Microsoft.Xna.Framework.Storage;

    namespace T2___Model

    {

    public class Game1 : Microsoft.Xna.Framework.Game

    {

    GraphicsDeviceManager graphics;

    ContentManager content;

    Model myModel;

    SpriteBatch batch;

    Texture2D background;

    public Game1()

    {

    graphics = new GraphicsDeviceManager(this);

    content = new ContentManager(Services);

    }

    protected override void Initialize()

    {

    base.Initialize();

    }

    protected override void LoadGraphicsContent(bool loadAllContent)

    {

    if (loadAllContent)

    {

    myModel = content.Load<Model>("Content\\Models\\p1_wedge");

    batch = new SpriteBatch(graphics.GraphicsDevice);

    background = content.Load<Texture2D>("Content\\Textures\\B1_stars");

    }

    }

    protected override void UnloadGraphicsContent(bool unloadAllContent)

    {

    if (unloadAllContent == true)

    {

    content.Unload();

    }

    }

    KeyboardState ks;

    protected override void Update(GameTime gameTime)

    {

    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

    this.Exit();

    ks = Keyboard.GetState();

    if( ks.IsKeyDown(Keys.Left) )

    modelrotationY -= gameTime.ElapsedGameTime.Milliseconds * MathHelper.ToRadians(0.1f);

    if (ks.IsKeyDown(Keys.Right))

    modelrotationY+= gameTime.ElapsedGameTime.Milliseconds * MathHelper.ToRadians(0.1f);

    if (ks.IsKeyDown(Keys.Up))

    modelrotationX -= gameTime.ElapsedGameTime.Milliseconds * MathHelper.ToRadians(0.1f);

    if (ks.IsKeyDown(Keys.Down))

    modelrotationX += gameTime.ElapsedGameTime.Milliseconds * MathHelper.ToRadians(0.1f);

    base.Update(gameTime);

    }

    Vector3 modelposition = Vector3.Zero;

    float modelrotationX = 0.0f;

    float modelrotationY = 0.0f;

    protected override void Draw(GameTime gameTime)

    {

    graphics.GraphicsDevice.Clear(Color.Black);

    batch.Begin(SpriteBlendMode.None);

    batch.Draw(background, new Rectangle(0, 0, this.Window.ClientBounds.Width,

    this.Window.ClientBounds.Height), new Rectangle(0, 0, background.Width,

    background.Height), Color.White, 0.0f, new Vector2(0, 0), SpriteEffects.None, 1.0f);

    batch.End();

    foreach (ModelMesh mesh in myModel.Meshes)

    {

    foreach (BasicEffect effect in mesh.Effects)

    {

    effect.EnableDefaultLighting();

    effect.World = Matrix.CreateRotationY(modelrotationY)

    * Matrix.CreateRotationX(modelrotationX)

    * mesh.ParentBone.Transform * Matrix.CreateTranslation(modelposition);

    effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, 6000.0f),

    Vector3.Zero, Vector3.Up);

    effect.Projection = Matrix.CreatePerspectiveFieldOfView(

    MathHelper.ToRadians(45.0f), 640 / 480, 1.0f, 10000.0f);

    }

    mesh.Draw();

    }

    base.Draw(gameTime);

    }

    }

    }

    Monday, November 6, 2006 5:41 PM

Answers

  • When you draw sprites, that will change the renderstates to good settings for 2D rendering. You will probably want to use different renderstates for drawing a 3D mesh.

    Specifically, you probably need to set:

        device.RenderState.DepthBufferEnable = true;
        device.RenderState.AlphaBlendEnable = false;

    Monday, November 6, 2006 6:41 PM

All replies

  • When you draw sprites, that will change the renderstates to good settings for 2D rendering. You will probably want to use different renderstates for drawing a 3D mesh.

    Specifically, you probably need to set:

        device.RenderState.DepthBufferEnable = true;
        device.RenderState.AlphaBlendEnable = false;

    Monday, November 6, 2006 6:41 PM
  • Thanks Shawn. That was indeed my problem.

    Since I render the background before drawing the model I had to change the render state after my spritbatch.end call and before rendering the model.

     

    Thanks again.

     

    Monday, November 6, 2006 6:53 PM