2015-10-23 50 views
0

我想使用solidwireframe顯示一些幾何圖形,它似乎是一個帶有多個效果和單遍的fx文件。NVidia SolidWireFrame

所以我寫了一個類來執行我認爲效果文件正在做的事情。頭文件是我編譯的着色器,它們是NVidia的副本。與4.0版

# include "SolidWireFrame.h" 
# include "WireGeometryShader.h" 
# include "GeometryShader.h" 
# include "WirePixelShader.h" 
# include "ColorPixelShader.h" 
# include "WireVertexShader.h" 

namespace DirectX11DrawingAdapter 
{ 
    SolidWireFrame::SolidWireFrame() 
    { 
    } 

    bool SolidWireFrame::Init(ID3D11Device* device) 
    { 
     m_pdepthLessEqualStencilState = nullptr; 
     m_pfillRasterizerState = nullptr; 
     m_pBlendingState = nullptr; 
     m_pdepthWriteLessStencilState = nullptr; 
     m_vertexShader = nullptr; 
     m_geometryShader = nullptr; 
     m_pixelColorShader = nullptr; 
     m_geometrySolidWireShader = nullptr; 
     m_pixelSolidWireShader = nullptr; 
     m_vertexLayout = nullptr; 
     if (!InitializeDepthStencilState(device)) 
      return false; 
     if (!InitializeRaterizerState(device)) 
      return false; 
     if (!InitializeBlendState(device)) 
      return false; 
     if (!InitializeVertexShader(device)) 
      return false; 
     if (!InitializeGeometryShader(device)) 
      return false; 
     if (!InitializePixelShader(device)) 
      return false; 
     return true; 
    } 
    void SolidWireFrame::ApplySolidWirePattern(ID3D11DeviceContext* deviceContext) 
    {  
     SetDepthStencilState(deviceContext, m_pdepthLessEqualStencilState); 
     SetRasterizerState(deviceContext, m_pfillRasterizerState); 
     SetBlendState(deviceContext, m_pBlendingState); 
     deviceContext->VSSetShader(m_vertexShader, NULL, 0); 
     deviceContext->GSSetShader(m_geometrySolidWireShader, NULL, 0); 
     deviceContext->PSSetShader(m_pixelSolidWireShader, NULL, 0); 
    } 

    void SolidWireFrame::ApplyDepthAndSolid(ID3D11DeviceContext* deviceContext) 
    { 
     SetDepthStencilState(deviceContext, m_pdepthWriteLessStencilState); 
     SetRasterizerState(deviceContext, m_pfillRasterizerState); 
     SetBlendState(deviceContext, m_pBlendingState); 
     deviceContext->VSSetShader(m_vertexShader, NULL, 0); 
     deviceContext->GSSetShader(m_geometryShader, NULL, 0); 
     deviceContext->PSSetShader(m_pixelColorShader, NULL, 0); 
    } 

    void SolidWireFrame::ApplyDepthOnly(ID3D11DeviceContext* deviceContext) 
    { 
     SetDepthStencilState(deviceContext, m_pdepthWriteLessStencilState); 
     SetRasterizerState(deviceContext, m_pfillRasterizerState); 
     SetBlendState(deviceContext, m_pnoColorBlendingState); 
     deviceContext->VSSetShader(m_vertexShader, NULL, 0); 
     deviceContext->GSSetShader(m_geometryShader, NULL, 0); 
     deviceContext->PSSetShader(m_pixelColorShader, NULL, 0); 
    } 

    void SolidWireFrame::ApplySolidOnly(ID3D11DeviceContext* deviceContext) 
    { 
     SetDepthStencilState(deviceContext, m_pdepthLessEqualStencilState); 
     SetRasterizerState(deviceContext, m_pfillRasterizerState); 
     SetBlendState(deviceContext, m_pBlendingState); 
     deviceContext->VSSetShader(m_vertexShader, NULL, 0); 
     deviceContext->GSSetShader(m_geometryShader, NULL, 0); 
     deviceContext->PSSetShader(m_pixelColorShader, NULL, 0); 
    } 

