D3D9渲染到透明纹理

时间:2017-11-13 15:59:55

标签: c++ directx-9

我正在尝试渲染纹理,使渲染的表面透明。

这样我以后可以使用该纹理将3D场景覆盖在2D场景上(显然,如果不透明,则无法看到下面的内容)。

以下适用于渲染到纹理,但是,保存的图像完全不透明(我认为这是RenderTargets不支持透明度),并且由于它保存完全不透明,我必须想象一下它在以后渲染的几何体上的显示方式。

    $data = array($row['cities']);
    $city = explode(', ', $data[0]);
    $cities = $city[ rand(0, count($city)-1) ];
    $citylink = "img/".$cities.".png";

CreateTexture功能:

HRESULT Camera::RenderToTexture(Node* pScene)
{
    Wnd* pDisplay = pScene->GetDisplay();

    if (!pDisplay->m_pDevice)
        return E__NODEVICE;

    IDirect3DDevice9* pDevice = pDisplay->m_pDevice;

    HRESULT hr = S_OK;

    // original
    IDirect3DSurface9* renderTarget = NULL;
    IDirect3DSurface9* depthTarget = NULL;

    // new
    IDirect3DSurface9* renderTarget_ = NULL;
    IDirect3DSurface9* depthTarget_ = NULL;

    // render target
    hr = pDevice->GetRenderTarget(0, &renderTarget);    // currently set render target for restoration
    if (D3D_OK == hr)
    {
        D3DSURFACE_DESC desc; renderTarget->GetDesc(&desc);

        // m_fCamWidth = 1080; m_fCamHeight = 1920;
        hr = pDevice->CreateRenderTarget((UINT)m_fCamWidth, (UINT)m_fCamHeight, desc.Format,
                                         desc.MultiSampleType, desc.MultiSampleQuality,
                                         false, &renderTarget_, NULL);
        THROW_IF_FAILED(hr);
    }

    // matching depth target
    hr = pDevice->GetDepthStencilSurface(&depthTarget); // currently set depth target for restoration
    if (D3D_OK == hr)
    {
        D3DSURFACE_DESC desc; depthTarget->GetDesc(&desc);

        // m_fCamWidth = 1080; m_fCamHeight = 1920;
        hr = pDevice->CreateDepthStencilSurface((UINT)m_fCamWidth, (UINT)m_fCamHeight, desc.Format,
                                                desc.MultiSampleType, desc.MultiSampleQuality,
                                                false, &depthTarget_, NULL);
        THROW_IF_FAILED(hr);
    }

    if (renderTarget_) pDevice->SetRenderTarget(0, renderTarget_);
    if (depthTarget_) pDevice->SetDepthStencilSurface(depthTarget_);

    // hierarchy flattening
    THROW_IF_FAILED(pScene->UpdateTransform());
    THROW_IF_FAILED(pDevice->SetTransform(D3DTS_PROJECTION, &m_matProjection));
    THROW_IF_FAILED(pDevice->SetTransform(D3DTS_VIEW, &m_matView));

    THROW_IF_FAILED(pDevice->BeginScene());
    THROW_IF_FAILED(pDevice->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_ARGB(0, 128, 128, 128), 1.0f, 0));

    // hierarchy rendering
    pScene->Render();

    pDevice->SetRenderTarget(0, renderTarget);
    pDevice->SetDepthStencilSurface(depthTarget);

    LPDIRECT3DTEXTURE9 textureMap; CreateTexture(textureMap, pDisplay);
    LPDIRECT3DSURFACE9 l_Surface; textureMap->GetSurfaceLevel(0, &l_Surface);
    pDevice->StretchRect(renderTarget_, NULL, l_Surface, NULL, D3DTEXF_NONE);

    D3DXSaveTextureToFile("D:\\output.png", D3DXIMAGE_FILEFORMAT::D3DXIFF_PNG, textureMap, NULL);

    SAFE_RELEASE(renderTarget_);
    SAFE_RELEASE(depthTarget_);

    SAFE_RELEASE(renderTarget);
    SAFE_RELEASE(depthTarget);

    THROW_IF_FAILED(pDevice->EndScene());

    return hr;
}

当前渲染状态:

HRESULT Camera::CreateTexture(LPDIRECT3DTEXTURE9& _pTextureMap, Wnd* _pDisplay)
{
    IDirect3DDevice9* pDevice = _pDisplay->m_pDevice;

    HRESULT hr = S_OK;

    //m_fCamWidth = 1080; m_fCamHeight = 1920;
    hr = D3DXCreateTexture(pDevice,
                           (UINT)m_fCamWidth, (UINT)m_fCamHeight,
                           D3DX_DEFAULT,
                           D3DUSAGE_RENDERTARGET,
                           D3DFMT_A8R8G8B8,
                           D3DPOOL_DEFAULT,
                           &_pTextureMap);
    //Note: If you want the alpha channel then use D3DFMT_A8R8G8B8 format.

    return hr;
}

不幸的是,我必须在没有着色器的帮助下完成此操作。

虽然从技术上讲我可以每帧保存图像并用颜色键加载它,但这对我来说不是一个很好的解决方案。

当前输出(源代码) Current Output

更正输出(通过Paint.net编辑) Corrected Output

编辑: 添加以下内容无济于事:

D3DRS_AMBIENT, RGB(255, 255, 255)
0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2
0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU
1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2
1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU

我已经验证了我的RenderTarget&的形式。质地;两者都是pDevice->SetRenderState(D3DRENDERSTATETYPE::D3DRS_ALPHABLENDENABLE, TRUE); pDevice->SetRenderState(D3DRENDERSTATETYPE::D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); pDevice->SetRenderState(D3DRENDERSTATETYPE::D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

编辑2:

意识到我有一些裁员,我减少了一些我正在做的事情,使它变得不那么复杂。但是,我似乎无法将我的RenderTarget清除为透明。

D3DFMT_A8R8G8B8

0 个答案:

没有答案