private bool InitializeBuffers(SharpDX.Direct3D11.Device device) { // Create the vertex array. var vertices = new PositionVertex[VertexCount]; // Create the index array. int[] indices = new int[IndexCount]; // Load the vertex array and index array with data. for (int i = 0; i < VertexCount; i++) { vertices[i].position = new Vector3(Model[i].x, Model[i].y, Model[i].z); indices[i] = i; } // Now finally create the vertex buffer. // Create the vertex buffer. VertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, vertices); // Create the index buffer. IndexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indices); // Release the arrays now that the buffers have been created and loaded. vertices = null; indices = null; ReleaseSkyDomeModel(); return(true); }
public static GeometryData FromVertexArray(float[] vertices, int[] indices, int indicesPerFace = 3, float[] bindShapeMatrix = null) { var data = new PositionVertex[indices.Length]; var meshIndices = new int[indices.Length]; for (var i = 0; i < indices.Length; i++) { var vertex = (bindShapeMatrix != null) ? new float[] { vertices[indices[i] * 3], vertices[indices[i] * 3 + 1], vertices[indices[i] * 3 + 2] }.VectorTransform(bindShapeMatrix) : new float[] { vertices[indices[i] * 3], vertices[indices[i] * 3 + 1], vertices[indices[i] * 3 + 2] }; data[i].PosX = vertex[0]; data[i].PosY = vertex[1]; data[i].PosZ = vertex[2]; meshIndices[i] = i; } // check if we need to triangulate the mesh if (indicesPerFace == 4) { meshIndices = QuadIndicesToTriangles(meshIndices); } var result = new GeometryData() { VertexCount = data.Length, Indices = meshIndices }; using (var stream = new System.IO.MemoryStream()) using (var writer = new System.IO.BinaryWriter(stream)) { for (int i = 0; i < data.Length; i++) { writer.Write(data[i].PosX); writer.Write(data[i].PosY); writer.Write(data[i].PosZ); } result.Data = stream.ToArray(); } // add vertex attribs result.Attribs = new VertexAttrib[] { new VertexAttrib() { Size = 3, Type = (int)OpenTK.Graphics.OpenGL4.VertexAttribPointerType.Float, Stride = 3 * sizeof(float), Offset = 0 } }; return(result); }