// These files can be created using binary blender objects public string ReadBinaryFile(string filename) { string BlenderFilesDirectory = GlsTutorialsClass.ProjectDirectory + @"/Blender/"; blenderObjects = new List<BlenderObject>(); int offset = 0; StringBuilder result = new StringBuilder(); byte[] binaryBlenderObjects = File.ReadAllBytes(BlenderFilesDirectory + filename); int objectCount = BitConverter.ToInt32 (binaryBlenderObjects, 0); result.AppendLine("Found " + objectCount.ToString() + " Blender Objects"); offset = offset + 4; for (int i = 0; i < objectCount; i++) { BlenderObject bo = new BlenderObject("Object" + i.ToString()); int blenderObjectSize = bo.CreateFromBinaryData(binaryBlenderObjects, offset); offset = offset + blenderObjectSize; result.AppendLine("Object " + i.ToString() + " size = " + blenderObjectSize.ToString()); bo.Setup(); blenderObjects.Add(bo); } return result.ToString(); }
public void ReadFile(string filename) { string BlenderFilesDirectory = GlsTutorialsClass.ProjectDirectory + @"/Blender/"; string nextLine; blenderObjects = new List<BlenderObject>(); using (StreamReader sr = new StreamReader(new FileStream(BlenderFilesDirectory + filename, FileMode.Open))) { short vertexCount = 1; short normalCount = 1; short previousObjectVertexCount = 1; // change from 1 to zero based short previousObjectNormalCount = 1; // change from 1 to zero based nextLine = sr.ReadLine(); while (!sr.EndOfStream) { if (nextLine[0] == 'o') { BlenderObject bo = new BlenderObject(nextLine); while (!sr.EndOfStream) { nextLine = sr.ReadLine(); if (nextLine[0] == 'o') break; if (nextLine[0] == 'v') { if (nextLine[1] == ' ') { bo.AddVertex(nextLine); vertexCount++; } if (nextLine[1] == 'n') { bo.AddNormal(nextLine); normalCount++; } } if (nextLine[0] == 'f') { bo.AddTriangle(nextLine, previousObjectVertexCount, previousObjectNormalCount); } } previousObjectVertexCount = vertexCount; previousObjectNormalCount = normalCount; bo.Setup(); blenderObjects.Add(bo); } else { if (!sr.EndOfStream) nextLine = sr.ReadLine(); } } } }