Game.cs 6.9 KB

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