BasicDemo.cs 5.5 KB

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