topical media & game development

talk show tell print

game-xna-intro-XnaGraphicEngineChapter6-Shaders-SimpleShader.cs / cs



  #region Using directives
  using System;
  using System.Collections.Generic;
  using System.Text;
  using XnaGraphicEngine.Graphics;
  using XnaGraphicEngine.Helpers;
  using XnaGraphicEngine.Game;
  using Microsoft.Xna.Framework.Graphics;
  using Texture = XnaGraphicEngine.Graphics.Texture;
  using Model = XnaGraphicEngine.Graphics.Model;
  using XnaModel = Microsoft.Xna.Framework.Graphics.Model;
  using Microsoft.Xna.Framework;
  using System.IO;
  using Microsoft.Xna.Framework.Input;
  #endregion
  
  namespace XnaGraphicEngine.Shaders
  {
  
<summary> Simple shader </summary> class SimpleShader : IGraphicContent { #region Variables Effect effect = null; #endregion

                  #region Constructor
                  public SimpleShader()
                  {
                          Load();
                          BaseGame.RegisterGraphicContentObject(this);
                  } // SimpleShader()
                  #endregion
  
                  #region Load
                  public void Load()
                  {
                          effect = BaseGame.Content.Load<Effect>(
                                  Path.Combine(Directories.ContentDirectory, "SimpleShader"));
                  } // Load()
                  #endregion
  
                  #region Dispose
                  public void Dispose()
                  {
                          if (effect != null)
                                  effect.Dispose();
                          effect = null;
                  } // Dispose()
                  #endregion
  
                  #region Render mesh
                  public void RenderModel(XnaModel someModel, Texture texture)
                  {
                          if (effect == null)
                                  return;
  
                          BaseGame.WorldMatrix =
                                  Matrix.CreateScale(0.25f, 0.25f, 0.25f);
  
                          effect.Parameters["worldViewProj"].SetValue(
                                  BaseGame.WorldMatrix *
                                  BaseGame.ViewMatrix *
                                  BaseGame.ProjectionMatrix);
                          effect.Parameters["world"].SetValue(
                                  BaseGame.WorldMatrix);
                          effect.Parameters["viewInverse"].SetValue(
                                  BaseGame.InverseViewMatrix);
                          effect.Parameters["lightDir"].SetValue(
                                  BaseGame.LightDirection);
                          effect.Parameters["diffuseTexture"].SetValue(
                                  texture.XnaTexture);
  
                          effect.Parameters["ambientColor"].SetValue(
                                  Color.DarkBlue.ToVector4());
                          effect.Parameters["diffuseColor"].SetValue(
                                  Color.LightCyan.ToVector4());
                          effect.Parameters["specularColor"].SetValue(
                                  Color.LightPink.ToVector4());
  
                          // We use the VertexPositionNormalTexture vertex format in SimpleShader.fx
                          BaseGame.Device.VertexDeclaration =
                                  new VertexDeclaration(BaseGame.Device,
                                  VertexPositionNormalTexture.VertexElements);
                          //BaseGame.Device.RenderState.FillMode = FillMode.WireFrame;
  
                          if (Input.Keyboard.IsKeyDown(Keys.Space))
                                  effect.CurrentTechnique = effect.Techniques["DiffusePerPixel"];
                          else
                                  effect.CurrentTechnique = effect.Techniques["SpecularPerPixel"];
  
                          effect.Begin();
                          foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                          {
                                  pass.Begin();
                                  
                                  // Render all meshes
                                  foreach (ModelMesh mesh in someModel.Meshes)
                                  {
                                          // Render all mesh parts
                                          foreach (ModelMeshPart part in mesh.MeshParts)
                                          {
                                                  // Render data our own way
                                                  BaseGame.Device.Vertices[0].SetSource(
                                                          mesh.VertexBuffer, part.StreamOffset, part.VertexStride);
                                                  BaseGame.Device.Indices = mesh.IndexBuffer;
  
                                                  // And render
                                                  BaseGame.Device.DrawIndexedPrimitives(
                                                          PrimitiveType.TriangleList,
                                                          part.BaseVertex, 0, part.NumVertices,
                                                          part.StartIndex, part.PrimitiveCount);
                                          } // foreach
                                  } // foreach
  
                                  pass.End();
                          } // for
                          effect.End();
                  } // RenderMesh(someMesh, texture)
                  #endregion
  
                  #region Unit Testing
  if DEBUG
                  public static void TestSimpleShader()
                  {
                          SimpleShader shader = null;
                          Model testModel = null;
                          Texture testTexture = null;
  
                          TestGame.Start("Test SimpleShader.fx",
                                  delegate
                                  {
                                          shader = new SimpleShader();
                                          testModel = new Model("Apple");
                                          testTexture = new Texture("Marble");
                                  },
                                  delegate
                                  {
                                          // Render model
                                          shader.RenderModel(testModel.XnaModel, testTexture);
                                  });
                  } // TestSimpleShader
  endif
                  #endregion
          } // class SimpleShader
  } // 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.