#region File Description //----------------------------------------------------------------------------- // Game.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; #endregion namespace ObjImporterSample { /// /// This class displays the imported model and animates it rotating. /// It is useful for testing importer. /// public class ObjImporterGame : Microsoft.Xna.Framework.Game { #region Fields GraphicsDeviceManager graphics; Model model; #endregion #region Initialization public ObjImporterGame() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// /// Load your graphics content. /// protected override void LoadContent() { model = Content.Load("Tank"); } #endregion #region Update and Draw /// /// Allows the game to run logic. /// protected override void Update(GameTime gameTime) { base.Update(gameTime); KeyboardState keyboardState = Keyboard.GetState(); GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); // Check for exit. if (keyboardState.IsKeyDown(Keys.Escape) || gamePadState.Buttons.Back == ButtonState.Pressed) { Exit(); } } /// /// This is called when the game should draw itself. /// protected override void Draw(GameTime gameTime) { GraphicsDevice device = graphics.GraphicsDevice; device.Clear(Color.CornflowerBlue); // Animated the model rotating float modelRotation = (float)gameTime.TotalGameTime.TotalSeconds / 5.0f; // Set the positions of the camera in world space, for our view matrix. Vector3 cameraPosition = new Vector3(0.0f, 200.0f, 350.0f); Vector3 lookAt = new Vector3(0.0f, 35.0f, 0.0f); // Copy any parent transforms. Matrix[] transforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(transforms); // Draw the model. A model can have multiple meshes, so loop. foreach (ModelMesh mesh in model.Meshes) { // This is where the mesh orientation is set, // as well as our camera and projection. foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation); effect.View = Matrix.CreateLookAt(cameraPosition, lookAt, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), device.Viewport.AspectRatio, 1.0f, 10000.0f); } // Draw the mesh, using the effects set above. mesh.Draw(); } base.Draw(gameTime); } #endregion } #region Entry Point /// /// The main entry point for the application. /// static class Program { static void Main() { using (ObjImporterGame game = new ObjImporterGame()) { game.Run(); } } } #endregion }