我现在正在学习 DirectX 11,并且正在尝试“开始 DirectX 11 编程”一书中的演示之一。我按照书籍指南制作了一个处理窗口创建和销毁的基类(包括 d3d11 设置)。所有其他演示都基于这个类。所以我正在编写 Triangle 演示,其目的是绘制一个 2d 三角形。下面是 Triangle 类的实现:
#include "Triangle.h"
Triangle::Triangle(void): mVertexShader(0), mPixelShader(0), mInputLayout(0), mVertexBuffer(0)
{
}
Triangle::~Triangle(void)
{
}
void Triangle::Update(float deltaTime){
}
void Triangle::UnloadContent(){
if(mVertexShader)
mVertexShader->Release();
if(mPixelShader)
mPixelShader->Release();
if(mInputLayout)
mInputLayout->Release();
if(mVertexBuffer)
mVertexBuffer->Release();
mVertexBuffer = 0;
mPixelShader = 0;
mVertexShader = 0;
mInputLayout = 0;
}
bool Triangle::LoadContent(){
//Load Vertex Shader and make it active
ID3DBlob* vsBlob = 0;
if(!CompileShader(L"shaders.fx", "VS_Main", "vs_4_0", &vsBlob)){
MessageBox(0, L"Error Loading Vertex Shader", L"Compile Error", MB_OK);
return false;
}
HRESULT hr;
if(FAILED(hr = mDevice->CreateVertexShader(vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(), 0, &mVertexShader))){
if(vsBlob)
vsBlob->Release();
return false;
}
D3D11_INPUT_ELEMENT_DESC vertexLayout[] = {
{
"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0
}
};
unsigned int layoutSize = ARRAYSIZE(vertexLayout);
if(FAILED(mDevice->CreateInputLayout(
vertexLayout,
layoutSize,
vsBlob->GetBufferPointer(),
vsBlob->GetBufferSize(),
&mInputLayout
))){
vsBlob->Release();
return false;
}
vsBlob->Release();
ID3DBlob* psBlob = 0;
if(!CompileShader(L"shaders.fx", "PS_Main", "ps_4_0", &psBlob)){
MessageBox(0, L"Error Loading Pixel Shader", L"Compile Error", MB_OK);
if(psBlob)
psBlob->Release();
return false;
}
if(FAILED(mDevice->CreatePixelShader(
psBlob->GetBufferPointer(),
psBlob->GetBufferSize(),
0,
&mPixelShader
))){
psBlob->Release();
return false;
}
psBlob->Release();
//vertex buffer creation
VertexPos vertices[] =
{
XMFLOAT3( 0.5f, 0.5f, 0.5f ),
XMFLOAT3( 0.5f, -0.5f, 0.5f ),
XMFLOAT3( -0.5f, -0.5f, 0.5f )
};
D3D11_BUFFER_DESC vertexDesc;
ZeroMemory(&vertexDesc, sizeof(vertexDesc));
vertexDesc.Usage = D3D11_USAGE_DEFAULT;
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexDesc.ByteWidth = sizeof(VertexPos) * 3;
D3D11_SUBRESOURCE_DATA subresourceData;
ZeroMemory(&subresourceData, sizeof(subresourceData));
subresourceData.pSysMem = vertices;
if(FAILED(mDevice->CreateBuffer(
&vertexDesc,
&subresourceData,
&mVertexBuffer
))){
return false;
}
return true;
}
bool Triangle::CompileShader(wchar_t* filePath, char* entry, char* shaderModel, ID3DBlob** buffer){
DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined(DEBUG) || defined(_DEBUG)
shaderFlags |= D3DCOMPILE_DEBUG;
#endif
ID3DBlob* errorBuffer = 0;
HRESULT result;
result = D3DX11CompileFromFileW(
filePath,
0,0,
entry,
shaderModel,
shaderFlags,
0,0,
buffer,
&errorBuffer,
0
);
if(FAILED(result)){
if(errorBuffer != 0){
OutputDebugStringA((char*) errorBuffer->GetBufferPointer());
errorBuffer->Release();
}
return false;
}
if(errorBuffer)
errorBuffer->Release();
return true;
}
void Triangle::Render(){
float clearColor[4] = { 1.0f, 0.0f, 1.25f, 1.0f };
mDeviceContext->ClearRenderTargetView(mRenderTarget, clearColor);
unsigned int stride = sizeof(VertexPos);
unsigned int offset = 0;
mDeviceContext->IASetInputLayout(mInputLayout);
mDeviceContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &offset);
mDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
mDeviceContext->VSSetShader(mVertexShader, 0, 0);
mDeviceContext->PSSetShader(mPixelShader, 0, 0);
mDeviceContext->Draw(3, 0);
mSwapChain->Present(0, 0);
}
现在,当我运行它时,屏幕会清除并采用我指定的颜色,但屏幕上没有绘制任何内容。只是为了确保我刚刚编译了本书提供的源代码,它工作得很好。使用的着色器也相同。我的电脑确实支持directx 11。我认为大多数名称都是不言自明的,但你可以问我是否令人困惑。我已经用其他演示测试了我的框架,它运行良好。我似乎无法在我的代码中找到错误。