我的DX程序在C ++中没有渲染(使用PS_5_0和VS_5_0)。我检查了所有返回HRESULT的DX函数,它们都返回S_OK,我也经过仔细调试并查看了所有内容没有任何东西是空的或未初始化的。我甚至搜索了这里和bing的所有地方,我没有找到任何帮助我的帖子(尝试了所有的解决方案,看看它们是否有效,没有一个做过)。我不知道它为什么不渲染,所有的参数很好。
这是我的代码初始化深度模板,设备上下文和设备。
//create the graphics device factory
result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
//use the factor to create the adapter for the primary graphics interface(video card)
result = factory->EnumAdapters(0, &adapter);
if (FAILED(result))
return false;
//enumerate the primary adapter output(monitor)
result = adapter->EnumOutputs(0, &adapterOutput);
if (FAILED(result))
return false;
//get the adapter(video card) description
result = adapter->GetDesc(&adapterDesc);
if (FAILED(result))
return false;
//zero out the swap chain description
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
//Set ot to a single back buffer
swapChainDesc.BufferCount = 1;
//set regular 32 bit surface for the back buffer.
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
//set the usage of the back buffer.
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
//set the handle for the window to render to.
swapChainDesc.OutputWindow = hwnd;
//turn multi sampling off
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
//set the scan line odering and scaling to unspecified.
swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
//discard the back buffer contents after presenting
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
//donw set the advanced flags
swapChainDesc.Flags = 0;
//set the feature level to directX 11
featureLevel = D3D_FEATURE_LEVEL_11_0;
//create the swap chain,Direct3D, and the driect3D device context.
result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1,
D3D11_SDK_VERSION, &swapChainDesc, &m_swapChain, &m_device, NULL, &m_deviceContext);
if (FAILED(result))
return false;
//Get the pointer to the back buffer
result = m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr);
if (FAILED(result))
return false;
//create the render target view with the back buffer pointer
result = m_device->CreateRenderTargetView(backBufferPtr, NULL, &m_renderTarget);
//set up the description of the depth buffer
depthBufferDesc.Width = screenWidth;
depthBufferDesc.Height = screenHeight;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = 0;
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;
//Create the texture for the depth buffer using the filled out description
result = m_device->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer);
if (FAILED(result))
return false;
// Set up the description of the stencil state.
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
// Stencil operations if pixel is front-facing.
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
// Stencil operations if pixel is back-facing.
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
//create the depth stencil state
result = m_device->CreateDepthStencilState(&depthStencilDesc, &m_depthStencil);
//set the depth stencil state
m_deviceContext->OMSetDepthStencilState(m_depthStencil, 1);
//Set up the depth stencil view description
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
//Create the depth stencil view
result = m_device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc,&m_depthStencilView);
if (FAILED(result))
return false;
//Bind the render target view and depth stencil buffer to the output render pipeline
m_deviceContext->OMSetRenderTargets(1, &m_renderTarget, m_depthStencilView);
//Setup the raster description which determines how and what polygons get drawn
rasterDesc.AntialiasedLineEnable = false;
rasterDesc.CullMode = D3D11_CULL_BACK;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = false;
rasterDesc.SlopeScaledDepthBias = 0.0f;
//create the rasterizer state from the description
result = m_device->CreateRasterizerState(&rasterDesc, &m_rasterState);
// Now set the rasterizer state.
m_deviceContext->RSSetState(m_rasterState);
//Setup the viewport for rendering
viewport.Width = (float)screenWidth;
viewport.Height = (float)screenHeight;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
// Create the view por
m_deviceContext->RSSetViewports(1, &viewport);
//set up the projection matrix
fieldOfView = (float)D3DX_PI / 4.0f;
screenAspect = (float)screenWidth / (float)screenHeight;
//create the projection matrix for 3d rendering
D3DXMatrixPerspectiveFovLH(&m_projectionMatrix, fieldOfView, screenAspect, screenNear, screenDepth);
这是我的代码渲染模型(我确保所有顶点都很好)
float color[4];
//set up the color to clear the buffer to
color[0] = red;
color[1] = green;
color[2] = blue;
color[3] = alpha;
//clear the back buffer
m_deviceContext->ClearRenderTargetView(m_renderTarget, color);
//clear the depth buffer
m_deviceContext->ClearDepthStencilView(m_depthStencilView, D3D11_CLEAR_DEPTH, 1.0f,0);
unsigned int stride;
unsigned int offset;
//set the vertex buffer stride and offset
stride = sizeof(Vertex);
offset = 0;
//Set the vertex buffer to be active on the device context
deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
//Set the index buffer to active in the input assembler so it can be renderered
deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
//set the type of primitive topology to use for rendering
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//Set the vertex input layout
deviceContext->IASetInputLayout(m_layout);
//Set the vertex and pixel shaders that will be used to render this triangle.
deviceContext->VSSetShader(m_vertexShader, NULL, 0);
deviceContext->PSSetShader(m_pixelShader, NULL, 0);
//Set the sampler state in the pixel shader.
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
//Render the model
deviceContext->DrawIndexed(indexCount, 0, 0);
//present the back buffer to the screen now that rendering is complete
m_swapChain->Present(m_vsync, 0);
有谁知道我做错了什么?(PS我在GitHub中有这个程序的最新版本:https://github.com/JustinWeq/Test-Engine。它包含所有文件(包括模型和着色器文件) ,所以如果你需要查看它以获得更多信息,你可以。)
答案 0 :(得分:0)
我想通了,这是我在设置时缺少的一些参数。我修好了,谢谢你试图帮助我。