static void ReadHeader(BinaryReader br, M2Model model) { var header = model.Header = new ModelHeader(); header.Magic = br.ReadUInt32(); header.Version = br.ReadUInt32(); header.NameLength = br.ReadInt32(); header.NameOffset = br.ReadInt32(); header.GlobalModelFlags = (GlobalModelFlags)br.ReadUInt32(); br.ReadOffsetLocation(ref header.GlobalSequences); br.ReadOffsetLocation(ref header.Animations); br.ReadOffsetLocation(ref header.AnimationLookup); br.ReadOffsetLocation(ref header.Bones); br.ReadOffsetLocation(ref header.KeyBoneLookup); br.ReadOffsetLocation(ref header.Vertices); header.ViewCount = br.ReadUInt32(); br.ReadOffsetLocation(ref header.Colors); br.ReadOffsetLocation(ref header.Textures); br.ReadOffsetLocation(ref header.Transparency); br.ReadOffsetLocation(ref header.UVAnimation); br.ReadOffsetLocation(ref header.TexReplace); br.ReadOffsetLocation(ref header.RenderFlags); br.ReadOffsetLocation(ref header.BoneLookupTable); br.ReadOffsetLocation(ref header.TexLookup); br.ReadOffsetLocation(ref header.TexUnits); br.ReadOffsetLocation(ref header.TransLookup); br.ReadOffsetLocation(ref header.UVAnimLookup); header.VertexBox = br.ReadBoundingBox(); header.VertexRadius = br.ReadSingle(); header.BoundingBox = br.ReadBoundingBox(); header.BoundingRadius = br.ReadSingle(); br.ReadOffsetLocation(ref header.BoundingTriangles); br.ReadOffsetLocation(ref header.BoundingVertices); br.ReadOffsetLocation(ref header.BoundingNormals); br.ReadOffsetLocation(ref header.Attachments); br.ReadOffsetLocation(ref header.AttachLookup); br.ReadOffsetLocation(ref header.Events); br.ReadOffsetLocation(ref header.Lights); br.ReadOffsetLocation(ref header.Cameras); br.ReadOffsetLocation(ref header.CameraLookup); br.ReadOffsetLocation(ref header.RibbonEmitters); br.ReadOffsetLocation(ref header.ParticleEmitters); if (header.HasUnknownFinalPart) { br.ReadOffsetLocation(ref header.OptionalUnk); } br.BaseStream.Position = model.Header.NameOffset; //model.Name = Encoding.UTF8.GetString(br.ReadBytes(model.Header.NameLength)); model.Name = br.ReadCString(); }
public static M2Model Process(MpqManager manager, string fileName) { if (!manager.FileExists(fileName)) { throw new Exception("File does not exist: " + fileName); } var model = new M2Model(); using (var fs = manager.OpenFile(fileName)) using (var br = new BinaryReader(fs)) { ReadHeader(br, model); ReadGlobalSequences(br, model); ReadAnimations(br, model); ReadAnimationLookup(br, model); ReadBones(br, model); ReadKeyBoneLookup(br, model); ReadVertices(br, model); ReadColors(br, model); ReadTextures(br, model); ReadTransparency(br, model); ReadUVAnimation(br, model); ReadTexReplace(br, model); ReadRenderFlags(br, model); ReadBoneLookupTable(br, model); ReadTexLookup(br, model); ReadTexUnits(br, model); ReadTransLookup(br, model); ReadUVAnimLookup(br, model); ReadBoundingTriangles(br, model); ReadBoundingVertices(br, model); ReadBoundingNormals(br, model); ReadAttachments(br, model); ReadAttachLookups(br, model); ReadEvents(br, model); ReadLights(br, model); ReadCameras(br, model); ReadCameraLookup(br, model); ReadRibbonEmitters(br, model); ReadParticleEmitters(br, model); if (model.Header.HasUnknownFinalPart) { ReadOptionalSection(br, model); } } return model; }
private static TileModel TransformM2Model(MapObjectDefinition wmo, DoodadDefinition definition, M2Model m2model) { var model = new TileModel { Vertices = new Vector3[m2model.BoundingVertices.Length] }; var origin = new Vector3(-definition.Position.X, definition.Position.Z, definition.Position.Y); var wmoOrigin = CorrectWMOOrigin(wmo.Position); var rotation = definition.Rotation; var rotateY = Matrix.CreateRotationY((wmo.OrientationB - 90) * RadiansPerDegree); for (var i = 0; i < m2model.BoundingVertices.Length; i++) { var vector = m2model.BoundingVertices[i]; var rotatedVector = Vector3.Transform(vector, rotation); rotatedVector = TransformToIntermediateCoords(rotatedVector); var finalModelVector = rotatedVector + origin; var rotatedWMOVector = Vector3.Transform(finalModelVector, rotateY); var finalWMOVector = rotatedWMOVector + wmoOrigin; model.Vertices[i] = finalWMOVector; } // Add the triangle indices to the model model.Triangles = new Index3[m2model.BoundingTriangles.Length]; for (var i = 0; i < m2model.BoundingTriangles.Length; i++) { var tri = m2model.BoundingTriangles[i]; model.Triangles[i] = new Index3 { Index0 = (short)tri[2], Index1 = (short)tri[1], Index2 = (short)tri[0] }; } // Calculate the boundingbox model.Bounds = new BoundingBox(model.Vertices); return model; }
static void ReadOptionalSection(BinaryReader br, M2Model model) { }
static void ReadRibbonEmitters(BinaryReader br, M2Model model) { }
static void ReadCameras(BinaryReader br, M2Model model) { }
static void ReadEvents(BinaryReader br, M2Model model) { }
static void ReadAttachments(BinaryReader br, M2Model model) { }
static void ReadBoundingTriangles(BinaryReader br, M2Model model) { var btInfo = model.Header.BoundingTriangles; model.BoundingTriangles = new ushort[btInfo.Count / 3][]; br.BaseStream.Position = btInfo.Offset; for (int i = 0; i < model.BoundingTriangles.Length; i++) { model.BoundingTriangles[i] = new ushort[3]; model.BoundingTriangles[i][0] = br.ReadUInt16(); model.BoundingTriangles[i][1] = br.ReadUInt16(); model.BoundingTriangles[i][2] = br.ReadUInt16(); } }
static void ReadColors(BinaryReader br, M2Model model) { }
static void ReadVertices(BinaryReader br, M2Model model) { var vertInfo = model.Header.Vertices; model.Vertices = new ModelVertices[vertInfo.Count]; br.BaseStream.Position = vertInfo.Offset; for (int i = 0; i < vertInfo.Count; i++) { var mv = new ModelVertices { Position = br.ReadVector3(), BoneWeight = br.ReadBytes(4), BoneIndices = br.ReadBytes(4), Normal = br.ReadVector3(), TextureCoordinates = br.ReadVector2(), Float_1 = br.ReadSingle(), Float_2 = br.ReadSingle() }; model.Vertices[i] = mv; } }
static void ReadKeyBoneLookup(BinaryReader br, M2Model model) { }
static void ReadBones(BinaryReader br, M2Model model) { }
static void ReadAnimationLookup(BinaryReader br, M2Model model) { }
static void ReadGlobalSequences(BinaryReader br, M2Model model) { var gsInfo = model.Header.GlobalSequences; model.GlobalSequenceTimestamps = new uint[gsInfo.Count]; br.BaseStream.Position = gsInfo.Offset; for (int i = 0; i < model.GlobalSequenceTimestamps.Length; i++) { model.GlobalSequenceTimestamps[i] = br.ReadUInt32(); } }
static void ReadTransLookup(BinaryReader br, M2Model model) { }
static void ReadUVAnimLookup(BinaryReader br, M2Model model) { }
static void ReadTextures(BinaryReader br, M2Model model) { }
static void ReadBoundingNormals(BinaryReader br, M2Model model) { var bnInfo = model.Header.BoundingVertices; model.BoundingNormals = new Vector3[bnInfo.Count]; br.BaseStream.Position = bnInfo.Offset; for (int i = 0; i < bnInfo.Count; i++) { model.BoundingNormals[i] = br.ReadVector3(); } }
static void ReadTransparency(BinaryReader br, M2Model model) { }
static void ReadAttachLookups(BinaryReader br, M2Model model) { }
static void ReadUVAnimation(BinaryReader br, M2Model model) { }
static void ReadLights(BinaryReader br, M2Model model) { }
static void ReadTexReplace(BinaryReader br, M2Model model) { }
static void ReadCameraLookup(BinaryReader br, M2Model model) { }
static void ReadRenderFlags(BinaryReader br, M2Model model) { }
static void ReadParticleEmitters(BinaryReader br, M2Model model) { }
static void ReadBoneLookupTable(BinaryReader br, M2Model model) { }
static void ReadTexUnits(BinaryReader br, M2Model model) { }
private static TileModel TransformM2Model(MapDoodadDefinition definition, M2Model m2model) { var model = new TileModel { Vertices = new Vector3[m2model.BoundingVertices.Length] }; // Rotate var origin = new Vector3( (TerrainConstants.CenterPoint - definition.Position.X), definition.Position.Y, (TerrainConstants.CenterPoint - definition.Position.Z) ); for (var i = 0; i < model.Vertices.Length; i++) { // Reverse the previos coord transform for the rotation. var vector = TransformToIntermediateCoords(m2model.BoundingVertices[i]); // Create the rotation matrices var rotateX = Matrix.CreateRotationX(definition.OrientationC * RadiansPerDegree); var rotateY = Matrix.CreateRotationY((definition.OrientationB - 90) * RadiansPerDegree); var rotateZ = Matrix.CreateRotationZ(-definition.OrientationA * RadiansPerDegree); // Rotate the vector var rotatedVector = Vector3.Transform(vector, rotateY); rotatedVector = Vector3.Transform(rotatedVector, rotateZ); rotatedVector = Vector3.Transform(rotatedVector, rotateX); var finalVector = rotatedVector + origin; model.Vertices[i] = finalVector; } // Add the triangle indices to the model model.Triangles = new Index3[m2model.BoundingTriangles.Length]; for (var i = 0; i < m2model.BoundingTriangles.Length; i++) { var tri = m2model.BoundingTriangles[i]; model.Triangles[i] = new Index3 { Index0 = (short)tri[2], Index1 = (short)tri[1], Index2 = (short)tri[0] }; } // Calculate the boundingbox model.Bounds = new BoundingBox(model.Vertices); return model; }