//---------------------------------------------------------------------------------------------------------------------------------------------------
//
// Copyright (C)2007 DarkWynter Studios. All rights reserved.
//
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {License Information: Creative Commons}
//---------------------------------------------------------------------------------------------------------------------------------------------------
namespace ElementalGame
{
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Xml;
#endregion
public struct ModelInfo
{
public string subTypeName;
public Texture2D currentTexture;
public Texture2D bumpTexture;
public int startingIndex; // Which index to start counting in your Index Buffer
public int minNumberOfVertexIndices; // Minimum number of used indices
public int numVertices; // Number of used vertices
public int startingVertex; // Starting Point in the Vertex Buffer
public int primitiveCount;
}
public class ModelManager
{
// Vertex Buffer
private VertexBuffer vertexBuffer;
private List vertexList;
private VertexDeclaration vertexDeclaration;
// Vertex index buffer
private IndexBuffer indexBuffer;
private List indexList;
private VertexPositionNormalTexture[] vertices;
private int[] indices;
private List modelInfoList;
private ModelInfo newModelInfo;
public ModelManager() { }
public void LoadGame(GraphicsDeviceManager graphics, ContentManager content)
{
modelInfoList = new List();
vertexList = new List();
indexList = new List();
}
public void LoadLevel(GraphicsDeviceManager graphics, ContentManager content, bool loadAllContent)
{
}
public void SetModelVerticesNIndices(GraphicsDeviceManager graphics)
{
vertices = new VertexPositionNormalTexture[vertexList.Count];
vertexList.CopyTo(vertices);
indices = new int[indexList.Count];
indexList.CopyTo(indices);
vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice,
VertexPositionNormalTexture.VertexElements);
vertexBuffer = new VertexBuffer(graphics.GraphicsDevice,
VertexPositionNormalTexture.SizeInBytes * vertexList.Count,
ResourceUsage.None,
ResourceManagementMode.Automatic);
indexBuffer = new IndexBuffer(graphics.GraphicsDevice,
sizeof(int) * indexList.Count,
ResourceUsage.None,
ResourceManagementMode.Automatic,
IndexElementSize.ThirtyTwoBits);
vertexBuffer.SetData(vertices);
indexBuffer.SetData(indices);
SetGraphicsBuffers(graphics.GraphicsDevice);
}
public void SetGraphicsBuffers(GraphicsDevice graphicsDevice)
{
graphicsDevice.VertexDeclaration = vertexDeclaration;
graphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionNormalTexture.SizeInBytes);
graphicsDevice.Indices = indexBuffer;
}
public ModelInfo GetModelInfo(string modelName)
{
foreach (ModelInfo modelInfo in modelInfoList)
{
if (modelInfo.subTypeName == modelName)
{
return modelInfo;
}
}
return new ModelInfo();
}
// Not sure if this is needed but still adding it just in case
public void LoadXML(ContentManager content, XmlNode node)
{
}
public bool CheckIfWeHaveANewModel(string newModelName)
{
// Check to make sure that this model hasn't already been loaded
foreach (ModelInfo modelInfo in modelInfoList)
{
if (modelInfo.subTypeName == newModelName)
{
return false;
}
}
return true;
}
public void AddNewModel(string newModelName, Model newModel, Texture2D newTexture)
{
// Its a new Model, so lets load it
newModelInfo = new ModelInfo();
newModelInfo.subTypeName = newModelName;
newModelInfo.currentTexture = newTexture;
int startingVertexLocation = vertexList.Count;
newModelInfo.startingIndex = indexList.Count;
newModelInfo.minNumberOfVertexIndices = 0;
newModelInfo.startingVertex = startingVertexLocation;
newModelInfo.primitiveCount = 0;
foreach (ModelMesh mesh in newModel.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
newModelInfo.primitiveCount += meshPart.PrimitiveCount;
newModelInfo.startingIndex = vertexList.Count + meshPart.BaseVertex;
newModelInfo.minNumberOfVertexIndices = 0;
newModelInfo.startingVertex = indexList.Count + meshPart.StartIndex;
newModelInfo.numVertices = meshPart.NumVertices;
}
VertexPositionNormalTexture[] vb = ((ModelVertexData)mesh.Tag).Vertices;
foreach (VertexPositionNormalTexture vertex in vb)
{
vertexList.Add(vertex);
}
int[] ib32 = ((ModelVertexData)mesh.Tag).Indices;
foreach (int index in ib32)
{
//indexList.Add(index + vertexList.Count);
indexList.Add(index);
}
}
//newModelInfo.primitiveCount = (int)(newModelInfo.numVertices / 3.0f);
modelInfoList.Add(newModelInfo);
}
}
}