纹理无法渲染 - DirectX 9

时间:2012-06-28 08:19:18

标签: c++ directx

我在DirectX 9中有这个代码,我需要从COM接口获取DC并绘制它。我确实得到DC,它包含图像,但我得到一个黑屏。有什么想法吗?

    LPDIRECT3DSURFACE9 pRenderSurface = NULL, pRenderSurfaceTMP = NULL;

    m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface);
    if (pRenderSurface == NULL)
        return FALSE;

    m_pD3DDevice->CreateOffscreenPlainSurface(m_nWidth, m_nHeight,
        D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &pRenderSurfaceTMP, 0);

    m_pD3DDevice->GetRenderTargetData(pRenderSurface,pRenderSurfaceTMP);

    HDC hDC = NULL;
    hr = pRenderSurfaceTMP->GetDC(&hDC);
    if (FAILED(hr))
        return FALSE;   

    if (m_pViewObject != NULL)
    {
        // RECT is relative to the windowless container rect
        RECTL rcRect = {0, 0, m_nWidth, m_nHeight};  

        // Draw onto the DC!
        hr = m_pViewObject->Draw(DVASPECT_CONTENT, 1,
            NULL, NULL, NULL, hDC, &rcRect, NULL, NULL,
            0);
    }


    pRenderSurface->ReleaseDC(hDC);
    pRenderSurface->Release();


    // Draw the surface
    m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
    m_pD3DDevice->SetTexture( 0, m_pRenderTexture );


    hr = m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

    m_hbrBackground = NULL;
    pRenderSurfaceTMP->Release();

提前致谢

2 个答案:

答案 0 :(得分:1)

确保使用m_pRenderTexture创建D3DUSAGE_RENDERTARGETpRenderSurfacepRenderSurfaceTMP的大小和格式应该匹配。有关详细信息,请参阅GetRenderTargetData失败时间:http://msdn.microsoft.com/en-us/library/windows/desktop/bb174405(v=vs.85).aspx

答案 1 :(得分:1)

试试这个:

m_pD3DDevice->CreateTexture(m_nWidth, m_nHeight, 1, 0, D3DFMT_X8R8G8B8,
                            D3DPOOL_MANAGED, &m_pRenderTexture, NULL);

// ...

LPDIRECT3DSURFACE9 pRenderSurface = NULL;
if (FAILED(m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface))) return FALSE;

HDC hDC = NULL;
if (FAILED(pRenderSurface->GetDC(&hDC))) return FALSE;   

if (m_pViewObject != NULL)
{
    RECTL rcRect = {0, 0, m_nWidth, m_nHeight};

    m_pViewObject->Draw(DVASPECT_CONTENT, 1,
        NULL, NULL, NULL, hDC, &rcRect, NULL, NULL, 0);
}

pRenderSurface->ReleaseDC(hDC);
pRenderSurface->Release();

m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
m_pD3DDevice->SetTexture( 0, m_pRenderTexture );

m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

或者这个:

m_pD3DDevice->CreateTexture(m_nWidth, m_nHeight, 1, 0, D3DFMT_X8R8G8B8,
                            D3DPOOL_DEFAULT, &m_pRenderTexture, NULL);

// ...

LPDIRECT3DSURFACE9 pRenderSurfaceTMP = NULL;
m_pD3DDevice->CreateOffscreenPlainSurface(m_nWidth, m_nHeight,
    D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &pRenderSurfaceTMP, 0);

HDC hDC = NULL;
if (FAILED(pRenderSurfaceTMP->GetDC(&hDC))) return FALSE;   

if (m_pViewObject != NULL)
{
    RECTL rcRect = {0, 0, m_nWidth, m_nHeight};

    m_pViewObject->Draw(DVASPECT_CONTENT, 1,
        NULL, NULL, NULL, hDC, &rcRect, NULL, NULL, 0);
}

pRenderSurfaceTMP->ReleaseDC(hDC);

LPDIRECT3DSURFACE9 pRenderSurface = NULL;
if (FAILED(m_pRenderTexture->GetSurfaceLevel(0, &pRenderSurface)))
    return FALSE;

if (FAILED(m_pD3DDevice->UpdateSurface(pRenderSurfaceTMP, NULL,
                                       pRenderSurface, NULL))) return FALSE;

pRenderSurface->Release();

m_pD3DDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof(Vertex) );
m_pD3DDevice->SetTexture( 0, m_pRenderTexture );

m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);