2013-01-11 104 views
2

我想將我的Backbuffer捕獲到我的LPDIRECT3DSURFACE9中,然後將表面複製到我的IDirect3DTexture9中,最後使用該紋理作爲我的對象外觀。我編寫了代碼,但只收到黑色像素。Directx9表面紋理

IDirect3DTexture9* texture; 
LPDIRECT3DSURFACE9 pd3dsBack = NULL; 

void init()//init point 
{ 

    D3DXCreateTexture(g_pd3dDevice, 640, 480, D3DUSAGE_DYNAMIC, 
     0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &texture); 

} 

void render()//render point 
{ 

    g_pd3dDevice->BeginScene(); 

    g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
     D3DCOLOR_COLORVALUE(0.0f, 1.0f, 0.0f, 1.0f), 1.0f, 0); 

    //my scene (1) 3d objects codes for draw. 

    g_pd3dDevice->EndScene(); 

    //now try to get back-buffer into surface 

    g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pd3dsBack); 


    //i add this Save section to ensure the backbuffer data received complete and work.(it was ok and save complete true picture of Scene(1)). 

    D3DXSaveSurfaceToFileA("BackbufferImage.BMP", D3DXIFF_BMP, pd3dsBack, NULL, NULL); 


    //this line put surface into my texture ; if u think this way is false please give me a simple code to fill texture by surface. 

    texture->GetSurfaceLevel(0, &pd3dsBack); 

    pd3dsBack->Release();//release my surface 


         //scene(2) 
    g_pd3dDevice->BeginScene(); 

    g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
     D3DCOLOR_COLORVALUE(1.0f, 0.0f, 0.0f, 1.0f), 1.0f, 0); 

    //now render my scene(2) and useing the texture object for draw it as skin of my 3d  object 
    g_pd3dDevice->SetTexture(0, texture); 

    g_pd3dDevice->EndScene(); 


    g_pd3dDevice->Present(NULL, NULL, NULL, NULL); 

} 

回答

4

你可以嘗試這樣的事情

LPDIRECT3DDEVICE9 l_Device= RenderManager()->GetDirectXDevice(); 
LPDIRECT3DSURFACE9 l_RenderTarget, l_Surface; 

m_Texture->GetSurfaceLevel(0,&l_Surface); 
l_Device->GetRenderTarget(IdStage,&l_RenderTarget); 
l_Device->StretchRect(l_RenderTarget,NULL, l_Surface,NULL,D3DTEXF_NONE); 
l_RenderTarget->Release(); 

IdStage是當前的渲染要複製的目標,你的情況這將是0

m_Texture是一個DirectX紋理,你想從backbuffer接收副本

+0

謝謝卡洛斯爲您help.i做到這一點,但沒有變化(也許我這樣做是錯誤的),所以請你看看我的代碼編着的。代碼在這裏完成,你也可以編譯它。 – daniel

+0

心中已經上傳它這裏 - > http://www.mediafire.com/?vwin2zj6mhe5wv6 – daniel

+0

嘗試改變0參數中創建紋理1. D3DXCreateTexture(g_pd3dDevice,640480,D3DUSAGE_DYNAMIC,1,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&My_3DTexture ); –

3

One An另一種方式:

步驟1:獲取後緩衝器作爲表面:

LPDIRECT3DSURFACE9 pBackBuffer; 
GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer) 

步驟2:創建一個空的紋理

LPDIRECT3DTEXTURE9 textureMap 
D3DXCreateTexture(device, width, height, D3DX_DEFAULT, D3DUSAGE_RENDERTARGET, 
           D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT,&textureMap); 

注意:如果你想要的alpha通道,然後使用D3DFMT_A8R8G8B8格式。

步驟3:獲取其指向空紋理的頂表面

LPDIRECT3DSURFACE9 pTexSurface; 
textureMap->GetSurfaceLevel(0, &pTexSurface); 

步驟4的指針:地地複製(後臺緩存表面到空面)

StretchRect(pBBsurface, NULL, pTexSurface, NULL, D3DTEXF_NONE); 

步驟5:合力質感與後臺緩衝區內容

textureMap