EnvmapDemo.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // EnvmapDemo.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 EnvironmentMapEffect.
  22. /// </summary>
  23. class EnvmapDemo : MenuComponent
  24. {
  25. // Fields.
  26. Model model;
  27. Texture2D background;
  28. FloatMenuEntry amount;
  29. FloatMenuEntry fresnel;
  30. FloatMenuEntry specular;
  31. /// <summary>
  32. /// Constructor.
  33. /// </summary>
  34. public EnvmapDemo(DemoGame game)
  35. : base(game)
  36. {
  37. Entries.Add(amount = new FloatMenuEntry() { Text = "envmap" });
  38. Entries.Add(fresnel = new FloatMenuEntry() { Text = "fresnel" });
  39. Entries.Add(specular = new FloatMenuEntry() { Text = "specular" });
  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. amount.Value = 1;
  48. fresnel.Value = 0.25f;
  49. specular.Value = 0.5f;
  50. base.Reset();
  51. }
  52. /// <summary>
  53. /// Loads content for this demo.
  54. /// </summary>
  55. protected override void LoadContent()
  56. {
  57. background = Game.Content.Load<Texture2D>("background");
  58. model = Game.Content.Load<Model>("saucer");
  59. }
  60. /// <summary>
  61. /// Draws the EnvironmentMapEffect demo.
  62. /// </summary>
  63. public override void Draw(GameTime gameTime)
  64. {
  65. GraphicsDevice.Clear(Color.Black);
  66. // Draw the background image.
  67. SpriteBatch.Begin(0, BlendState.Opaque);
  68. SpriteBatch.Draw(background, new Rectangle(0, 0, 480, 800), Color.White);
  69. SpriteBatch.End();
  70. DrawTitle("environment map effect", null, new Color(93, 142, 196));
  71. // Compute camera matrices.
  72. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  73. Matrix rotation = Matrix.CreateRotationX(time * 0.3f) *
  74. Matrix.CreateRotationY(time);
  75. Matrix view = Matrix.CreateLookAt(new Vector3(4500, -400, 0),
  76. new Vector3(0, -400, 0),
  77. Vector3.Up);
  78. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  79. GraphicsDevice.Viewport.AspectRatio,
  80. 10, 10000);
  81. Matrix[] transforms = new Matrix[model.Bones.Count];
  82. model.CopyAbsoluteBoneTransformsTo(transforms);
  83. GraphicsDevice.BlendState = BlendState.Opaque;
  84. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  85. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  86. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  87. // Draw the spaceship model.
  88. foreach (ModelMesh mesh in model.Meshes)
  89. {
  90. foreach (EnvironmentMapEffect effect in mesh.Effects)
  91. {
  92. Matrix world = transforms[mesh.ParentBone.Index] * rotation;
  93. effect.World = world;
  94. effect.View = view;
  95. effect.Projection = projection;
  96. effect.EnableDefaultLighting();
  97. effect.EnvironmentMapAmount = amount.Value;
  98. effect.FresnelFactor = fresnel.Value * 2;
  99. effect.EnvironmentMapSpecular = new Vector3(1, 1, 0.5f) * specular.Value;
  100. }
  101. mesh.Draw();
  102. }
  103. base.Draw(gameTime);
  104. }
  105. }
  106. }