public VertexBufferObject(Mesh[] meshes) { List<Vertex> vertices = new List<Vertex>(); List<int> indices = new List<int>(); foreach (Mesh m in meshes) { foreach (Face f in m.Faces) { for (int i = 0; i < f.vIndices.Length; i++) { Vertex v = new Vertex(); v.Position = m.Vertices[(int)f.vIndices[i]]; v.Normal = m.Normals[(int)f.nIndices[i]]; v.TextureCoordinate = m.TexCoords[(int)f.tIndices[i]]; vertices.Add(v); indices.Add(vertices.Count - 1); } } } ids = new int[2]; GL.GenBuffers(2, ids); GL.BindBuffer(BufferTarget.ArrayBuffer,ids[0]); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(Vertex.SizeInBytes*vertices.Count), vertices.ToArray(), BufferUsageHint.StaticDraw); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ids[1]); GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(sizeof(int) * indices.Count), indices.ToArray(), BufferUsageHint.StaticDraw); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); elemets = indices.Count / 3; }
public VertexBufferObject(Mesh mesh) : this(new Mesh[] { mesh }) { }
public static Mesh[] loadModelfromOBJ(string path) { if (!File.Exists(path)) return null; System.IO.StreamReader sr = new StreamReader(path); List<Mesh> meshes = new List<Mesh>(); List<Vector3> vertices = new List<Vector3>(); List<Vector3> normals = new List<Vector3>(); List<Vector2> texCoords = new List<Vector2>(); //List<Face> faces = new List<Face>(); while (!sr.EndOfStream) { string[] s = sr.ReadLine().Split(' '); Mesh m = null; if (meshes.Count > 0) m = meshes[meshes.Count - 1]; switch (s[0]) { case "o": if(meshes.Count > 0){ m.Vertices.AddRange(vertices); m.Normals.AddRange(normals); m.TexCoords.AddRange(texCoords); } meshes.Add(new Mesh()); meshes[meshes.Count - 1].Name = s[1]; break; case "g": m = new Mesh(); m.Name = s[1]; meshes.Add(m); break; case "v": vertices.Add(new Vector3(Single.Parse(s[1], CultureInfo.InvariantCulture), Single.Parse(s[2], CultureInfo.InvariantCulture), Single.Parse(s[3], CultureInfo.InvariantCulture))); break; case "vn": normals.Add(new Vector3(Single.Parse(s[1], CultureInfo.InvariantCulture), Single.Parse(s[2], CultureInfo.InvariantCulture), Single.Parse(s[3], CultureInfo.InvariantCulture))); break; case "vt": texCoords.Add(new Vector2(Single.Parse(s[1], CultureInfo.InvariantCulture), Single.Parse(s[2], CultureInfo.InvariantCulture))); break; case "f": uint[] vI = new uint[] { UInt32.Parse(s[1].Split('/')[0], CultureInfo.InvariantCulture) - 1, UInt32.Parse(s[2].Split('/')[0], CultureInfo.InvariantCulture) - 1, UInt32.Parse(s[3].Split('/')[0], CultureInfo.InvariantCulture) - 1 }; uint[] nI = new uint[] { 0, 0, 0 }; uint[] tI = new uint[] { 0, 0, 0 }; if (texCoords.Count > 0) { tI = new uint[] { UInt32.Parse(s[1].Split('/')[1], CultureInfo.InvariantCulture) - 1, UInt32.Parse(s[2].Split('/')[1], CultureInfo.InvariantCulture) - 1, UInt32.Parse(s[3].Split('/')[1], CultureInfo.InvariantCulture) - 1 }; if (normals.Count > 0) { nI = new uint[] { UInt32.Parse(s[1].Split('/')[2], CultureInfo.InvariantCulture) - 1, UInt32.Parse(s[2].Split('/')[2], CultureInfo.InvariantCulture) - 1, UInt32.Parse(s[3].Split('/')[2], CultureInfo.InvariantCulture) - 1 }; } } else if (normals.Count > 0) { nI = new uint[] { UInt32.Parse(s[1].Split('/')[1], CultureInfo.InvariantCulture) - 1, UInt32.Parse(s[2].Split('/')[1], CultureInfo.InvariantCulture) - 1, UInt32.Parse(s[3].Split('/')[1], CultureInfo.InvariantCulture) - 1 }; } m.Faces.Add(new Face(vI, nI, tI)); break; default: break; } } if (meshes.Count > 0) { meshes[meshes.Count - 1].Vertices.AddRange(vertices); meshes[meshes.Count - 1].Normals.AddRange(normals); meshes[meshes.Count - 1].TexCoords.AddRange(texCoords); } return meshes.ToArray(); }