Esempio n. 1
0
        /// <summary>
        /// Parse a diffuse texture map in a .MTL file (a line of the kind "Map_Kd test.png").
        /// </summary>
        private void ParseMtlDiffuseTextureMapDeclaration()
        {
            // Check that a material declaration was started
            if (currentLoadMaterial == null)
            {
                throw new InvalidObjMtlFileException(string.Format(
                                                         "{0}: Texture map declaration before starting a material.", mtlParser.GetFilePositionStr()));
            }

            // Check that the line contains a material name
            mtlParser.AdvanceToNextNonWhiteSpace();
            if (mtlParser.IsEndOfLine)
            {
                throw new InvalidObjMtlFileException(string.Format(
                                                         "{0}: Texture map declaration without a corresponding texture name.", mtlParser.GetFilePositionStr()));
            }

            // Load the associated texture
            string textureFileName = mtlParser.ReadRestOfLine().Trim();

            // Handle unsupported flags
            if (textureFileName.StartsWith("-"))
            {
                string keyword = textureFileName.Substring(0, 2);
                switch (keyword)
                {
                case "-o":
                case "-s":
                case "-t":
                    warningLog.Add(string.Format(
                                       "{0}: Unrecognized map_kd keyword '{1}'.", mtlParser.GetFilePositionStr(), keyword));
                    // Assume format similar to "-s 5.000000 50.000000 1.000000 ..."
                    String[] split = textureFileName.Split(' ');
                    System.Text.StringBuilder newTextureFilename = new System.Text.StringBuilder();
                    for (int i = 4; i < split.Length; i++)
                    {
                        newTextureFilename.Append(split[i]);
                        if (i != split.Length - 1)
                        {
                            newTextureFilename.Append(' ');
                        }
                    }
                    textureFileName = newTextureFilename.ToString();
                    break;
                }
            }
            string textureFilePath = Path.Combine(Path.GetDirectoryName(mtlPath), textureFileName);

            // https://stackoverflow.com/a/8701748
            Bitmap tempImage;

            using (var bmpOnDisk = new Bitmap(textureFilePath))
            {
                tempImage = new Bitmap(bmpOnDisk);
            }
            currentLoadMaterial.DiffuseTextureMap = new Bitmap(tempImage);
        }
Esempio n. 2
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;
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Parse a diffuse texture map in a .MTL file (a line of the kind "Map_Kd test.png").
        /// </summary>
        private void ParseMtlDiffuseTextureMapDeclaration()
        {
            // Check that a material declaration was started
            if (currentLoadMaterial == null)
            {
                throw new InvalidObjMtlFileException(string.Format(
                                                         "{0}: Texture map declaration before starting a material.", mtlParser.GetFilePositionStr()));
            }

            // Check that the line contains a material name
            mtlParser.AdvanceToNextNonWhiteSpace();
            if (mtlParser.IsEndOfLine)
            {
                throw new InvalidObjMtlFileException(string.Format(
                                                         "{0}: Texture map declaration without a corresponding texture name.", mtlParser.GetFilePositionStr()));
            }

            // Load the associated texture
            string textureFileName = mtlParser.ReadRestOfLine().Trim();
            string textureFilePath = Path.Combine(Path.GetDirectoryName(mtlPath), textureFileName);

            currentLoadMaterial.DiffuseTextureMap = new Bitmap(textureFilePath);
        }
Esempio n. 4
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);
        }
Esempio n. 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));
                    }
                }
            }
        }