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); }
private void loadModel(string fileName) { treeView1.Nodes.Clear(); m = new WavefrontOBJ(); m.loadObjModel(fileName); TreeNode nodeObjects = new TreeNode("Objects (" + m.getNumObjects() + ")"); for (int i = 0; i < m.getNumObjects(); i++) { ObjObject o = m.getObject(i); TreeNode nodeObject = new TreeNode("Object " + i + " - \"" + o.getName() + "\""); // object name TreeNode nodeObjectName = new TreeNode("Name"); TreeNode nodeObjectNameValue = new TreeNode(o.getName()); nodeObjectNameValue.Tag = o; nodeObjectName.Nodes.Add(nodeObjectNameValue); nodeObject.Nodes.Add(nodeObjectName); // first face TreeNode nodeObjectFirstFace = new TreeNode("First face"); TreeNode nodeObjectFirstFaceValue = new TreeNode(o.getFirstFace().ToString()); nodeObjectFirstFace.Nodes.Add(nodeObjectFirstFaceValue); nodeObject.Nodes.Add(nodeObjectFirstFace); // face count TreeNode nodeObjectNumFaces = new TreeNode("Faces count"); TreeNode nodeObjectNumFacesValue = new TreeNode(o.getNumFaces().ToString()); nodeObjectNumFaces.Nodes.Add(nodeObjectNumFacesValue); nodeObject.Nodes.Add(nodeObjectNumFaces); nodeObjects.Nodes.Add(nodeObject); } treeView1.Nodes.Add(nodeObjects); TreeNode nodeGroups = new TreeNode("Groups (" + m.getNumGroups() + ")"); for (int i = 0; i < m.getNumGroups(); i++) { ObjGroup o = m.getGroup(i); TreeNode nodeGroup = new TreeNode("Group " + i + " - \"" + o.getName() + "\""); // object name TreeNode nodeGroupName = new TreeNode("Name"); TreeNode nodeGroupNameValue = new TreeNode(o.getName()); nodeGroupNameValue.Tag = o; nodeGroupName.Nodes.Add(nodeGroupNameValue); nodeGroup.Nodes.Add(nodeGroupName); // first face TreeNode nodeGroupFirstFace = new TreeNode("First face"); TreeNode nodeGroupFirstFaceValue = new TreeNode(o.getFirstFace().ToString()); nodeGroupFirstFace.Nodes.Add(nodeGroupFirstFaceValue); nodeGroup.Nodes.Add(nodeGroupFirstFace); // face count TreeNode nodeGroupNumFaces = new TreeNode("Faces count"); TreeNode nodeGroupNumFacesValue = new TreeNode(o.getNumFaces().ToString()); nodeGroupNumFaces.Nodes.Add(nodeGroupNumFacesValue); nodeGroup.Nodes.Add(nodeGroupNumFaces); nodeGroups.Nodes.Add(nodeGroup); } treeView1.Nodes.Add(nodeGroups); TreeNode nodeFaces = new TreeNode("Faces (" + m.getNumFaces() + ")"); for (int i = 0; i < m.getNumFaces(); i++) { ObjFace f = m.getFace(i); TreeNode nodeFace = new TreeNode("Face " + i); // number of vertices /*TreeNode nodeFaceNumFaceVerts = new TreeNode("FaceVerts count"); * TreeNode nodeFaceNumFaceVertsValue = new TreeNode(f.getNumVerts().ToString()); * nodeFaceNumFaceVerts.Nodes.Add(nodeFaceNumFaceVertsValue); * nodeFace.Nodes.Add(nodeFaceNumFaceVerts);*/ // vertices TreeNode nodeFaceFaceVerts = new TreeNode("FaceVerts (" + f.getNumVerts() + ")"); for (int j = 0; j < f.getNumVerts(); j++) { TreeNode nodeFaceFaceVert = new TreeNode("FaceVert " + j + " (abs " + (j + f.getFirstVert()) + ")"); nodeFaceFaceVerts.Nodes.Add(nodeFaceFaceVert); } nodeFace.Nodes.Add(nodeFaceFaceVerts); nodeFaces.Nodes.Add(nodeFace); } treeView1.Nodes.Add(nodeFaces); TreeNode nodePositions = new TreeNode("XYZs (" + m.getNumXYZs() + ")"); for (int i = 0; i < m.getNumXYZs(); i++) { TreeNode nodePosition = new TreeNode("XYZ " + i); TreeNode nodePositionValue = new TreeNode(m.getXYZ(i).ToString()); nodePosition.Nodes.Add(nodePositionValue); nodePositions.Nodes.Add(nodePosition); } treeView1.Nodes.Add(nodePositions); if (m.getNumTexCoords() > 0) { TreeNode nodeTexCoords = new TreeNode("TexCoords (" + m.getNumTexCoords() + ")"); for (int i = 0; i < m.getNumTexCoords(); i++) { TreeNode nodeTexCoord = new TreeNode("TexCoord " + i); TreeNode nodeTexCoordValue = new TreeNode(m.getTexCoord(i).ToString()); nodeTexCoord.Nodes.Add(nodeTexCoordValue); nodeTexCoords.Nodes.Add(nodeTexCoord); } treeView1.Nodes.Add(nodeTexCoords); } ; if (m.getNumNormals() > 0) { TreeNode nodeNormals = new TreeNode("Normals (" + m.getNumNormals() + ")"); for (int i = 0; i < m.getNumNormals(); i++) { TreeNode nodeNormal = new TreeNode("Normal " + i); TreeNode nodeNormalValue = new TreeNode(m.getNormal(i).ToString()); nodeNormal.Nodes.Add(nodeNormalValue); nodeNormals.Nodes.Add(nodeNormal); } treeView1.Nodes.Add(nodeNormals); } }
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); }