I have a little problem with my 3D model (.fbx) rendered in XNA, look at screenshot.
Screenshot:
It should be a lego brick, which was modelated in Blender, then exported to .fbx and added to XNA project. My problem is transparency, I tried so much with depthbuffer, z-buffer etc., but nothing helped. Here's my code:
namespace Using3DModels
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
float myModelRotation;
Vector3 myModelPosition;
/// <summary>
/// Stores the model that we are going to draw.
/// </summary>
private Model model;
/// <summary>
/// Stores the world matrix for the model, which transforms the
/// model to be in the correct position, scale, and rotation
/// in the game world.
/// </summary>
private Matrix world = Matrix.CreateTranslation(new Vector3(6, 4, 0));
/// <summary>
/// Stores the view matrix for the model, which gets the model
/// in the right place, relative to the camera.
/// </summary>
private Matrix view = Matrix.CreateLookAt(new Vector3(30, 25, -10), new Vector3(5, 5, 0), Vector3.UnitY);
/// <summary>
/// Stores the projection matrix, which gets the model projected
/// onto the screen in the correct way. Essentially, this defines the
/// properties of the camera you are using.
/// </summary>
private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.1f, 100f);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferMultiSampling = true;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
model = Content.Load<Model>("lego6");
myModelPosition = Vector3.Zero;
myModelRotation = MathHelper.ToRadians(45f);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
myModelRotation += MathHelper.ToRadians(1f);
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
GraphicsDevice.Clear(Color.DarkGray);
DrawModel(model, world, view, projection);
base.Draw(gameTime);
}
/// <summary>
/// Does the work of drawing a model, given specific world, view, and projection
/// matrices.
/// </summary>
/// <param name="model">The model to draw</param>
/// <param name="world">The transformation matrix to get the model in the right place in the world.</param>
/// <param name="view">The transformation matrix to get the model in the right place, relative to the camera.</param>
/// <param name="projection">The transformation matrix to project the model's points onto the screen correctly.</param>
private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
{
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(myModelRotation) * Matrix.CreateTranslation(myModelPosition);
effect.View = view;
effect.Projection = projection;
}
mesh.Draw();
}
}
}
}