public Triangle[] Read(string path) { var obj = new ObjParser.Obj(); obj.LoadObj(path); return obj.FaceList.Select( v => { var p1 = new Vector3( (float) obj.VertexList[v.VertexIndexList[0] - 1].X, (float) obj.VertexList[v.VertexIndexList[0] - 1].Y, (float) obj.VertexList[v.VertexIndexList[0] - 1].Z); var p2 = new Vector3( (float) obj.VertexList[v.VertexIndexList[1] - 1].X, (float) obj.VertexList[v.VertexIndexList[1] - 1].Y, (float) obj.VertexList[v.VertexIndexList[1] - 1].Z); var p3 = new Vector3( (float) obj.VertexList[v.VertexIndexList[2] - 1].X, (float) obj.VertexList[v.VertexIndexList[2] - 1].Y, (float) obj.VertexList[v.VertexIndexList[2] - 1].Z); return new Triangle( p1, p2, p3 ); } ).ToArray(); }
public void Load(string path) { string objFileName = Path.ChangeExtension(path, ".obj"); string mtlFileName = Path.ChangeExtension(path, ".mtl"); if (File.Exists(mtlFileName)) { mtl = new ObjParser.Mtl(); mtl.LoadMtl(mtlFileName); materials = ListMaterialsOfObjMaterials(mtl.MaterialList); } if (!File.Exists(objFileName)) { return; } obj = new ObjParser.Obj(); obj.LoadObj(objFileName); BuildFromFaces(); }
public static void LoadObj(bool enable, string filename, Matrix transform, int materialIndex, out IEnumerable <Vector3> vertices, out IEnumerable <Vector2> uvs, out IEnumerable <Vector3> normals, out IEnumerable <Primitive> faces) { vertices = new List <Vector3>(); faces = new List <Primitive>(); uvs = new List <Vector2>(); normals = new List <Vector3>(); if (!enable) { return; } List <Vector3> v = new List <Vector3>(); List <Vector2> vuv = new List <Vector2>(); List <Vector3> vn = new List <Vector3>(); List <Primitive> tris = new List <Primitive>(); var obj = new ObjParser.Obj(); var fileStream = new FileStream(filename, FileMode.Open); obj.LoadObj(fileStream); for (int i = 0; i < obj.VertexList.Count; i++) { var vt = obj.VertexList[i]; v.Add(new Vector3((float)vt.X, (float)vt.Y, (float)vt.Z)); } for (int i = 0; i < obj.NormalList.Count; i++) { var n = obj.NormalList[i]; vn.Add(new Vector3((float)n.X, (float)n.Y, (float)n.Z)); } for (int i = 0; i < obj.TextureList.Count; i++) { var uv = obj.TextureList[i]; vuv.Add(new Vector2((float)uv.X, (float)uv.Y)); } foreach (var f in obj.FaceList) { Primitive triangle = new Primitive(PrimitiveType.Triangle, materialIndex, Vector4.Zero, transform); triangle.Va = f.VertexIndexList[0] - 1; triangle.Vb = f.VertexIndexList[1] - 1; triangle.Vc = f.VertexIndexList[2] - 1; triangle.UVa = f.TextureVertexIndexList[0] - 1; triangle.UVb = f.TextureVertexIndexList[1] - 1; triangle.UVc = f.TextureVertexIndexList[2] - 1; triangle.Na = f.NormalIndexList[0] - 1; triangle.Nb = f.NormalIndexList[1] - 1; triangle.Nc = f.NormalIndexList[2] - 1; Vertex a = obj.VertexList[triangle.Va]; Vector3 va = new Vector3((float)a.X, (float)a.Y, (float)a.Z); Vertex b = obj.VertexList[triangle.Vb]; Vector3 vb = new Vector3((float)b.X, (float)b.Y, (float)b.Z); Vertex c = obj.VertexList[triangle.Vc]; Vector3 vc = new Vector3((float)c.X, (float)c.Y, (float)c.Z); Vector3 ta = (Vector3)Vector3.Transform(va, transform); Vector3 tb = (Vector3)Vector3.Transform(vb, transform); Vector3 tc = (Vector3)Vector3.Transform(vc, transform); BoundingBox aabb = BoundingBox.FromPoints(new Vector3[] { ta, tb, tc }); triangle.MinBounds = aabb.Minimum; triangle.MaxBounds = aabb.Maximum; triangle.MaterialIndex = f.MtlIndex; tris.Add(triangle); } vertices = v; uvs = vuv; normals = vn; faces = tris; }
// I'm doing this with too many loops but whatever. // Also I'm shattering the ability to put multiple meshes in one file. void LoadObjFile(string filename) { ObjParser.Obj obj = new ObjParser.Obj(); obj.LoadObj(filename); mGeometry = new float[3 * 3 * obj.FaceList.Count]; mGeometryIndices = new int[3 * obj.FaceList.Count]; mNormals = new float[3 * 3 * obj.FaceList.Count]; mUvs = new float[2 * 3 * obj.FaceList.Count]; // This is the smart indexed way, but I need flat shading right now. //int i = 0; //foreach (var v in obj.VertexList) //{ // mGeometry[3 * i + 0] = (float)v.X; // mGeometry[3 * i + 1] = (float)v.Y; // mGeometry[3 * i + 2] = (float)v.Z; // i++; //} //i = 0; //foreach (var f in obj.FaceList) //{ // mGeometryIndices[3 * i + 0] = f.VertexIndexList[0] - 1; // mGeometryIndices[3 * i + 1] = f.VertexIndexList[1] - 1; // mGeometryIndices[3 * i + 2] = f.VertexIndexList[2] - 1; // i++; //} mNumPrimitives = obj.FaceList.Count; primitiveType = OpenTK.Graphics.OpenGL4.BeginMode.Triangles; int i = 0; Vector3[] normalCreators = new Vector3[3]; Vector3 n; foreach (var f in obj.FaceList) { for (int v = 0; v < 3; v++) { normalCreators[v] = new Vector3((float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).X, (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Y, (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Z); mGeometry[9 * i + 3 * v + 0] = (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).X; mGeometry[9 * i + 3 * v + 1] = (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Y; mGeometry[9 * i + 3 * v + 2] = (float)obj.VertexList.ElementAt(f.VertexIndexList[v] - 1).Z; } n = Math.calculateFaceNormal(normalCreators[0], normalCreators[1], normalCreators[2]); for (int j = 0; j < 3; j++) { mNormals[9 * i + 3 * j + 0] = n.X; mNormals[9 * i + 3 * j + 1] = n.Y; mNormals[9 * i + 3 * j + 2] = n.Z; } if (obj.TextureList.Count > 0) { for (int j = 0; j < 3; j++) { mUvs[6 * i + 2 * j + 0] = (float)obj.TextureList[f.TextureVertexIndexList.ElementAt(j) - 1].X; mUvs[6 * i + 2 * j + 1] = (float)obj.TextureList[f.TextureVertexIndexList.ElementAt(j) - 1].Y; } } i++; } for (i = 0; i < mGeometryIndices.Length; i++) { mGeometryIndices[i] = i; } }