private static void WriteMeshMaterialList(XMesh mesh, XofFileSaveData saveData)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write(mesh.Materials.Count);
                    writer.Write(mesh.MaterialsFacesIndices.Count);

                    for (int i = 0; i < mesh.MaterialsFacesIndices.Count; i++)
                    {
                        writer.Write(mesh.MaterialsFacesIndices[i]);
                    }

                    data = ms.ToArray();
                }

            using (var child = saveData.AddData(XofFileDefaultTemplates.MeshMaterialListId, null, null, data))
            {
                foreach (var material in mesh.Materials)
                {
                    WriteMaterial(material, child);
                }
            }
        }
        private static void WriteVertexDuplicationIndices(XMesh mesh, XofFileSaveData saveData)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write(mesh.VertexDuplicationIndices?.Length ?? 0);
                    writer.Write(mesh.OriginalVerticesCount);

                    if (mesh.VertexDuplicationIndices != null)
                    {
                        for (int i = 0; i < mesh.VertexDuplicationIndices.Length; i++)
                        {
                            writer.Write(mesh.VertexDuplicationIndices[i]);
                        }
                    }

                    data = ms.ToArray();
                }

            using (saveData.AddData(XofFileDefaultTemplates.VertexDuplicationIndicesId, null, null, data))
            {
            }
        }
        private static byte[] BuildMeshData(XMesh mesh)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write(mesh.Vertices.Count);

                    foreach (var vector in mesh.Vertices)
                    {
                        WriteVector(vector, writer);
                    }

                    writer.Write(mesh.FacesIndices.Count);

                    foreach (var face in mesh.FacesIndices)
                    {
                        writer.Write(face.Count);

                        for (int i = 0; i < face.Count; i++)
                        {
                            writer.Write(face[i]);
                        }
                    }

                    data = ms.ToArray();
                }

            return(data);
        }
Beispiel #4
0
        private static void ReadMeshVertexColors(XMesh mesh, XofFileEnumData enumData)
        {
            byte[] data = enumData.GetData();

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    int nVertexColors = reader.ReadInt32();
                    mesh.VertexColors.Capacity = nVertexColors;

                    for (int index = 0; index < nVertexColors; index++)
                    {
                        mesh.VertexColors.Add(ReadIndexedColor(reader));
                    }

                    if (ms.Position != ms.Length)
                    {
                        throw new InvalidDataException();
                    }
                }

            if (enumData.GetChildrenCount() != 0)
            {
                throw new InvalidDataException();
            }
        }
        private static void WriteDeclData(XMesh mesh, XofFileSaveData saveData)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write(mesh.VertexElements.Count);

                    foreach (var element in mesh.VertexElements)
                    {
                        WriteVertexElement(element, writer);
                    }

                    writer.Write(mesh.VertexElementsData?.Length ?? 0);

                    if (mesh.VertexElementsData != null)
                    {
                        for (int i = 0; i < mesh.VertexElementsData.Length; i++)
                        {
                            writer.Write(mesh.VertexElementsData[i]);
                        }
                    }

                    data = ms.ToArray();
                }

            using (saveData.AddData(XofFileDefaultTemplates.DeclDataId, null, null, data))
            {
            }
        }
Beispiel #6
0
        private static void ReadFVFData(XMesh mesh, XofFileEnumData enumData)
        {
            byte[] data = enumData.GetData();

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    mesh.FVF = reader.ReadUInt32();

                    int nDWords = reader.ReadInt32();
                    mesh.FVFData.Capacity = nDWords;

                    for (int i = 0; i < nDWords; i++)
                    {
                        mesh.FVFData.Add(reader.ReadUInt32());
                    }

                    if (ms.Position != ms.Length)
                    {
                        throw new InvalidDataException();
                    }
                }

            if (enumData.GetChildrenCount() != 0)
            {
                throw new InvalidDataException();
            }
        }
