2015-07-10 101 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,謝謝你試圖幫助我所有人。