Jump to content
  • Advertisement
Sign in to follow this  

DX11 [solved] SharpDX: Draw a simple triangle using drawindexed() [help, please]

This topic is 521 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 uint[] indices = new uint[] { 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

            // Set back buffer as current render target view
            // 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);




Edited by joergz

Share this post

Link to post
Share on other sites

uint -> Format.R32_UInt

ushort -> Format.R16_UInt

Edit : Note that even after the edit you still use signed integers. Won't matter here though :P


Edited by unbird

Share this post

Link to post
Share on other sites

That was the error, thank you!!


Edit: damn you're right. Edited once again :-)

Edited by joergz

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!