Game.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.Graphics;
  12. using Microsoft.Xna.Framework.Input;
  13. #endregion
  14. namespace LensFlare
  15. {
  16. /// <summary>
  17. /// Sample showing how to implement a lensflare effect, using occlusion
  18. /// queries to hide the flares when the sun is hidden behind the landscape.
  19. /// </summary>
  20. public class LensFlareGame : Microsoft.Xna.Framework.Game
  21. {
  22. #region Fields
  23. GraphicsDeviceManager graphics;
  24. KeyboardState currentKeyboardState = new KeyboardState();
  25. GamePadState currentGamePadState = new GamePadState();
  26. Vector3 cameraPosition = new Vector3(-200, 30, 30);
  27. Vector3 cameraFront = new Vector3(1, 0, 0);
  28. Model terrain;
  29. LensFlareComponent lensFlare;
  30. #endregion
  31. #region Initialization
  32. public LensFlareGame()
  33. {
  34. graphics = new GraphicsDeviceManager(this);
  35. Content.RootDirectory = "Content";
  36. // Create and add the lensflare component.
  37. lensFlare = new LensFlareComponent(this);
  38. Components.Add(lensFlare);
  39. }
  40. /// <summary>
  41. /// Load your graphics content.
  42. /// </summary>
  43. protected override void LoadContent()
  44. {
  45. terrain = Content.Load<Model>("terrain");
  46. }
  47. #endregion
  48. #region Update and Draw
  49. /// <summary>
  50. /// Allows the game to run logic.
  51. /// </summary>
  52. protected override void Update(GameTime gameTime)
  53. {
  54. HandleInput();
  55. UpdateCamera(gameTime);
  56. base.Update(gameTime);
  57. }
  58. /// <summary>
  59. /// This is called when the game should draw itself.
  60. /// </summary>
  61. protected override void Draw(GameTime gameTime)
  62. {
  63. GraphicsDevice.Clear(Color.CornflowerBlue);
  64. // Compute camera matrices.
  65. Matrix view = Matrix.CreateLookAt(cameraPosition,
  66. cameraPosition + cameraFront,
  67. Vector3.Up);
  68. float aspectRatio = GraphicsDevice.Viewport.AspectRatio;
  69. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  70. aspectRatio,
  71. 0.1f, 500);
  72. // Draw the terrain.
  73. GraphicsDevice.RasterizerState = RasterizerState.CullNone;
  74. foreach (ModelMesh mesh in terrain.Meshes)
  75. {
  76. foreach (BasicEffect effect in mesh.Effects)
  77. {
  78. effect.World = Matrix.Identity;
  79. effect.View = view;
  80. effect.Projection = projection;
  81. effect.LightingEnabled = true;
  82. effect.DiffuseColor = new Vector3(1f);
  83. effect.AmbientLightColor = new Vector3(0.5f);
  84. effect.DirectionalLight0.Enabled = true;
  85. effect.DirectionalLight0.DiffuseColor = Vector3.One;
  86. effect.DirectionalLight0.Direction = lensFlare.LightDirection;
  87. effect.FogEnabled = true;
  88. effect.FogStart = 200;
  89. effect.FogEnd = 500;
  90. effect.FogColor = Color.CornflowerBlue.ToVector3();
  91. }
  92. mesh.Draw();
  93. }
  94. // Tell the lensflare component where our camera is positioned.
  95. lensFlare.View = view;
  96. lensFlare.Projection = projection;
  97. base.Draw(gameTime);
  98. }
  99. #endregion
  100. #region Handle Input
  101. /// <summary>
  102. /// Handles input for quitting the game.
  103. /// </summary>
  104. private void HandleInput()
  105. {
  106. currentKeyboardState = Keyboard.GetState();
  107. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  108. // Check for exit.
  109. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  110. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  111. {
  112. Exit();
  113. }
  114. }
  115. /// <summary>
  116. /// Handles camera input.
  117. /// </summary>
  118. private void UpdateCamera(GameTime gameTime)
  119. {
  120. float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
  121. // Check for input to rotate the camera.
  122. float pitch = -currentGamePadState.ThumbSticks.Right.Y * time * 0.001f;
  123. float turn = -currentGamePadState.ThumbSticks.Right.X * time * 0.001f;
  124. if (currentKeyboardState.IsKeyDown(Keys.Up))
  125. pitch += time * 0.001f;
  126. if (currentKeyboardState.IsKeyDown(Keys.Down))
  127. pitch -= time * 0.001f;
  128. if (currentKeyboardState.IsKeyDown(Keys.Left))
  129. turn += time * 0.001f;
  130. if (currentKeyboardState.IsKeyDown(Keys.Right))
  131. turn -= time * 0.001f;
  132. Vector3 cameraRight = Vector3.Cross(Vector3.Up, cameraFront);
  133. Vector3 flatFront = Vector3.Cross(cameraRight, Vector3.Up);
  134. Matrix pitchMatrix = Matrix.CreateFromAxisAngle(cameraRight, pitch);
  135. Matrix turnMatrix = Matrix.CreateFromAxisAngle(Vector3.Up, turn);
  136. Vector3 tiltedFront = Vector3.TransformNormal(cameraFront, pitchMatrix *
  137. turnMatrix);
  138. // Check angle so we can't flip over.
  139. if (Vector3.Dot(tiltedFront, flatFront) > 0.001f)
  140. {
  141. cameraFront = Vector3.Normalize(tiltedFront);
  142. }
  143. // Check for input to move the camera around.
  144. if (currentKeyboardState.IsKeyDown(Keys.W))
  145. cameraPosition += cameraFront * time * 0.1f;
  146. if (currentKeyboardState.IsKeyDown(Keys.S))
  147. cameraPosition -= cameraFront * time * 0.1f;
  148. if (currentKeyboardState.IsKeyDown(Keys.A))
  149. cameraPosition += cameraRight * time * 0.1f;
  150. if (currentKeyboardState.IsKeyDown(Keys.D))
  151. cameraPosition -= cameraRight * time * 0.1f;
  152. cameraPosition += cameraFront *
  153. currentGamePadState.ThumbSticks.Left.Y * time * 0.1f;
  154. cameraPosition -= cameraRight *
  155. currentGamePadState.ThumbSticks.Left.X * time * 0.1f;
  156. if (currentGamePadState.Buttons.RightStick == ButtonState.Pressed ||
  157. currentKeyboardState.IsKeyDown(Keys.R))
  158. {
  159. cameraPosition = new Vector3(-200, 30, 30);
  160. cameraFront = new Vector3(1, 0, 0);
  161. }
  162. }
  163. #endregion
  164. }
  165. }