public bool saveObjModel(string fileName) { System.IO.StreamWriter file = new System.IO.StreamWriter(fileName); file.WriteLine("# obj file exported by objFileExplorer"); for (int i = 0; i < xyzs.Count; i++) { file.WriteLine("v " + xyzs[i].ToString().Replace(',', '.')); } for (int i = 0; i < texCoords.Count; i++) { file.WriteLine("vt " + texCoords[i].ToString().Replace(',', '.')); } for (int i = 0; i < normals.Count; i++) { file.WriteLine("vn " + normals[i].ToString().Replace(',', '.')); } for (int i = 0; i < objects.Count; i++) { ObjObject obj = objects[i]; file.WriteLine("o " + obj.getName()); for (int j = 0; j < obj.getNumFaces(); j++) { ObjFace face = faces[obj.getFirstFace() + j]; file.Write("f"); for (int k = 0; k < face.getNumVerts(); k++) { int faceVertIndex = k + face.getFirstVert(); ObjFaceVertex faceVertex = faceVerts[faceVertIndex]; string faceVertString = " " + faceVertex.ToString(); file.Write(faceVertString); } file.WriteLine(); } } for (int i = 0; i < groups.Count; i++) { ObjGroup g = groups[i]; file.WriteLine("g " + g.getName()); for (int j = 0; j < g.getNumFaces(); j++) { ObjFace face = faces[g.getFirstFace() + j]; file.Write("f"); for (int k = 0; k < face.getNumVerts(); k++) { int faceVertIndex = k + face.getFirstVert(); ObjFaceVertex faceVertex = faceVerts[faceVertIndex]; string faceVertString = " " + faceVertex.ToString(); file.Write(faceVertString); } file.WriteLine(); } } if (groups.Count == 0 && objects.Count == 0) { for (int j = 0; j < faces.Count; j++) { ObjFace face = faces[j]; file.Write("f"); for (int k = 0; k < face.getNumVerts(); k++) { int faceVertIndex = k + face.getFirstVert(); ObjFaceVertex faceVertex = faceVerts[faceVertIndex]; string faceVertString = " " + faceVertex.ToString(); file.Write(faceVertString); } file.WriteLine(); } } file.Close(); return(false); }
public int getObjectIndex(ObjObject o) { return(objects.IndexOf(o)); }
public bool loadObjModel(string fileName) { StreamReader r = new StreamReader(fileName); string fileData = r.ReadToEnd(); int lineNumber = 0; ObjObject lastObject = null; ObjGroup lastGroup = null; foreach (string line in fileData.Split('\n')) { lineNumber++; // skip comments int commentStart = line.IndexOf('#'); string sl; if (commentStart >= 0) { sl = line.Substring(0, commentStart); } else { sl = line; } // remove extra whitespaces sl.Trim(); // tokenize string[] tokens = sl.Split(lineSplitChars, StringSplitOptions.RemoveEmptyEntries); if (tokens == null || tokens.Length == 0) { continue; } switch (tokens[0]) { case "mtllib": { if (tokens.Length < 2) { showOBJParseError("Missing materialName after 'mtllib' token at line " + lineNumber); return(true); } else { string mtlLibName = tokens[1]; } } break; case "o": { if (tokens.Length < 2) { showOBJParseError("Missing objectName after 'o' token at line " + lineNumber); return(true); } else { if (lastObject != null) { lastObject.setNumFaces(faces.Count - lastObject.getFirstFace()); } string objectName = tokens[1]; ObjObject no = new ObjObject(); no.setName(objectName); no.setFirstFace(faces.Count); objects.Add(no); lastObject = no; } } break; case "g": { if (tokens.Length < 2) { showOBJParseError("Missing groupName after 'g' token at line " + lineNumber); return(true); } else { if (lastGroup != null) { lastGroup.setNumFaces(faces.Count - lastGroup.getFirstFace()); } string groupName = tokens[1]; ObjGroup no = new ObjGroup(); no.setName(groupName); no.setFirstFace(faces.Count); groups.Add(no); lastObject = no; } } break; case "v": { if (tokens.Length < 4) { showOBJParseError("Missing groupName after 'g' token at line " + lineNumber); return(true); } else { float x, y, z; if (float.TryParse(tokens[1].Replace('.', ','), out x) == false) { showOBJParseError("'X' component of 'v' is not a valid float at line " + lineNumber); return(true); } if (float.TryParse(tokens[2].Replace('.', ','), out y) == false) { showOBJParseError("'Y' component of 'v' is not a valid float at line " + lineNumber); return(true); } if (float.TryParse(tokens[3].Replace('.', ','), out z) == false) { showOBJParseError("'Z' component of 'v' is not a valid float at line " + lineNumber); return(true); } xyzs.Add(new Vec3(x, y, z)); } } break; case "vt": { if (tokens.Length < 3) { showOBJParseError("Missing texcoord values after 'vt' token at line " + lineNumber); return(true); } else { float x, y; if (float.TryParse(tokens[1].Replace('.', ','), out x) == false) { showOBJParseError("'X' component of 'vt' is not a valid float at line " + lineNumber); return(true); } if (float.TryParse(tokens[2].Replace('.', ','), out y) == false) { showOBJParseError("'Y' component of 'v' is not a valid float at line " + lineNumber); return(true); } texCoords.Add(new Vec2(x, y)); } } break; case "vn": { if (tokens.Length < 4) { showOBJParseError("Missing normal values after 'vn' token at line " + lineNumber); return(true); } else { float x, y, z; if (float.TryParse(tokens[1].Replace('.', ','), out x) == false) { showOBJParseError("'X' component of 'vn' is not a valid float at line " + lineNumber); return(true); } if (float.TryParse(tokens[2].Replace('.', ','), out y) == false) { showOBJParseError("'Y' component of 'vn' is not a valid float at line " + lineNumber); return(true); } if (float.TryParse(tokens[3].Replace('.', ','), out z) == false) { showOBJParseError("'Z' component of 'vn' is not a valid float at line " + lineNumber); return(true); } normals.Add(new Vec3(x, y, z)); } } break; case "f": { if (tokens.Length < 4) { showOBJParseError("Missing face vertex indices after 'f' token at line " + lineNumber); return(true); } else { int numFaceVerts = tokens.Length - 1; ObjFace face = new ObjFace(); face.setFirstVertex(faceVerts.Count); face.setNumVerts(numFaceVerts); for (int i = 0; i < numFaceVerts; i++) { string faceVertexString = tokens[1 + i]; ObjFaceVertex faceVert = new ObjFaceVertex(); if (faceVert.setFromString(faceVertexString)) { showOBJParseError("Vertex string '" + faceVertexString + "' is not a valid at line " + lineNumber); return(true); } faceVerts.Add(faceVert); } faces.Add(face); } } break; default: { } break; } } if (lastObject != null) { lastObject.setNumFaces(faces.Count - lastObject.getFirstFace()); } if (lastGroup != null) { lastGroup.setNumFaces(faces.Count - lastGroup.getFirstFace()); } /* if (objects.Count == 0) * { * ObjObject obj = new ObjObject(); * obj.setName("default_object"); * obj.setFirstFace(0); * obj.setNumFaces(faces.Count); * objects.Add(obj); * } * if (groups.Count == 0) * { * ObjGroup obj = new ObjGroup(); * obj.setName("default_group"); * obj.setFirstFace(0); * obj.setNumFaces(faces.Count); * groups.Add(obj); * }*/ return(false); }