I got stuck to draw a triangle using an index and DrawIndexed(), the triangle is not drawn. The windows just renders the blue background. While looking at other tutorials I think I did everything necessary to use an index, but maybe I am missing something?!
Edit: Solved (thx Unbird), the array data type did not match the format in SetIndexBuffer, changing it back to int to match the format did the trick. I updated the code below for the next one using indices for SharpDX 1) Index array and buffer at the class variables
// Vertices
private VertexPositionColor[] vertices = new VertexPositionColor[]
{
new VertexPositionColor(new Vector3(-0.5f, 0.5f, 0.0f), SharpDX.Color.Red),
new VertexPositionColor(new Vector3(0.5f, 0.5f, 0.0f), SharpDX.Color.Green),
new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.0f), SharpDX.Color.Blue),
};
// Indices
private int[] indices = new int[] { 0, 1, 2 };
// Vertice-Buffer / Index-Buffer
private D3D11.Buffer vertexBuffer;
private D3D11.Buffer indexBuffer;
2) Set the indexbuffer
private void InitializeTriangle()
{
vertexBuffer = D3D11.Buffer.Create(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices);
indexBuffer = D3D11.Buffer.Create(d3dDevice, D3D11.BindFlags.IndexBuffer, indices);
}
3) Draw-Method:
/// <summary>
/// Draw the scene.
/// </summary>
private void Draw()
{
//Re-Initialize triangle
InitializeTriangle();
// Set back buffer as current render target view
d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView);
// Clear the screen
d3dDeviceContext.ClearRenderTargetView(renderTargetView, new SharpDX.Color(32, 103, 178));
// Draw the triangle on the screen
d3dDeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
d3dDeviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(vertexBuffer, Utilities.SizeOf<VertexPositionColor>(), 0));
d3dDeviceContext.InputAssembler.SetIndexBuffer(indexBuffer, Format.R32_UInt, 0);
d3dDeviceContext.DrawIndexed(3, 0, 0);
// Swap front and back buffer
swapChain.Present(1, PresentFlags.None);
}
.
SharpDX_Tutorial_5_(using_an_index).zip
↧