public string SerializeOBJ(int vertexIndexOffset, int textureCoordinateIndexOffset, int normalIndexOffset) { //Temp StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine("o " + m_Name); foreach (Vertex vertex in m_Vertices) { //Add offset if needed if (m_Position != null && m_Position.IsNull() == false) { Vertex tempVertex = new Vertex(vertex); tempVertex.AddVector3f(m_Position); stringBuilder.AppendLine("v " + tempVertex.ToString()); } else { stringBuilder.AppendLine("v " + vertex.ToString()); } } foreach (TextureCoordinate textureCoordinate in m_TextureCoordinates) { stringBuilder.AppendLine("vt " + textureCoordinate.ToString()); } foreach (Normal normal in m_Normals) { stringBuilder.AppendLine("vn " + normal.ToString()); } foreach (Face face in m_Faces) { stringBuilder.AppendLine(face.SerializeOBJ(vertexIndexOffset, textureCoordinateIndexOffset, normalIndexOffset)); } return(stringBuilder.ToString()); }
//Serialization public bool DeserializeOBJPart(string data, int vertexIndexOffset, int textureCoordinateIndexOffset, int normalIndexOffset) { //NOTE: We are only parsing a very small selection of OBJ features! //Full list of features: https://en.wikipedia.org/wiki/Wavefront_.obj_file //Vertex if (data.StartsWith("v ")) { //Remove the "v " so Vector4f can parse it (w is optional) data = data.Substring(2, data.Length - 2); Vertex newVertex = new Vertex(); bool success = newVertex.Deserialize(data); if (success == true) { m_Vertices.Add(newVertex); } return(success); } //Texture coordinate else if (data.StartsWith("vt ")) { //Remove the "vt " so Vector3f can parse it (w is optional) data = data.Substring(3, data.Length - 3); TextureCoordinate newTextureCoordinate = new TextureCoordinate(); bool success = newTextureCoordinate.Deserialize(data); if (success == true) { m_TextureCoordinates.Add(newTextureCoordinate); } return(success); } //Normal (currently not used by Prodeus, but here for the sake of completeness) else if (data.StartsWith("vn ")) { //Remove the "vn " so Vector3f can parse it data = data.Substring(3, data.Length - 3); Normal newNormal = new Normal(); bool success = newNormal.Deserialize(data); if (success == true) { m_Normals.Add(newNormal); } return(success); } //Face else if (data.StartsWith("f ")) { Face newFace = new Face(); bool success = newFace.DeserializeOBJ(data, vertexIndexOffset, textureCoordinateIndexOffset, normalIndexOffset); if (success == true) { m_Faces.Add(newFace); } return(success); } return(true); }