topical media & game development

talk show tell print

game-xna-intro-XnaShooterSoundProject-Shaders-PreScreenSkyCubeMapping.cs / cs



  // Project: XnaGraphicEngine, File: PreScreenSkyCubeMapping.cs
  // Namespace: XnaGraphicEngine.Shaders, Class: PreScreenSkyCubeMapping
  // Path: C:\code\XnaGraphicEngine\Shaders, Author: Abi
  // Code lines: 217, Size of file: 5,51 KB
  // Creation date: 08.09.2006 06:06
  // Last modified: 05.11.2006 00:52
  // Generated with Commenter by abi.exDream.com
  
  #region Using directives
  using XnaGraphicEngine.Graphics;
  using XnaGraphicEngine.Helpers;
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Text;
  using System.Threading;
  using Microsoft.Xna.Framework.Graphics;
  using Microsoft.Xna.Framework;
  using XnaGraphicEngine.Game;
  #endregion
  
  namespace XnaGraphicEngine.Shaders
  {
  
<summary> Pre screen sky cube mapping, if shaders are not supported a special fallback to fixed function pipeline rendering is provided also! </summary> public class PreScreenSkyCubeMapping : ShaderEffect { #region Variables <summary> Shader effect filename. </summary> const string Filename = "PreScreenSkyCubeMapping.fx";

  
<summary> Sky cube map texture filename. </summary> const string SkyCubeMapFilename = //"Textures\\SkyCubeMap.dds"; "SpaceSkyCubeMap"; // content name // Use same content name as used by models: //"SkyCubeMap~0"; // content name

  
<summary> Default sky color </summary> static readonly Color DefaultSkyColor = new Color(232, 232, 232);

  
<summary> The Cube Map texture for the sky! </summary> private TextureCube skyCubeMapTexture = null;

  
<summary> Sky cube map texture </summary> <returns>Texture cube</returns> public TextureCube SkyCubeMapTexture { get { return skyCubeMapTexture; } // get } // SkyCubeMapTexture #endregion

                  #region Constructor
  
<summary> Create pre screen sky cube mapping </summary> public PreScreenSkyCubeMapping() : base(Filename) { // All loading is done in the ShaderEffect class.

                          BaseGame.RegisterGraphicContentObject(this);
                  } // PreScreenSkyCubeMapping()
                  #endregion
  
                  #region Dispose
  
<summary> Dispose </summary> public override void Dispose() { if (skyCubeMapTexture != null) skyCubeMapTexture.Dispose(); skyCubeMapTexture = null; base.Dispose(); } // Dispose() #endregion

                  #region Get parameters
  
<summary> Reload </summary> protected override void GetParameters() { base.GetParameters();

                          // Load and set cube map texture
                          /*obs
                          skyCubeMapTexture = TextureCube.FromFile(
                                  BaseGame.Device, SkyCubeMapFilename);
  			 */
                          skyCubeMapTexture = BaseGame.Content.Load<TextureCube>(
                                  Path.Combine(Directories.ContentDirectory, SkyCubeMapFilename));
                          diffuseTexture.SetValue(skyCubeMapTexture);
  
                          // Set sky color to nearly white and scale to 1
                          AmbientColor = DefaultSkyColor;
  
                          // Select SkyCubeMap technique
                          //unused: effect.te.Technique = "SkyCubeMap";
                  } // GetParameters()
                  #endregion
  
                  #region Render sky
  
<summary> Render sky with help of shader. </summary> public void RenderSky(Color setSkyColor) { // Can't render with shader if shader is not valid! if (this.Valid == false) return;

                          try
                          {
                                  // Don't use or write to the z buffer
                                  BaseGame.Device.RenderState.DepthBufferEnable = false;
                                  BaseGame.Device.RenderState.DepthBufferWriteEnable = false;
  
                                  // Also don't use any kind of blending.
                                  BaseGame.Device.RenderState.AlphaBlendEnable = false;
  
                                  AmbientColor = setSkyColor;
                                  // Rotate view matrix by level number, this way we look to a different
                                  // direction depending on which level we are in.
                                  Matrix invViewMatrix = BaseGame.InverseViewMatrix;// *
                                          //TODO: Matrix.RotationY(-Level.currentLevelNumber * (float)Math.PI / 2.0f);
                                  InverseViewMatrix = invViewMatrix;
  
                                  // Start shader
                                  try
                                  {
                                          // Remember old state because we will use clamp texturing here
                                          effect.Begin(SaveStateMode.SaveState);
  
                                          // Render with specific pass
                                          //foreach (EffectPass pass in effect.Techniques[0].Passes)
                                          for (int num = 0; num < effect.CurrentTechnique.Passes.Count; num++)
                                          {
                                                  EffectPass pass = effect.CurrentTechnique.Passes[num];
  
                                                  // Render each pass
                                                  pass.Begin();
                                                  VBScreenHelper.Render();
                                                  pass.End();
                                          } // foreach (pass)
                                  } // try
                                  finally
                                  {
                                          // End shader
                                          effect.End();
                                  } // finally
                          } // try
                          catch (Exception ex)
                          {
                                  Log.Write("Failed to render " +
                                          Filename + " shader: " + ex.ToString());
                                  // Make shader invalid, so we don't call it anymore
                                  effect = null;
                          } // catch
                          finally
                          {
                                  // Enable z buffer again
                                  BaseGame.Device.RenderState.DepthBufferEnable = true;
                                  BaseGame.Device.RenderState.DepthBufferWriteEnable = true;
                          } // finally
                  } // RenderSky(setWrappingScale, setSkyColor)
  
  
<summary> Render sky </summary> public void RenderSky() { RenderSky(lastUsedAmbientColor); } // RenderSky() #endregion

                  #region Unit testing
  if DEBUG
  
<summary> Test sky cube mapping </summary> //[Test] public static void TestSkyCubeMapping() { PreScreenSkyCubeMapping skyCube = null;

                          TestGame.Start("TestSkyCubeMapping",
                                  delegate
                                  {
                                          skyCube = new PreScreenSkyCubeMapping();
                                  },
                                  delegate
                                  {
                                          skyCube.RenderSky();
                                  });
                  } // TestSkyCubeMapping()
  endif
                  #endregion
          } // class PreScreenSkyCubeMapping
  } // namespace XnaGraphicEngine.Shaders
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.