PreScreenSkyCubeMapping.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //-----------------------------------------------------------------------------
  2. // PreScreenSkyCubeMapping.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using RacingGame.Graphics;
  8. using RacingGame.Helpers;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Text;
  13. using System.Threading;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework;
  16. using RacingGame.GameScreens;
  17. using XnaModel = Microsoft.Xna.Framework.Graphics.Model;
  18. namespace RacingGame.Shaders
  19. {
  20. /// <summary>
  21. /// Pre screen sky cube mapping
  22. /// </summary>
  23. public class PreScreenSkyCubeMapping : ShaderEffect
  24. {
  25. /// <summary>
  26. /// Shader effect filename.
  27. /// </summary>
  28. const string Filename = "PreScreenSkyCubeMapping.fx";
  29. /// <summary>
  30. /// Sky cube map texture filename.
  31. /// </summary>
  32. const string SkyCubeMapFilename = "SkyCubeMap";
  33. /// <summary>
  34. /// Default sky color
  35. /// </summary>
  36. static readonly Color DefaultSkyColor = new Color(232, 232, 232);
  37. /// <summary>
  38. /// The Cube Map texture for the sky!
  39. /// </summary>
  40. private TextureCube skyCubeMapTexture = null;
  41. /// <summary>
  42. /// Sky cube map texture
  43. /// </summary>
  44. /// <returns>Texture cube</returns>
  45. public TextureCube SkyCubeMapTexture
  46. {
  47. get
  48. {
  49. return skyCubeMapTexture;
  50. }
  51. }
  52. private XnaModel cube;
  53. /// <summary>
  54. /// Create pre screen sky cube mapping
  55. /// </summary>
  56. public PreScreenSkyCubeMapping()
  57. : base(Filename)
  58. {
  59. cube = BaseGame.Content.Load<XnaModel>(Path.Combine(Directories.ContentDirectory, "Models", "Cube"));
  60. }
  61. /// <summary>
  62. /// Reload
  63. /// </summary>
  64. protected override void GetParameters()
  65. {
  66. base.GetParameters();
  67. // Load and set cube map texture.
  68. // On Android the DDS asset uses DXT1 compression which is not supported
  69. // on GLES. Catch the failure so the rest of the game still loads;
  70. // sky rendering and reflections will be skipped until the content
  71. // pipeline is configured to produce an Android-compatible format.
  72. try
  73. {
  74. skyCubeMapTexture = BaseGame.Content.Load<TextureCube>(
  75. Path.Combine(Directories.ContentDirectory,
  76. "Textures",
  77. SkyCubeMapFilename));
  78. diffuseTexture.SetValue(skyCubeMapTexture);
  79. }
  80. catch (Exception ex)
  81. {
  82. Log.Write("SkyCubeMap load failed (likely unsupported DXT format on this platform): " + ex.Message);
  83. skyCubeMapTexture = null;
  84. }
  85. // Set sky color to nearly white
  86. AmbientColor = DefaultSkyColor;
  87. }
  88. /// <summary>
  89. /// Render sky with help of shader.
  90. /// </summary>
  91. public void RenderSky(Color setSkyColor)
  92. {
  93. // Can't render with shader if shader is not valid!
  94. if (this.Valid == false)
  95. return;
  96. // Texture failed to load (e.g. unsupported format on this platform).
  97. if (skyCubeMapTexture == null)
  98. return;
  99. // Don't use or write to the z buffer
  100. BaseGame.Device.DepthStencilState = DepthStencilState.None;
  101. BaseGame.Device.RasterizerState = RasterizerState.CullNone;
  102. // Also don't use any kind of blending.
  103. BaseGame.Device.BlendState = BlendState.Opaque;
  104. // Set effect parameters
  105. AmbientColor = setSkyColor;
  106. effect.Parameters["view"].SetValue(BaseGame.ViewMatrix);
  107. ProjectionMatrix = BaseGame.ProjectionMatrix;
  108. // Override model's effect and render
  109. cube.Meshes[0].MeshParts[0].Effect = effect;
  110. cube.Meshes[0].Draw();
  111. // Reset previous render states
  112. BaseGame.Device.DepthStencilState = DepthStencilState.Default;
  113. BaseGame.Device.RasterizerState = RasterizerState.CullCounterClockwise;
  114. BaseGame.Device.BlendState = BlendState.AlphaBlend;
  115. }
  116. /// <summary>
  117. /// Render sky
  118. /// </summary>
  119. public void RenderSky()
  120. {
  121. RenderSky(lastUsedAmbientColor);
  122. }
  123. }
  124. }