我想将我的Backbuffer捕获到我的LPDIRECT3DSURFACE9中,然后将表面复制到我的IDirect3DTexture9中,最后将该纹理用作我的对象皮肤。我写了代码,但刚收到黑色像素。
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);
}
答案 0 :(得分: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纹理
答案 1 :(得分:4)
另一种方式:
第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