DualDemo.cs 5.1 KB

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