hopeztm

Member
  • Content count

    2
  • Joined

  • Last visited

Community Reputation

100 Neutral

About hopeztm

  • Rank
    Newbie
  1. I want to set a texture as render target and then save it to the disk. but the rendering is ok, which means I can draw the tea port on the screen, but the file I saved is not ok, I can't open it with texture format [font=Courier New][size=3][font=Courier New][size=3]D3DFMT_A8R8G8B8, or the file corropted when I use [/size][/font][/size][/font] [font=Courier New][size=3][font=Courier New][size=3][font=Courier New][size=3][font=Courier New][size=3]D3DFMT_R5G6B5. [/size][/font][/size][/font][/size][/font][/size][/font] [font=Courier New][size=3][font=Courier New][size=3]the following is my code, I started use DX for 1 week, I am really weak in this technique, thank you so much~[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"]HRESULT DXMainFrame::FrameSnapShot(HWND hWnd, TCHAR *filename)[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"]{[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] D3DDISPLAYMODE mode;[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] HRESULT hr;[/size][/font][/size][/font] [font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"] if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="3"][font="Courier New"][size="3"] (FAILED(hr = m_pD3DDevice->GetDisplayMode(0, &mode)))[/size][/font][/size][/font] [font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"] return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="3"][font="Courier New"][size="3"] hr;[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] IDirect3DTexture9 * g_pRenderTexture = NULL;[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] hr = m_pD3DDevice->CreateTexture(mode.Width,mode.Height,1,[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] D3DUSAGE_RENDERTARGET,[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] D3DFMT_R5G6B5,[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] D3DPOOL_DEFAULT,&g_pRenderTexture,[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] NULL);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] LPDIRECT3DSURFACE9 surf,oldRenderTarget;[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] hr = g_pRenderTexture->GetSurfaceLevel(0,&surf);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] m_pD3DDevice->GetRenderTarget(0,&oldRenderTarget);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] m_pD3DDevice->SetRenderTarget(0,surf);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR [/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] (0.0f,0.25f,0.25f,0.55f), 1.0f, 0);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] drawTeaPort(m_pD3DDevice);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] m_pD3DDevice->SetRenderTarget(0,oldRenderTarget);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR (0.0f,0.25f,0.25f,0.55f), 1.0f, 0);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] OnFrameRender(m_pD3DDevice,DXUTGetTime(),DXUTGetElapsedTime(),NULL);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] hWnd = DXUTGetHWND();[/size][/font][/size][/font] [font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"]// area to capture[/color][/size][/font][/color][/size][/font][/color][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] RECT *rect = NULL;[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] WINDOWINFO windowInfo;[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] windowInfo.cbSize = [/size][/font][/size][/font] [font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"] sizeof[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="3"][font="Courier New"][size="3"](WINDOWINFO);[/size][/font][/size][/font] [font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"] if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="3"][font="Courier New"][size="3"](hWnd) [/size][/font][/size][/font][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"]// capture windo[/color][/size][/font][/color][/size][/font][/color][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] {[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] GetWindowInfo(hWnd, &windowInfo);[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] rect = &windowInfo.rcWindow;[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] }[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] hr = D3DXSaveTextureToFile(filename,D3DXIFF_BMP,g_pRenderTexture,NULL);[/size][/font][/size][/font] [font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"] //hr = D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, surf, NULL, rect); didn't work either[/color][/size][/font][/color][/size][/font][/color][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] surf->Release();[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"] g_pRenderTexture->Release();[/size][/font][/size][/font] [font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"] return[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="3"][font="Courier New"][size="3"] hr;[/size][/font][/size][/font] [font="Courier New"][size="3"][font="Courier New"][size="3"]}[/size][/font][/size][/font]