BasicDemo.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //-----------------------------------------------------------------------------
  2. // BasicDemo.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using SkinnedModel;
  14. using SimpleAnimation;
  15. using GeneratedGeometry;
  16. namespace XnaGraphicsDemo
  17. {
  18. /// <summary>
  19. /// Enum controls what kind of lighting to use.
  20. /// </summary>
  21. public enum LightingMode
  22. {
  23. NoLighting,
  24. OneVertexLight,
  25. ThreeVertexLights,
  26. ThreePixelLights,
  27. }
  28. /// <summary>
  29. /// Demo shows how to use BasicEffect.
  30. /// </summary>
  31. class BasicDemo : MenuComponent
  32. {
  33. // Fields.
  34. Model grid;
  35. Tank tank = new Tank();
  36. LightModeMenu lightMode;
  37. BoolMenuEntry textureEnable;
  38. float zoom = 1;
  39. /// <summary>
  40. /// Constructor.
  41. /// </summary>
  42. public BasicDemo(DemoGame game)
  43. : base(game)
  44. {
  45. Entries.Add(textureEnable = new BoolMenuEntry("texture"));
  46. Entries.Add(lightMode = new LightModeMenu());
  47. Entries.Add(new MenuEntry { Text = "back", Clicked = delegate { Game.SetActiveMenu(0); } });
  48. }
  49. /// <summary>
  50. /// Resets the menu state.
  51. /// </summary>
  52. public override void Reset()
  53. {
  54. lightMode.LightMode = LightingMode.ThreeVertexLights;
  55. textureEnable.Value = true;
  56. zoom = 1;
  57. base.Reset();
  58. }
  59. /// <summary>
  60. /// Loads content for this demo.
  61. /// </summary>
  62. protected override void LoadContent()
  63. {
  64. tank.Load(Game.Content);
  65. grid = Game.Content.Load<Model>("grid");
  66. }
  67. /// <summary>
  68. /// Updates the tank animation.
  69. /// </summary>
  70. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  71. public override void Update(GameTime gameTime)
  72. {
  73. tank.Animate(gameTime);
  74. base.Update(gameTime);
  75. }
  76. /// <summary>
  77. /// Draws the BasicEffect demo.
  78. /// </summary>
  79. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  80. public override void Draw(GameTime gameTime)
  81. {
  82. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  83. // Compute camera matrices.
  84. Matrix rotation = Matrix.CreateRotationY(time * 0.1f);
  85. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  86. GraphicsDevice.Viewport.AspectRatio,
  87. 10,
  88. 20000);
  89. Matrix view = Matrix.CreateLookAt(new Vector3(1500, 550, 0) * zoom + new Vector3(0, 150, 0),
  90. new Vector3(0, 150, 0),
  91. Vector3.Up);
  92. // Draw the title.
  93. DrawTitle("basic effect", new Color(192, 192, 192), new Color(156, 156, 156));
  94. // Set render states.
  95. GraphicsDevice.BlendState = BlendState.Opaque;
  96. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  97. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  98. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  99. // Draw the background grid.
  100. grid.Draw(Matrix.CreateScale(1.5f) * rotation, view, projection);
  101. // Draw the tank model.
  102. tank.Draw(rotation, view, projection, lightMode.LightMode, textureEnable.Value);
  103. base.Draw(gameTime);
  104. }
  105. /// <summary>
  106. /// Dragging up and down on the menu background zooms in and out.
  107. /// </summary>
  108. /// <param name="delta">The amount the mouse was dragged.</param>
  109. protected override void OnDrag(Vector2 delta)
  110. {
  111. zoom = MathHelper.Clamp(zoom * (float)Math.Exp(delta.Y / 400), 0.4f, 6);
  112. }
  113. /// <summary>
  114. /// Custom menu entry subclass for cycling through the different lighting options.
  115. /// </summary>
  116. class LightModeMenu : MenuEntry
  117. {
  118. public LightingMode LightMode = LightingMode.ThreeVertexLights;
  119. /// <summary>
  120. /// Handles the click event to cycle through lighting modes.
  121. /// </summary>
  122. public override void OnClicked()
  123. {
  124. if (LightMode == LightingMode.ThreePixelLights)
  125. LightMode = 0;
  126. else
  127. LightMode++;
  128. base.OnClicked();
  129. }
  130. /// <summary>
  131. /// Gets or sets the text for the lighting mode menu entry.
  132. /// </summary>
  133. public override string Text
  134. {
  135. get
  136. {
  137. switch (LightMode)
  138. {
  139. case LightingMode.NoLighting: return "no lighting";
  140. case LightingMode.OneVertexLight: return "one vertex light";
  141. case LightingMode.ThreeVertexLights: return "three vertex lights";
  142. case LightingMode.ThreePixelLights: return "three pixel lights";
  143. default:
  144. throw new NotSupportedException();
  145. }
  146. }
  147. set { }
  148. }
  149. }
  150. }
  151. }