使用Windows8 SDK中包含的DirectX SDK。 编译是成功的,但程序在一个地方停止。调试显示此函数出错:
//ID3D11DeviceContext* deviceContext
//IDXGISwapChain* swapChain
//const float BackGround[4] = {0.0f, 0.0f, 0.0f, 0.0f};
void DrawScene(){
deviceContext->ClearRenderTargetView(renderTargetView, BackGroung);
deviceContext->Draw(3, 0); //<-this
swapChain->Present(0, 0);
}
在评论此功能时一切正常。我认为我在这个功能中遇到了一些问题:
bool InitScene(){
hr = D3DCompileFromFile(L"Effects.fx", 0, 0, "VS", "vs_4_0", NULL, NULL, &vsBuffer, NULL);
hr = D3DCompileFromFile(L"Effects.fx", 0, 0, "PS", "ps_4_0", NULL, NULL, &psBuffer, NULL);
hr = device->CreateVertexShader(vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), NULL, &VS);
hr = device->CreatePixelShader(psBuffer->GetBufferPointer(), psBuffer->GetBufferSize(), NULL, &PS);
Vertex v[] = {
Vertex(0.0f, 0.5f, 0.5f),
Vertex(0.5f, -0.5f, 0.5f),
Vertex(-0.5f, -0.5f, 0.5f),
};
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(&vertexBufferDesc));
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(Vertex) * 3;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA vertexBufferData;
ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
vertexBufferData.pSysMem = v;
hr = device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &triangleVertexBuffer);
UINT stride = sizeof(Vertex);
UINT offset = 0;
deviceContext->IASetVertexBuffers(0, 0, &triangleVertexBuffer, &stride, &offset);
hr = device->CreateInputLayout(layout, numElements, vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(), &vertLayout);
deviceContext->IASetInputLayout(vertLayout);
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Height = Height;
viewport.Width = Width;
deviceContext->RSSetViewports(1, &viewport);
return true;
}
或者在这里:
bool Init3D(){
DXGI_MODE_DESC bufferDesc;
ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
bufferDesc.Width = Width;
bufferDesc.Height = Height;
bufferDesc.RefreshRate.Numerator = 60;
bufferDesc.RefreshRate.Denominator = 1;
bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
swapChainDesc.BufferDesc = bufferDesc;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.Windowed = TRUE;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
hr = D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&swapChainDesc,
&swapChain,
&device,
NULL,
&deviceContext);
ID3D11Texture2D *backBuffer;
hr = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
hr = device->CreateRenderTargetView(backBuffer, NULL, &renderTargetView);
backBuffer->Release();
deviceContext->OMSetRenderTargets(1, &renderTargetView, NULL);
return true;
}
我是完美的莱默,无法理解出了什么问题。请帮帮我。