SpinningTriangleControl.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SpinningTriangleControl.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.Diagnostics;
  11. using System.Windows.Forms;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. #endregion
  15. namespace WinFormsGraphicsDevice
  16. {
  17. /// <summary>
  18. /// Example control inherits from GraphicsDeviceControl, which allows it to
  19. /// render using a GraphicsDevice. This control shows how to draw animating
  20. /// 3D graphics inside a WinForms application. It hooks the Application.Idle
  21. /// event, using this to invalidate the control, which will cause the animation
  22. /// to constantly redraw.
  23. /// </summary>
  24. class SpinningTriangleControl : GraphicsDeviceControl
  25. {
  26. BasicEffect effect;
  27. Stopwatch timer;
  28. // Vertex positions and colors used to display a spinning triangle.
  29. public readonly VertexPositionColor[] Vertices =
  30. {
  31. new VertexPositionColor(new Vector3(-1, -1, 0), Color.Black),
  32. new VertexPositionColor(new Vector3( 1, -1, 0), Color.Black),
  33. new VertexPositionColor(new Vector3( 0, 1, 0), Color.Black),
  34. };
  35. /// <summary>
  36. /// Initializes the control.
  37. /// </summary>
  38. protected override void Initialize()
  39. {
  40. // Create our effect.
  41. effect = new BasicEffect(GraphicsDevice);
  42. effect.VertexColorEnabled = true;
  43. // Start the animation timer.
  44. timer = Stopwatch.StartNew();
  45. // Hook the idle event to constantly redraw our animation.
  46. Application.Idle += delegate { Invalidate(); };
  47. }
  48. /// <summary>
  49. /// Draws the control.
  50. /// </summary>
  51. protected override void Draw()
  52. {
  53. GraphicsDevice.Clear(Color.CornflowerBlue);
  54. // Spin the triangle according to how much time has passed.
  55. float time = (float)timer.Elapsed.TotalSeconds;
  56. float yaw = time * 0.7f;
  57. float pitch = time * 0.8f;
  58. float roll = time * 0.9f;
  59. // Set transform matrices.
  60. float aspect = GraphicsDevice.Viewport.AspectRatio;
  61. effect.World = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);
  62. effect.View = Matrix.CreateLookAt(new Vector3(0, 0, -5),
  63. Vector3.Zero, Vector3.Up);
  64. effect.Projection = Matrix.CreatePerspectiveFieldOfView(1, aspect, 1, 10);
  65. // Set renderstates.
  66. GraphicsDevice.RasterizerState = RasterizerState.CullNone;
  67. // Draw the triangle.
  68. effect.CurrentTechnique.Passes[0].Apply();
  69. GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList,
  70. Vertices, 0, 1);
  71. }
  72. }
  73. }