private static void PrepareM2Info(MapDoodadDefinition def, M2Model model) { TerrainConstants.TilePositionToWorldPosition(ref def.Position); Matrix scaleMatrix; Matrix.CreateScale(def.Scale, out scaleMatrix); var rotateZ = Matrix.CreateRotationZ(MathHelper.ToRadians(def.OrientationB + 180)); var rotateY = Matrix.CreateRotationY(MathHelper.ToRadians(def.OrientationA)); var rotateX = Matrix.CreateRotationX(MathHelper.ToRadians(def.OrientationC)); var modelToWorld = Matrix.Multiply(scaleMatrix, rotateZ); modelToWorld = Matrix.Multiply(modelToWorld, rotateX); modelToWorld = Matrix.Multiply(modelToWorld, rotateY); def.ModelToWorld = modelToWorld; Matrix worldToModel; Matrix.Invert(ref modelToWorld, out worldToModel); def.WorldToModel = worldToModel; CalculateModelBounds(def, model); }
private static TileModel ExtractModel(MapDoodadDefinition definition) { var filePath = definition.FilePath; var m2model = ExtractM2Model(filePath); return(TransformM2Model(definition, m2model)); }
public void Add(MapDoodadDefinition doodadDefinition) { if (doodadDefinition is ExtractedMapM2Definition) { Add((ExtractedMapM2Definition)doodadDefinition); } }
static void ReadMDDF(BinaryReader fileReader, ADT adt) { var type = fileReader.ReadUInt32(); var size = fileReader.ReadUInt32(); var endPos = fileReader.BaseStream.Position + size; while (fileReader.BaseStream.Position < endPos) { var doodadDefinition = new MapDoodadDefinition(); var nameIndex = fileReader.ReadInt32(); doodadDefinition.FilePath = adt.ModelFiles[nameIndex]; // 4 bytes doodadDefinition.UniqueId = fileReader.ReadUInt32(); // 4 bytes var Y = fileReader.ReadSingle(); var Z = fileReader.ReadSingle(); var X = fileReader.ReadSingle(); doodadDefinition.Position = new Vector3(X, Y, Z); // 12 bytes doodadDefinition.OrientationA = fileReader.ReadSingle(); // 4 Bytes doodadDefinition.OrientationB = fileReader.ReadSingle(); // 4 Bytes doodadDefinition.OrientationC = fileReader.ReadSingle(); // 4 Bytes doodadDefinition.Scale = fileReader.ReadUInt16() / 1024f; // 2 bytes doodadDefinition.Flags = fileReader.ReadUInt16(); // 2 bytes adt.DoodadDefinitions.Add(doodadDefinition); } }
public void Add(MapDoodadDefinition doodadDefinition) { if (doodadDefinition is ExtractedMapM2Definition) { Add((ExtractedMapM2Definition)doodadDefinition); } }
public M2 GetOrReadM2(MapDoodadDefinition definition) { M2 m2; if (!M2s.TryGetValue(definition.FilePath, out m2)) { M2s.Add(definition.FilePath, m2 = M2Reader.ReadM2(WCellTerrainSettings.GetDefaultMPQFinder(), definition)); } return(m2); }
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); }
private static M2 Transform(M2Model model, IEnumerable <int> indicies, MapDoodadDefinition mddf) { var currentM2 = new M2(); currentM2.Vertices.Clear(); currentM2.Indices.Clear(); var posX = (mddf.Position.X - TerrainConstants.CenterPoint) * -1; var posY = (mddf.Position.Y - TerrainConstants.CenterPoint) * -1; var origin = new Vector3(posX, posY, mddf.Position.Z); // Create the scale matrix used in the following loop. Matrix scaleMatrix; Matrix.CreateScale(mddf.Scale, out scaleMatrix); // Creation the rotations var rotateZ = Matrix.CreateRotationZ(MathHelper.ToRadians(mddf.OrientationB + 180)); var rotateY = Matrix.CreateRotationY(MathHelper.ToRadians(mddf.OrientationA)); var rotateX = Matrix.CreateRotationX(MathHelper.ToRadians(mddf.OrientationC)); var worldMatrix = Matrix.Multiply(scaleMatrix, rotateZ); worldMatrix = Matrix.Multiply(worldMatrix, rotateX); worldMatrix = Matrix.Multiply(worldMatrix, rotateY); for (var i = 0; i < model.BoundingVertices.Length; i++) { var position = model.BoundingVertices[i]; var normal = model.BoundingNormals[i]; // Scale and Rotate Vector3 rotatedPosition; Vector3.Transform(ref position, ref worldMatrix, out rotatedPosition); Vector3 rotatedNormal; Vector3.Transform(ref normal, ref worldMatrix, out rotatedNormal); rotatedNormal.Normalize(); // Translate Vector3 finalVector; Vector3.Add(ref rotatedPosition, ref origin, out finalVector); currentM2.Vertices.Add(finalVector); } currentM2.Indices.AddRange(indicies); return(currentM2); }
/// <summary> /// Add a M2 to this manager. /// </summary> /// <param name="doodadDefinition">MDDF (placement information) for this M2</param> public void Add(MapDoodadDefinition doodadDefinition) { var filePath = doodadDefinition.FilePath; if (Path.GetExtension(filePath).Equals(".mdx") || Path.GetExtension(filePath).Equals(".mdl")) { filePath = Path.ChangeExtension(filePath, ".m2"); } _names.Add(filePath); var currentM2 = Process(doodadDefinition); // Ignore models with no bounding volumes if (currentM2.Vertices.Count < 1) return; M2s.Add(currentM2); }
private static void CalculateModelBounds(MapDoodadDefinition def, M2Model model) { var vecs = new List <Vector3>(model.BoundingVertices); for (int i = 0; i < model.BoundingVertices.Length; i++) { var vec = model.BoundingVertices[i]; Vector3 rotatedVec; Vector3.Transform(ref vec, ref def.ModelToWorld, out rotatedVec); Vector3 finalVec; Vector3.Add(ref rotatedVec, ref def.Position, out finalVec); vecs.Add(finalVec); } def.Extents = new BoundingBox(vecs.ToArray()); }
public static M2 ReadM2(MpqLibrarian librarian, MapDoodadDefinition doodadDefinition) { var model = M2Reader.ReadM2(librarian, doodadDefinition.FilePath, false); var tempIndices = new List <int>(); foreach (var tri in model.BoundingTriangles) { tempIndices.Add(tri.Index0); tempIndices.Add(tri.Index1); tempIndices.Add(tri.Index2); } var currentM2 = TransformM2(model, tempIndices, doodadDefinition); currentM2.Bounds = new BoundingBox(currentM2.Vertices.ToArray()); return(currentM2); }
private static M2 Process(MapDoodadDefinition doodadDefinition) { var model = M2ModelParser.Process(MpqTerrainManager.MpqManager, doodadDefinition.FilePath); var tempIndices = new List <int>(); foreach (var tri in model.BoundingTriangles) { tempIndices.Add(tri.Index2); tempIndices.Add(tri.Index1); tempIndices.Add(tri.Index0); } var currentM2 = Transform(model, tempIndices, doodadDefinition); var tempVertices = currentM2.Vertices; var bounds = new BoundingBox(tempVertices.ToArray()); currentM2.Bounds = bounds; return(currentM2); }
/// <summary> /// Add a M2 to this manager. /// </summary> /// <param name="doodadDefinition">MDDF (placement information) for this M2</param> public void Add(MapDoodadDefinition doodadDefinition) { var filePath = doodadDefinition.FilePath; if (Path.GetExtension(filePath).Equals(".mdx") || Path.GetExtension(filePath).Equals(".mdl")) { filePath = Path.ChangeExtension(filePath, ".m2"); } _names.Add(filePath); var currentM2 = Process(doodadDefinition); // Ignore models with no bounding volumes if (currentM2.Vertices.Count < 1) { return; } M2s.Add(currentM2); }
private static void CalculateModelBounds(MapDoodadDefinition def, M2Model model) { var vecs = new List<Vector3>(model.BoundingVertices); for (var i = 0; i < model.BoundingVertices.Length; i++) { var vec = model.BoundingVertices[i]; Vector3 rotatedVec; Vector3.Transform(ref vec, ref def.ModelToWorld, out rotatedVec); Vector3 finalVec; Vector3.Add(ref rotatedVec, ref def.Position, out finalVec); vecs.Add(finalVec); } def.Extents = new BoundingBox(vecs.ToArray()); }
private static void PrepareM2Info(MapDoodadDefinition def, M2Model model) { TerrainConstants.TilePositionToWorldPosition(ref def.Position); Matrix scaleMatrix; Matrix.CreateScale(def.Scale, out scaleMatrix); var rotateZ = Matrix.CreateRotationZ(MathHelper.ToRadians(def.OrientationB + 180)); var rotateY = Matrix.CreateRotationY(MathHelper.ToRadians(def.OrientationA)); var rotateX = Matrix.CreateRotationX(MathHelper.ToRadians(def.OrientationC)); var modelToWorld = Matrix.Multiply(scaleMatrix, rotateZ); modelToWorld = Matrix.Multiply(modelToWorld, rotateX); modelToWorld = Matrix.Multiply(modelToWorld, rotateY); def.ModelToWorld = modelToWorld; Matrix worldToModel; Matrix.Invert(ref modelToWorld, out worldToModel); def.WorldToModel = worldToModel; CalculateModelBounds(def, model); }
static void ReadMDDF(BinaryReader fileReader, ADTFile adt) { var type = fileReader.ReadUInt32(); var size = fileReader.ReadUInt32(); var endPos = fileReader.BaseStream.Position + size; while (fileReader.BaseStream.Position < endPos) { var doodadDefinition = new MapDoodadDefinition(); var nameIndex = fileReader.ReadInt32(); doodadDefinition.FilePath = adt.ModelFiles[nameIndex]; // 4 bytes doodadDefinition.UniqueId = fileReader.ReadUInt32(); // 4 bytes doodadDefinition.Position = fileReader.ReadVector3(); // 12 bytes doodadDefinition.OrientationA = fileReader.ReadSingle(); // 4 Bytes doodadDefinition.OrientationB = fileReader.ReadSingle(); // 4 Bytes doodadDefinition.OrientationC = fileReader.ReadSingle(); // 4 Bytes doodadDefinition.Scale = fileReader.ReadUInt32() / 1024f; // 4 bytes adt.DoodadDefinitions.Add(doodadDefinition); } }
private static M2 Process(MapDoodadDefinition doodadDefinition) { var model = M2ModelParser.Process(MpqTerrainManager.MpqManager, doodadDefinition.FilePath); var tempIndices = new List<int>(); foreach (var tri in model.BoundingTriangles) { tempIndices.Add(tri.Index2); tempIndices.Add(tri.Index1); tempIndices.Add(tri.Index0); } var currentM2 = Transform(model, tempIndices, doodadDefinition); var tempVertices = currentM2.Vertices; var bounds = new BoundingBox(tempVertices.ToArray()); currentM2.Bounds = bounds; return currentM2; }
private static M2 Transform(M2Model model, IEnumerable<int> indicies, MapDoodadDefinition mddf) { var currentM2 = new M2(); currentM2.Vertices.Clear(); currentM2.Indices.Clear(); var posX = (mddf.Position.X - TerrainConstants.CenterPoint)*-1; var posY = (mddf.Position.Y - TerrainConstants.CenterPoint)*-1; var origin = new Vector3(posX, posY, mddf.Position.Z); // Create the scale matrix used in the following loop. Matrix scaleMatrix; Matrix.CreateScale(mddf.Scale, out scaleMatrix); // Creation the rotations var rotateZ = Matrix.CreateRotationZ(MathHelper.ToRadians(mddf.OrientationB + 180)); var rotateY = Matrix.CreateRotationY(MathHelper.ToRadians(mddf.OrientationA)); var rotateX = Matrix.CreateRotationX(MathHelper.ToRadians(mddf.OrientationC)); var worldMatrix = Matrix.Multiply(scaleMatrix, rotateZ); worldMatrix = Matrix.Multiply(worldMatrix, rotateX); worldMatrix = Matrix.Multiply(worldMatrix, rotateY); for (var i = 0; i < model.BoundingVertices.Length; i++) { var position = model.BoundingVertices[i]; var normal = model.BoundingNormals[i]; // Scale and Rotate Vector3 rotatedPosition; Vector3.Transform(ref position, ref worldMatrix, out rotatedPosition); Vector3 rotatedNormal; Vector3.Transform(ref normal, ref worldMatrix, out rotatedNormal); rotatedNormal.Normalize(); // Translate Vector3 finalVector; Vector3.Add(ref rotatedPosition, ref origin, out finalVector); currentM2.Vertices.Add(finalVector); } currentM2.Indices.AddRange(indicies); return currentM2; }