XNA – Load 3D Model

Posted: July 11, 2009 in C#, Programming, XNA
Tags: , , ,

xna_logo

here’s the screenshot (click image for larger view):

one two

three four

five six

source code :

variables

GraphicsDeviceManager graphics;
GraphicsDevice device;
BasicEffect basicEffect;

Model myModel;
Matrix[] modelTransforms;

how to load content

protected override void LoadContent()
{
    device = graphics.GraphicsDevice;
    basicEffect = new BasicEffect(device, null);
    cCross = new CoordCross(device);

    LoadModel(1);
}

private void LoadModel(int num)
{
    switch (num)
    {
        case 1:
        {
            myModel = Content.Load<Model>("p2_wedge");
            break;
        }
        case 2:
        {
            myModel = Content.Load<Model>("tank");
            break;
        }
        case 3:
        {
            myModel = Content.Load<Model>("T-88-FBX");
            break;
        }
        case 4:
        {
            myModel = Content.Load<Model>("p1_dual");
            break;
        }
        case 5:
        {
            myModel = Content.Load<Model>("p1_pencil");
            break;
        }
        case 6:
        {
            myModel = Content.Load<Model>("p2_mgun");
            break;
        }
        default:
        {
            break;
        }
    }
    modelTransforms = new Matrix[myModel.Bones.Count];
}

Update Method()

protected override void Update(GameTime gameTime)
{
    MouseState mouseState = Mouse.GetState();
    KeyboardState keyState = Keyboard.GetState();

    GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
#if XBOX
    if (gamePadState.Buttons.Back == ButtonState.Pressed)
        this.Exit();
#else

    if (keyState.IsKeyDown(Keys.Escape))
    {
        this.Exit();
    }
#endif

    if (keyState.IsKeyDown(Keys.D1))
    {
        LoadModel(1);
    }
    else if (keyState.IsKeyDown(Keys.D2))
    {
        LoadModel(2);
    }
    else if (keyState.IsKeyDown(Keys.D3))
    {
        LoadModel(3);
    }
    else if (keyState.IsKeyDown(Keys.D4))
    {
        LoadModel(4);
    }
    else if (keyState.IsKeyDown(Keys.D5))
    {
        LoadModel(5);
    }
    else if (keyState.IsKeyDown(Keys.D6))
    {
        LoadModel(6);
    }

    fpsCam.Update(mouseState, keyState, gamePadState);
    base.Update(gameTime);
}

Draw() Method

protected override void Draw(GameTime gameTime)
{
    device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.CornflowerBlue, 1, 0);

    cCross.Draw(fpsCam.ViewMatrix, fpsCam.ProjectionMatrix);

    //draw model
    Matrix worldMatrix = Matrix.CreateScale(0.01f, 0.01f, 0.01f);
    myModel.CopyAbsoluteBoneTransformsTo(modelTransforms);

    foreach (ModelMesh mesh in myModel.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();
            effect.World = modelTransforms[mesh.ParentBone.Index] * worldMatrix;
            effect.View = fpsCam.ViewMatrix;
            effect.Projection = fpsCam.ProjectionMatrix;
        }
        mesh.Draw();
    }

    base.Draw(gameTime);
}
Comments
  1. 3d models says:

    Sorry man i am not understanding

  2. umek says:

    thx bro…. i will try it.. :D

  3. Mohamed Gamal says:

    Please Could You Upload These Models And Attach It With The Topic ? :)

    Thanks In Advance :)

  4. Mohamed Gamal says:

    (Model.fbx & Model.tga ) Files Please if Yes :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s