Sky.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Sky.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 Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Content;
  12. using Microsoft.Xna.Framework.Graphics;
  13. #endregion
  14. namespace GeneratedGeometry
  15. {
  16. /// <summary>
  17. /// Runtime class for loading and rendering a textured skydome
  18. /// that was created during the build process by the SkyProcessor.
  19. /// </summary>
  20. public class Sky
  21. {
  22. #region Fields
  23. public Model Model;
  24. public Texture2D Texture;
  25. // The sky texture is a cylinder, so it should wrap from left to right,
  26. // but not from top to bottom. This requires a custom sampler state object.
  27. static readonly SamplerState WrapUClampV = new SamplerState
  28. {
  29. AddressU = TextureAddressMode.Wrap,
  30. AddressV = TextureAddressMode.Clamp,
  31. };
  32. #endregion
  33. /// <summary>
  34. /// Helper for drawing the skydome mesh.
  35. /// </summary>
  36. public void Draw(Matrix view, Matrix projection)
  37. {
  38. GraphicsDevice device = Texture.GraphicsDevice;
  39. // Set renderstates for drawing the sky. For maximum efficiency, we draw the sky
  40. // after everything else, with depth mode set to read only. This allows the GPU to
  41. // entirely skip drawing sky in the areas that are covered by other solid objects.
  42. device.DepthStencilState = DepthStencilState.DepthRead;
  43. device.SamplerStates[0] = WrapUClampV;
  44. device.BlendState = BlendState.Opaque;
  45. // Because the sky is infinitely far away, it should not move sideways as the camera
  46. // moves around the world, so we force the view matrix translation to zero. This
  47. // way the sky only takes the camera rotation into account, ignoring its position.
  48. view.Translation = Vector3.Zero;
  49. // The sky should be drawn behind everything else, at the far clip plane.
  50. // We achieve this by tweaking the projection matrix to force z=w.
  51. projection.M13 = projection.M14;
  52. projection.M23 = projection.M24;
  53. projection.M33 = projection.M34;
  54. projection.M43 = projection.M44;
  55. // Draw the sky model.
  56. foreach (ModelMesh mesh in Model.Meshes)
  57. {
  58. foreach (BasicEffect effect in mesh.Effects)
  59. {
  60. effect.View = view;
  61. effect.Projection = projection;
  62. effect.Texture = Texture;
  63. effect.TextureEnabled = true;
  64. }
  65. mesh.Draw();
  66. }
  67. // Set modified renderstates back to their default values.
  68. device.DepthStencilState = DepthStencilState.Default;
  69. device.SamplerStates[0] = SamplerState.LinearWrap;
  70. }
  71. }
  72. }