Directx9 surface to texture
Solution 1
You can try something like this
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();
Where IdStage is current render target you want to copy, in your case it will be 0
m_Texture is a DirectX Texture that you want to receive the copy from the backbuffer
Solution 2
One Another way:
Step 1: Get the back buffer as a surface:
LPDIRECT3DSURFACE9 pBackBuffer;
GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer)
Step 2: Create an Empty texture
LPDIRECT3DTEXTURE9 textureMap
D3DXCreateTexture(device, width, height, D3DX_DEFAULT, D3DUSAGE_RENDERTARGET,
D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT,&textureMap);
Note: If you want the alpha channel then use D3DFMT_A8R8G8B8 format.
Step 3: Get the pointer which is pointing to the top surface of the empty texture
LPDIRECT3DSURFACE9 pTexSurface;
textureMap->GetSurfaceLevel(0, &pTexSurface);
Step 4: Surface to Surface Copy (back buffer surface to empty surface)
StretchRect(pBBsurface, NULL, pTexSurface, NULL, D3DTEXF_NONE);
Step 5: Resultant texture with back buffer content
textureMap
daniel
Updated on June 04, 2022Comments
-
daniel almost 2 years
I want to capture my Backbuffer into my LPDIRECT3DSURFACE9 and then copy the surface into my IDirect3DTexture9 finally use that texture as my object skin. I wrote the codes but just received black pixels.
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); }
-
daniel over 11 yearsThank you carlos for your help.i do it but nothing change.(maybe i do it wrong)so could you please look into my code edited by your code here.it's complete and you can compile it too.
-
Carlos Ch over 11 yearsTry changing the 0 parameter in Create texture to 1. D3DXCreateTexture(g_pd3dDevice,640,480,D3DUSAGE_DYNAMIC, 1,D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT, &My_3DTexture);