Beispiel #7
0
        private static void ReadVertexDuplicationIndices(XMesh mesh, XofFileEnumData enumData)
        {
            byte[] data = enumData.GetData();

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    int nIndices = reader.ReadInt32();

                    mesh.OriginalVerticesCount = reader.ReadInt32();

                    var indices = new int[nIndices];

                    for (int i = 0; i < nIndices; i++)
                    {
                        indices[i] = reader.ReadInt32();
                    }

                    mesh.VertexDuplicationIndices = indices;

                    if (ms.Position != ms.Length)
                    {
                        throw new InvalidDataException();
                    }
                }

            if (enumData.GetChildrenCount() != 0)
            {
                throw new InvalidDataException();
            }
        }
        private static void WriteMeshNormals(XMesh mesh, XofFileSaveData saveData)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write(mesh.Normals.Count);

                    foreach (var normal in mesh.Normals)
                    {
                        WriteVector(normal, writer);
                    }

                    writer.Write(mesh.FacesNormalsIndices.Count);

                    foreach (var indices in mesh.FacesNormalsIndices)
                    {
                        writer.Write(indices.Count);

                        for (int i = 0; i < indices.Count; i++)
                        {
                            writer.Write(indices[i]);
                        }
                    }

                    data = ms.ToArray();
                }

            using (saveData.AddData(XofFileDefaultTemplates.MeshNormalsId, null, null, data))
            {
            }
        }
        private static void WriteMesh(XMesh mesh, XofFileSaveData saveData)
        {
            byte[] data = BuildMeshData(mesh);

            using (var child = saveData.AddData(XofFileDefaultTemplates.MeshId, mesh.Name, null, data))
            {
                WriteMeshChildren(mesh, child);
            }
        }
Beispiel #10
0
        private static void ReadMeshMaterialList(XMesh mesh, XofFileEnumData enumData)
        {
            byte[] data = enumData.GetData();

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    int nMaterials = reader.ReadInt32();
                    mesh.Materials.Capacity = nMaterials;

                    int nFaceIndexes = reader.ReadInt32();
                    mesh.MaterialsFacesIndices.Capacity = nFaceIndexes;

                    for (int index = 0; index < nFaceIndexes; index++)
                    {
                        mesh.MaterialsFacesIndices.Add(reader.ReadInt32());
                    }

                    if (ms.Position != ms.Length)
                    {
                        throw new InvalidDataException();
                    }
                }

            int materialsCount = mesh.Materials.Capacity;

            int childrenCount = enumData.GetChildrenCount();

            for (int childIndex = 0; childIndex < childrenCount; childIndex++)
            {
                using (var child = enumData.GetChild(childIndex))
                {
                    Guid type = child.GetTemplateType();

                    if (type == XofFileDefaultTemplates.MaterialId)
                    {
                        mesh.Materials.Add(ReadMaterial(child));
                    }
                    else
                    {
                        throw new InvalidDataException();
                    }
                }
            }

            if (mesh.Materials.Count != materialsCount)
            {
                throw new InvalidDataException();
            }
        }
        private static void WriteMeshChildren(XMesh mesh, XofFileSaveData child)
        {
            WriteMeshNormals(mesh, child);
            WriteMeshTextureCoords(mesh, child);
            WriteMeshMaterialList(mesh, child);
            WriteVertexDuplicationIndices(mesh, child);
            WriteFVFData(mesh, child);
            WriteMeshVertexColors(mesh, child);
            WriteDeclData(mesh, child);
            WriteXSkinMeshHeader(mesh, child);

            foreach (var skin in mesh.SkinWeights)
            {
                WriteSkinWeights(skin, child);
            }
        }
        private static void WriteXSkinMeshHeader(XMesh mesh, XofFileSaveData saveData)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write(mesh.MaxSkinWeightsPerVertex);
                    writer.Write(mesh.MaxSkinWeightsPerFace);
                    writer.Write(mesh.BonesCount);

                    data = ms.ToArray();
                }

            using (saveData.AddData(XofFileDefaultTemplates.XSkinMeshHeaderId, null, null, data))
            {
            }
        }
