//--------------------------------------------------------------------------------------------------------------------------------------------------- // // Copyright (C)2007 DarkWynter Studios. All rights reserved. // //--------------------------------------------------------------------------------------------------------------------------------------------------- // {Contact : darkwynter.com for licensing information //--------------------------------------------------------------------------------------------------------------------------------------------------- namespace DarkWynter.Stream { #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Xml; using System.ComponentModel; #endregion /// /// Vertex data containing Position and Normal info /// public struct VertexPositionNormal { /// /// Position /// public Vector3 Position; /// /// Normal /// public Vector3 Normal; } /// /// Used to pass instance data to shaders through existing gpu pipelines. /// /// World matrix values are stored as follows: /// Fog = World.M11, World.M12, World.M13, World.M14 /// Binormal = World.M21, World.M22, World.M23, World.M24 /// Tangent = World.M31, World.M32, World.M33, World.M34 /// Depth = World.M41, World.M42, World.M43, World.M44 /// public struct VertexFogBinormalTangentDepth { /// /// World.M11, World.M12, World.M13, World.M14 /// public Vector4 Fog; /// /// World.M21, World.M22, World.M23, World.M24 /// public Vector4 Binormal; /// /// World.M31, World.M32, World.M33, World.M34 /// public Vector4 Tangent; /// /// World.M41, World.M42, World.M43, World.M44 /// public Vector4 Depth; } /// /// Stores all the draw function calls for game objects /// public class Draw { /// /// Usage of the draw object /// public enum DrawMethod : byte { /// /// Basic game object draw method /// BasicGameObject_Draw, /// /// Draw GPU object list /// GPUObjectList_Draw }; /// /// Model used by this GameObject. /// public Model model { get { return _model; } set { _model = value; } } private Model _model; /// /// Location and orientation matrix passed to shaders. /// public Matrix matrix { get { return _matrix; } set { _matrix = value; } } private Matrix _matrix; /// /// Textures associated with this object. /// public List textureList { get { return _textureList; } set { _textureList = value; } } private List _textureList = new List(); /// /// Needed to fix the rotation caused to animated models /// public Matrix initialTransform { get { return _initialTransform; } set { _initialTransform = value; } } private Matrix _initialTransform = Matrix.Identity; /// /// Buffer to store vertices /// public VertexBuffer vertexBuffer { get { return _vertexBuffer; } set { _vertexBuffer = value; } } private VertexBuffer _vertexBuffer; /// /// Vertex Declaration. /// public VertexDeclaration vertexDeclaration { get { return _vertexDeclaration; } set { _vertexDeclaration = value; } } private VertexDeclaration _vertexDeclaration; /// /// Instance data (Fog, Binormal, Tangent, Depth) /// Used for GameObject instancing /// public VertexFogBinormalTangentDepth[] instanceDataFBTD { get { return _instanceDataFBTD; } set { _instanceDataFBTD = value; } } private VertexFogBinormalTangentDepth[] _instanceDataFBTD; /// /// Usage of the draw object /// public DrawMethod drawMethod { get { return _drawMethod; } set { _drawMethod = value; } } private DrawMethod _drawMethod; /// /// Shader technique to use /// public string technique { get { return _technique; } set { _technique = value; } } private string _technique; #region Stream formats for instancing /// /// Tells the GPU how to format the streams for instancing /// Uses the Fog, Binormal, Tangent and Depth fields of the stream /// public VertexElement[] FBTDElements = new VertexElement[] { new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0), new VertexElement(0, sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0), new VertexElement(0, sizeof(float) * 6, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0), new VertexElement(1, 0, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1), new VertexElement(1, sizeof(float) * 4, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 2), new VertexElement(1, sizeof(float) * 8, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 3), new VertexElement(1, sizeof(float) * 12, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 4), }; /// /// Tells the GPU how to format the streams for instancing /// Used for instanced animation /// public VertexElement[] InstAnimElements = new VertexElement[] { new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0), new VertexElement(0, sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0), new VertexElement(0, sizeof(float) * 6, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0), new VertexElement(0, sizeof(float) * 8, VertexElementFormat.Byte4, VertexElementMethod.Default, VertexElementUsage.BlendIndices, 0), new VertexElement(0, sizeof(float) * 9, VertexElementFormat.Color, VertexElementMethod.Default, VertexElementUsage.BlendWeight, 0), new VertexElement(1, 0, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 1), new VertexElement(1, sizeof(float) * 4, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 2), new VertexElement(1, sizeof(float) * 8, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 3), new VertexElement(1, sizeof(float) * 12, VertexElementFormat.Vector4, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 4), }; #endregion /// /// Constructor /// public Draw() { } /// /// Constructor /// /// The draw method to use /// Technique to use public Draw(DrawMethod method, string drawTechnique) { drawMethod = method; technique = drawTechnique; } /// /// Base draw function which calls the required functions depending on the draw method of this object /// public void DoDraw(GraphicsDevice gd) { switch (drawMethod) { case DrawMethod.BasicGameObject_Draw: { DrawBasicGO(gd); break; } case DrawMethod.GPUObjectList_Draw: { DrawGPUObjectList(gd); break; } } } private void DrawBasicGO(GraphicsDevice gd) { //ShaderParameters.DrawFX.World.SetValue(matrix); //ShaderParameters.DrawFX.modelTexture1.SetValue(textureList[0]); //ShaderParameters.DrawFX.bumpTexture1.SetValue(textureList[1]); // Draw the model TriStrips(gd); } private void DrawGPUObjectList(GraphicsDevice gd) { //ShaderParameters.DrawFX.modelTexture1.SetValue(textureList[0]); //ShaderParameters.DrawFX.bumpTexture1.SetValue(textureList[1]); // Set the vertex declaration gd.VertexDeclaration = vertexDeclaration; gd.Vertices[0].SetFrequencyOfIndexData(instanceDataFBTD.Length); // Tell GPU how many times to run through the instance data and set the stream. gd.Vertices[1].SetSource(vertexBuffer, 0, gd.VertexDeclaration.GetVertexStrideSize(1)); gd.Vertices[1].SetFrequencyOfInstanceData(1); // Draw the model InstancedTriStrips(gd); } /// /// Draws a stand alone model with no instancing or triangle stripping. /// private void TriList(GraphicsDevice gd) { foreach (ModelMesh mesh in model.Meshes) { gd.Indices = mesh.IndexBuffer; foreach (ModelMeshPart part in mesh.MeshParts) { gd.VertexDeclaration = part.VertexDeclaration; gd.Vertices[0].SetSource(mesh.VertexBuffer, part.StreamOffset, part.VertexStride); part.Effect.CurrentTechnique = part.Effect.Techniques[technique]; part.Effect.Begin(); for (int k = 0; k < part.Effect.CurrentTechnique.Passes.Count; k++) { EffectPass pass = part.Effect.CurrentTechnique.Passes[k]; pass.Begin(); gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.BaseVertex, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount); pass.End(); } part.Effect.End(); } } } /// /// Draws a stand alone model that uses the triangle-strip content processor for loading. /// private void TriStrips(GraphicsDevice gd) { foreach (ModelMesh mesh in model.Meshes) { gd.Indices = mesh.IndexBuffer; foreach (ModelMeshPart part in mesh.MeshParts) { part.Effect.CurrentTechnique = part.Effect.Techniques[technique]; gd.VertexDeclaration = part.VertexDeclaration; gd.Vertices[0].SetSource(mesh.VertexBuffer, part.StreamOffset, part.VertexStride); int numPrimitives; if (mesh.IndexBuffer.IndexElementSize == IndexElementSize.SixteenBits) { numPrimitives = mesh.IndexBuffer.SizeInBytes / sizeof(ushort); } else { numPrimitives = mesh.IndexBuffer.SizeInBytes / sizeof(int); } numPrimitives -= 2; part.Effect.Begin(); foreach (EffectPass pass in part.Effect.CurrentTechnique.Passes) { pass.Begin(); gd.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, part.BaseVertex, 0, part.NumVertices, part.StartIndex, numPrimitives); pass.End(); } part.Effect.End(); } } } /// /// Draws an instanced model that uses the standard content processor. /// private void InstancedTriList(GraphicsDevice gd) { foreach (ModelMesh mesh in model.Meshes) { // Set the index buffer gd.Indices = mesh.IndexBuffer; // Tell the GPU how many times to run through the vertex data and set the stream. gd.Vertices[0].SetSource(mesh.VertexBuffer, 0, gd.VertexDeclaration.GetVertexStrideSize(0)); foreach (ModelMeshPart part in mesh.MeshParts) { part.Effect.CurrentTechnique = part.Effect.Techniques[technique]; part.Effect.Begin(); for (int k = 0; k < part.Effect.CurrentTechnique.Passes.Count; k++) { EffectPass pass = part.Effect.CurrentTechnique.Passes[k]; pass.Begin(); gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.BaseVertex, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount); pass.End(); } part.Effect.End(); } } } /// /// Draws an instanced model that uses the triangle-strip content processor. /// private void InstancedTriStrips(GraphicsDevice gd) { foreach (ModelMesh mesh in model.Meshes) { // Set the index buffer gd.Indices = mesh.IndexBuffer; int numPrimitives; if (mesh.IndexBuffer.IndexElementSize == IndexElementSize.SixteenBits) { numPrimitives = mesh.IndexBuffer.SizeInBytes / sizeof(ushort); } else { numPrimitives = mesh.IndexBuffer.SizeInBytes / sizeof(int); } numPrimitives -= 2; // Tell the GPU how many times to run through the vertex data and set the stream. gd.Vertices[0].SetSource(mesh.VertexBuffer, 0, gd.VertexDeclaration.GetVertexStrideSize(0)); foreach (ModelMeshPart part in mesh.MeshParts) { part.Effect.CurrentTechnique = part.Effect.Techniques[technique]; part.Effect.Begin(); for (int k = 0; k < part.Effect.CurrentTechnique.Passes.Count; k++) { EffectPass pass = part.Effect.CurrentTechnique.Passes[k]; pass.Begin(); //gd.RenderState.FillMode = Stream.Statics_Stream.RenderSettings.fillMode; gd.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, part.BaseVertex, 0, part.NumVertices, part.StartIndex, numPrimitives); pass.End(); } part.Effect.End(); } } } } }