Example #1
0
        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);
        }