internal static MeshDef Load(FileSystem fs, string path) { MeshDef def = new MeshDef(); using (Stream s = fs.open(path)) { BinaryReader br = new BinaryReader(s); int version = br.ReadInt32(); int materials = br.ReadInt32(); for (int materialid = 0; materialid < materials; ++materialid) { MeshDef.MaterialDef m = def.addMaterial("m" + materialid.ToString()); m.texture = br.ReadString(); m.ambient = vec3.Read(br); m.diffuse = vec3.Read(br); m.specular = vec3.Read(br); m.emissive = vec3.Read(br); m.alpha = br.ReadSingle(); m.shininess = br.ReadSingle(); } int bonecount = br.ReadInt32(); for (int boneid = 0; boneid < bonecount; ++boneid) { MeshDef.Bone bone = def.newBone(); bone.name = br.ReadString(); bone.parent = br.ReadInt32(); bone.pos = vec3.Read(br); vec3 qv = vec3.Read(br); bone.rot = new quat(br.ReadSingle(), qv); } int pointcount = br.ReadInt32(); for (int pointid = 0; pointid < pointcount; ++pointid) { int boneid = br.ReadInt32(); vec3 p = new vec3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); def.addPoint(p, boneid); } int uvcount = br.ReadInt32(); for (int uvid = 0; uvid < uvcount; ++uvid) { vec2 v = new vec2(br.ReadSingle(), br.ReadSingle()); def.AddUv(v); } int normalcount = br.ReadInt32(); for (int normalid = 0; normalid < normalcount; ++normalid) { vec3 p = new vec3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); def.addNomal(p); } for (int materialid = 0; materialid < materials; ++materialid) { def.selectMaterial("m" + materialid.ToString()); int tricount = br.ReadInt32(); for (int triid = 0; triid < tricount; ++triid) { MeshDef.VertexData[] data = new MeshDef.VertexData[3]; for (int i = 0; i < 3; ++i) { data[i].vertex = br.ReadInt32(); data[i].uv = br.ReadInt32(); data[i].normal = br.ReadInt32(); } def.addTri(new MeshDef.Tri(data)); } } } return(def); }
public static MeshDef Load(FileSystem fs, string path) { MeshDef mesh = new MeshDef(); using (var file = fs.open(path)) { foreach (string l in FileUtil.LinesIn(file)) { string line = l.Trim(); if (string.IsNullOrEmpty(line) == false && line[0] != '#') { string[] data = line.Split(" \t".ToCharArray()); if (data[0] == "v") { mesh.addPoint(new vec3(floatParse(data[1]), floatParse(data[2]), floatParse(data[3])), -1); } else if (data[0] == "vt") { mesh.AddUv(new vec2(floatParse(data[1]), floatParse(data[2]))); } else if (data[0] == "vn") { mesh.addNomal(new vec3(floatParse(data[1]), floatParse(data[2]), floatParse(data[3]))); } else if (data[0] == "f") { List <MeshDef.VertexData> vd = new List <MeshDef.VertexData>(); for (int i = 1; i < data.Length; ++i) { string[] ind = data[i].Split("/".ToCharArray()); MeshDef.VertexData v = new MeshDef.VertexData(); v.vertex = int.Parse(ind[0]) - 1; v.uv = int.Parse(ind[1]) - 1; v.normal = int.Parse(ind[2]) - 1; vd.Add(v); } if (vd.Count < 3) { throw new Exception("Face data incomplete"); } for (int i = 2; i < vd.Count; ++i) { List <MeshDef.VertexData> arr = new List <MeshDef.VertexData>(); arr.Add(vd[0]); arr.Add(vd[1]); arr.Add(vd[i]); mesh.addTri(new MeshDef.Tri(arr.ToArray())); } } else if (data[0] == "usemtl") { mesh.selectMaterial(data[1].Trim()); } else if (data[0] == "mtllib") { LoadMaterialLibrary(mesh, fs, Resolve(path, data[1].Trim())); } } } } return(mesh); }