2015-07-10 103 views
-2

我的DX程序在C++中没有渲染(使用PS_5_0和VS_5_0)。我检查了所有返回HRESULT的DX函数,它们都返回S_OK,我也经历了仔细的调试并查看所有内容,没有任何内容为空或未初始化。我甚至在这里搜索了每一处,并且我没有找到任何帮助我的帖子(尝试了所有的解决方案,看看它们是否有效,没有一个是。)我不知道为什么它不是渲染,所有的参数很好。DX中没有任何东西在渲染DX 11

这里是我的代码初始化深度模板,设备上下文和设备。

//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。它包含了所有的文件(包括模型和着色器文件,所以如果你需要查看它以获取更多信息,你可以)

+4

发布5亿行代码并说“这里有什么不对”没有帮助。您是否可以将其缩小到您认为无法使用的代码部分,或者是某些未按预期工作的**最小示例**? – tadman

+0

其中一个问题是我不知道我可能做错了什么,我已经看过所有参数和所有变量,它们都似乎工作正常。我甚至从我的一个工作示例中复制了此代码仍然没有工作。如果有人能指引我正确的方向,那么我可以仔细检查我可能做错了什么。我会张贴屏幕截图,但我没有10声望。后台缓冲区正在渲染(其绿色和窗口背景是红色的,所以我知道这是工作),但立方体根本不是渲染 – JustinWeq

+4

这不是重现您的问题所需的最短代码。这会造成你试图挽救自己一段时间的外观,代价是每个可能试图回答你问题的人的时间。 –

回答

0

我想它出来了,这是我设置它时缺少的一些参数,我修好了d it,谢谢你试图帮助我所有人。