首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NVidia SolidWireFrame

NVidia SolidWireFrame
EN

Stack Overflow用户
提问于 2015-10-23 22:09:37
回答 1查看 145关注 0票数 0

我想使用solidwireframe显示一些几何图形,它似乎是一个具有多个效果和单通道的fx文件。

所以我写了一个类来执行我认为效果文件正在做的事情。头文件是我的编译着色器,它们是NVidia着色器的副本。使用4.0版编译

代码语言:javascript
复制
# 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;
    }
}

现在在我的应用程序中,在初始化设备和上下文等结束时,我调用。

代码语言:javascript
复制
        SolidWireFrame* solidWireFrame = new SolidWireFrame();
        solidWireFrame->Init(m_pd3dDevice.Get());
        m_solidWireFrame.reset(solidWireFrame);

和我的渲染循环。

代码语言:javascript
复制
    //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);
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2015-11-06 00:02:08

修好了。

我没有为线框设置世界,视图,投影常量缓冲区。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33304422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档