protected override void CreateDeviceDependentResources() { base.CreateDeviceDependentResources(); // release resources vertexBuffers.ForEach(b => RemoveAndDispose(ref b)); vertexBuffers.Clear(); indexBuffers.ForEach(b => RemoveAndDispose(ref b)); indexBuffers.Clear(); RemoveAndDispose(ref frontState); var device = DeviceManager.Direct3DDevice; // Create the vertex buffers Vertex[] verts = new Vertex[faces.Count]; for (int i = 0; i < faces.Count; i++) { // create vertex var f = faces[i]; verts[i] = new Vertex(new Vector3(vertices[(int)f.PosId - 1]), new Vector3(normals[(int)f.NormId - 1]), Color.Gray); indices.Add((ushort)f.PosId); } vertexBuffers.Add(ToDispose(Buffer.Create(device, BindFlags.VertexBuffer, verts.ToArray()))); vertexBinding_ = new VertexBufferBinding(vertexBuffers.First(), Utilities.SizeOf <Vertex>(), 0); // Create the index buffers indexBuffers.Add(ToDispose(Buffer.Create(device, BindFlags.IndexBuffer, indices.ToArray()))); frontState = ToDispose(new RasterizerState(DeviceManager.Direct3DDevice, new RasterizerStateDescription { FillMode = FillMode.Solid, CullMode = CullMode.Front, IsFrontCounterClockwise = false })); var max = verts.Max().Position; var min = verts.Min().Position; var center = (max - min) * .5f; meshExtent = new Mesh.MeshExtent { Min = min, Max = max, Radius = 0.5f, Center = center }; }
private void CreateVertexBinding() { RemoveAndDispose(ref indexBuffer); // Retrieve our SharpDX.Direct3D11.Device1 instance var device = this.DeviceManager.Direct3DDevice; var color = Color.SaddleBrown; var data = new Vertex[] { /* Vertex Position Color */ new Vertex(new Vector3(-1f, 1f, -.5f), color), // 0-Top-left new Vertex(new Vector3(1f, 1f, -.5f), color), // 1-Top-right new Vertex(new Vector3(1f, -1f, -.5f), color), // 2-Base-right new Vertex(new Vector3(-1f, -1f, -.5f), color), // 3-Base-left new Vertex(new Vector3(-1f, 1f, .5f), color), // 4-Top-left new Vertex(new Vector3(1f, 1f, .5f), color), // 5-Top-right new Vertex(new Vector3(1f, -1f, .5f), color), // 6-Base-right new Vertex(new Vector3(-1f, -1f, .5f), color), // 7-Base-left }; // Create vertex buffer for cube buffer_ = ToDispose(Buffer.Create(device, BindFlags.VertexBuffer, data)); vertexBinding_ = new VertexBufferBinding(buffer_, Utilities.SizeOf <Vertex>(), 0); // Front Right Top Back Left Bottom // v0 v1 v1 v5 v1 v0 v5 v4 v4 v0 v3 v2 // |-----| |-----| |-----| |-----| |-----| |-----| // | \ A | | \ A | | \ A | | \ A | | \ A | | \ A | // | B \ | | B \ | | B \ | | B \ | | B \ | | B \ | // |-----| |-----| |-----| |-----| |-----| |-----| // v3 v2 v2 v6 v5 v4 v6 v7 v7 v3 v7 v6 indexBuffer = ToDispose(Buffer.Create(device, BindFlags.IndexBuffer, new ushort[] { 0, 1, 2, // Front A 0, 2, 3, // Front B 1, 5, 6, // Right A 1, 6, 2, // Right B 1, 0, 4, // Top A 1, 4, 5, // Top B 5, 4, 7, // Back A 5, 7, 6, // Back B 4, 0, 3, // Left A 4, 3, 7, // Left B 3, 2, 6, // Bottom A 3, 6, 7, // Bottom B })); //indexBuffer = ToDispose(Buffer.Create(device, BindFlags.IndexBuffer, new ushort[] { // 2, 1, 0, // Front A // 0, 3, 2, // Front B // 6, 5, 1, // Right A // 1, 2, 6, // Right B // 4, 0, 1, // Top A // 1, 5, 4, // Top B // 7, 4, 5, // Back A // 5, 6, 7, // Back B // 3, 0, 4, // Left A // 4, 7, 3, // Left B // 6, 2, 3, // Bottom A // 3, 7, 6, // Bottom B //})); PrimitiveCount = Utilities.SizeOf <Vertex>(); var max = data.Max().Position; var min = data.Min().Position; var center = (max - min) * .5f; meshExtent = new Mesh.MeshExtent { Min = min, Max = max, Radius = 0.5f, Center = center }; }