Beispiel #1
0
        /// <summary>
        /// Imports the texture node where the xml reader is currently located.
        /// </summary>
        /// <param name="inStreamXml">The xml reader object.</param>
        /// <param name="container">The container where to import to.</param>
        /// <param name="xglImportOptions">Current import options.</param>
        private void ImportMesh(XmlReader inStreamXml, ImportedModelContainer container, XglImportOptions xglImportOptions)
        {
            string          id = inStreamXml.GetAttribute("ID");
            VertexStructure actVertexStructure = new VertexStructure();
            int             minVertexID        = int.MaxValue;
            int             actVertexIndex     = -1;
            int             actNormalIndex     = -1;
            int             actTextureIndex    = -1;
            Vertex          actTempVertex      = Vertex.Empty;

            int[] actFaceReferences = new int[3];
            Dictionary <int, MaterialProperties> localMaterialInfos = new Dictionary <int, MaterialProperties>();

            while (inStreamXml.Read())
            {
                // Ending condition
                if ((inStreamXml.NodeType == XmlNodeType.EndElement) &&
                    (inStreamXml.Name == NODE_NAME_MESH))
                {
                    break;
                }

                // Continue condition
                if (inStreamXml.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                switch (inStreamXml.Name)
                {
                case NODE_NAME_SURFACE:
                    // If this tag is present, faces will be visible from both sides. If this flag is absent, faces will be visible from only one side as described in the <F> tag
                    break;

                case NODE_NAME_MAT:
                    // Read the next material
                    var materialAndID = ImportMaterial(inStreamXml, container, xglImportOptions);
                    localMaterialInfos[materialAndID.Item1] = materialAndID.Item2;
                    break;

                case NODE_NAME_PATCH:
                    // A patch is a group of faces
                    // We don't need to handle this one
                    break;

                case NODE_NAME_POINT:
                    // Read next point
                    if (minVertexID == int.MaxValue)
                    {
                        minVertexID = Int32.Parse(inStreamXml.GetAttribute("ID"));
                    }
                    actVertexIndex++;
                    actTempVertex       = actVertexStructure.EnsureVertexAt(actVertexIndex);
                    actTempVertex.Color = Color4.White;
                    inStreamXml.Read();
                    actTempVertex.Position = inStreamXml.ReadContentAsVector3() * xglImportOptions.ResizeFactor;
                    actVertexStructure.Vertices[actVertexIndex] = actTempVertex;
                    break;

                case NODE_NAME_NORMAL:
                    // Read next normal
                    if (minVertexID == int.MaxValue)
                    {
                        minVertexID = Int32.Parse(inStreamXml.GetAttribute("ID"));
                    }
                    actNormalIndex++;
                    actTempVertex = actVertexStructure.EnsureVertexAt(actNormalIndex);
                    inStreamXml.Read();
                    actTempVertex.Normal = inStreamXml.ReadContentAsVector3();
                    actVertexStructure.Vertices[actNormalIndex] = actTempVertex;
                    break;

                case NODE_NAME_TC:
                    // Read next texture coordinate
                    if (minVertexID == int.MaxValue)
                    {
                        minVertexID = Int32.Parse(inStreamXml.GetAttribute("ID"));
                    }
                    actTextureIndex++;
                    actTempVertex = actVertexStructure.EnsureVertexAt(actTextureIndex);
                    inStreamXml.Read();
                    actTempVertex.TexCoord = inStreamXml.ReadContentAsVector2();
                    actVertexStructure.Vertices[actTextureIndex] = actTempVertex;
                    break;

                case NODE_NAME_FACE:
                    // Read next face
                    actFaceReferences[0] = 0;
                    actFaceReferences[1] = 0;
                    actFaceReferences[2] = 0;
                    int loopFacePoint     = 0;
                    int referencedMat     = -1;
                    int referencedTexture = -1;
                    while (inStreamXml.Read())
                    {
                        // Ending condition
                        if ((inStreamXml.NodeType == XmlNodeType.EndElement) &&
                            (inStreamXml.Name == NODE_NAME_FACE))
                        {
                            break;
                        }
                        if (inStreamXml.NodeType != XmlNodeType.Element)
                        {
                            continue;
                        }

                        // Read next face index
                        if (inStreamXml.Name == NODE_NAME_FACE_MATERIAL_REF)
                        {
                            inStreamXml.Read();
                            referencedMat = inStreamXml.ReadContentAsInt();
                        }
                        else if (inStreamXml.Name == NODE_NAME_FACE_TEXTUREREF_REF)
                        {
                            inStreamXml.Read();
                            referencedTexture = inStreamXml.ReadContentAsInt();
                        }
                        else if (inStreamXml.Name == NODE_NAME_FACE_POINT_REF)
                        {
                            if (loopFacePoint >= 3)
                            {
                                throw new SeeingSharpGraphicsException("Invalid face index count!");
                            }
                            inStreamXml.Read();
                            actFaceReferences[loopFacePoint] = inStreamXml.ReadContentAsInt() - minVertexID;
                            loopFacePoint++;
                        }
                        else
                        {
                        }
                    }

                    // Get the correct material
                    MaterialProperties referencedMatObject = MaterialProperties.Empty;
                    if (referencedMat > -1)
                    {
                        localMaterialInfos.TryGetValue(referencedMat, out referencedMatObject);
                    }
                    if (referencedTexture > -1)
                    {
                        referencedMatObject            = referencedMatObject.Clone();
                        referencedMatObject.TextureKey = container.GetResourceKey(RES_CLASS_TEXTURE, referencedTexture.ToString());
                    }

                    //for (int actFaceLoc = 0; actFaceLoc < 3; actFaceLoc++)
                    //{
                    //    int actVertexIndexInner = actFaceReferences[actFaceLoc];
                    //    actTempVertex = actVertexStructure.Vertices[actVertexIndexInner];
                    //    actTempVertex.Color = referencedMatObject.DiffuseColor;
                    //    actVertexStructure.Vertices[actVertexIndexInner] = actTempVertex;
                    //}

                    // Add the triangle
                    if (loopFacePoint != 3)
                    {
                        throw new SeeingSharpGraphicsException("Invalid face index count!");
                    }
                    actVertexStructure
                    .CreateOrGetExistingSurface(referencedMatObject)
                    .AddTriangle(actFaceReferences[0], actFaceReferences[1], actFaceReferences[2]);
                    break;

                default:
                    //throw new SeeingSharpGraphicsException(string.Format(
                    //    "Unknown element {0} in xgl file!",
                    //    inStreamXml.Name));
                    break;
                }
            }

            // Add the geometry resource
            container.ImportedResources.Add(new ImportedResourceInfo(
                                                container.GetResourceKey(RES_CLASS_MESH, id),
                                                () => new GeometryResource(actVertexStructure)));
        }