我想在屏幕上画一个矩形 但是有些不对劲,我在屏幕上看不到任何东西。 我正在使用dx11(metro风格的应用程序) 着色器代码与VS 2012中的示例代码(D3D-Cude Sample)相同
有人给我一些建议吗?
这是我的代码:
// get color
COLOR4F clr = m_SytleInterface->GetBackGroundDefaultClr();
VPOINTF3 ptDes[4];
VFloat iWidth = 256.0;
VFloat iHeight = 256.0;
VFloat fLX = -466.0;
VFloat fBY = -819.0;
//set world view projection matrix
g_constantBufferData.world =
XMMatrixTranslation(-116.5, -204.75, 0) * CVBGL::m_world;
g_constantBufferData.view = CVBGL::m_view;
g_constantBufferData.projection = (XMMATRIX)CVBGL::m_projection;
D3D11_BUFFER_DESC constantBufferDesc = {0};
constantBufferDesc.ByteWidth = sizeof(g_constantBufferData);
constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constantBufferDesc.CPUAccessFlags = 0;
constantBufferDesc.MiscFlags = 0;
//constantBufferDesc.StructureByteStride = 0;
DX::ThrowIfFailed(
m_d3dDevice->CreateBuffer(
&constantBufferDesc,
nullptr,
&m_constantBuffer
)
);
m_d3dContext->UpdateSubresource(
m_constantBuffer.Get(),
0,
nullptr,
&g_constantBufferData,
0,
0
);
//init Vertex
ptDes[0].x = 0.0f;
ptDes[0].y = 0.0f;
ptDes[0].z = 0.0f;
ptDes[1].x = 0.0f;
ptDes[1].y = 256;
ptDes[1].z = 0.0f;
ptDes[2].x = 256;
ptDes[2].y = 256;
ptDes[2].z = 0.0f;
ptDes[3].x = 256;
ptDes[3].y = 0.0f;
ptDes[3].z = 0.0f;
ByteArray loadVSTask = ReadData("SimpleVertexShader.cso");
ByteArray loadPSTask = ReadData("SimplePixelShader.cso");
DX::ThrowIfFailed(
m_d3dDevice->CreateVertexShader(
bytecodeVS->Data,
bytecodeVS->Length,
nullptr,
&vertexShader
)
);
const D3D11_INPUT_ELEMENT_DESC basicVertexLayoutDesc[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
ComPtr<ID3D11InputLayout> inputLayout;
DX::ThrowIfFailed(
m_d3dDevice->CreateInputLayout(
basicVertexLayoutDesc,
ARRAYSIZE(basicVertexLayoutDesc),
bytecodeVS->Data,
bytecodeVS->Length,
&inputLayout)
);
m_d3dContext->IASetInputLayout(inputLayout.Get());
ComPtr<ID3D11PixelShader> pixelShader;
DX::ThrowIfFailed(
m_d3dDevice->CreatePixelShader(
bytecodePS->Data,
bytecodePS->Length,
nullptr,
&pixelShader
)
);
VertexPositionColor Vertices[4] =
{
{ XMFLOAT3(ptDes[0].x, ptDes[0].y, ptDes[0].z), XMFLOAT4( clr.fRed, clr.fGreen, clr.fBlue, clr.fAlpha) }, // +Y (top face)
{ XMFLOAT3(ptDes[1].x, ptDes[1].y, ptDes[1].z), XMFLOAT4( clr.fRed, clr.fGreen, clr.fBlue, clr.fAlpha) },
{ XMFLOAT3(ptDes[2].x, ptDes[2].y, ptDes[2].z), XMFLOAT4( clr.fRed, clr.fGreen, clr.fBlue, clr.fAlpha) },
{ XMFLOAT3(ptDes[3].x, ptDes[3].y, ptDes[3].z), XMFLOAT4( clr.fRed, clr.fGreen, clr.fBlue, clr.fAlpha) },
};
D3D11_BUFFER_DESC vertexBufferDesc = {0};
vertexBufferDesc.ByteWidth = sizeof(VertexPositionColor) * ARRAYSIZE(Vertices);
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
//vertexBufferDesc.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA vertexBufferData;
vertexBufferData.pSysMem =Vertices;
vertexBufferData.SysMemPitch = 0;
vertexBufferData.SysMemSlicePitch = 0;
ComPtr<ID3D11Buffer> vertexBuffer;
DX::ThrowIfFailed(
m_d3dDevice->CreateBuffer(
&vertexBufferDesc,
&vertexBufferData,
&vertexBuffer
)
);
UINT stride = sizeof(VertexPositionColor);
UINT offset = 0;
m_d3dContext->IASetVertexBuffers(
0,
1,
vertexBuffer.GetAddressOf(),
&stride,
&offset
);
// Specify the render target and depth stencil we created as the output target.
m_d3dContext->OMSetRenderTargets(
1,
m_renderTargetView.GetAddressOf(),
m_depthStencilView.Get()
);
// Clear the render target to a solid color, and reset the depth stencil.
const float clearColor[4] = { 0.071f, 0.04f, 0.561f, 1.0f };
m_d3dContext->ClearRenderTargetView(
m_renderTargetView.Get(),
clearColor
);
m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
m_d3dContext->VSSetShader(
vertexShader.Get(),
nullptr,
0
);
m_d3dContext->PSSetShader(
pixelShader.Get(),
nullptr,
0
);
m_d3dContext->Draw(4,0);
DX::ThrowIfFailed(
m_swapChain->Present(1, 0)
);