    bool SolidWireFrame::InitializeBlendState(ID3D11Device* device) 
    { 
     D3D11_BLEND_DESC blendStateDescription; 
     // Clear the blend state description. 
     ZeroMemory(&blendStateDescription, sizeof(D3D11_BLEND_DESC)); 
     blendStateDescription.RenderTarget[0].BlendEnable = TRUE; 
     blendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; 
     blendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; 
     blendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; 
     blendStateDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA; 
     blendStateDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA; 
     blendStateDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; 
     blendStateDescription.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; 

     // Create the blend state using the description. 
     HRESULT result = device->CreateBlendState(&blendStateDescription, &m_pBlendingState); 
     if (FAILED(result)) 
     { 
      return false; 
     } 

     //ZeroMemory(&blendStateDescription, sizeof(D3D11_BLEND_DESC)); 
     blendStateDescription.RenderTarget[0].BlendEnable = FALSE; 
     result = device->CreateBlendState(&blendStateDescription, &m_pnoColorBlendingState); 
     if (FAILED(result)) 
     { 
      return false; 
     } 
     return true; 
    } 

    bool SolidWireFrame::InitializeDepthStencilState(ID3D11Device* device) 
    { 
     D3D11_DEPTH_STENCIL_DESC depthStencilDesc; 
     // Initialize the description of the stencil state. 
     ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc)); 
     // Set up the description of the stencil state. 
     depthStencilDesc.DepthEnable = TRUE; 
     depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; 
     depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; 

     depthStencilDesc.StencilEnable = FALSE; 
     depthStencilDesc.StencilReadMask = 255; 
     depthStencilDesc.StencilWriteMask = 255; 
     depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; 
     depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; 
     depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; 
     depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; 
     depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; 
     depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP; 
     depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; 
     depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; 

     // Create the depth stencil state. 
     HRESULT result = device->CreateDepthStencilState(&depthStencilDesc, &m_pdepthLessEqualStencilState); 
     if (FAILED(result)) 
     { 
      return false; 
     } 

     //ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc)); 
     depthStencilDesc.DepthEnable = true; 
     depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; 
     depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; 

     result = device->CreateDepthStencilState(&depthStencilDesc, &m_pdepthWriteLessStencilState); 
     if (FAILED(result)) 
     { 
      return false; 
     } 
     return true; 
    } 

    bool SolidWireFrame::InitializeRaterizerState(ID3D11Device* device) 
    { 
     D3D11_RASTERIZER_DESC rasterizerDescription; 
     ZeroMemory(&rasterizerDescription, sizeof(D3D11_RASTERIZER_DESC)); 
     rasterizerDescription.FillMode = D3D11_FILL_SOLID; 
     rasterizerDescription.CullMode = D3D11_CULL_NONE; 
     rasterizerDescription.DepthBias = FALSE; 
     rasterizerDescription.MultisampleEnable = TRUE; 

     rasterizerDescription.FrontCounterClockwise = FALSE; 
     rasterizerDescription.DepthBiasClamp = 0.000000000; 
     rasterizerDescription.SlopeScaledDepthBias = 0.000000000; 
     rasterizerDescription.DepthClipEnable = TRUE; 
     rasterizerDescription.ScissorEnable = FALSE; 
     rasterizerDescription.AntialiasedLineEnable = FALSE; 

     HRESULT hr = device->CreateRasterizerState(&rasterizerDescription, &m_pfillRasterizerState); 
     if (FAILED(hr)) 
      return false; 
     return true; 
    } 

    bool SolidWireFrame::SetDepthStencilState(ID3D11DeviceContext* deviceContext, ID3D11DepthStencilState* depthStencilState) 
    { 
     deviceContext->OMSetDepthStencilState(depthStencilState, 0); 
     return false; 
    } 

    bool SolidWireFrame::SetRasterizerState(ID3D11DeviceContext* deviceContext, ID3D11RasterizerState* raterizerState) 
    { 
     deviceContext->RSSetState(raterizerState); 
     return false; 
    } 

    bool SolidWireFrame::SetBlendState(ID3D11DeviceContext* deviceContext, ID3D11BlendState* blendState) 
    { 
     float blendFactor[4]; 
     // Setup the blend factor. 
     blendFactor[0] = 0.0f; 
     blendFactor[1] = 0.0f; 
     blendFactor[2] = 0.0f; 
     blendFactor[3] = 0.0f; 
     // Turn off the alpha blending. 
     deviceContext->OMSetBlendState(blendState, blendFactor, 0xffffffff); 
     return false; 
    } 

    bool SolidWireFrame::InitializeVertexShader(ID3D11Device* device) 
    { 
     // Create the vertex shader 
     HRESULT hr = device->CreateVertexShader(m_pwireVertexShader, sizeof(m_pwireVertexShader), NULL, &m_vertexShader); 
     if (FAILED(hr)) 
     { 
      return false; 
     } 

     // Define the input layout 
     D3D11_INPUT_ELEMENT_DESC layout[] = 
     { 
      { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
      { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
      { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, 
     }; 

     UINT numElements = ARRAYSIZE(layout); 

     // Create the input layout 
     hr = device->CreateInputLayout(layout, numElements, m_pwireVertexShader, sizeof(m_pwireVertexShader), &m_vertexLayout); 
     if (FAILED(hr)) 
      return false; 
     return true; 
    } 

    bool SolidWireFrame::InitializeGeometryShader(ID3D11Device* device) 
    { 
     // Create the geometry shader 
     HRESULT hr = device->CreateGeometryShader(m_pwireGeometryShader, sizeof(m_pwireGeometryShader), NULL, &m_geometrySolidWireShader); 
     if (FAILED(hr)) 
      return false; 

     hr = device->CreateGeometryShader(m_pgeometryShader, sizeof(m_pgeometryShader), NULL, &m_geometryShader); 
     if (FAILED(hr)) 
      return false; 

     return true; 
    } 

    bool SolidWireFrame::InitializePixelShader(ID3D11Device* device) 
    { 
     // Create the pixel shader 
     HRESULT hr = device->CreatePixelShader(m_pwirePixelShader, sizeof(m_pwirePixelShader), NULL, &m_pixelSolidWireShader); 
     if (FAILED(hr)) 
      return false; 

     hr = device->CreatePixelShader(m_pcolorPixelShader, sizeof(m_pcolorPixelShader), NULL, &m_pixelColorShader); 
     if (FAILED(hr)) 
      return false; 

     return true; 
    } 
} 
現在

在我的應用程序編譯,在初始化設備和上下文等我通話結束。

 SolidWireFrame* solidWireFrame = new SolidWireFrame(); 
     solidWireFrame->Init(m_pd3dDevice.Get()); 
     m_solidWireFrame.reset(solidWireFrame); 

和我的渲染循環。

//m_solidWireFrame->ApplyDepthAndSolid(m_pImmediateContext); 
    //m_solidWireFrame->ApplyDepthOnly(m_pImmediateContext); 
    //m_solidWireFrame->ApplySolidOnly(m_pImmediateContext); 

    /*for (it_drawingData iterator = dd.begin(); iterator != dd.end(); iterator++) 
    { 
     DrawingData *drawingData = iterator->second; 
     if (drawingData->IsRendered) 
     { 
      m_pImmediateContext->IASetVertexBuffers(0, 1, &drawingData->VertexBuffer, &stride, &offset); 
      m_pImmediateContext->IASetIndexBuffer(drawingData->IndexBuffer, DXGI_FORMAT_R16_UINT, 0); 
      m_pImmediateContext->DrawIndexed(drawingData->IndexData.size(), 0, 0); 
     } 
    } 
    //not sure if i should call this between renders? 
    m_pdesignerSwapChain->Present(0, 0);*/ 

    m_solidWireFrame->ApplySolidWirePattern(m_pImmediateContext); 

    for (it_drawingData iterator = dd.begin(); iterator != dd.end(); iterator++) 
    { 
     DrawingData *drawingData = iterator->second; 
     if (drawingData->IsRendered) 
     { 
      m_pImmediateContext->IASetVertexBuffers(0, 1, &drawingData->VertexBuffer, &stride, &offset); 
      m_pImmediateContext->IASetIndexBuffer(drawingData->IndexBuffer, DXGI_FORMAT_R16_UINT, 0); 
      m_pImmediateContext->DrawIndexed(drawingData->IndexData.size(), 0, 0); 
     } 
    } 
+0

當然,它們可以被轉換。什麼是問題/問題? – Drop

+0

我覺得效果只是一個包裝。 –

+0

我添加了我創建的類的代碼。所以我想我可以創建方法來改變狀態。所以在我的渲染循環中,我調用Apply來設置新的狀態,然後將我的緩衝區拉出來繪製。然而我在屏幕上什麼也看不到。注意:這適用於我的常規vs和ps。所以我想我的問題是。那些SetDepthStencilState,SetRasterizerState和SetBlendState將它們的ID3D11 _ ** _ DESC的部分設置爲與我不同的設置。 –

回答

0

修正了它。

我沒有設置線框的世界,視圖,投影常量緩衝區與