using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Havok
{
namespace Utilities
{
public class HkpMeshBuffer
{
public List VertexBuffer = new List();
public List IndexBuffer = new List();
public List IndexBuffer32Bit = new List();
public IndexElementSize IndexSize;
}
public class MeshUtilities
{
public static List CalculateOverallVertices(Model mdl)
{
List vertices = new List();
foreach (ModelMesh mesh in mdl.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
int offset = vertices.Count;
Vector3[] a = new Vector3[meshPart.NumVertices];
mesh.VertexBuffer.GetData(meshPart.StreamOffset + meshPart.BaseVertex * meshPart.VertexStride,
a, 0, meshPart.NumVertices, meshPart.VertexStride);
Matrix mat = Matrix.Identity;
for (int i = 0; i != a.Length; i++)
{
Vector3.Transform(ref a[i], ref mat, out a[i]);
}
vertices.AddRange(a);
}
}
return vertices;
}
public static HkpMeshBuffer GetMeshPartMeshBuffer(ModelMesh mesh, int partIndex)
{
HkpMeshBuffer meshBuffer = new HkpMeshBuffer();
switch (mesh.MeshParts[partIndex].VertexStride)
{
case 32:
{
VertexPositionNormalTexture[] vb = new VertexPositionNormalTexture[mesh.MeshParts[partIndex].NumVertices];
mesh.VertexBuffer.GetData(vb);
foreach (VertexPositionNormalTexture vertex in vb)
{
meshBuffer.VertexBuffer.Add(vertex.Position);
}
break;
}
case 24:
{
VertexPositionColorTexture[] vb = new VertexPositionColorTexture[mesh.MeshParts[partIndex].NumVertices];
mesh.VertexBuffer.GetData(vb);
foreach (VertexPositionColorTexture vertex in vb)
{
meshBuffer.VertexBuffer.Add(vertex.Position);
}
break;
}
case 20:
{
VertexPositionTexture[] vb = new VertexPositionTexture[mesh.MeshParts[partIndex].NumVertices];
mesh.VertexBuffer.GetData(vb);
foreach (VertexPositionTexture vertex in vb)
{
meshBuffer.VertexBuffer.Add(vertex.Position);
}
break;
}
case 16:
{
VertexPositionColor[] vb = new VertexPositionColor[mesh.MeshParts[partIndex].NumVertices];
mesh.VertexBuffer.GetData(vb);
foreach (VertexPositionColor vertex in vb)
{
meshBuffer.VertexBuffer.Add(vertex.Position);
}
break;
}
}
if (mesh.IndexBuffer.IndexElementSize == IndexElementSize.SixteenBits)
{
meshBuffer.IndexSize = IndexElementSize.SixteenBits;
short[] ib = new short[mesh.IndexBuffer.SizeInBytes / sizeof(short)];
mesh.IndexBuffer.GetData(mesh.MeshParts[partIndex].StreamOffset, ib, mesh.MeshParts[partIndex].BaseVertex + mesh.MeshParts[partIndex].StartIndex,
mesh.IndexBuffer.SizeInBytes / sizeof(short));
foreach (short index in ib)
{
meshBuffer.IndexBuffer.Add(index);
}
}
else if (mesh.IndexBuffer.IndexElementSize == IndexElementSize.ThirtyTwoBits)
{
meshBuffer.IndexSize = IndexElementSize.ThirtyTwoBits;
int[] ib = new int[mesh.IndexBuffer.SizeInBytes / sizeof(int)];
mesh.IndexBuffer.GetData(mesh.MeshParts[partIndex].StreamOffset, ib, mesh.MeshParts[partIndex].BaseVertex + mesh.MeshParts[partIndex].StartIndex,
mesh.IndexBuffer.SizeInBytes / sizeof(int));
foreach (int index in ib)
{
meshBuffer.IndexBuffer32Bit.Add(index);
}
}
else
{
throw new Exception("Unknown index format!");
}
return meshBuffer;
}
}
}
}