private bool InitializeBuffers(Device device) { try { // Create the vertex array. var vertices = new LightShader.Vertex[VertexCount]; // Create the index array. var indices = new int[IndexCount]; for (var i = 0; i < VertexCount; i++) { vertices[i] = new LightShader.Vertex() { position = new Vector3(ModelObject[i].x, ModelObject[i].y, ModelObject[i].z), texture = new Vector2(ModelObject[i].tu, ModelObject[i].tv), normal = new Vector3(ModelObject[i].nx, ModelObject[i].ny, ModelObject[i].nz) }; indices[i] = i; } // Create the vertex buffer. VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, indices); return(true); } catch { return(false); } }
public ObjModel(Device device, String modelFileName, Texture texture) { StreamReader reader = new StreamReader("Data/Models/" + modelFileName); List <Vector3> vertices = new List <Vector3>(); List <Vector2> textures = new List <Vector2>(); List <Vector3> normals = new List <Vector3>(); List <Tuple <int, int, int> > points = new List <Tuple <int, int, int> >(); while (!reader.EndOfStream) { String line = reader.ReadLine(); if (String.IsNullOrWhiteSpace(line) || line[0] == '#') { continue; } String[] items = line.Split(' '); if (items[0] == "v") { vertices.Add(new Vector3(float.Parse(items[1]), float.Parse(items[2]), -float.Parse(items[3]))); } if (items[0] == "vt") { textures.Add(new Vector2(float.Parse(items[1]), 1.0f - float.Parse(items[2]))); } if (items[0] == "vn") { normals.Add(new Vector3(float.Parse(items[1]), float.Parse(items[2]), -float.Parse(items[3]))); } if (items[0] == "f") { for (int i = 3; i > 0; i--) { String[] items2 = items[i].Split('/'); points.Add(new Tuple <int, int, int>(Int32.Parse(items2[0]), Int32.Parse(items2[1]), Int32.Parse(items2[2]))); } } } LightShader.Vertex[] verticesDefinition = new LightShader.Vertex[points.Count]; for (int i = 0; i < points.Count; i++) { verticesDefinition[i].position = vertices[points[i].Item1 - 1]; verticesDefinition[i].texture = textures[points[i].Item2 - 1]; verticesDefinition[i].normal = normals[points[i].Item3 - 1]; } VertexCount = points.Count; Int32[] indicesDefinition = new int[points.Count]; for (int i = 0; i < points.Count; i++) { indicesDefinition[i] = i; } IndexCount = points.Count; Texture = texture; VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, verticesDefinition); IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, indicesDefinition); }
private bool InitializeBuffers(Device device) { try { // Create the vertex array. var vertices = new LightShader.Vertex[VertexCount]; // Create the index array. var indices = new int[IndexCount]; for (var i = 0; i < VertexCount; i++) { vertices[i] = new LightShader.Vertex() { position = new Vector3(ModelObject[i].x, ModelObject[i].y, ModelObject[i].z), texture = new Vector2(ModelObject[i].tu, ModelObject[i].tv), normal = new Vector3(ModelObject[i].nx, ModelObject[i].ny, ModelObject[i].nz) }; indices[i] = i; } // Create the vertex buffer. VertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = Buffer.Create(device, BindFlags.IndexBuffer, indices); return true; } catch { return false; } }