const VCNBool VCND3D9EffectCore::DebugRender() const { HRESULT hr = S_FALSE; VCND3D9* renderer = static_cast<VCND3D9*>(VCNRenderCore::GetInstance()); LPDIRECT3DDEVICE9 device = renderer->GetD3DDevice(); // Draw the SSAO result to final screen if (::GetAsyncKeyState(VK_F6) & 0x8000) { // Take a snapshot and save it to disk, when SHIFT is pressed. if ( ::GetAsyncKeyState(VK_SHIFT) & 0x8000 ) { static unsigned int frame = 0; const unsigned int kFrameGrab = 512; if (frame++ % kFrameGrab == 0) { char textureName[128]; _snprintf(textureName, sizeof(textureName), "ShadowMap%d.png", frame / kFrameGrab); hr = D3DXSaveTextureToFileA(textureName, D3DXIFF_PNG, mShadowMapTexture, NULL); VCN_ASSERT( SUCCEEDED(hr) ); } } mAllStateBlock->Capture(); device->SetVertexDeclaration(NULL); device->SetVertexShader(NULL); device->SetPixelShader(NULL); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO); device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); device->SetTexture(0, mShadowMapTexture); // Select the vertex buffer to display device->SetFVF(SCREENVERTEX::FVF); device->SetStreamSource(0, mScreenVertexBuffer, 0, sizeof(SCREENVERTEX)); device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); mAllStateBlock->Apply(); return true; } return false; }
bool SprConverter::SaveDdsFile( const char* pszSprName, int nFrame, IDirect3DTexture9* pTexture ) { if (pszSprName == NULL || pTexture == NULL) { return false; } string szDDS = pszSprName; szDDS.resize(szDDS.size() - 4); char cszFrame[16]; sprintf(cszFrame, ".%d.png", nFrame); szDDS += cszFrame; D3DXSaveTextureToFileA(szDDS.c_str(), D3DXIFF_PNG, pTexture, NULL); return true; }
//----------------------------------------------------------------------------- // Desc: 渲染图形 //----------------------------------------------------------------------------- VOID Render() { //清空后台缓冲区 g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(45, 50, 170), 1.0f, 0); //开始在后台缓冲区绘制图形 if (SUCCEEDED(g_pd3dDevice->BeginScene())) { SetupMatrices(); g_pd3dDevice->GetRenderTarget(0, &g_pBackupSur); g_pd3dDevice->SetRenderTarget(0, g_pRenderSur); /*RTT*/ g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); g_pd3dDevice->SetTexture(0, g_pTexture); //设置纹理 g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX)); g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); D3DXSaveTextureToFileA("tt.tga", D3DXIFF_TGA, g_pRenderTex, NULL); D3DXMATRIXA16 matWorld1; D3DXMatrixIdentity(&matWorld1); D3DXMatrixRotationY(&matWorld1, timeGetTime() / 5000.0f); g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld1); g_pd3dDevice->SetRenderTarget(0, g_pRender2Sur); /*RTT*/ g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); g_pd3dDevice->SetTexture(0, g_pTexture2); //设置纹理 g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX)); g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); D3DXSaveTextureToFileA("tt2.tga", D3DXIFF_TGA, g_pRender2Tex, NULL); D3DXMATRIXA16 matWorld; D3DXMatrixIdentity(&matWorld); //D3DXMatrixRotationY(&matWorld, timeGetTime() / 2000.0f); g_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld); //创建并设置观察矩阵 D3DXVECTOR3 vEyePt(10.0f, 0.0f, -25); D3DXVECTOR3 vLookatPt(10.0f, 0.0f, 0.0f); D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f); D3DXMATRIXA16 matView; D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec); g_pd3dDevice->SetTransform(D3DTS_VIEW, &matView); /*真是场景*/ g_pd3dDevice->SetRenderTarget(0, g_pBackupSur); g_pd3dDevice->SetTexture(0, g_pRenderTex); g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX)); g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); g_pd3dDevice->SetTexture(0, g_pRender2Tex); g_pd3dDevice->SetStreamSource(0, g_pVB2, 0, sizeof(CUSTOMVERTEX)); g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //结束在后台缓冲区绘制图形 g_pd3dDevice->EndScene(); } //将在后台缓冲区绘制的图形提交到前台缓冲区显示 g_pd3dDevice->Present(NULL, NULL, NULL, NULL); }
bool parseCodeImage(const char* pszPath) { D3DXIMAGE_INFO info; HRESULT re = D3DXGetImageInfoFromFileA(pszPath, &info); if(re != S_OK) return false; IDirect3DTexture9 *pText = NULL; //D3DXCreateTextureFromFileA(g_pDevice, pszPath, &pText); //用没有 EX 的函数, 创建的纹理长宽会变成 2的 n次方 re = D3DXCreateTextureFromFileExA(g_pDevice, pszPath, info.Width, info.Height, info.MipLevels, 0/*D3DUSAGE_RENDERTARGET*/, info.Format, D3DPOOL_SYSTEMMEM, D3DX_FILTER_TRIANGLE,D3DX_FILTER_TRIANGLE,D3DCOLOR_ARGB(255,0,0,0), NULL, NULL, &pText); FILE* pFile = fopen("D:/hehe.txt", "w"); D3DLOCKED_RECT rc; pText->LockRect(0, &rc, NULL, 0); BYTE bR = 0, bG = 0, bB = 0; DWORD* pSrc = (DWORD*)rc.pBits; for (int i = 0; i < info.Height ; i++) { for (int j = 0; j < info.Width; j++) { bR = (*pSrc) >> 16; bG = (*pSrc) << 16 >> 24; bB = (*pSrc) & 0x000000ff; if (bR >= 205 && bG >= 205 && bB >= 205) { fprintf(pFile, "1"); } else { fprintf(pFile, "8"); } BYTE t = max( max(bR, bG), bB); *pSrc = t | t << 8 | t << 16; ++pSrc; } fprintf(pFile, "\r\n"); } pText->UnlockRect(0); // 保存灰度图 re = D3DXSaveTextureToFileA("D:/hehe.jpg", D3DXIFF_BMP, pText, NULL); pText->Release(); fclose(pFile); }
void CD3D9Texture::SaveToFile(const std::string& strFilename) { D3DXSaveTextureToFileA(strFilename.c_str(), D3DXIFF_BMP, m_pd3dTexture, NULL); }