GeneratedGeometry.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GeneratedGeometry.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 System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Input;
  15. #endregion
  16. namespace GeneratedGeometry
  17. {
  18. /// <summary>
  19. /// Sample showing how to use geometry that is programatically
  20. /// generated as part of the content pipeline build process.
  21. /// </summary>
  22. public class GeneratedGeometryGame : Microsoft.Xna.Framework.Game
  23. {
  24. #region Fields
  25. GraphicsDeviceManager graphics;
  26. Model terrain;
  27. Sky sky;
  28. #endregion
  29. #region Initialization
  30. public GeneratedGeometryGame()
  31. {
  32. graphics = new GraphicsDeviceManager(this);
  33. Content.RootDirectory = "Content";
  34. #if WINDOWS_PHONE
  35. // Frame rate is 30 fps by default for Windows Phone.
  36. TargetElapsedTime = TimeSpan.FromTicks(333333);
  37. graphics.IsFullScreen = true;
  38. #endif
  39. }
  40. /// <summary>
  41. /// Load your graphics content.
  42. /// </summary>
  43. protected override void LoadContent()
  44. {
  45. terrain = Content.Load<Model>("terrain");
  46. sky = Content.Load<Sky>("sky");
  47. }
  48. #endregion
  49. #region Update and Draw
  50. /// <summary>
  51. /// Allows the game to run logic.
  52. /// </summary>
  53. protected override void Update(GameTime gameTime)
  54. {
  55. HandleInput();
  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 device = graphics.GraphicsDevice;
  64. device.Clear(Color.Black);
  65. // Calculate the projection matrix.
  66. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  67. device.Viewport.AspectRatio,
  68. 1, 10000);
  69. // Calculate a view matrix, moving the camera around a circle.
  70. float time = (float)gameTime.TotalGameTime.TotalSeconds * 0.333f;
  71. float cameraX = (float)Math.Cos(time);
  72. float cameraY = (float)Math.Sin(time);
  73. Vector3 cameraPosition = new Vector3(cameraX, 0, cameraY) * 64;
  74. Vector3 cameraFront = new Vector3(-cameraY, 0, cameraX);
  75. Matrix view = Matrix.CreateLookAt(cameraPosition,
  76. cameraPosition + cameraFront,
  77. Vector3.Up);
  78. // Draw the terrain first, then the sky. This is faster than
  79. // drawing the sky first, because the depth buffer can skip
  80. // bothering to draw sky pixels that are covered up by the
  81. // terrain. This trick works because the code used to draw
  82. // the sky forces all the sky vertices to be as far away as
  83. // possible, and turns depth testing on but depth writes off.
  84. DrawTerrain(view, projection);
  85. sky.Draw(view, projection);
  86. // If there was any alpha blended translucent geometry in
  87. // the scene, that would be drawn here, after the sky.
  88. base.Draw(gameTime);
  89. }
  90. /// <summary>
  91. /// Helper for drawing the terrain model.
  92. /// </summary>
  93. void DrawTerrain(Matrix view, Matrix projection)
  94. {
  95. foreach (ModelMesh mesh in terrain.Meshes)
  96. {
  97. foreach (BasicEffect effect in mesh.Effects)
  98. {
  99. effect.View = view;
  100. effect.Projection = projection;
  101. effect.EnableDefaultLighting();
  102. // Set the specular lighting to match the sky color.
  103. effect.SpecularColor = new Vector3(0.6f, 0.4f, 0.2f);
  104. effect.SpecularPower = 8;
  105. // Set the fog to match the distant mountains
  106. // that are drawn into the sky texture.
  107. effect.FogEnabled = true;
  108. effect.FogColor = new Vector3(0.15f);
  109. effect.FogStart = 100;
  110. effect.FogEnd = 320;
  111. }
  112. mesh.Draw();
  113. }
  114. }
  115. #endregion
  116. #region Handle Input
  117. /// <summary>
  118. /// Handles input for quitting the game.
  119. /// </summary>
  120. private void HandleInput()
  121. {
  122. KeyboardState currentKeyboardState = Keyboard.GetState();
  123. GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
  124. // Check for exit.
  125. if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
  126. currentGamePadState.Buttons.Back == ButtonState.Pressed)
  127. {
  128. Exit();
  129. }
  130. }
  131. #endregion
  132. }
  133. #region Entry Point
  134. /// <summary>
  135. /// The main entry point for the application.
  136. /// </summary>
  137. static class Program
  138. {
  139. static void Main()
  140. {
  141. using (GeneratedGeometryGame game = new GeneratedGeometryGame())
  142. {
  143. game.Run();
  144. }
  145. }
  146. }
  147. #endregion
  148. }