Commit 58bfba78 authored by Administrator's avatar Administrator
Browse files

add index buffer

parent 0da6faac
......@@ -54,21 +54,28 @@ const Vertex vertices[] =
{ -0.5f, -0.5f, 0.0f, 0.0f, 1.0f },
// left top
{ 0.0f, 0.5f, 1.0f, 0.0f, 0.0f },
{ -0.5f, -0.5f, 0.0f, 0.0f, 1.0f },
{ -0.3f, 0.3f, 0.0f, 1.0f, 0.0f },
// right top
{ 0.5f, -0.5f, 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.5f, 1.0f, 0.0f, 0.0f },
{ 0.3f, 0.3f, 0.0f, 0.0f, 1.0f },
// center bottom
{ 0.5f, -0.5f, 0.0f, 1.0f, 0.0f },
{ 0.0f, -0.8f, 1.0f, 0.0f, 0.0f },
{ -0.5f, -0.5f, 0.0f, 0.0f, 1.0f },
};
// Define index buffer
Microsoft::WRL::ComPtr<ID3D11Buffer> pIndexBuffer;
// create index buffer
const unsigned short indices[] =
{
// draw center
0, 1, 2,
// draw left top
0, 2, 3,
// draw right top
0, 4, 1,
// draw bottom
1, 5, 2,
};
// Define constant buffer
......@@ -127,6 +134,26 @@ void DefineVertexBuffer()
dev->CreateBuffer(&bd, &sd, &pVertexBuffer);
}
void DefineIndexBuffer()
{
D3D11_BUFFER_DESC ibd = {};
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.Usage = D3D11_USAGE_DEFAULT;
ibd.CPUAccessFlags = 0u;
ibd.MiscFlags = 0u;
ibd.ByteWidth = sizeof(indices);
ibd.StructureByteStride = sizeof(unsigned short);
D3D11_SUBRESOURCE_DATA isd = {};
isd.pSysMem = indices;
// Create index buffer
dev->CreateBuffer(&ibd, &isd, &pIndexBuffer);
// Bind index buffer to pipeline
devcon->IASetIndexBuffer(pIndexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0u);
}
void DefineConstantBuffer(float angle)
{
cb =
......@@ -195,13 +222,15 @@ void DrawPrimitive()
devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Draw the triangle.
devcon->Draw((UINT)sizeof(vertices), 0u);
// devcon->Draw((UINT)sizeof(vertices), 0u);
devcon->DrawIndexed((UINT)std::size(indices), 0u, 0u);
}
void drawTriangle()
{
DefineShader();
DefineVertexBuffer();
DefineIndexBuffer();
DefineConstantBuffer(timer.Peek());
DefineInputLayout();
DrawPrimitive();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment