Question

I want to draw a Model in XNA. I've gona ahead and produced it in Blender and Exported it to fbx File Format so the Content Pipeline can work with it. What code should I add to the Draw() method of my WindowsGame() ? I've tried the Following but All I get is a gray screen (Gray not Blue, which is the clear color, mind you) The Model is Imported with content.Load, and this throws no error, and I called it "Bowl".

Can Anyone tell my why this here won't work?

protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            BasicEffect b = new BasicEffect (graphics.GraphicsDevice, new EffectPool ( ));
            foreach (ModelMesh m in Bowl.Meshes)
            {
                b.View = Cam.mView;
                b.Projection = Cam.mProj;
                b.World = mWorld;
                b.EnableDefaultLighting ( );
                b.Begin ( );
                m.Draw ( );
                b.End ( );
            }

            base.Draw(gameTime);
        }

I've just noticed that this is equivalent to murder in terms of efficiency but I've tried so many things, I just need it to work before I optimize it.

Was it helpful?

Solution

A really common issue when first trying to render something is that the camera isn't looking at what you think it's looking at. Another possible issue is that the model is not at a scale that you'd expect. So for example, if the camera is 5 units back on the z, but the model is 10 units wide, your camera is effectively inside the model.

As far as the rendering question goes, Microsoft has pretty good docs on this: http://msdn.microsoft.com/en-us/library/bb203933.aspx

You can use this snippet as a helper:

private void DrawModel(Model m)
{
    Matrix[] transforms = new Matrix[m.Bones.Count];
    float aspectRatio = graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height;
    m.CopyAbsoluteBoneTransformsTo(transforms);
    Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
        aspectRatio, 1.0f, 10000.0f);
    Matrix view = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, Zoom), Vector3.Zero, Vector3.Up);

    foreach (ModelMesh mesh in m.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();

            effect.View = view;
            effect.Projection = projection;
            effect.World = gameWorldRotation * transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(Position);
        }
        mesh.Draw();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top