I need to draw a simple rectangle (not a filled box) with directx 11.
I have found this code:
const float x = 0.1;
const float y = 0.1;
const float height = 0.9;
const float width = 0.9;
VERTEX OurVertices[] =
{
{ x, y, 0, col },
{ x width, y, 0, col },
{ x, y height, 0, col },
{ x width, y, 0, col },
{ x width, y height, 0 , col },
{ x, y height, 0, col }
};
static const XMVECTORF32 col = { 1.f, 2.f, 3.f, 4.f };
// this is the function used to render a single frame
void RenderFrameTest(void)
{
// float color[4] = { 0.0f, 0.2f, 0.4f, 1.0f };
float color[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
// clear the back buffer to a deep blue
devcon->ClearRenderTargetView(backbuffer, color);
// select which vertex buffer to display
UINT stride = sizeof(VERTEX);
UINT offset = 0;
devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
// select which primtive type we are using
// draw the vertex buffer to the back buffer
devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
devcon->Draw(sizeof(OurVertices) / sizeof(VERTEX), 0);
swapchain->Present(0, 0);
}
void DrawRectangle(float x, float y, float width, float height, XMVECTORF32 col)
{
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(OurVertices);
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
HRESULT val = dev->CreateBuffer(&bd, NULL, &pVBuffer); // create the buffer
D3D11_MAPPED_SUBRESOURCE ms;
val = devcon->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms); // map the buffer
memcpy(ms.pData, OurVertices, sizeof(OurVertices)); // copy the data
devcon->Unmap(pVBuffer, NULL);
}
but the result is not what I expected:
I suspect the problem is OurVertices array and "D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP" but I don't have have experience with DirectX in general.
Can you help me please ?
CodePudding user response:
You have 6 vertices defined for a rectangle, which means you want to use TriangleList topology and not TriangleStrip topology.