public static WFObject Load(string filename) { int linesSkipped = 0, unknownTokens = 0, faces = 0; StreamReader reader = new StreamReader(filename); WFObject result = new WFObject(); string currentGroup = DEFAULT_GROUP_ID; string[] sep1 = { " " }; string[] sep2 = { "/" }; while (!reader.EndOfStream) { string line = reader.ReadLine().Trim(); string[] tokens = line.Split(sep1, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 0) { linesSkipped++; continue; } string command = tokens[0].ToLower(); if (command.Equals(T_VERTEX)) // new vertex { result.vertices.Add(new WFVertex(float.Parse(tokens[1]), float.Parse(tokens[2]), float.Parse(tokens[3]))); } else if (command.Equals(T_NORMAL)) // new normal { result.normals.Add(new WFNormal(float.Parse(tokens[1]), float.Parse(tokens[2]), float.Parse(tokens[3]))); } else if (command.Equals(T_TEXCOORD)) // new texcoord { result.texCoords.Add(new WFTexCoord(float.Parse(tokens[1]), float.Parse(tokens[2]))); } else if (command.Equals(T_FACE)) // new face { WFFace face = new WFFace(); // create the new face for (int i = 1; i < tokens.Length; i++) // get the vertices { string faceDescr = tokens[i]; string[] components = faceDescr.Split(sep2, StringSplitOptions.None); int v = int.Parse(components[0]) - 1; int vt = -1; int vn = -1; if (components.Length == 2) // vi/vt { vt = int.Parse(components[1]) - 1; } else if (components.Length == 3) // vi/(vt)/vn { vt = components[1].Equals(string.Empty) ? -1 : int.Parse(components[1]) - 1; vn = int.Parse(components[2]) - 1; } face.AddVertex(v, vn, vt); // add the vertex to the face } faces++; // add the face to the current group if (result.faceGroups.ContainsKey(currentGroup)) { result.faceGroups[currentGroup].AddFace(face); } else { WFFaceGroup grp = new WFFaceGroup(currentGroup); grp.AddFace(face); result.faceGroups.Add(currentGroup, grp); } } else if (command.Equals(T_USEMATERIAL)) { currentGroup = tokens[1]; } else if (command.Equals(T_COMMENT)) { result.WriteLog(line, LOG_COMMENT); } else { unknownTokens++; } Thread.Yield(); } result.WriteLogSeparator(); result.WriteLog("Loading done!", LOG_INFO); result.WriteLogSeparator(); result.WriteLog("Vertices: " + result.vertices.Count, LOG_INFO); result.WriteLog("Normals: " + result.normals.Count, LOG_INFO); result.WriteLog("TexCoords: " + result.texCoords.Count, LOG_INFO); result.WriteLog("Faces: " + faces, LOG_INFO); result.WriteLog("Groups: " + result.faceGroups.Count, LOG_INFO); result.WriteLogSeparator(); result.WriteLog("Empty lines: " + linesSkipped, LOG_INFO); result.WriteLog("Unknown tokens: " + unknownTokens, LOG_WARNING); return(result); }
public void AddFace(WFFace face) { faces.Add(face); }
public void AddFace(WFFace face) { faces.Add(face); }
public static WFObject Load(string filename) { int linesSkipped = 0, unknownTokens = 0, faces = 0; StreamReader reader = new StreamReader(filename); WFObject result = new WFObject(); string currentGroup = DEFAULT_GROUP_ID; string[] sep1 = { " " }; string[] sep2 = { "/" }; while (!reader.EndOfStream) { string line = reader.ReadLine().Trim(); string[] tokens = line.Split(sep1, StringSplitOptions.RemoveEmptyEntries); if (tokens.Length == 0) { linesSkipped++; continue; } string command = tokens[0].ToLower(); if (command.Equals(T_VERTEX)) // new vertex { result.vertices.Add(new WFVertex(float.Parse(tokens[1]), float.Parse(tokens[2]), float.Parse(tokens[3]))); } else if (command.Equals(T_NORMAL)) // new normal { result.normals.Add(new WFNormal(float.Parse(tokens[1]), float.Parse(tokens[2]), float.Parse(tokens[3]))); } else if (command.Equals(T_TEXCOORD)) // new texcoord { result.texCoords.Add(new WFTexCoord(float.Parse(tokens[1]), float.Parse(tokens[2]))); } else if(command.Equals(T_FACE)) { // new face WFFace face = new WFFace(); // create the new face for (int i = 1; i < tokens.Length; i++) // get the vertices { string faceDescr = tokens[i]; string[] components = faceDescr.Split(sep2, StringSplitOptions.None); int v = int.Parse(components[0]) - 1; int vt = -1; int vn = -1; if (components.Length == 2) // vi/vt { vt = int.Parse(components[1]) - 1; } else if (components.Length == 3) // vi/(vt)/vn { vt = components[1].Equals(string.Empty) ? -1 : int.Parse(components[1]) - 1; vn = int.Parse(components[2]) - 1; } face.AddVertex(v,vn, vt); // add the vertex to the face } faces++; // add the face to the current group if (result.faceGroups.ContainsKey(currentGroup)) { result.faceGroups[currentGroup].AddFace(face); } else { WFFaceGroup grp = new WFFaceGroup(currentGroup); grp.AddFace(face); result.faceGroups.Add(currentGroup, grp); } } else if (command.Equals(T_USEMATERIAL)) { currentGroup = tokens[1]; } else if (command.Equals(T_COMMENT)) { result.WriteLog(line, LOG_COMMENT); } else { unknownTokens++; } Thread.Yield(); } result.WriteLogSeparator(); result.WriteLog("Loading done!", LOG_INFO); result.WriteLogSeparator(); result.WriteLog("Vertices: " + result.vertices.Count, LOG_INFO); result.WriteLog("Normals: " + result.normals.Count, LOG_INFO); result.WriteLog("TexCoords: " + result.texCoords.Count, LOG_INFO); result.WriteLog("Faces: " + faces, LOG_INFO); result.WriteLog("Groups: " + result.faceGroups.Count, LOG_INFO); result.WriteLogSeparator(); result.WriteLog("Empty lines: " + linesSkipped, LOG_INFO); result.WriteLog("Unknown tokens: " + unknownTokens, LOG_WARNING); return result; }
public override void Write(WFObject inputObject, FileStream outStream) { BinaryWriter writer = new BinaryWriter(outStream); string[] faceGroupIDs = inputObject.FaceGroups; int shadingGroupsCount = inputObject.FaceGroups.Length; /* Remove the empty groups from the count */ for (int i = 0; i < inputObject.FaceGroups.Length; i++) { if (inputObject[i].FaceCount == 0) { shadingGroupsCount--; } } // write header; writer.Write(CURRENT_VERSION); writer.Write(shadingGroupsCount); // write face groups foreach (string gID in faceGroupIDs) { WFFaceGroup currentGroup = inputObject[gID]; List <float> vertices = new List <float>(); //store all the vertices in this group for (int fi = 0; fi < currentGroup.FaceCount; fi++) // foreach face { WFFace currentFace = currentGroup[fi]; for (int vi = 0; vi < currentFace.VerticesCount; vi++) // foreach vertex { int[] indexes = currentFace[vi]; WFVertex vertex = inputObject.Vertices[indexes[0]]; WFNormal normal = inputObject.Normals[indexes[1]]; WFTexCoord texCoord = inputObject.TexCoords[indexes[2]]; vertices.Add(vertex.X); vertices.Add(vertex.Y); vertices.Add(vertex.Z); vertices.Add(normal.Nx); vertices.Add(normal.Ny); vertices.Add(normal.Nz); vertices.Add(texCoord.Tu); vertices.Add(texCoord.Tv); } } byte[] verticesData = new byte[vertices.Count * sizeof(float)]; System.Buffer.BlockCopy(vertices.ToArray(), 0, verticesData, 0, verticesData.Length); byte[] gIDbin = System.Text.Encoding.ASCII.GetBytes(gID); // if no vertices in this group, let's not write it if (vertices.Count == 0) { continue; } // write face group header writer.Write(gIDbin.Length); writer.Write(gIDbin); writer.Write(vertices.Count / VERTEX_SIZE_IN_WORDS); // write veriices data writer.Write(verticesData); } }