Exemple #1
0
        /// <summary>
        /// Load a .MTL file from the specified path.
        /// </summary>
        /// <param name="mtlPath">The path of the .MTL file.</param>
        private void LoadMtl(string mtlPath)
        {
            this.mtlPath   = mtlPath;
            this.mtlParser = null;

            this.materials = new Dictionary <string, ObjMtlMaterial>();

            this.currentLoadMaterial = null;

            using (mtlParser = new ObjMtlParser(mtlPath))
            {
                while (mtlParser.ReadNextLine())
                {
                    // Check for empty and comment lines
                    if (!mtlParser.AdvanceToNextNonWhiteSpace())
                    {
                        continue;
                    }
                    if (mtlParser.PeekCharacter() == '#')
                    {
                        continue;
                    }

                    // Parse according to the keyword
                    string keyword = mtlParser.GetNextWord();

                    switch (keyword)
                    {
                    case "newmtl":
                        ParseMtlMaterialDeclaration();
                        break;

                    case "map_Kd":
                        ParseMtlDiffuseTextureMapDeclaration();
                        break;

                    default:
                        warningLog.Add(string.Format(
                                           "{0}: Unrecognized keyword '{1}'.", mtlParser.GetFilePositionStr(), keyword));
                        break;
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Parse a material declaration in a .MTL file (a line of the kind "newmtl testmtl").
        /// </summary>
        private void ParseMtlMaterialDeclaration()
        {
            // Check that the line contains a material name
            if (mtlParser.IsEndOfLine)
            {
                throw new InvalidObjMtlFileException(string.Format(
                                                         "{0}: Material declaration without a corresponding material name.", mtlParser.GetFilePositionStr()));
            }

            // Create the new material instance
            string materialName = mtlParser.ReadRestOfLine().Trim();

            currentLoadMaterial = new ObjMtlMaterial();

            if (materials.ContainsKey(materialName))
            {
                throw new InvalidObjMtlFileException(string.Format(
                                                         "{0}: Duplicate material name '{1}'.", mtlParser.GetFilePositionStr(), materialName));
            }
            materials.Add(materialName, currentLoadMaterial);
        }
Exemple #3
0
        /// <summary>
        /// Load the model from the .OBJ file
        /// </summary>
        /// <param name="objPath">The path of a file containing the .OBJ file.</param>
        /// <returns>A list of warnings while loading the file.</returns>
        public List <string> LoadObj(string objPath)
        {
            if (objPath == null)
            {
                throw new ArgumentNullException("objPath");
            }

            // Set up the parser status to load a new .OBJ file
            this.warningLog = new List <string>();

            this.objPath   = objPath;
            this.objParser = null;

            this.vertexPositions = new List <Vector3>();
            this.vertexNormals   = new List <Vector3>();
            this.vertexTexCoords = new List <Vector2>();

            this.currentObject       = null;
            this.currentMesh         = null;
            this.currentUsedMaterial = null;

            this.mtlPath   = null;
            this.mtlParser = null;

            this.materials = new Dictionary <string, ObjMtlMaterial>();

            this.currentLoadMaterial = null;

            using (objParser = new ObjMtlParser(objPath))
            {
                while (objParser.ReadNextLine())
                {
                    // Check for empty and comment lines
                    if (!objParser.AdvanceToNextNonWhiteSpace())
                    {
                        continue;
                    }
                    if (objParser.PeekCharacter() == '#')
                    {
                        continue;
                    }

                    // Parse according to the keyword
                    string keyword = objParser.GetNextWord();

                    switch (keyword)
                    {
                    case "o":
                        ParseObjObjectDeclaration();
                        break;

                    case "mtllib":
                        ParseObjMtlLibDeclaration();
                        break;

                    case "usemtl":
                        ParseObjUseMtlDeclaration();
                        break;

                    case "v":
                        ParseObjVertexPositionDeclaration();
                        break;

                    case "vn":
                        ParseObjVertexNormalDeclaration();
                        break;

                    case "vt":
                        ParseObjVertexTexCoordDeclaration();
                        break;

                    case "f":
                        ParseObjFaceDeclaration();
                        break;

                    default:
                        warningLog.Add(string.Format(
                                           "{0}: Unrecognized keyword '{1}'.", objParser.GetFilePositionStr(), keyword));
                        break;
                    }
                }
            }

            return(warningLog);
        }
Exemple #4
0
        /// <summary>
        /// Parses a object name declaration in a .OBJ file (a line of the kind "o [object name]").
        /// </summary>
        private void ParseObjObjectDeclaration()
        {
            // Check that the line contains an object name
            if (objParser.IsEndOfLine)
            {
                throw new InvalidObjMtlFileException(string.Format(
                                                         "{0}: Object declaration without a corresponding object name.", objParser.GetFilePositionStr()));
            }

            // Create the new object instance
            string newObjectName = objParser.ReadRestOfLine().Trim();

            currentObject = new ObjMtlObject();
            currentMesh   = null;

            // Add the object to the object list
            if (model.Objects.ContainsKey(newObjectName))
            {
                throw new InvalidObjMtlFileException(string.Format(
                                                         "{0}: Duplicate object name '{1}'.", objParser.GetFilePositionStr(), newObjectName));
            }
            model.Objects.Add(newObjectName, currentObject);
        }
Exemple #5
0
        /// <summary>
        /// Load a .MTL file from the specified path.
        /// </summary>
        /// <param name="mtlPath">The path of the .MTL file.</param>
        private void LoadMtl(string mtlPath)
        {
            this.mtlPath   = mtlPath;
            this.mtlParser = null;

            this.materials = new Dictionary <string, ObjMtlMaterial>();

            this.currentLoadMaterial = null;

            using (mtlParser = new ObjMtlParser(mtlPath))
            {
                while (mtlParser.ReadNextLine())
                {
                    // Check for empty and comment lines
                    if (!mtlParser.AdvanceToNextNonWhiteSpace())
                    {
                        continue;
                    }
                    if (mtlParser.PeekCharacter() == '#')
                    {
                        continue;
                    }

                    // Parse according to the keyword
                    string keyword = mtlParser.GetNextWord();

                    switch (keyword)
                    {
                    case "newmtl":
                        ParseMtlMaterialDeclaration();
                        break;

                    case "map_Kd":
                        ParseMtlDiffuseTextureMapDeclaration();
                        break;

                    case "d":
                        ParseMtlTransparencyDeclaration();
                        break;

                    case "Ka":
                    case "Kd":
                    case "Ks":
                    case "Ke":
                    case "Ni":
                    case "Ns":
                    case "illum":
                        break;

                    default:
                        warningLog.Add(string.Format(
                                           "{0}: Unrecognized keyword '{1}'.", mtlParser.GetFilePositionStr(), keyword));
                        break;
                    }
                }
                // Check for untextured materials
                foreach (KeyValuePair <string, ObjMtlMaterial> objectMaterial in materials)
                {
                    if (objectMaterial.Value.DiffuseTextureMap == null)
                    {
                        warningLog.Add(string.Format(
                                           "Material name \"{0}\": No texture associated with the material.", objectMaterial.Key));
                    }
                }
            }
        }