xbdev - software development
Tuesday June 25, 2024
Home | Contact | Support | XNA & 360 The bits and bytes of XNA and C# for your 360... | XNA & 360 The bits and bytes of XNA and C# for your 360...

XNA & 360

The bits and bytes of XNA and C# for your 360...



With XNA as with DirectX, loading in a 3D Model is just a couple of lines....and a further few lines to render it!


The code below is pretty simple, but is a good example I think.  I added a 3d .x model to the project, just add it to the project contents.  It was called "basiccar.x".... and then I can just load it using the Content.Load<Model>("basicCar");


Easy eh?


Of course to render it we have to lookup a default shader and set some default camera stuff, which again makes it look a bit more work than it is...but where in 3D..we can't make it to easy!  The code lets you use the thumbstick on the xbox360 controller rotate and zoom around the 3d model of a car.





Game.cs [Download SourceCode]

using System;

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.GamerServices;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Storage;



public class GameClass : Microsoft.Xna.Framework.Game


    float dist = -20.0f;

    Vector2 rot = new Vector2();

    GraphicsDeviceManager graphics;

    Model myModel;


    public GameClass()


        graphics = new GraphicsDeviceManager(this);

        Content.RootDirectory = "Content";



    protected override void LoadContent()


        myModel = Content.Load<Model>("basiccar");



    protected override void Draw(GameTime gameTime)


        // Just a few lines that let the controller move the camera

        // all the way around our 3d model...mode is at (0,0,0)

        rot.Y += GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X * 0.05f;

        rot.X -= GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y * 0.05f;

        dist  += GamePad.GetState(PlayerIndex.One).ThumbSticks.Right.Y * 0.1f;

        Vector3 dir = Vector3.Transform(new Vector3(0, 0, 1),

                      Matrix.CreateRotationX(rot.X) * Matrix.CreateRotationY(rot.Y));

        Vector3 camPos = dir * dist;




        Matrix worldMatrix = Matrix.CreateScale(0.01f, 0.01f, 0.01f) ;

        Matrix[] modelTransforms = new Matrix[myModel.Bones.Count];


        foreach (ModelMesh mesh in myModel.Meshes)


            foreach (BasicEffect effect in mesh.Effects)


                GraphicsDevice device = graphics.GraphicsDevice;



                effect.World = modelTransforms[mesh.ParentBone.Index] * worldMatrix;


                float aspectRatio = (float)device.Viewport.Width / device.Viewport.Height;

                effect.View = Matrix.CreateLookAt(camPos,



                effect.Projection = Matrix.CreatePerspectiveFieldOfView(















static class Program


    static void Main(string[] args)


        using (GameClass game = new GameClass())




















Advert (Support Website)

Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.