/// <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()) { // Parse according to the keyword string keyword = mtlParser.GetNextWord(); switch (keyword) { case "newmtl": ParseMtlMaterialDeclaration(); break; case "map_Kd": ParseMtlDiffuseTextureMapDeclaration(); break; case null: // Empty line break; default: warningLog.Add(string.Format( "{0}: Unrecognized keyword '{1}'.", mtlParser.GetFilePositionStr(), keyword)); break; } } } }
/// <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 <Vector4>(); this.vertexNormals = new List <Vector3>(); this.vertexTexCoords = new List <Vector3>(); 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()) { // 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; case null: // Empty line break; default: warningLog.Add(string.Format( "{0}: Unrecognized keyword '{1}'.", objParser.GetFilePositionStr(), keyword)); break; } } } return(warningLog); }
/// <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); }
/// <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); }