DualDemo.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //-----------------------------------------------------------------------------
  2. // DualDemo.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. namespace XnaGraphicsDemo
  15. {
  16. /// <summary>
  17. /// Demo shows how to use DualTextureEffect.
  18. /// </summary>
  19. class DualDemo : MenuComponent
  20. {
  21. // Fields.
  22. Model model;
  23. BoolMenuEntry showTexture;
  24. BoolMenuEntry showLightmap;
  25. Texture2D grey;
  26. float cameraRotation = 0;
  27. float cameraArc = 0;
  28. /// <summary>
  29. /// Constructor.
  30. /// </summary>
  31. public DualDemo(DemoGame game)
  32. : base(game)
  33. {
  34. Entries.Add(showTexture = new BoolMenuEntry("texture"));
  35. Entries.Add(showLightmap = new BoolMenuEntry("light map"));
  36. Entries.Add(new MenuEntry { Text = "back", Clicked = delegate { Game.SetActiveMenu(0); } });
  37. }
  38. /// <summary>
  39. /// Resets the menu state.
  40. /// </summary>
  41. public override void Reset()
  42. {
  43. showTexture.Value = true;
  44. showLightmap.Value = true;
  45. cameraRotation = 124;
  46. cameraArc = -12;
  47. base.Reset();
  48. }
  49. /// <summary>
  50. /// Loads content for this demo.
  51. /// </summary>
  52. protected override void LoadContent()
  53. {
  54. model = Game.Content.Load<Model>("model");
  55. grey = new Texture2D(GraphicsDevice, 1, 1);
  56. grey.SetData(new Color[] { new Color(128, 128, 128, 255) });
  57. }
  58. /// <summary>
  59. /// Draws the DualTextureEffect demo.
  60. /// </summary>
  61. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  62. public override void Draw(GameTime gameTime)
  63. {
  64. DrawTitle("dual texture effect", new Color(128, 160, 128), new Color(96, 128, 96));
  65. // Compute camera matrices.
  66. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  67. Matrix rotation = Matrix.CreateRotationY(MathHelper.ToRadians(cameraRotation)) *
  68. Matrix.CreateRotationZ(MathHelper.ToRadians(cameraArc));
  69. Matrix view = Matrix.CreateLookAt(new Vector3(35, 13, 0),
  70. new Vector3(0, 3, 0),
  71. Vector3.Up);
  72. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  73. GraphicsDevice.Viewport.AspectRatio,
  74. 2, 100);
  75. Matrix[] transforms = new Matrix[model.Bones.Count];
  76. model.CopyAbsoluteBoneTransformsTo(transforms);
  77. GraphicsDevice.BlendState = BlendState.Opaque;
  78. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  79. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  80. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  81. foreach (ModelMesh mesh in model.Meshes)
  82. {
  83. List<Texture2D> textures = new List<Texture2D>();
  84. foreach (DualTextureEffect effect in mesh.Effects)
  85. {
  86. Matrix world = transforms[mesh.ParentBone.Index] * rotation;
  87. effect.World = world;
  88. effect.View = view;
  89. effect.Projection = projection;
  90. effect.DiffuseColor = new Vector3(0.75f);
  91. // Store the previous textures.
  92. textures.Add(effect.Texture);
  93. textures.Add(effect.Texture2);
  94. // Optionally disable one or both textures.
  95. if (!showTexture.Value)
  96. effect.Texture = grey;
  97. if (!showLightmap.Value)
  98. effect.Texture2 = grey;
  99. }
  100. // Draw the mesh.
  101. mesh.Draw();
  102. // Restore the original textures.
  103. int i = 0;
  104. foreach (DualTextureEffect effect in mesh.Effects)
  105. {
  106. effect.Texture = textures[i++];
  107. effect.Texture2 = textures[i++];
  108. }
  109. }
  110. base.Draw(gameTime);
  111. }
  112. /// <summary>
  113. /// Dragging on the menu background rotates the camera.
  114. /// </summary>
  115. /// <param name="delta">The amount the mouse was dragged.</param>
  116. protected override void OnDrag(Vector2 delta)
  117. {
  118. cameraRotation = MathHelper.Clamp(cameraRotation + delta.X / 8, 0, 180);
  119. cameraArc = MathHelper.Clamp(cameraArc - delta.Y / 8, -50, 15);
  120. }
  121. }
  122. }