Beispiel #13
0
        private static void ReadMeshNormals(XMesh mesh, XofFileEnumData enumData)
        {
            byte[] data = enumData.GetData();

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    int nNormals = reader.ReadInt32();
                    mesh.Normals.Capacity = nNormals;

                    for (int index = 0; index < nNormals; index++)
                    {
                        mesh.Normals.Add(ReadVector(reader));
                    }

                    int nFacesNormals = reader.ReadInt32();
                    mesh.FacesNormalsIndices.Capacity = nFacesNormals;

                    for (int index = 0; index < nFacesNormals; index++)
                    {
                        int indicesCount = reader.ReadInt32();
                        var vertices     = new List <int>(indicesCount);

                        for (int i = 0; i < indicesCount; i++)
                        {
                            vertices.Add(reader.ReadInt32());
                        }

                        mesh.FacesNormalsIndices.Add(vertices);
                    }

                    if (ms.Position != ms.Length)
                    {
                        throw new InvalidDataException();
                    }
                }

            if (enumData.GetChildrenCount() != 0)
            {
                throw new InvalidDataException();
            }
        }
        private static void WriteMeshVertexColors(XMesh mesh, XofFileSaveData saveData)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write(mesh.VertexColors.Count);

                    foreach (var indexedColor in mesh.VertexColors)
                    {
                        WriteIndexedColor(indexedColor, writer);
                    }

                    data = ms.ToArray();
                }

            using (saveData.AddData(XofFileDefaultTemplates.MeshVertexColorsId, null, null, data))
            {
            }
        }
        private static void WriteMeshTextureCoords(XMesh mesh, XofFileSaveData saveData)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write(mesh.TextureCoords.Count);

                    foreach (var coords in mesh.TextureCoords)
                    {
                        WriteCoords2d(coords, writer);
                    }

                    data = ms.ToArray();
                }

            using (saveData.AddData(XofFileDefaultTemplates.MeshTextureCoordsId, null, null, data))
            {
            }
        }
Beispiel #16
0
        private static void ReadXSkinMeshHeader(XMesh mesh, XofFileEnumData enumData)
        {
            byte[] data = enumData.GetData();

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    mesh.MaxSkinWeightsPerVertex = reader.ReadInt16();
                    mesh.MaxSkinWeightsPerFace   = reader.ReadInt16();
                    mesh.BonesCount = reader.ReadInt16();

                    if (ms.Position != ms.Length)
                    {
                        throw new InvalidDataException();
                    }
                }

            if (enumData.GetChildrenCount() != 0)
            {
                throw new InvalidDataException();
            }
        }
        private static void WriteFVFData(XMesh mesh, XofFileSaveData saveData)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write(mesh.FVF);

                    writer.Write(mesh.FVFData.Count);

                    for (int i = 0; i < mesh.FVFData.Count; i++)
                    {
                        writer.Write(mesh.FVFData[i]);
                    }

                    data = ms.ToArray();
                }

            using (saveData.AddData(XofFileDefaultTemplates.FVFDataId, null, null, data))
            {
            }
        }
Beispiel #18
0
        private static void ReadDeclData(XMesh mesh, XofFileEnumData enumData)
        {
            byte[] data = enumData.GetData();

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    int nElements = reader.ReadInt32();
                    mesh.VertexElements.Capacity = nElements;

                    for (int index = 0; index < nElements; index++)
                    {
                        mesh.VertexElements.Add(ReadVertexElement(reader));
                    }

                    int nDWords      = reader.ReadInt32();
                    var elementsData = new uint[nDWords];

                    for (int i = 0; i < nDWords; i++)
                    {
                        elementsData[i] = reader.ReadUInt32();
                    }

                    mesh.VertexElementsData = elementsData;

                    if (ms.Position != ms.Length)
                    {
                        throw new InvalidDataException();
                    }
                }

            if (enumData.GetChildrenCount() != 0)
            {
                throw new InvalidDataException();
            }
        }
