ShapeRenderingSampleGame.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ShapeRenderingSampleGame.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Input;
  13. namespace ShapeRenderingSample
  14. {
  15. /// <summary>
  16. /// This is the main type for your game
  17. /// </summary>
  18. public class ShapeRenderingSampleGame : Game
  19. {
  20. GraphicsDeviceManager graphics;
  21. SpriteBatch spriteBatch;
  22. // The shapes that we'll be drawing
  23. BoundingBox box;
  24. BoundingFrustum frustum;
  25. BoundingSphere sphere;
  26. public ShapeRenderingSampleGame()
  27. {
  28. graphics = new GraphicsDeviceManager(this);
  29. Content.RootDirectory = "Content";
  30. #if WINDOWS_PHONE
  31. TargetElapsedTime = TimeSpan.FromTicks(333333);
  32. graphics.IsFullScreen = true;
  33. #endif
  34. }
  35. /// <summary>
  36. /// LoadContent will be called once per game and is the place to load
  37. /// all of your content.
  38. /// </summary>
  39. protected override void LoadContent()
  40. {
  41. // Create a new SpriteBatch, which can be used to draw textures.
  42. spriteBatch = new SpriteBatch(GraphicsDevice);
  43. // Create a box that is centered on the origin and extends from (-3, -3, -3) to (3, 3, 3)
  44. box = new BoundingBox(new Vector3(-3f), new Vector3(3f));
  45. // Create our frustum to simulate a camera sitting at the origin, looking down the X axis, with a 16x9
  46. // aspect ratio, a near plane of 1, and a far plane of 5
  47. Matrix frustumView = Matrix.CreateLookAt(Vector3.Zero, Vector3.UnitX, Vector3.Up);
  48. Matrix frustumProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 16f / 9f, 1f, 5f);
  49. frustum = new BoundingFrustum(frustumView * frustumProjection);
  50. // Create a sphere that is centered on the origin and has a radius of 3
  51. sphere = new BoundingSphere(Vector3.Zero, 3f);
  52. // Initialize our renderer
  53. DebugShapeRenderer.Initialize(GraphicsDevice);
  54. }
  55. /// <summary>
  56. /// Allows the game to run logic such as updating the world,
  57. /// checking for collisions, gathering input, and playing audio.
  58. /// </summary>
  59. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  60. protected override void Update(GameTime gameTime)
  61. {
  62. // Allow the game to exit
  63. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
  64. Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
  65. this.Exit();
  66. base.Update(gameTime);
  67. }
  68. /// <summary>
  69. /// This is called when the game should draw itself.
  70. /// </summary>
  71. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  72. protected override void Draw(GameTime gameTime)
  73. {
  74. GraphicsDevice.Clear(Color.CornflowerBlue);
  75. // Figure out our camera location. We spin around the origin based on time.
  76. float angle = (float)gameTime.TotalGameTime.TotalSeconds;
  77. Vector3 eye = new Vector3((float)Math.Cos(angle * .5f), 0f, (float)Math.Sin(angle * .5f)) * 12f;
  78. eye.Y = 5f;
  79. // Construct our view and projection matrices
  80. Matrix view = Matrix.CreateLookAt(eye, Vector3.Zero, Vector3.Up);
  81. Matrix projection = Matrix.CreatePerspectiveFieldOfView(
  82. MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, .1f, 1000f);
  83. // Add our shapes to be rendered
  84. DebugShapeRenderer.AddBoundingBox(box, Color.Yellow);
  85. DebugShapeRenderer.AddBoundingFrustum(frustum, Color.Green);
  86. DebugShapeRenderer.AddBoundingSphere(sphere, Color.Red);
  87. // Also add a triangle and a line
  88. DebugShapeRenderer.AddTriangle(new Vector3(-1f, 0f, 0f), new Vector3(1f, 0f, 0f), new Vector3(0f, 2f, 0f), Color.Purple);
  89. DebugShapeRenderer.AddLine(new Vector3(0f, 0f, 0f), new Vector3(3f, 3f, 3f), Color.Brown);
  90. // Render our shapes now
  91. DebugShapeRenderer.Draw(gameTime, view, projection);
  92. base.Draw(gameTime);
  93. }
  94. }
  95. }