EnvmapDemo.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //-----------------------------------------------------------------------------
  2. // EnvmapDemo.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 EnvironmentMapEffect.
  18. /// </summary>
  19. class EnvmapDemo : MenuComponent
  20. {
  21. // Fields.
  22. Model model;
  23. Texture2D background;
  24. FloatMenuEntry amount;
  25. FloatMenuEntry fresnel;
  26. FloatMenuEntry specular;
  27. /// <summary>
  28. /// Constructor.
  29. /// </summary>
  30. public EnvmapDemo(DemoGame game)
  31. : base(game)
  32. {
  33. Entries.Add(amount = new FloatMenuEntry() { Text = "envmap" });
  34. Entries.Add(fresnel = new FloatMenuEntry() { Text = "fresnel" });
  35. Entries.Add(specular = new FloatMenuEntry() { Text = "specular" });
  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. amount.Value = 1;
  44. fresnel.Value = 0.25f;
  45. specular.Value = 0.5f;
  46. base.Reset();
  47. }
  48. /// <summary>
  49. /// Loads content for this demo.
  50. /// </summary>
  51. protected override void LoadContent()
  52. {
  53. background = Game.Content.Load<Texture2D>("background");
  54. model = Game.Content.Load<Model>("saucer");
  55. }
  56. /// <summary>
  57. /// Draws the EnvironmentMapEffect demo.
  58. /// </summary>
  59. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  60. public override void Draw(GameTime gameTime)
  61. {
  62. GraphicsDevice.Clear(Color.Black);
  63. // Draw the background image.
  64. SpriteBatch.Begin(0, BlendState.Opaque);
  65. SpriteBatch.Draw(background, new Rectangle(0, 0, 480, 800), Color.White);
  66. SpriteBatch.End();
  67. DrawTitle("environment map effect", null, new Color(93, 142, 196));
  68. // Compute camera matrices.
  69. float time = (float)gameTime.TotalGameTime.TotalSeconds;
  70. Matrix rotation = Matrix.CreateRotationX(time * 0.3f) *
  71. Matrix.CreateRotationY(time);
  72. Matrix view = Matrix.CreateLookAt(new Vector3(4500, -400, 0),
  73. new Vector3(0, -400, 0),
  74. Vector3.Up);
  75. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
  76. GraphicsDevice.Viewport.AspectRatio,
  77. 10, 10000);
  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. // Draw the spaceship model.
  85. foreach (ModelMesh mesh in model.Meshes)
  86. {
  87. foreach (EnvironmentMapEffect 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.EnableDefaultLighting();
  94. effect.EnvironmentMapAmount = amount.Value;
  95. effect.FresnelFactor = fresnel.Value * 2;
  96. effect.EnvironmentMapSpecular = new Vector3(1, 1, 0.5f) * specular.Value;
  97. }
  98. mesh.Draw();
  99. }
  100. base.Draw(gameTime);
  101. }
  102. }
  103. }