[英]Draw a rectangle with DX11
I need to draw a simple rectangle (not a filled box) with directx 11.我需要用 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.我怀疑问题出在 OurVertices 数组和“D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP”上,但我一般没有使用 DirectX 的经验。
Can you help me please?你能帮我吗?
You have 6 vertices defined for a rectangle, which means you want to use TriangleList topology and not TriangleStrip topology.您为一个矩形定义了 6 个顶点,这意味着您要使用 TriangleList 拓扑而不是 TriangleStrip 拓扑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.