public static VertexBufferBinding Load(GraphicsDevice device, String filename, Matrix3?positionTransform = null) { var library = Injector.Global.Resolve <Library>(); var loader = new WavefrontObjLoader(); using (var stream = library.OpenRead(filename)) { loader.LoadObj(stream); } if (positionTransform.HasValue) { Matrix3 transform = positionTransform.Value; for (int j = 0; j < loader.VertexList.Count; j++) { loader.VertexList[j].Vector = Matrix3.Transform(loader.VertexList[j].Vector, transform); } } // vertex buffer var vertexCount = loader.TriangleCount * 3; var vertices = new PositionColorTexture[vertexCount]; int i = 0; foreach (var face in loader.FaceList) { var v = face.VertexIndexList; var t = face.TextureVertexIndexList; vertices[i + 0] = new PositionColorTexture { Position = loader.VertexList[v[0] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[0] - 1].Vector }; vertices[i + 2] = new PositionColorTexture { Position = loader.VertexList[v[1] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[1] - 1].Vector }; vertices[i + 1] = new PositionColorTexture { Position = loader.VertexList[v[2] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[2] - 1].Vector }; i += 3; if (v.Length == 4) { vertices[i + 0] = new PositionColorTexture { Position = loader.VertexList[v[2] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[2] - 1].Vector }; vertices[i + 2] = new PositionColorTexture { Position = loader.VertexList[v[3] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[3] - 1].Vector }; vertices[i + 1] = new PositionColorTexture { Position = loader.VertexList[v[0] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[0] - 1].Vector }; i += 3; } } var buffer = GraphicsBuffer.Create(device, vertices, false); return(new VertexBufferBinding { Buffer = buffer, Count = vertexCount, Declaration = PositionColorTexture.Layout, Stride = PositionColorTexture.Layout.Stride, }); }
public static Mesh LoadMesh(Device device, string path) { var loader = new WavefrontObjLoader(); loader.LoadObj(path); //if (positionTransform.HasValue) //{ // Matrix3 transform = positionTransform.Value; // for (int j = 0; j < loader.VertexList.Count; j++) // { // loader.VertexList[j].Vector = Matrix3.Transform(loader.VertexList[j].Vector, transform); // } //} // vertex buffer var vertexCount = loader.TriangleCount * 3; var vertices = new ColoredTexturedVertex[vertexCount]; int i = 0; foreach (var face in loader.FaceList) { var v = face.VertexIndexList; var n = face.NormalVertexIndexList; var t = face.TextureVertexIndexList; vertices[i + 0] = new ColoredTexturedVertex { Position = loader.VertexList[v[0] - 1].Vector, Normal = loader.NormalList[n[0] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[0] - 1].Vector }; vertices[i + 1] = new ColoredTexturedVertex { Position = loader.VertexList[v[1] - 1].Vector, Normal = loader.NormalList[n[1] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[1] - 1].Vector }; vertices[i + 2] = new ColoredTexturedVertex { Position = loader.VertexList[v[2] - 1].Vector, Normal = loader.NormalList[n[2] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[2] - 1].Vector }; i += 3; if (v.Length == 4) { vertices[i + 0] = new ColoredTexturedVertex { Position = loader.VertexList[v[2] - 1].Vector, Normal = loader.NormalList[n[2] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[2] - 1].Vector }; vertices[i + 1] = new ColoredTexturedVertex { Position = loader.VertexList[v[3] - 1].Vector, Normal = loader.NormalList[n[3] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[3] - 1].Vector }; vertices[i + 2] = new ColoredTexturedVertex { Position = loader.VertexList[v[0] - 1].Vector, Normal = loader.NormalList[n[0] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[0] - 1].Vector }; i += 3; } } var indices = new int[vertexCount]; for (int j = 0; j < vertexCount; j++) { indices[j] = j; } Mesh mesh = new Mesh(device); mesh.VertexBuffer = Buffer11.Create <ColoredTexturedVertex>(device.NativeDevice, BindFlags.VertexBuffer, vertices.ToArray()); mesh.IndexBuffer = Buffer11.Create(device.NativeDevice, BindFlags.IndexBuffer, indices.ToArray()); mesh.VertexSize = Utilities.SizeOf <ColoredTexturedVertex>(); mesh.IndexCount = indices.Count(); return(mesh); }