public BrgMesh(BrgBinaryReader reader, BrgFile file)
: base()
{
this.ParentFile = file;
this.Header = new BrgMeshHeader(reader);
this.VertexMaterials = new List<Int16>();
if (!this.Header.Flags.HasFlag(BrgMeshFlag.PARTICLEPOINTS))
{
this.Vertices = new List<Vector3D>(this.Header.NumVertices);
for (int i = 0; i < this.Header.NumVertices; i++)
{
this.Vertices.Add(reader.ReadVector3D(true, this.Header.Version == 22));
}
this.Normals = new List<Vector3D>(this.Header.NumVertices);
for (int i = 0; i < this.Header.NumVertices; i++)
{
if (this.Header.Version >= 13 && this.Header.Version <= 17)
{
reader.ReadInt16(); // No idea what this is
}
else // v == 18, 19 or 22
{
this.Normals.Add(reader.ReadVector3D(true, this.Header.Version == 22));
}
}
if ((!this.Header.Flags.HasFlag(BrgMeshFlag.SECONDARYMESH) ||
this.Header.Flags.HasFlag(BrgMeshFlag.ANIMTEXCOORDS) ||
this.Header.Flags.HasFlag(BrgMeshFlag.PARTICLESYSTEM)) &&
this.Header.Flags.HasFlag(BrgMeshFlag.TEXCOORDSA))
{
this.TextureCoordinates = new List<Vector3D>(this.Header.NumVertices);
for (int i = 0; i < this.Header.NumVertices; i++)
{
this.TextureCoordinates.Add(new Vector3D(reader.ReadVector2D(this.Header.Version == 22), 0f));
}
}
if (!this.Header.Flags.HasFlag(BrgMeshFlag.SECONDARYMESH) ||
this.Header.Flags.HasFlag(BrgMeshFlag.PARTICLESYSTEM))
{
this.Faces = new List<Face>(this.Header.NumFaces);
for (int i = 0; i < this.Header.NumFaces; ++i)
{
this.Faces.Add(new Face());
}
if (this.Header.Flags.HasFlag(BrgMeshFlag.MATERIAL))
{
for (int i = 0; i < this.Header.NumFaces; i++)
{
this.Faces[i].MaterialIndex = reader.ReadInt16();
}
}
for (int i = 0; i < this.Header.NumFaces; i++)
{
this.Faces[i].Indices.Add(reader.ReadInt16());
this.Faces[i].Indices.Add(reader.ReadInt16());
this.Faces[i].Indices.Add(reader.ReadInt16());
}
if (this.Header.Flags.HasFlag(BrgMeshFlag.MATERIAL))
{
this.VertexMaterials = new List<Int16>(this.Header.NumVertices);
for (int i = 0; i < this.Header.NumVertices; i++)
{
this.VertexMaterials.Add(reader.ReadInt16());
}
}
}
}
this.UserDataEntries = new BrgUserDataEntry[this.Header.UserDataEntryCount];
if (!this.Header.Flags.HasFlag(BrgMeshFlag.PARTICLEPOINTS))
{
for (int i = 0; i < this.Header.UserDataEntryCount; i++)
{
this.UserDataEntries[i] = reader.ReadUserDataEntry(false);
}
}
this.ExtendedHeader = new BrgMeshExtendedHeader(reader, this.Header.ExtendedHeaderSize);
this.particleData = new float[0];
if (this.Header.Flags.HasFlag(BrgMeshFlag.PARTICLEPOINTS))
{
this.particleData = new float[4 * this.Header.NumVertices];
for (int i = 0; i < this.particleData.Length; i++)
{
this.particleData[i] = reader.ReadSingle();
}
for (int i = 0; i < this.Header.UserDataEntryCount; i++)
{
this.UserDataEntries[i] = reader.ReadUserDataEntry(true);
}
}
if (this.Header.Version == 13)
{
reader.ReadBytes(this.ExtendedHeader.NameLength);
}
if (this.Header.Version >= 13 && this.Header.Version <= 18)
{
this.Header.Flags |= BrgMeshFlag.ATTACHPOINTS;
}
if (this.Header.Flags.HasFlag(BrgMeshFlag.ATTACHPOINTS))
{
Int16 numMatrix = this.ExtendedHeader.NumDummies;
Int16 numIndex = this.ExtendedHeader.NumNameIndexes;
if (this.Header.Version == 19 || this.Header.Version == 22)
{
numMatrix = reader.ReadInt16();
numIndex = reader.ReadInt16();
reader.ReadInt16();
}
BrgAttachpoint[] attpts = new BrgAttachpoint[numMatrix];
for (int i = 0; i < numMatrix; i++)
{
attpts[i] = new BrgAttachpoint();
}
for (int i = 0; i < numMatrix; i++)
{
attpts[i].XVector = reader.ReadVector3D(true, this.Header.Version == 22);
}
for (int i = 0; i < numMatrix; i++)
{
attpts[i].YVector = reader.ReadVector3D(true, this.Header.Version == 22);
}
for (int i = 0; i < numMatrix; i++)
{
attpts[i].ZVector = reader.ReadVector3D(true, this.Header.Version == 22);
}
if (this.Header.Version == 19 || this.Header.Version == 22)
{
for (int i = 0; i < numMatrix; i++)
{
attpts[i].Position = reader.ReadVector3D(true, this.Header.Version == 22);
}
}
for (int i = 0; i < numMatrix; i++)
{
attpts[i].BoundingBoxMin = reader.ReadVector3D(true, this.Header.Version == 22);
}
for (int i = 0; i < numMatrix; i++)
{
attpts[i].BoundingBoxMax = reader.ReadVector3D(true, this.Header.Version == 22);
}
List<int> nameId = new List<int>();
for (int i = 0; i < numIndex; i++)
{
int duplicate = reader.ReadInt32(); // have yet to find a model with duplicates
reader.ReadInt32(); // Skip the id (at least I think its an ID)
for (int j = 0; j < duplicate; j++)
{
nameId.Add(i);
}
}
this.Attachpoints = new List<BrgAttachpoint>();
for (int i = 0; i < nameId.Count; i++)
{
this.Attachpoints.Add(new BrgAttachpoint(attpts[reader.ReadByte()]));
this.Attachpoints[i].NameId = nameId[i];
//attpts[reader.ReadByte()].NameId = nameId[i];
}
//attachpoints = new List<BrgAttachpoint>(attpts);
}
else
{
this.Attachpoints = new List<BrgAttachpoint>();
}
if (((this.Header.Flags.HasFlag(BrgMeshFlag.COLORALPHACHANNEL) ||
this.Header.Flags.HasFlag(BrgMeshFlag.COLORCHANNEL)) &&
!this.Header.Flags.HasFlag(BrgMeshFlag.SECONDARYMESH)) ||
this.Header.Flags.HasFlag(BrgMeshFlag.ANIMVERTCOLORALPHA))
{
this.Colors = new List<Color4D>(this.Header.NumVertices);
for (int i = 0; i < this.Header.NumVertices; i++)
{
this.Colors.Add(reader.ReadTexel());
}
}
// Only seen on first mesh
this.NonUniformKeys = new float[0];
if (this.Header.AnimationType.HasFlag(BrgMeshAnimType.NonUniform))
{
this.NonUniformKeys = new float[this.ExtendedHeader.NumNonUniformKeys];
for (int i = 0; i < this.ExtendedHeader.NumNonUniformKeys; i++)
{
this.NonUniformKeys[i] = reader.ReadSingle();
}
}
if (this.Header.Version >= 14 && this.Header.Version <= 19)
{
// Face Normals??
Vector3D[] legacy = new Vector3D[this.Header.NumFaces];
for (int i = 0; i < this.Header.NumFaces; i++)
{
legacy[i] = reader.ReadVector3D();
}
}
if (!this.Header.Flags.HasFlag(BrgMeshFlag.SECONDARYMESH) && this.Header.Version != 22)
{
reader.ReadBytes(ExtendedHeader.ShadowNameLength0 + ExtendedHeader.ShadowNameLength1);
}
}