Пример #1
0
        public static Model Load(string fileName)
        {
            if (new FileInfo(fileName).Extension == ".xml" &&
                new FileInfo(fileName.Substring(0, fileName.Length - 4) + ".gz.xml").Exists)
                return null;

            var model = new Model();

            var settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            settings.CloseInput = true;

            var document = new XmlDocument();
            Stream inStream;

            if (fileName.EndsWith(".gz.xml"))
                inStream = new GZipStream(System.IO.File.Open(fileName, FileMode.Open, FileAccess.Read), CompressionMode.Decompress);
            else
                inStream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.Read);

            document.Load(inStream);

            model.Name = document.DocumentElement.Attributes["Name"].InnerText;

            if (document.DocumentElement.Attributes["DefaultWidth"] != null)
            {
                model.DefaultWidth = float.Parse(document.DocumentElement.Attributes["DefaultWidth"].InnerText,
                                                 CultureInfo.InvariantCulture);
            }
            if (document.DocumentElement.Attributes["DefaultHeight"] != null)
            {
                model.DefaultHeight = float.Parse(document.DocumentElement.Attributes["DefaultHeight"].InnerText,
                                                  CultureInfo.InvariantCulture);
            }

            foreach (XmlElement n in document.DocumentElement.ChildNodes)
            {
                var mesh = new Mesh(n.Attributes["Name"].InnerText);

                if (n.Attributes["Mode"] != null)
                    mesh.Mode = (BeginMode)Enum.Parse(typeof(BeginMode), n.Attributes["Mode"].InnerText);
                if (n.Attributes["Translate"] != null)
                    mesh.Translate = Mesh.StringToVertex3(n.Attributes["Translate"].InnerText);
                if (n.Attributes["Pivot"] != null)
                    mesh.Pivot = Mesh.StringToVertex3(n.Attributes["Pivot"].InnerText);
                if (n.Attributes["IsHelmet"] != null)
                    mesh.HasTransparency = bool.Parse(n.Attributes["IsHelmet"].InnerText);
                if (n.Attributes["FollowCursor"] != null)
                    mesh.FollowCursor = bool.Parse(n.Attributes["FollowCursor"].InnerText);
                if (n.Attributes["RotateFactor"] != null)
                    mesh.RotateFactor = float.Parse(n.Attributes["RotateFactor"].InnerText);
                if (n.Attributes["Rotate"] != null)
                    mesh.Rotate = Mesh.StringToVertex3(n.Attributes["Rotate"].InnerText);
                if (n.Attributes["Part"] != null)
                    mesh.Part = (ModelPart)Enum.Parse(typeof(ModelPart), n.Attributes["Part"].InnerText);

                mesh.Faces = new List<Face>();

                foreach (XmlElement faceNode in n.ChildNodes)
                {
                    if (faceNode.Name != "Face")
                        continue;

                    var face = new Face();

                    face.Vertices = new Vector3[faceNode["Vertices"].ChildNodes.Count];
                    face.TexCoords = new Vector2[face.Vertices.Length];
                    face.Indices = new int[face.Vertices.Length];

                    int i = 0;
                    foreach (XmlElement vertexNode in faceNode["Vertices"])
                        face.Vertices[i++] = Mesh.StringToVertex3(vertexNode.InnerText);

                    i = 0;
                    foreach (XmlElement vertexNode in faceNode["TexCoords"])
                        face.TexCoords[i++] = Mesh.StringToVertex2(vertexNode.InnerText);

                    i = 0;
                    foreach (XmlElement vertexNode in faceNode["Indices"])
                        face.Indices[i++] = int.Parse(vertexNode.InnerText);

                    Vector3 zero = TranslateVertex(mesh.Translate, mesh.Rotate, mesh.Pivot, face.Vertices[0]);
                    Vector3 one = TranslateVertex(mesh.Translate, mesh.Rotate, mesh.Pivot, face.Vertices[1]);
                    Vector3 two = TranslateVertex(mesh.Translate, mesh.Rotate, mesh.Pivot, face.Vertices[2]);

                    Vector3 dir = Vector3.Cross(one - zero, two - zero);
                    face.Normal = Vector3.Normalize(dir);

                    dir = Vector3.Cross(face.Vertices[1] - face.Vertices[0], face.Vertices[2] - face.Vertices[0]);
                    Vector3 realNormal = Vector3.Normalize(dir);

                    if (realNormal == new Vector3(0, 1, 0))
                        face.Downface = true;

                    mesh.Faces.Add(face);
                }

                mesh.CalculateCenter();
                mesh.CalculateMatrix();

                model.Meshes.Add(mesh);
            }

            inStream.Dispose();

            return model;
        }
