CustomModelEffect.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CustomModelEffect.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 CustomModelEffect
  16. {
  17. /// <summary>
  18. /// Sample shows how to render a model using a custom effect.
  19. /// </summary>
  20. public class CustomModelEffectGame : Microsoft.Xna.Framework.Game
  21. {
  22. #region Fields
  23. GraphicsDeviceManager graphics;
  24. Model model;
  25. #endregion
  26. #region Initialization
  27. public CustomModelEffectGame()
  28. {
  29. graphics = new GraphicsDeviceManager(this);
  30. Content.RootDirectory = "Content";
  31. }
  32. /// <summary>
  33. /// Load your graphics content.
  34. /// </summary>
  35. protected override void LoadContent()
  36. {
  37. model = Content.Load<Model>("saucer");
  38. }
  39. #endregion
  40. #region Update and Draw
  41. /// <summary>
  42. /// Allows the game to run logic.
  43. /// </summary>
  44. protected override void Update(GameTime gameTime)
  45. {
  46. HandleInput();
  47. base.Update(gameTime);
  48. }
  49. /// <summary>
  50. /// This is called when the game should draw itself.
  51. /// </summary>
  52. protected override void Draw(GameTime gameTime)
  53. {
  54. GraphicsDevice device = graphics.GraphicsDevice;
  55. device.Clear(Color.CornflowerBlue);
  56. // Calculate the camera matrices.
  57. Viewport viewport = device.Viewport;
  58. float aspectRatio = (float)viewport.Width / (float)viewport.Height;
  59. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  60. Matrix rotation = Matrix.CreateRotationX(time * 0.3f) *
  61. Matrix.CreateRotationY(time);
  62. Matrix view = Matrix.CreateLookAt(new Vector3(4000, 0, 0),
  63. Vector3.Zero,
  64. Vector3.Up);
  65. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  66. aspectRatio,
  67. 10, 10000);
  68. // Draw the model.
  69. Matrix[] transforms = new Matrix[model.Bones.Count];
  70. model.CopyAbsoluteBoneTransformsTo(transforms);
  71. foreach (ModelMesh mesh in model.Meshes)
  72. {
  73. foreach (Effect effect in mesh.Effects)
  74. {
  75. Matrix world = transforms[mesh.ParentBone.Index] * rotation;
  76. effect.Parameters["World"].SetValue(world);
  77. effect.Parameters["View"].SetValue(view);
  78. effect.Parameters["Projection"].SetValue(projection);
  79. }
  80. mesh.Draw();
  81. }
  82. base.Draw(gameTime);
  83. }
  84. #endregion
  85. #region Handle Input
  86. /// <summary>
  87. /// Handles input for quitting the game.
  88. /// </summary>
  89. private void HandleInput()
  90. {
  91. KeyboardState currentKeyboardState = Keyboard.GetState();
  92. GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
  93. // Check for exit.
  94. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  95. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  96. {
  97. Exit();
  98. }
  99. }
  100. #endregion
  101. }
  102. #region Entry Point
  103. /// <summary>
  104. /// The main entry point for the application.
  105. /// </summary>
  106. static class Program
  107. {
  108. static void Main()
  109. {
  110. using (CustomModelEffectGame game = new CustomModelEffectGame())
  111. {
  112. game.Run();
  113. }
  114. }
  115. }
  116. #endregion
  117. }