Game.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Game.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Content;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Input;
  14. #endregion
  15. namespace ObjImporterSample
  16. {
  17. /// <summary>
  18. /// This class displays the imported model and animates it rotating.
  19. /// It is useful for testing importer.
  20. /// </summary>
  21. public class ObjImporterGame : Microsoft.Xna.Framework.Game
  22. {
  23. #region Fields
  24. GraphicsDeviceManager graphics;
  25. Model model;
  26. #endregion
  27. #region Initialization
  28. public ObjImporterGame()
  29. {
  30. graphics = new GraphicsDeviceManager(this);
  31. Content.RootDirectory = "Content";
  32. }
  33. /// <summary>
  34. /// Load your graphics content.
  35. /// </summary>
  36. protected override void LoadContent()
  37. {
  38. model = Content.Load<Model>("Tank");
  39. }
  40. #endregion
  41. #region Update and Draw
  42. /// <summary>
  43. /// Allows the game to run logic.
  44. /// </summary>
  45. protected override void Update(GameTime gameTime)
  46. {
  47. base.Update(gameTime);
  48. KeyboardState keyboardState = Keyboard.GetState();
  49. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  50. // Check for exit.
  51. if (keyboardState.IsKeyDown(Keys.Escape) ||
  52. gamePadState.Buttons.Back == ButtonState.Pressed)
  53. {
  54. Exit();
  55. }
  56. }
  57. /// <summary>
  58. /// This is called when the game should draw itself.
  59. /// </summary>
  60. protected override void Draw(GameTime gameTime)
  61. {
  62. GraphicsDevice device = graphics.GraphicsDevice;
  63. device.Clear(Color.CornflowerBlue);
  64. // Animated the model rotating
  65. float modelRotation = (float)gameTime.TotalGameTime.TotalSeconds / 5.0f;
  66. // Set the positions of the camera in world space, for our view matrix.
  67. Vector3 cameraPosition = new Vector3(0.0f, 200.0f, 350.0f);
  68. Vector3 lookAt = new Vector3(0.0f, 35.0f, 0.0f);
  69. // Copy any parent transforms.
  70. Matrix[] transforms = new Matrix[model.Bones.Count];
  71. model.CopyAbsoluteBoneTransformsTo(transforms);
  72. // Draw the model. A model can have multiple meshes, so loop.
  73. foreach (ModelMesh mesh in model.Meshes)
  74. {
  75. // This is where the mesh orientation is set,
  76. // as well as our camera and projection.
  77. foreach (BasicEffect effect in mesh.Effects)
  78. {
  79. effect.EnableDefaultLighting();
  80. effect.World = transforms[mesh.ParentBone.Index] *
  81. Matrix.CreateRotationY(modelRotation);
  82. effect.View = Matrix.CreateLookAt(cameraPosition, lookAt,
  83. Vector3.Up);
  84. effect.Projection = Matrix.CreatePerspectiveFieldOfView(
  85. MathHelper.ToRadians(45.0f), device.Viewport.AspectRatio, 1.0f, 10000.0f);
  86. }
  87. // Draw the mesh, using the effects set above.
  88. mesh.Draw();
  89. }
  90. base.Draw(gameTime);
  91. }
  92. #endregion
  93. }
  94. #region Entry Point
  95. /// <summary>
  96. /// The main entry point for the application.
  97. /// </summary>
  98. static class Program
  99. {
  100. static void Main()
  101. {
  102. using (ObjImporterGame game = new ObjImporterGame())
  103. {
  104. game.Run();
  105. }
  106. }
  107. }
  108. #endregion
  109. }