Beispiel #19
0
        private static XMesh ReadMesh(XofFileEnumData enumData)
        {
            var mesh = new XMesh
            {
                Name = enumData.GetName()
            };

            byte[] data = enumData.GetData();

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    int nVertices = reader.ReadInt32();
                    mesh.Vertices.Capacity = nVertices;

                    for (int index = 0; index < nVertices; index++)
                    {
                        mesh.Vertices.Add(ReadVector(reader));
                    }

                    int nFaces = reader.ReadInt32();
                    mesh.FacesIndices.Capacity = nFaces;

                    for (int index = 0; index < nFaces; index++)
                    {
                        int indicesCount = reader.ReadInt32();
                        var vertices     = new List <int>(indicesCount);

                        for (int i = 0; i < indicesCount; i++)
                        {
                            vertices.Add(reader.ReadInt32());
                        }

                        mesh.FacesIndices.Add(vertices);
                    }

                    if (ms.Position != ms.Length)
                    {
                        throw new InvalidDataException();
                    }
                }

            bool meshNormalsRead              = false;
            bool meshTextureCoordsRead        = false;
            bool meshMaterialListRead         = false;
            bool vertexDuplicationIndicesRead = false;
            bool fvfDataRead            = false;
            bool meshVertexColorsIdRead = false;
            bool declDataRead           = false;
            bool xSkinMeshHeaderRead    = false;

            int childrenCount = enumData.GetChildrenCount();

            for (int childIndex = 0; childIndex < childrenCount; childIndex++)
            {
                using (var child = enumData.GetChild(childIndex))
                {
                    Guid type = child.GetTemplateType();

                    if (type == XofFileDefaultTemplates.MeshNormalsId)
                    {
                        if (meshNormalsRead)
                        {
                            mesh.Normals.Clear();
                            mesh.FacesNormalsIndices.Clear();
                            //throw new InvalidDataException();
                        }

                        ReadMeshNormals(mesh, child);
                        meshNormalsRead = true;
                    }
                    else if (type == XofFileDefaultTemplates.MeshTextureCoordsId)
                    {
                        if (meshTextureCoordsRead)
                        {
                            throw new InvalidDataException();
                        }

                        ReadMeshTextureCoords(mesh, child);
                        meshTextureCoordsRead = true;
                    }
                    else if (type == XofFileDefaultTemplates.MeshMaterialListId)
                    {
                        if (meshMaterialListRead)
                        {
                            throw new InvalidDataException();
                        }

                        ReadMeshMaterialList(mesh, child);
                        meshMaterialListRead = true;
                    }
                    else if (type == XofFileDefaultTemplates.VertexDuplicationIndicesId)
                    {
                        if (vertexDuplicationIndicesRead)
                        {
                            throw new InvalidDataException();
                        }

                        ReadVertexDuplicationIndices(mesh, child);
                        vertexDuplicationIndicesRead = true;
                    }
                    else if (type == XofFileDefaultTemplates.FVFDataId)
                    {
                        if (fvfDataRead)
                        {
                            throw new InvalidDataException();
                        }

                        ReadFVFData(mesh, child);
                        fvfDataRead = true;
                    }
                    else if (type == XofFileDefaultTemplates.MeshVertexColorsId)
                    {
                        if (meshVertexColorsIdRead)
                        {
                            throw new InvalidDataException();
                        }

                        ReadMeshVertexColors(mesh, child);
                        meshVertexColorsIdRead = true;
                    }
                    else if (type == XofFileDefaultTemplates.DeclDataId)
                    {
                        if (declDataRead)
                        {
                            throw new InvalidDataException();
                        }

                        ReadDeclData(mesh, child);
                        declDataRead = true;
                    }
                    else if (type == XofFileDefaultTemplates.XSkinMeshHeaderId)
                    {
                        if (xSkinMeshHeaderRead)
                        {
                            throw new InvalidDataException();
                        }

                        ReadXSkinMeshHeader(mesh, child);
                        xSkinMeshHeaderRead = true;
                    }
                    else if (type == XofFileDefaultTemplates.SkinWeightsId)
                    {
                        mesh.SkinWeights.Add(ReadSkinWeights(child));
                    }
                    else
                    {
                        throw new InvalidDataException();
                    }
                }
            }

            return(mesh);
        }