Пример #2
0
            public Model Compile(string name, float scale, float defaultWidth, float defaultHeight)
            {
                var model = new Model();
                model.Name = name;
                model.DefaultWidth = defaultWidth;
                model.DefaultHeight = defaultHeight;

                foreach (object boxObj in boxList)
                {
                    if (boxObj is ModelRenderer)
                    {
                        var box = (ModelRenderer) boxObj;
                        foreach (ModelBox face in box.cubeList)
                        {
                            var mesh = new Mesh(face.Name);
                            mesh.Faces = new List<Face>();
                            mesh.Translate = new Vector3(box.rotationPointX, box.rotationPointY, box.rotationPointZ);
                            mesh.Part = box.Flags;
                            mesh.Rotate = new Vector3(MathHelper.RadiansToDegrees(box.rotateAngleX),
                                                      MathHelper.RadiansToDegrees(box.rotateAngleY),
                                                      MathHelper.RadiansToDegrees(box.rotateAngleZ));
                            mesh.Pivot = mesh.Translate;
                            mesh.IsSolid = box.isSolid;

                            mesh.Mode = BeginMode.Quads;

                            var cwIndices = new[] {0, 1, 2, 3};
                            var cwwIndices = new[] {3, 2, 1, 0};

                            foreach (TexturedQuad quad in face.field_40680_i)
                            {
                                var vertices = new List<Vector3>();
                                var texcoords = new List<Vector2>();

                                foreach (PositionTextureVertex x in quad.vertexPositions)
                                {
                                    vertices.Add(x.vector3D * scale);
                                    texcoords.Add(new Vector2(x.texturePositionX, x.texturePositionY));
                                }

                                var newFace = new Face(vertices.ToArray(), texcoords.ToArray(), cwwIndices);

                                Vector3 zero = Model.TranslateVertex(mesh.Translate, mesh.Rotate, mesh.Pivot, newFace.Vertices[0]);
                                Vector3 one = Model.TranslateVertex(mesh.Translate, mesh.Rotate, mesh.Pivot, newFace.Vertices[1]);
                                Vector3 two = Model.TranslateVertex(mesh.Translate, mesh.Rotate, mesh.Pivot, newFace.Vertices[2]);

                                Vector3 dir = Vector3.Cross(one - zero, two - zero);
                                newFace.Normal = Vector3.Normalize(dir);

                                dir = Vector3.Cross(newFace.Vertices[1] - newFace.Vertices[0], newFace.Vertices[2] - newFace.Vertices[0]);
                                Vector3 realNormal = Vector3.Normalize(dir);

                                if (realNormal == new Vector3(0, 1, 0))
                                    newFace.Downface = true;

                                mesh.Faces.Add(newFace);
                            }

                            mesh.CalculateCenter();
                            mesh.CalculateMatrix();

                            model.Meshes.Add(mesh);
                        }
                    }
                    else if (boxObj is PlaneRenderer)
                    {
                        var box = (PlaneRenderer) boxObj;

                        var mesh = new Mesh(box.Name);
                        mesh.Faces = new List<Face>();
                        mesh.Translate = new Vector3(box.rotationPointX, box.rotationPointY, box.rotationPointZ);
                        mesh.Part = box.Flags;
                        mesh.Rotate = new Vector3(MathHelper.RadiansToDegrees(box.rotateAngleX),
                                                  MathHelper.RadiansToDegrees(box.rotateAngleY),
                                                  MathHelper.RadiansToDegrees(box.rotateAngleZ));
                        mesh.Pivot = mesh.Translate;

                        mesh.Mode = BeginMode.Quads;

                        var cwIndices = new[] {0, 1, 2, 3};
                        var cwwIndices = new[] {3, 2, 1, 0};

                        foreach (TexturedQuad quad in box.faces)
                        {
                            var vertices = new List<Vector3>();
                            var texcoords = new List<Vector2>();

                            foreach (PositionTextureVertex x in quad.vertexPositions)
                            {
                                vertices.Add(x.vector3D * scale);
                                texcoords.Add(new Vector2(x.texturePositionX, x.texturePositionY));
                            }

                            var newFace = new Face(vertices.ToArray(), texcoords.ToArray(), cwIndices);

                            Vector3 zero = Model.TranslateVertex(mesh.Translate, mesh.Rotate, mesh.Pivot, newFace.Vertices[0]);
                            Vector3 one = Model.TranslateVertex(mesh.Translate, mesh.Rotate, mesh.Pivot, newFace.Vertices[1]);
                            Vector3 two = Model.TranslateVertex(mesh.Translate, mesh.Rotate, mesh.Pivot, newFace.Vertices[2]);

                            Vector3 dir = Vector3.Cross(one - zero, two - zero);
                            newFace.Normal = Vector3.Normalize(dir);

                            dir = Vector3.Cross(newFace.Vertices[1] - newFace.Vertices[0], newFace.Vertices[2] - newFace.Vertices[0]);
                            Vector3 realNormal = Vector3.Normalize(dir);

                            if (realNormal == new Vector3(0, 1, 0))
                                newFace.Downface = true;

                            mesh.Faces.Add(newFace);
                        }

                        mesh.CalculateCenter();
                        mesh.CalculateMatrix();

                        model.Meshes.Add(mesh);
                    }
                }

                model.PartsEnabled = new bool[model.Meshes.Count];

                for (int i = 0; i < model.Meshes.Count; ++i)
                    model.PartsEnabled[i] = true;

                return model;
            }