topical media & game development
game-xna-intro-XnaShooterSoundProject-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
{
class SimpleShader
{
#region Variables
Effect effect = null;
#endregion
#region Constructor
public SimpleShader()
{
effect = BaseGame.Content.Load<Effect>(
Path.Combine(Directories.ContentDirectory, "SimpleShader"));
} // SimpleShader()
#endregion
#region Render mesh
public void RenderModel(XnaModel someModel, Texture texture)
{
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.Blue.ToVector4());
effect.Parameters["diffuseColor"].SetValue(
Color.Orange.ToVector4());
effect.Parameters["specularColor"].SetValue(
Color.Orchid.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()
public static void TestNormalMappingShader()
{
Model testModel = null;
Material testMaterial = null;
TestGame.Start("TestNormalMappingShader",
delegate
{
testModel = new Model("apple");
testMaterial = new Material(
Material.DefaultAmbientColor,
Material.DefaultDiffuseColor,
Material.DefaultSpecularColor,
"Marble",
"MarbleNormal", "", "");
},
delegate
{
// Render model
BaseGame.WorldMatrix = Matrix.CreateScale(0.25f, 0.25f, 0.25f);
BaseGame.Device.VertexDeclaration = TangentVertex.VertexDeclaration;
ShaderEffect.normalMapping.Render(testMaterial, "Specular20",
delegate
{
// Render all meshes
foreach (ModelMesh mesh in testModel.XnaModel.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
});
});
} // TestNormalMappingShader()
public static void TestAsteroidModel()
{
Model testModel = null;
TestGame.Start("TestNormalMappingShader",
delegate
{
testModel = new Model("asteroid4");
},
delegate
{
// Render model
testModel.Render(Matrix.CreateScale(10.25f, 10.25f, 10.25f));
});
} // TestAsteroidModel()
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.