HRESULT HookIDirect3DDevice9::GetRenderTarget(LPVOID _this, DWORD RenderTargetIndex, IDirect3DSurface9** ppRenderTarget) { LOG_API(); return pD3Dev->GetRenderTarget(RenderTargetIndex, ppRenderTarget); }
void InitStateDX9(void) { // 取得Direct3D 9裝置 LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); // 關閉打光 device->SetRenderState(D3DRS_LIGHTING, FALSE); // 使用trilinear device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); // device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE); device->GetRenderTarget(0, &g_pMainFrameBuffer); device->GetDepthStencilSurface(&g_pMainDepthBuffer); int width, height; GutGetWindowSize(width, height); g_iFrameBufferWidth = width * 2; g_iFrameBufferHeight = height * 2; device->CreateTexture(g_iFrameBufferWidth, g_iFrameBufferHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pTexture, NULL); device->CreateDepthStencilSurface(g_iFrameBufferWidth, g_iFrameBufferHeight, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, FALSE, &g_pDepthStencil, NULL); g_pTexture->GetSurfaceLevel(0, &g_pFrameBuffer); }
LPDIRECT3DTEXTURE9 CShimmer::Render(LPDIRECT3DTEXTURE9 tex) { LPDIRECT3DSURFACE9 OldSurface = nullptr; LPDIRECT3DSURFACE9 OldDepth = nullptr; LPDIRECT3DDEVICE9 Device = Window::Instance()->Device(); //深度バッファはそのまま参照する Device->GetRenderTarget(0,&OldSurface); Device->SetRenderTarget(0,Surface); //陽炎マップをクリアする Device->Clear(0L,nullptr,(D3DCLEAR_TARGET | D3DCLEAR_STENCIL),0x80808080,1.0f,0); Effect->Begin(0,0); //陽炎パーティクルを描画 ShimmerParticle2D::DrawAll(); //背景を送る Effect->SetTexture(Param[BACK_BUFFER],tex); //陽炎マップを送る Effect->SetTexture(Param[SHIMMER_MAP],Texture); if (_BlurFlag) { //ブラーをかける ConductBlur(); } else { Device->SetRenderTarget(0,ResultSurface); } Device->Clear(0L,nullptr,(D3DCLEAR_TARGET | D3DCLEAR_STENCIL),D3DXCOLOR(0.0f,0,0,0),1.0f,0); //歪ませて描く Effect->BeginPass(0); Device->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2); Effect->EndPass(); Effect->End(); //最初のサーフェイスに戻す Device->SetRenderTarget(0,OldSurface); //後始末 if (OldSurface != nullptr) { OldSurface->Release(); OldSurface = nullptr; } return Result; }
void Graphics::setRenderTarget(RenderTarget* target, int num) { //if (backBuffer != nullptr) backBuffer->Release(); if (num == 0) { if (backBuffer == nullptr) { device->GetRenderTarget(0, &backBuffer); device->GetDepthStencilSurface(&depthBuffer); } affirm(device->SetDepthStencilSurface(target->depthSurface)); } affirm(device->SetRenderTarget(num, target->colorSurface)); }
void Graphics::setRenderTarget(RenderTarget* target, int num, int additionalTargets) { // if (backBuffer != nullptr) backBuffer->Release(); System::makeCurrent(target->contextId); if (num == 0) { if (backBuffer == nullptr) { device->GetRenderTarget(0, &backBuffer); device->GetDepthStencilSurface(&depthBuffer); } affirm(device->SetDepthStencilSurface(target->depthSurface)); } affirm(device->SetRenderTarget(num, target->colorSurface)); }
void Graphics4::setRenderTargets(RenderTarget** targets, int count) { // if (backBuffer != nullptr) backBuffer->Release(); System::makeCurrent(targets[0]->contextId); if (backBuffer == nullptr) { device->GetRenderTarget(0, &backBuffer); device->GetDepthStencilSurface(&depthBuffer); } Microsoft::affirm(device->SetDepthStencilSurface(targets[0]->depthSurface)); for (int i = 0; i < count; ++i) { Microsoft::affirm(device->SetRenderTarget(i, targets[i]->colorSurface)); } }
static bool CopyRenderTargetToBackground() { bool ret = false; LPDIRECT3DDEVICE9 device = (LPDIRECT3DDEVICE9)GetUseDirect3DDevice9(); if (device == NULL) return nullptr; HRESULT hr; IDirect3DSurface9* tempRender = nullptr; IDirect3DSurface9* tempDepth = nullptr; hr = device->GetRenderTarget(0, &tempRender); if (FAILED(hr)) { goto Exit; } hr = device->GetDepthStencilSurface(&tempDepth); if (FAILED(hr)) { goto Exit; } device->SetRenderTarget(0, g_dx9_backgroundSurface); device->SetDepthStencilSurface(nullptr); D3DSURFACE_DESC desc; tempRender->GetDesc(&desc); const bool isSame = (desc.Width == g_backgroundWidth && desc.Height == g_backgroundHeight); device->StretchRect( tempRender, nullptr, g_dx9_backgroundSurface, nullptr, (isSame ? D3DTEXF_POINT : D3DTEXF_LINEAR) ); device->SetRenderTarget(0, tempRender); device->SetDepthStencilSurface(tempDepth); ret = true; Exit:; ES_SAFE_RELEASE(tempRender); ES_SAFE_RELEASE(tempDepth); return ret; }
VOID Render(float timeDelta) { SetupMatrix() ; g_ModelViewCamera->OnFrameMove(); // Save render target g_pd3dDevice->GetRenderTarget(0, &g_pOldRenderTarget) ; g_pd3dDevice->SetRenderTarget(0, g_pRenderSurface) ; g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Without this line, the teapot won't show up, what's the inner stuff of SetTexture? g_pd3dDevice->SetTexture(0, NULL) ; // Disable lighting, since we didn't specify color for vertex g_pd3dDevice->SetRenderState( D3DRS_LIGHTING , FALSE ); g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME) ; D3DXMATRIX word ; D3DXMatrixTranslation(&word, 0.0f, 0.0f, 0.0f) ; static float totalTime = 0.0f ; totalTime += timeDelta * 2 ; D3DXMATRIX rot ; D3DXMatrixRotationY(&rot, totalTime) ; word *= rot ; g_pd3dDevice->SetTransform(D3DTS_WORLD, &word) ; g_pTeapotMesh->DrawSubset(0) ; // End the scene g_pd3dDevice->EndScene(); } // Restore the back buffer g_pd3dDevice->SetRenderTarget(0, g_pOldRenderTarget) ; RenderCube(timeDelta) ; // Present the back-buffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
//---------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------- static bool CopyRenderTargetToBackground() { bool ret = false; HRESULT hr; IDirect3DSurface9* tempRender = nullptr; IDirect3DSurface9* tempDepth = nullptr; hr = g_d3d_device->GetRenderTarget(0, &tempRender); if (FAILED(hr)) { goto Exit; } hr = g_d3d_device->GetDepthStencilSurface(&tempDepth); if (FAILED(hr)) { goto Exit; } g_d3d_device->SetRenderTarget(0, g_backgroundSurface); g_d3d_device->SetDepthStencilSurface(nullptr); D3DSURFACE_DESC desc; tempRender->GetDesc(&desc); g_d3d_device->StretchRect( tempRender, nullptr, g_backgroundSurface, nullptr, D3DTEXF_POINT ); g_d3d_device->SetRenderTarget(0, tempRender); g_d3d_device->SetDepthStencilSurface(tempDepth); ret = true; Exit:; ES_SAFE_RELEASE(tempRender); ES_SAFE_RELEASE(tempDepth); return ret; }
bool ReInitResourceDX9(void) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); int w,h; GutGetWindowSize(w, h); float fAspect = (float)h/(float)w; g_proj_matrix = GutMatrixPerspectiveRH_DirectX(g_fFOV, fAspect, g_fCameraNearZ, g_fCameraFarZ); g_ImageInfo.m_iWidth = g_texture_width; g_ImageInfo.m_iHeight = g_texture_height; g_ImageInfo.m_bProcedure = true; //D3DFORMAT fmt = D3DFMT_A16B16G16R16F; D3DFORMAT fmt = D3DFMT_A32B32G32R32F; for ( int i=0; i<TEX_TEXTURES; i++ ) { device->CreateTexture( g_texture_width, g_texture_height, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pTextures[i], NULL); if ( NULL==g_pTextures[i] ) return false; g_pTextures[i]->GetSurfaceLevel(0, &g_pSurfaces[i]); if ( NULL==g_pSurfaces[i] ) return false; } device->GetRenderTarget(0, &g_pMainFramebuffer); device->GetDepthStencilSurface(&g_pMainDepthbuffer); device->SetRenderTarget(0, g_pSurfaces[0]); device->SetRenderTarget(1, g_pSurfaces[1]); device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 0), 1.0f, 0); device->SetRenderTarget(0, g_pMainFramebuffer); device->SetRenderTarget(1, NULL); return true; }
VOID Render() { SetupMatrix() ; g_ModelViewCamera->OnFrameMove(); g_pd3dDevice->GetRenderTarget(0, &g_pOldRenderTarget) ; g_pd3dDevice->SetRenderTarget(0, g_pRenderSurface) ; g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Without this line, the teapot won't show up, what's the inner stuff of SetTexture? g_pd3dDevice->SetTexture(0, NULL) ; // Disable lighting, since we didn't specify color for vertex g_pd3dDevice->SetRenderState( D3DRS_LIGHTING , FALSE ); g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME) ; g_pTeapotMesh->DrawSubset(0) ; // End the scene g_pd3dDevice->EndScene(); } g_pd3dDevice->SetRenderTarget(0, g_pOldRenderTarget) ; g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff00ff00, 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID) ; // Render texture RenderQuad() ; // End the scene g_pd3dDevice->EndScene(); } // Present the back-buffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
virtual void OnDistorting() { IDirect3DSurface9* targetSurface = nullptr; IDirect3DSurface9* texSurface = nullptr; HRESULT hr = S_OK; hr = texture->GetSurfaceLevel( 0, &texSurface ); assert(SUCCEEDED(hr)); hr = device->GetRenderTarget( 0, &targetSurface ); assert(SUCCEEDED(hr)); hr = device->StretchRect( targetSurface, NULL, texSurface, NULL, D3DTEXF_NONE); assert(SUCCEEDED(hr)); ES_SAFE_RELEASE( texSurface ); ES_SAFE_RELEASE( targetSurface ); renderer->SetBackground( texture ); }
//---------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------- void Rendering() { g_d3d_device->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 ); LPDIRECT3DSURFACE9 targetSurface = NULL; g_d3d_device->GetRenderTarget( 0, &targetSurface ); g_d3d_device->UpdateSurface( g_d3d_clearing_image, NULL, targetSurface, NULL ); ES_SAFE_RELEASE( targetSurface ); g_d3d_device->BeginScene(); g_renderer->SetLightDirection(::Effekseer::Vector3D(1.0f, 1.0f, 1.0f)); g_renderer->SetLightAmbientColor(::Effekseer::Color(40, 40, 40, 255)); g_renderer->BeginRendering(); g_manager->Draw(); g_renderer->EndRendering(); g_d3d_device->EndScene(); g_d3d_device->Present( NULL, NULL, NULL, NULL ); }
HRESULT KGraphicsEngine::Render() { HRESULT hr = S_OK; if(!g_hRenderWnd) return FALSE; if(TRUE!=::GetWindowRect(g_hRenderWnd,&m_RenderWindowRect)) { //MessageBox(g_hBaseWnd,"不知道该渲染到什么地方","Error!",0); return FALSE; } if (m_lpCurScene!=NULL) m_lpCurScene->FrameMove(); g_cGraphicsTool.SetCurScene(m_lpCurScene); RECT ScrRect; ScrRect.left = 0; ScrRect.right = m_RenderWindowRect.right - m_RenderWindowRect.left; ScrRect.top = 0; ScrRect.bottom = m_RenderWindowRect.bottom - m_RenderWindowRect.top; D3DVIEWPORT9 ViewPort; ViewPort.X = m_RenderWindowRect.left; ViewPort.Y = m_RenderWindowRect.top; ViewPort.Width = ScrRect.right; ViewPort.Height = ScrRect.bottom; ViewPort.MinZ = 0; ViewPort.MaxZ = 1.0f; g_pd3dDevice->SetViewport(&ViewPort); LPDIRECT3DSURFACE9 lpSurface = NULL; LPDIRECT3DSURFACE9 lpSurfaceSave = NULL; if (m_bUseMotionBlur) { if (m_BufferTextureID) { g_pd3dDevice->GetRenderTarget(0,&lpSurfaceSave); lpSurfaceSave->Release(); LPTEXTURE pTexture = NULL; if (FAILED(g_cTextureTable.GetTexture(&pTexture,m_BufferTextureID))) { return E_FAIL; } if (FAILED(hr = pTexture->m_lpTexture->GetSurfaceLevel(0,&lpSurface))) return hr; lpSurface->Release(); g_pd3dDevice->SetRenderTarget(0,lpSurface); } } m_cCamera.zFar = 1000000; if (!m_bWindowed) { m_cCamera.Aspect = m_DisplayMode.Width*1.0f/m_DisplayMode.Height; } m_cCamera.SetCamera(); if (m_lpCurScene!=NULL) m_lpCurScene->SetLighting(); g_pd3dDevice->BeginScene(); g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0xFF606060, 1.0f, 0 ); if (m_lpCurScene!=NULL) m_lpCurScene->Render(); ////////////////////////////////////////////////////////////////////////// /*D3DXVECTOR3 V = D3DXVECTOR3(400*sinf(timeGetTime()*0.001f) , 100, 400*cosf(timeGetTime()*0.001f) -2000 ); g_cGraphicsTool.DrawLine(&(V+D3DXVECTOR3(0,35,0)),&(V+D3DXVECTOR3(0,-35,0)),0xffffffff,0xffff0000); g_cGraphicsTool.DrawLine(&(V+D3DXVECTOR3(35,0,0)),&(V+D3DXVECTOR3(-35,0,0)),0xffffffff,0xffff0000); g_cGraphicsTool.DrawLine(&(V+D3DXVECTOR3(0,0,35)),&(V+D3DXVECTOR3(0,0,-35)),0xffffffff,0xffff0000); */ g_pd3dDevice->EndScene(); if (m_bUseMotionBlur) { if (m_BufferTextureID) { g_pd3dDevice->SetRenderState(D3DRS_FOGENABLE,FALSE); g_pd3dDevice->SetRenderTarget(0,lpSurfaceSave); Render2SmallerTexture(); g_pd3dDevice->BeginScene(); g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x00000000, 1.0f, 0 ); D3DXVECTOR2 A(0,0),C((float)m_DisplayMode.Width,(float)m_DisplayMode.Height); float K = 3; D3DXVECTOR2 T1(5,5); D3DXVECTOR2 T2(-3,3); //g_cTextureTable.SetTexture(0,m_BufferTextureID); g_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE ,FALSE); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); //g_cGraphicsTool.DrawScreenRectNormal(&A,&C,0.0f,0x90FFFFFF,0x90FFFFFF,0x90FFFFFF,0x90FFFFFF); g_cGraphicsTool.DrawScreenRect(&A,&C,0.0f,0xFFFFFFFF,m_BufferTextureID); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE); if (m_lpCurScene!=NULL) { g_cGraphicsTool.DrawScreenRect(&(A-T1),&(C),0.0f,m_lpCurScene->m_Glow.m_AddColor,m_SmallerTextureID); } /*g_cGraphicsTool.DrawScreenRect(&(A-T1),&(C-T1),0.0f,0xFF808080,m_BufferTextureID); g_cGraphicsTool.DrawScreenRect(&(A+T1),&(C+T1),0.0f,0xFF808080,m_BufferTextureID); g_cGraphicsTool.DrawScreenRect(&(A-T2),&(C-T2),0.0f,0xFF808080,m_BufferTextureID); g_cGraphicsTool.DrawScreenRect(&(A+T2),&(C+T2),0.0f,0xFF808080,m_BufferTextureID);*/ //g_cGraphicsTool.DrawScreenRectDp3(&A,&C,0.0f,0xFF8F8F8F,m_BufferTextureID); g_pd3dDevice->EndScene(); } } if (m_bWindowed) { //只将backbuffer中的一个区域提交到窗口上 if(g_hRenderWnd) { g_pd3dDevice->Present( &m_RenderWindowRect,NULL, g_hRenderWnd, NULL ); } else { MessageBox(g_hBaseWnd,"不知道该渲染到什么地方","Error!",0); } } else { g_pd3dDevice->Present( NULL, NULL, g_hRenderWnd, NULL ); } g_dwRenderCount++; return TRUE; }
HRESULT KGraphicsEngine::Render2SmallerTexture() { LPTEXTURE pTexBuffer = NULL; LPTEXTURE pTexSmaller = NULL; LPTEXTURE pTexBlur = NULL; if (FAILED(g_cTextureTable.GetTexture(&pTexBuffer,m_BufferTextureID))) return E_FAIL; if (FAILED(g_cTextureTable.GetTexture(&pTexSmaller,m_SmallerTextureID))) return E_FAIL; if (FAILED(g_cTextureTable.GetTexture(&pTexBlur,m_BlurTextureID))) return E_FAIL; if(!pTexBuffer->m_lpTexture) return E_FAIL; if(!pTexSmaller->m_lpTexture) return E_FAIL; if(!pTexBlur->m_lpTexture) return E_FAIL; D3DSURFACE_DESC Desc; if(FAILED(pTexSmaller->m_lpTexture->GetLevelDesc(0,&Desc))) return E_FAIL; D3DSURFACE_DESC DescBlur; if(FAILED(pTexBlur->m_lpTexture->GetLevelDesc(0,&DescBlur))) return E_FAIL; LPDIRECT3DSURFACE9 lpSurfaceSmaller = NULL; if(FAILED(pTexSmaller->m_lpTexture->GetSurfaceLevel(0,&lpSurfaceSmaller))) return E_FAIL; lpSurfaceSmaller->Release(); LPDIRECT3DSURFACE9 lpSurfaceBlur = NULL; if(FAILED(pTexBlur->m_lpTexture->GetSurfaceLevel(0,&lpSurfaceBlur))) return E_FAIL; lpSurfaceBlur->Release(); D3DXMATRIX Mat_View_Save; D3DXMATRIX Mat_Proj_Save; LPDIRECT3DSURFACE9 lpSurfaceSave = NULL; D3DVIEWPORT9 ViewPort,ViewPortBlur,ViewPortSave; ////////////////////////////////////////////////////////////////////////// //Save back buffer & transform & viewport g_pd3dDevice->GetRenderTarget(0,&lpSurfaceSave); lpSurfaceSave->Release(); g_pd3dDevice->GetTransform( D3DTS_VIEW, &Mat_View_Save ); g_pd3dDevice->GetTransform( D3DTS_PROJECTION, &Mat_Proj_Save ); g_pd3dDevice->GetViewport(&ViewPortSave); ////////////////////////////////////////////////////////////////////////// ViewPort.X = 0;ViewPort.Y = 0; ViewPort.Width = Desc.Width; ViewPort.Height = Desc.Height; ViewPort.MinZ = 0;ViewPort.MaxZ = 1.0f; ViewPortBlur.X = 0;ViewPortBlur.Y = 0; ViewPortBlur.Width = DescBlur.Width; ViewPortBlur.Height = DescBlur.Height; ViewPortBlur.MinZ = 0;ViewPortBlur.MaxZ = 1.0f; D3DXVECTOR2 A(0,0); D3DXVECTOR2 C((float)Desc.Width,(float)Desc.Height); D3DXVECTOR2 A1(0,0); D3DXVECTOR2 C1((float)DescBlur.Width,(float)DescBlur.Height); ////////////////////////////////////////////////////////////////////////// g_pd3dDevice->SetViewport(&ViewPort); g_pd3dDevice->SetRenderTarget(0,lpSurfaceSmaller); ////////////////////////////////////////////////////////////////////////// //reade began to render //g_pd3dDevice->BeginScene(); g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x00000000, 1.0f, 0 ); g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); float K = 2; D3DXVECTOR2 T1( K,K); D3DXVECTOR2 T2(-K,K); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); DWORD Color = m_lpCurScene->m_Glow.m_GateColor; g_cTextureTable.SetTexture(0,m_BufferTextureID); g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SUBTRACT); g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE); g_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE); /*g_cTextureTable.SetTexture(1,m_BufferTextureID); g_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_MODULATE2X); g_pd3dDevice->SetTextureStageState(1,D3DTSS_COLORARG1,D3DTA_TEXTURE); g_pd3dDevice->SetTextureStageState(1,D3DTSS_COLORARG2,D3DTA_CURRENT);*/ g_cGraphicsTool.DrawScreenRectNormal(&A,&C,0.0f,Color,Color,Color,Color); //g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); //g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE); //g_cGraphicsTool.DrawScreenRect(&A,&C,0.0f,Color,m_SmallerTextureID); //g_cGraphicsTool.DrawScreenRect(&(A-T1),&(C+T1),0.0f,Color,m_SmallerTextureID); //g_cGraphicsTool.DrawScreenRect(&A,&C,0.0f,Color,m_BufferTextureID); /*g_cGraphicsTool.DrawScreenRect(&(A-T1),&(C-T1),0.0f,Color,m_BufferTextureID); g_cGraphicsTool.DrawScreenRect(&(A+T1),&(C+T1),0.0f,Color,m_BufferTextureID); g_cGraphicsTool.DrawScreenRect(&(A-T2),&(C-T2),0.0f,Color,m_BufferTextureID); g_cGraphicsTool.DrawScreenRect(&(A+T2),&(C+T2),0.0f,Color,m_BufferTextureID);*/ if(1) { ////////////////////////////////////////////////////////////////////////// Color = 0x30FFFFFF; for(int i=0;i<8;i++) { g_pd3dDevice->SetViewport(&ViewPortBlur); g_pd3dDevice->SetRenderTarget(0,lpSurfaceBlur); //if(i==0) { g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x00000000, 1.0f, 0 ); } g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); g_cGraphicsTool.DrawScreenRect(&A1,&C1,0.0f,0xFFFFFFFF,m_SmallerTextureID); g_pd3dDevice->SetViewport(&ViewPort); g_pd3dDevice->SetRenderTarget(0,lpSurfaceSmaller); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_BLENDFACTOR); g_pd3dDevice->SetRenderState(D3DRS_TEXTUREFACTOR,0x60606060); g_cGraphicsTool.DrawScreenRect(&(A-T1),&(C+T1),0.0f,Color,m_BlurTextureID); } } else { //g_pd3dDevice->SetRenderState(D3DRS_TEXTUREFACTOR,0x60606060); for(int i=0;i<10;i++) { g_pd3dDevice->SetViewport(&ViewPortBlur); g_pd3dDevice->SetRenderTarget(0,lpSurfaceBlur); g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x00000000, 1.0f, 0 ); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); g_cGraphicsTool.DrawScreenRect(&A1,&C1,0.0f,0xFFFFFFFF,m_SmallerTextureID); g_pd3dDevice->SetViewport(&ViewPort); g_pd3dDevice->SetRenderTarget(0,lpSurfaceSmaller); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE); Color = 0x00FFFFFF|(255-i*25)<<24; float K = 5; T1 = D3DXVECTOR2(K*i,K*i); T2 = D3DXVECTOR2(-K*i,K*i); g_cGraphicsTool.DrawScreenRect(&(A-T2),&(C-T2),0.0f,Color,m_BlurTextureID); /*g_cGraphicsTool.DrawScreenRect(&(A+T1),&(C+T1),0.0f,Color,m_BlurTextureID); g_cGraphicsTool.DrawScreenRect(&(A-T2),&(C-T2),0.0f,Color,m_BlurTextureID); g_cGraphicsTool.DrawScreenRect(&(A+T2),&(C+T2),0.0f,Color,m_BlurTextureID);*/ } /*g_pd3dDevice->SetViewport(&ViewPortBlur); g_pd3dDevice->SetRenderTarget(0,lpSurfaceBlur); g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0x00000000, 1.0f, 0 ); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); g_cGraphicsTool.DrawScreenRect(&A1,&C1,0.0f,0xFFFFFFFF,m_SmallerTextureID); g_pd3dDevice->SetViewport(&ViewPort); g_pd3dDevice->SetRenderTarget(0,lpSurfaceSmaller); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_BLENDFACTOR); g_pd3dDevice->SetRenderState(D3DRS_TEXTUREFACTOR,0x60606060);*/ Color = 0x30FFFFFF; g_cGraphicsTool.DrawScreenRect(&(A),&(C),0.0f,Color,m_BlurTextureID); } //g_pd3dDevice->EndScene(); ////////////////////////////////////////////////////////////////////////// //Restore g_pd3dDevice->SetTransform( D3DTS_VIEW, &Mat_View_Save ); g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &Mat_Proj_Save ); g_pd3dDevice->SetRenderTarget(0,lpSurfaceSave); g_pd3dDevice->SetViewport(&ViewPortSave); g_cTextureTable.SetTexture(1,NULL); g_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE); g_pd3dDevice->SetTextureStageState(1,D3DTSS_COLORARG1,D3DTA_TEXTURE); g_pd3dDevice->SetTextureStageState(1,D3DTSS_COLORARG2,D3DTA_CURRENT); return S_OK; }
//************************************************************************************************************* void Render(float alpha, float elapsedtime) { static float time = 0; D3DXMATRIX inv; D3DXVECTOR3 axis(0, 1, 0); D3DXVECTOR4 texelsize(1.0f / (float)screenwidth, 1.0f / (float)screenheight, 0, 1); LPDIRECT3DSURFACE9 oldtarget = NULL; time += elapsedtime; D3DXMatrixRotationAxis(&inv, &axis, time); D3DXMatrixScaling(&world, 0.3f, 0.3f, 0.3f); //D3DXMatrixScaling(&world, 0.6f, 0.6f, 0.6f); D3DXMatrixMultiply(&world, &world, &inv); D3DXMatrixInverse(&inv, NULL, &world); device->SetTexture(0, texture); device->SetTexture(1, intensity); effect->SetMatrix("matWorld", &world); effect->SetMatrix("matWorldInv", &inv); effect->SetMatrix("matView", &view); effect->SetMatrix("matProj", &proj); if( useedgedetect ) { device->GetRenderTarget(0, &oldtarget); device->SetRenderTarget(0, colorsurface); device->SetRenderTarget(1, normalsurface); } device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL, 0xff6694ed, 1.0f, 0); if( SUCCEEDED(device->BeginScene()) ) { // draw scene + normals/depth effect->SetTechnique("celshading"); effect->Begin(NULL, 0); effect->BeginPass(0); { mesh->DrawSubset(0); } effect->EndPass(); effect->End(); if( useedgedetect ) { // edge detection device->SetVertexDeclaration(vertexdecl); device->SetRenderTarget(0, edgesurface); device->SetRenderTarget(1, NULL); device->SetTexture(0, normaltarget); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); effect->SetTechnique("edgedetect"); effect->SetVector("texelSize", &texelsize); effect->Begin(NULL, 0); effect->BeginPass(0); { device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, vertices, sizeof(D3DXVECTOR4) + sizeof(D3DXVECTOR2)); } effect->EndPass(); effect->End(); // put together device->SetRenderTarget(0, oldtarget); device->SetTexture(0, colortarget); device->SetTexture(2, edgetarget); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); oldtarget->Release(); effect->SetTechnique("final"); effect->Begin(NULL, 0); effect->BeginPass(0); { device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, vertices, sizeof(D3DXVECTOR4) + sizeof(D3DXVECTOR2)); } effect->EndPass(); effect->End(); } else { D3DXMATRIX offproj; // use the stencil buffer device->SetRenderState(D3DRS_COLORWRITEENABLE, 0); device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); device->SetRenderState(D3DRS_STENCILENABLE, TRUE); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); device->SetRenderState(D3DRS_STENCILREF, 1); device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); device->SetTransform(D3DTS_WORLD, &world); device->SetTransform(D3DTS_VIEW, &view); float thickness = 3.5f; // render object 4 times with offseted frustum for( float i = -thickness; i < thickness + 1; i += 2 * thickness ) { for( float j = -thickness; j < thickness + 1; j += 2 * thickness ) { D3DXMatrixTranslation(&offproj, i / (float)screenwidth, j / (float)screenheight, 0); D3DXMatrixMultiply(&offproj, &proj, &offproj); device->SetTransform(D3DTS_PROJECTION, &offproj); mesh->DrawSubset(0); } } // erase area in the center device->SetRenderState(D3DRS_STENCILREF, 0); device->SetTransform(D3DTS_PROJECTION, &proj); mesh->DrawSubset(0); // now render outlines device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_ALPHA); device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_NOTEQUAL); device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP); device->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP); device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_CONSTANT); device->SetTextureStageState(0, D3DTSS_CONSTANT, 0); { device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, vertices, sizeof(D3DXVECTOR4) + sizeof(D3DXVECTOR2)); } device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); device->SetRenderState(D3DRS_STENCILENABLE, FALSE); } device->SetTexture(2, NULL); // render text device->SetFVF(D3DFVF_XYZRHW|D3DFVF_TEX1); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetTexture(0, text); device->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 2, textvertices, 6 * sizeof(float)); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->SetTexture(0, NULL); device->EndScene(); } device->Present(NULL, NULL, NULL, NULL); }
bool ReInitResourceDX9(void) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); GutGetWindowSize(g_iWidth, g_iHeight); g_proj_matrix = GutMatrixOrthoRH_DirectX(g_iWidth, g_iHeight, 0.0f, 100.0f); int w = g_iWidth; int h = g_iHeight; g_ImageInfo.m_iWidth = g_iWidth; g_ImageInfo.m_iHeight = g_iHeight; g_ImageInfo.m_bProcedure = true; //D3DFORMAT fmt = D3DFMT_G16R16; D3DFORMAT fmt = D3DFMT_A8R8G8B8; device->SetRenderState(D3DRS_LIGHTING, FALSE); device->CreateTexture( w, h, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pTextures[TEX_HEIGHT0], NULL); if ( NULL==g_pTextures[TEX_HEIGHT0] ) return false; g_pTextures[TEX_HEIGHT0]->GetSurfaceLevel(0, &g_pSurfaces[TEX_HEIGHT0]); if ( NULL==g_pSurfaces[TEX_HEIGHT0] ) return false; device->CreateTexture( w, h, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pTextures[TEX_HEIGHT1], NULL); if ( NULL==g_pTextures[TEX_HEIGHT1] ) return false; if ( NULL==g_pTextures[TEX_HEIGHT1] ) return false; g_pTextures[TEX_HEIGHT1]->GetSurfaceLevel(0, &g_pSurfaces[TEX_HEIGHT1]); device->CreateTexture( w, h, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pTextures[TEX_HEIGHT2], NULL); if ( NULL==g_pTextures[TEX_HEIGHT2] ) return false; g_pTextures[TEX_HEIGHT2]->GetSurfaceLevel(0, &g_pSurfaces[TEX_HEIGHT2]); if ( NULL==g_pSurfaces[TEX_HEIGHT2] ) return false; device->CreateTexture( w, h, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pTextures[TEX_NORMAL], NULL); if ( NULL==g_pTextures[TEX_NORMAL] ) return false; g_pTextures[TEX_NORMAL]->GetSurfaceLevel(0, &g_pSurfaces[TEX_NORMAL]); if ( NULL==g_pSurfaces[TEX_NORMAL] ) return false; device->GetRenderTarget(0, &g_pMainFramebuffer); device->GetDepthStencilSurface(&g_pMainDepthbuffer); device->SetRenderTarget(0, g_pSurfaces[0]); device->SetRenderTarget(1, g_pSurfaces[1]); device->SetRenderTarget(2, g_pSurfaces[2]); device->Clear(0, NULL, D3DCLEAR_TARGET, 0x0, 1.0f, 0); device->SetRenderTarget(0, g_pMainFramebuffer); device->SetRenderTarget(1, NULL); device->SetRenderTarget(2, NULL); device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE); device->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL); device->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL); return true; }
HRESULT KG3DGraphicsEngine::ScreenShotImpl() { HRESULT hResult = E_FAIL; HRESULT hRetCode = E_FAIL; IDirect3DSurface9* pFrameSurface = NULL; IDirect3DTexture9* pTextureSys = NULL; ID3DXBuffer* pBuffer = NULL; D3DVIEWPORT9 ViewPort; RECT RectView; ASSERT(m_eShotImageType != D3DXIFF_FORCE_DWORD); hRetCode = g_pd3dDevice->GetRenderTarget(0, &pFrameSurface); KG_COM_PROCESS_ERROR(hRetCode); g_pd3dDevice->GetViewport(&ViewPort); SetRect(&RectView, ViewPort.X, ViewPort.Y, ViewPort.X + ViewPort.Width, ViewPort.Y + ViewPort.Height); if (m_eShotImageType == D3DXIFF_JPG) { D3DSURFACE_DESC SurfaceDesc; D3DLOCKED_RECT LockRect; hRetCode = D3DXSaveSurfaceToFileInMemory(&pBuffer, D3DXIFF_BMP, pFrameSurface, NULL, &RectView); KG_COM_PROCESS_ERROR(hRetCode); hRetCode = D3DXCreateTextureFromFileInMemoryEx(g_pd3dDevice, pBuffer->GetBufferPointer(), pBuffer->GetBufferSize(), ViewPort.Width, ViewPort.Height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pTextureSys); KG_COM_PROCESS_ERROR(hRetCode); /* hRetCode = D3DXSaveSurfaceToFile(TEXT("__temp_shot.bmp"), D3DXIFF_BMP, pFrameSurface, NULL, &RectView); KG_COM_PROCESS_ERROR(hRetCode); hRetCode = D3DXCreateTextureFromFileEx(g_pd3dDevice, TEXT("__temp_shot.bmp"), ViewPort.Width, ViewPort.Height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pTextureSys); KG_COM_PROCESS_ERROR(hRetCode); */ pTextureSys->GetLevelDesc(0, &SurfaceDesc); if (SUCCEEDED(pTextureSys->LockRect(0, &LockRect, NULL, D3DLOCK_NOSYSLOCK))) { struct _Rgba { BYTE r; BYTE g; BYTE b; BYTE a;}; struct _Rgb { BYTE b; BYTE g; BYTE r; }; _Rgb* pRgb = new _Rgb[SurfaceDesc.Height * SurfaceDesc.Width]; for (UINT v = 0; v < SurfaceDesc.Height; ++v) { _Rgba* pRgba = (_Rgba*)((BYTE*)LockRect.pBits + LockRect.Pitch * v); for (UINT u = 0; u < SurfaceDesc.Width; ++u) { pRgb[v * SurfaceDesc.Width + u].r = pRgba[u].r; pRgb[v * SurfaceDesc.Width + u].g = pRgba[u].g; pRgb[v * SurfaceDesc.Width + u].b = pRgba[u].b; } } JpegFile::RGBToJpegFile(m_szShotPath, (BYTE*)pRgb, SurfaceDesc.Width, SurfaceDesc.Height, TRUE, m_nShotImageQuty); delete[] pRgb; pTextureSys->UnlockRect(0); } else { KG_PROCESS_ERROR(FALSE); } } else { hRetCode = D3DXSaveSurfaceToFile(m_szShotPath, m_eShotImageType, pFrameSurface, NULL, &RectView); KG_COM_PROCESS_ERROR(hRetCode); } hResult = S_OK; Exit0 : SAFE_RELEASE(pBuffer); SAFE_RELEASE(pTextureSys); SAFE_RELEASE(pFrameSurface); return hResult; }
bool ReInitResourceDX9(void) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); device->SetRenderState(D3DRS_LIGHTING, FALSE); int w, h; GutGetWindowSize(w, h); g_ImageInfo.m_iWidth = w; g_ImageInfo.m_iHeight = h; float aspect = (float)h/(float)w; g_proj_matrix = GutMatrixPerspectiveRH_DirectX(g_fFovW, aspect, g_fNearZ, g_fFarZ); device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *)&g_proj_matrix); int width = w; int height = h; D3DFORMAT fmt = D3DFMT_A16B16G16R16F; device->CreateTexture(width, height, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pFrameBuffer[FULLSIZE], NULL); device->CreateTexture(256, 256, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pFrameBuffer[DOWNSAMPLED_256x256], NULL); device->CreateTexture(64, 64, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pFrameBuffer[DOWNSAMPLED_64x64], NULL); device->CreateTexture(16, 16, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pFrameBuffer[DOWNSAMPLED_16x16], NULL); device->CreateTexture(4, 4, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pFrameBuffer[DOWNSAMPLED_4x4], NULL); device->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pFrameBuffer[LUMINANCE_PREVIOUS], NULL); device->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pFrameBuffer[LUMINANCE_CURRENT], NULL); device->CreateTexture(1, 1, 1, D3DUSAGE_RENDERTARGET, fmt, D3DPOOL_DEFAULT, &g_pFrameBuffer[LUMINANCE_TEMP], NULL); for ( int i=0; i<NUM_FRAMEBUFFERS; i++ ) { if ( g_pFrameBuffer[i]==NULL ) return false; g_pFrameBuffer[i]->GetSurfaceLevel(0, &g_pFrameSurface[i]); } for ( int i=0; i<4; i++ ) { device->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); device->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); device->SetSamplerState(i, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); device->SetSamplerState(i, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); } device->GetRenderTarget(0, &g_pMainSurface); g_pMainSurface->Release(); device->GetDepthStencilSurface(&g_pDepthStencilSurface); g_pDepthStencilSurface->Release(); return true; }
HRESULT CProteinVistaRenderer::SaveScreenImage ( LPCSTR fileName, long imageWidth, long imageHeight , D3DXIMAGE_FILEFORMAT format) { HRESULT hr; LPDIRECT3DDEVICE9 pDev = GetD3DDevice(); BOOL bChangeSize = TRUE; if ( imageWidth == -1 || imageHeight == -1 ) { bChangeSize = FALSE; imageWidth = m_d3dpp.BackBufferWidth; imageHeight = m_d3dpp.BackBufferHeight; } // 홀수이면 짝수로 바꾼다. if ( imageWidth/2 != (imageWidth+1)/2 ) { imageWidth = (imageWidth/2)*2; bChangeSize = TRUE; } if ( imageHeight/2 != (imageHeight+1)/2 ) { imageHeight = (imageHeight/2)*2; bChangeSize = TRUE; } long imageWidthOld = m_d3dpp.BackBufferWidth; long imageHeightOld = m_d3dpp.BackBufferHeight; if ( bChangeSize == TRUE ) { m_d3dpp.BackBufferWidth = imageWidth; m_d3dpp.BackBufferHeight = imageHeight; m_d3dSettings.Windowed_Width = m_d3dpp.BackBufferWidth; m_d3dSettings.Windowed_Height = m_d3dpp.BackBufferHeight; if( FAILED( hr = Reset3DEnvironment() ) ) { if( hr == D3DERR_DEVICELOST ) { m_bDeviceLost = true; } else { return E_FAIL; } } } LPDIRECT3DSURFACE9 renderTargetOrig = NULL; hr = pDev->GetRenderTarget(0,&renderTargetOrig); LPDIRECT3DSURFACE9 renderTargetCapture; // 캡쳐된 내용이 들어갈 서피스 D3DSURFACE_DESC desc; renderTargetOrig->GetDesc(&desc); pDev->CreateRenderTarget(desc.Width, desc.Height, desc.Format, desc.MultiSampleType , desc.MultiSampleQuality, FALSE , &renderTargetCapture, NULL); pDev->SetRenderTarget(0, renderTargetCapture); pDev->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1, 0); // Render(); pDev->SetRenderTarget(0, renderTargetOrig); SAFE_RELEASE(renderTargetOrig); // save the image to specified file CString filename(fileName); hr=D3DXSaveSurfaceToFile(filename,format,renderTargetCapture,NULL,NULL); if ( FAILED(hr) ) return hr; SAFE_RELEASE(renderTargetCapture); // // return status of save to caller // if ( bChangeSize == TRUE ) { m_d3dpp.BackBufferWidth = imageWidthOld; m_d3dpp.BackBufferHeight = imageHeightOld; m_d3dSettings.Windowed_Width = m_d3dpp.BackBufferWidth; m_d3dSettings.Windowed_Height = m_d3dpp.BackBufferHeight; if( FAILED( hr = Reset3DEnvironment() ) ) { if( hr == D3DERR_DEVICELOST ) { m_bDeviceLost = true; } else { return E_FAIL; } } Render3DEnvironment(); } return hr; }
static LPDIRECT3DTEXTURE9 BlurTexture(LPDIRECT3DTEXTURE9 pSource) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); Matrix4x4 identiyMatrix = Matrix4x4::IdentityMatrix(); device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &identiyMatrix); device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &identiyMatrix); device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &identiyMatrix); LPDIRECT3DSURFACE9 pFrameBufferBackup, pDepthBufferBackup; device->GetRenderTarget(0, &pFrameBufferBackup); pFrameBufferBackup->Release(); device->GetDepthStencilSurface(&pDepthBufferBackup); pDepthBufferBackup->Release(); device->SetRenderState(D3DRS_CULLMODE , D3DCULL_NONE); float weight_table[7] = {1.0f, 3.0f, 4.0f, 5.0f, 4.0f, 3.0f, 1.0f}; float uv_offset_table[7] = {-3.0f, -2.0f, -1.0f, 0.0f, 1.0f, 2.0f, 3.0f}; float sum = 0.0f; for ( int i=0; i<7; i++ ) { sum += weight_table[i]; } for ( int i=0; i<7; i++ ) { weight_table[i] /= sum; } Vector4 vOneTexel_X(1.0f/(float)g_framebuffer_w, 0.0f, 0.0f, 0.0f); Vector4 vOneTexel_Y(0.0f, 1.0f/(float)g_framebuffer_h, 0.0f, 0.0f); Vertex_VT temp_quad[4]; LPDIRECT3DSURFACE9 pSurface; g_pBlurTextures[0]->GetSurfaceLevel(0, &pSurface); device->SetRenderTarget(0, pSurface); device->SetDepthStencilSurface(NULL); device->SetRenderState(D3DRS_ZENABLE, FALSE); device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 0), 1.0f, 0); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); device->SetTexture(0, pSource); device->SetFVF(D3DFVF_XYZW|D3DFVF_TEX1); // 設定顏色來源為 texture factor * texture device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR); device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); // X方向blur for ( int i=0; i<7; i++ ) { Vector4 vOffset = uv_offset_table[i] * vOneTexel_X; for ( int p=0; p<4; p++ ) { temp_quad[p].m_Position = g_Quad[p].m_Position; temp_quad[p].m_Texcoord = g_Quad[p].m_Texcoord + vOffset; } int weight = (int) (weight_table[i] * 255); device->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_RGBA(weight, weight, weight, weight)); device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, temp_quad, sizeof(Vertex_VT)); } pSurface->Release(); g_pBlurTextures[1]->GetSurfaceLevel(0, &pSurface); device->SetRenderTarget(0, pSurface); device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 0), 1.0f, 0); device->SetTexture(0, g_pBlurTextures[0]); // Y方向blur for ( int i=0; i<7; i++ ) { Vector4 vOffset = uv_offset_table[i] * vOneTexel_Y; for ( int p=0; p<4; p++ ) { temp_quad[p].m_Position = g_Quad[p].m_Position; temp_quad[p].m_Texcoord = g_Quad[p].m_Texcoord + vOffset; } int weight = (int) (weight_table[i] * 255); device->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_RGBA(weight, weight, weight, weight)); device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, temp_quad, sizeof(Vertex_VT)); } pSurface->Release(); // 還原設定 device->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_RGBA(255, 255, 255, 255)); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT); device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); device->SetRenderState(D3DRS_ZENABLE, TRUE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderTarget(0, pFrameBufferBackup); device->SetDepthStencilSurface(pDepthBufferBackup); return g_pBlurTextures[1]; }
void RenderWithIrregularPCF( const D3DXMATRIX& viewproj, const D3DXVECTOR3& eye, const D3DXMATRIX& lightview, const D3DXMATRIX& lightproj, const D3DXVECTOR4& lightpos, const D3DXVECTOR4& clipplanes, const D3DXVECTOR4& texelsize) { LPDIRECT3DSURFACE9 oldsurface = NULL; LPDIRECT3DSURFACE9 shadowsurface = NULL; D3DXVECTOR4 noisesize(16.0f, 16.0f, 0, 1); // STEP 1: render shadow map shadowmap->GetSurfaceLevel(0, &shadowsurface); device->GetRenderTarget(0, &oldsurface); device->SetRenderTarget(0, shadowsurface); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); //device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); shadowsurface->Release(); pcfirreg->SetTechnique("shadowmap"); pcfirreg->SetMatrix("lightView", &lightview); pcfirreg->SetMatrix("lightProj", &lightproj); pcfirreg->SetVector("clipPlanes", &clipplanes); pcfirreg->Begin(NULL, 0); pcfirreg->BeginPass(0); { RenderScene(pcfirreg, 1); // caster RenderScene(pcfirreg, 3); // caster & receiver } pcfirreg->EndPass(); pcfirreg->End(); // STEP 2: render scene //device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW); device->SetRenderTarget(0, oldsurface); device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xff6694ed, 1.0f, 0); device->SetRenderState(D3DRS_SRGBWRITEENABLE, TRUE); oldsurface->Release(); device->SetTexture(1, shadowmap); device->SetTexture(2, noise); pcfirreg->SetTechnique("irregular_light"); //pcfirreg->SetTechnique("irregular_screen"); pcfirreg->SetMatrix("matViewProj", &viewproj); pcfirreg->SetMatrix("lightView", &lightview); pcfirreg->SetMatrix("lightProj", &lightproj); pcfirreg->SetVector("clipPlanes", &clipplanes); pcfirreg->SetVector("lightPos", &lightpos); pcfirreg->SetVector("eyePos", (D3DXVECTOR4*)&eye); pcfirreg->SetVector("texelSize", &texelsize); pcfirreg->SetVector("noiseSize", &noisesize); pcfirreg->Begin(NULL, 0); pcfirreg->BeginPass(0); { RenderScene(pcfirreg, 2); // receiver RenderScene(pcfirreg, 3); // caster & receiver } pcfirreg->EndPass(); pcfirreg->End(); device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); }
// 使用DirectX 9來繪圖 void RenderFrameDX9(void) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); Vector4 vPlane(0.0f, 0.0f, 1.0f, -g_mirror_z); // 開始下繪圖指令 device->BeginScene(); { LPDIRECT3DSURFACE9 pFrameBufferBackup, pDepthBufferBackup; device->GetRenderTarget(0, &pFrameBufferBackup); pFrameBufferBackup->Release(); device->GetDepthStencilSurface(&pDepthBufferBackup); pDepthBufferBackup->Release(); LPDIRECT3DSURFACE9 pSurface; g_pTexture->GetSurfaceLevel(0, &pSurface); device->SetRenderTarget(0, pSurface); device->SetDepthStencilSurface(g_pDepthStencil); device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 200, 255), 1.0f, 0); RenderModelDX9(true, &vPlane); pSurface->Release(); device->SetRenderTarget(0, pFrameBufferBackup); device->SetDepthStencilSurface(pDepthBufferBackup); } // 把上一個步驟的結果當成貼圖來使用 { // 消除畫面 device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 150, 255), 1.0f, 0); RenderModelDX9(false, NULL); Matrix4x4 identMat; identMat.Identity(); device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &identMat); sModelMaterial_DX9 material; material.m_pTextures[0] = g_pTexture; material.Submit(); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_NONE); Matrix4x4 uv_offset_matrix; uv_offset_matrix.Scale_Replace(0.5f, -0.5f, 1.0f); uv_offset_matrix[3].Set(0.5f, 0.5f, 0.5f, 1.0f); Matrix4x4 inv_view_matrix = g_Control.GetViewMatrix(); inv_view_matrix.FastInvert(); Matrix4x4 texture_matrix = inv_view_matrix * g_mirror_view_matrix * g_projection_matrix * uv_offset_matrix; device->SetTransform(D3DTS_TEXTURE0, (D3DMATRIX *) &texture_matrix); device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION); // D3DTTFF_PROJECTED告知direct3d裝置texcoord需要除以w device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4|D3DTTFF_PROJECTED); float v[12]; for ( int i=0; i<4; i++ ) { g_Quad[i].m_Position.StoreXYZ(&v[i*3]); } // 畫出矩形 device->SetFVF(D3DFVF_XYZ); //device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Quad, sizeof(Vertex_V)); device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, v, 12); } // 宣告所有的繪圖指令都下完了 device->EndScene(); // 把背景backbuffer的畫面呈現出來 device->Present( NULL, NULL, NULL, NULL ); }
// `使用Direct3D9來繪圖` void RenderFrameDX9(void) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); Matrix4x4 light_projection_matrix; Matrix4x4 light_view_matrix; Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 world_matrix = g_Control.GetObjectMatrix(); // `開始下繪圖指令` device->BeginScene(); // `把剪影畫在動態貼圖中` if(1) { //! 设置临时framebuffer LPDIRECT3DSURFACE9 pFrameBufferBackup, pDepthBufferBackup; device->GetRenderTarget(0, &pFrameBufferBackup); pFrameBufferBackup->Release(); device->GetDepthStencilSurface(&pDepthBufferBackup); pDepthBufferBackup->Release(); LPDIRECT3DSURFACE9 pSurface; g_pTexture->GetSurfaceLevel(0, &pSurface); device->SetRenderTarget(0, pSurface); device->SetDepthStencilSurface(g_pDepthStencil); device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(255, 255, 255, 255), 1.0f, 0); Vector4 vLightPos = g_Light.m_Position; Vector4 vLightUp(0.0f, 1.0f, 0.0f); Vector4 vLightLookat(0.0f, 0.0f, 0.0f); // `把鏡頭放在光源的` light_projection_matrix = GutMatrixPerspectiveRH_DirectX(60.0f, 1.0f, 0.1f, 100.0f); light_view_matrix = GutMatrixLookAtRH(vLightPos, vLightLookat, vLightUp); // `設定轉換矩陣` device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *)&light_projection_matrix); device->SetTransform(D3DTS_VIEW, (D3DMATRIX *)&light_view_matrix); device->SetTransform(D3DTS_WORLD, (D3DMATRIX *)&world_matrix); D3DCOLORVALUE zero = {0.0f, 0.0f, 0.0f, 1.0f}; sModelMaterial_DX9 material; material.m_Material.Ambient = zero; material.m_Material.Emissive = zero; material.m_Material.Diffuse = zero; material.m_Material.Specular = zero; material.m_bCullFace = false; material.Submit(); device->SetRenderState(D3DRS_LIGHTING, 0); g_Model_DX9.Render(0); pSurface->Release(); device->SetRenderTarget(0, pFrameBufferBackup); device->SetDepthStencilSurface(pDepthBufferBackup); } // `把上一個步驟的結果當成貼圖來使用` { // `消除畫面` device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 150, 255), 1.0f, 0); // `設定轉換矩陣` device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix); device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix); device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix); #define DRAW_TEAPORT_FIRST 1 //! 画茶壶 if (DRAW_TEAPORT_FIRST) { SetupLightingDX9(); g_Model_DX9.Render(); } //! 设置矩形的纹理 sModelMaterial_DX9 material; material.m_pTextures[0] = g_pTexture; material.Submit(); Matrix4x4 inv_view_matrix = view_matrix; inv_view_matrix.FastInvert(); Matrix4x4 uv_offset_matrix; uv_offset_matrix.Identity(); uv_offset_matrix.Scale(0.5f, -0.5f, 0.5f); uv_offset_matrix[3].Set(0.5f, 0.5f, 0.5f, 1.0f); Matrix4x4 texture_matrix = inv_view_matrix * light_view_matrix * light_projection_matrix * uv_offset_matrix; Matrix4x4 indent_matrix = Matrix4x4::IdentityMatrix(); device->SetTransform(D3DTS_TEXTURE0, (D3DMATRIX *) &texture_matrix); device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &indent_matrix); device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION); device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4 | D3DTTFF_PROJECTED ); device->SetRenderState(D3DRS_LIGHTING, FALSE); // `畫出矩形` device->SetFVF(D3DFVF_XYZ); device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Quad, sizeof(Vertex_VT)); //! 关闭纹理矩阵 device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); if (!DRAW_TEAPORT_FIRST) { //SetupLightingDX9(); device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix); g_Model_DX9.Render(); } } // `宣告所有的繪圖指令都下完了` device->EndScene(); // `把背景backbuffer的畫面呈現出來` device->Present( NULL, NULL, NULL, NULL ); }
void initD3D() { d3d = Direct3DCreate9(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS PresentParams; memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS)); PresentParams.Windowed = TRUE; PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD; /////////////////////////////////////////////////////////////////////// // Set default settings UINT AdapterToUse = D3DADAPTER_DEFAULT; D3DDEVTYPE DeviceType = D3DDEVTYPE_HAL; #ifdef SHIPPING_VERSION if(FAILED(d3d->CreateDevice(AdapterToUse, DeviceType, hMainWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &PresentParams, &device))) { MessageBoxA(hMainWnd, "Unable to initialize PerfectHUD", 0, MB_ICONHAND); terminate(); } #else // Look for 'NVIDIA PerfHUD' adapter // If it is present, override default settings for (UINT Adapter = 0; Adapter < d3d->GetAdapterCount(); Adapter++) { D3DADAPTER_IDENTIFIER9 Identifier; HRESULT Res; Res = d3d->GetAdapterIdentifier(Adapter, 0, &Identifier); if (strstr(Identifier.Description, "PerfHUD") != 0) { AdapterToUse = Adapter; DeviceType = D3DDEVTYPE_REF; break; } } #endif ////////////////////////////////////////////////////////////////////// device->SetRenderState(D3DRS_POINTSIZE_MAX, *((DWORD*)&pointSize)); device->SetRenderState(D3DRS_POINTSIZE, *((DWORD*)&pointSize)); device->SetRenderState(D3DRS_LIGHTING, FALSE); device->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); device->SetRenderState(D3DRS_ZENABLE, FALSE); D3DXCreateTextureFromFile(device, L"particle.png", &particleTexture); D3DXCreateTextureFromFile(device, L"overlay.png", &overlayTexture); D3DXCreateTexture(device, Width, Height, 0, D3DUSAGE_RENDERTARGET, D3DFMT_X8B8G8R8, D3DPOOL_DEFAULT, &renderTexture); renderTexture->GetSurfaceLevel(0, &renderTarget); device->GetRenderTarget(0, &orig); //»нициализаци¤ матриц D3DXMATRIX matrixView; D3DXMATRIX matrixProjection; D3DXMatrixLookAtLH( &matrixView, &D3DXVECTOR3(0, 0, 0), &D3DXVECTOR3(0, 0, 1), &D3DXVECTOR3(0, 1, 0)); D3DXMatrixOrthoOffCenterLH(&matrixProjection, 0, Width, Height, 0, 0, 255); device->SetTransform(D3DTS_VIEW, &matrixView); device->SetTransform(D3DTS_PROJECTION, &matrixProjection); device->SetTexture(0, particleTexture); }
// 使用DirectX 9來繪圖 void RenderFrameDX9(void) { LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); LPDIRECT3DTEXTURE9 pBlurredTexture = NULL; Matrix4x4 light_projection_matrix; Matrix4x4 light_view_matrix; Matrix4x4 view_matrix = g_Control.GetViewMatrix(); Matrix4x4 world_matrix = g_Control.GetObjectMatrix(); // 開始下繪圖指令 device->BeginScene(); { // 保存主framebuffer LPDIRECT3DSURFACE9 pFrameBufferBackup, pDepthBufferBackup; device->GetRenderTarget(0, &pFrameBufferBackup); pFrameBufferBackup->Release(); device->GetDepthStencilSurface(&pDepthBufferBackup); pDepthBufferBackup->Release(); // 取出動態貼圖中的surface LPDIRECT3DSURFACE9 pSurface; g_pTexture->GetSurfaceLevel(0, &pSurface); // 把繪圖結果輸出到動態貼圖中 device->SetRenderTarget(0, pSurface); device->SetDepthStencilSurface(g_pDepthStencil); // 清除畫面 device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(255, 255, 255, 255), 1.0f, 0); // 設定光源位置 Vector4 vLightPos = g_Light.m_Position; Vector4 vLightUp(0.0f, 1.0f, 0.0f); Vector4 vLightLookat(0.0f, 0.0f, 0.0f); light_projection_matrix = GutMatrixPerspectiveRH_DirectX(60.0f, 1.0f, 0.1f, 100.0f); light_view_matrix = GutMatrixLookAtRH(vLightPos, vLightLookat, vLightUp); // 把鏡頭放到光源位置來畫陰影 device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *)&light_projection_matrix); device->SetTransform(D3DTS_VIEW, (D3DMATRIX *)&light_view_matrix); device->SetTransform(D3DTS_WORLD, (D3DMATRIX *)&world_matrix); // 把所有反射關閉,讓模型在畫面上呈現黑色。 D3DCOLORVALUE zero = {0.0f, 0.0f, 0.0f, 1.0f}; sModelMaterial_DX9 material; material.m_Material.Ambient = zero; material.m_Material.Emissive = zero; material.m_Material.Diffuse = zero; material.m_Material.Specular = zero; material.m_bCullFace = false; material.Submit(); SetupLightingDX9(); // 畫出模型 g_Model_DX9.Render(0); // 告知direct3d9裝置rendertarget使用完畢 pSurface->Release(); // 還原主framebuffer device->SetRenderTarget(0, pFrameBufferBackup); device->SetDepthStencilSurface(pDepthBufferBackup); } // 把影子柔化 { pBlurredTexture = BlurTexture(g_pTexture); } // 把上一個步驟的結果當成貼圖來使用 { // 消除畫面 device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 150, 255), 1.0f, 0); // 設定轉換矩陣 device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &g_projection_matrix); device->SetTransform(D3DTS_VIEW, (D3DMATRIX *) &view_matrix); device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &world_matrix); // 設定光源 SetupLightingDX9(); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); device->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); device->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); // 畫出茶壼 g_Model_DX9.Render(); sModelMaterial_DX9 material; material.m_pTextures[0] = pBlurredTexture; material.Submit(); // 計算貼圖矩陣 Matrix4x4 inv_view_matrix = g_Control.GetCameraMatrix(); Matrix4x4 uv_offset_matrix; uv_offset_matrix.Identity(); uv_offset_matrix.Scale(0.5f, -0.5f, 0.5f); uv_offset_matrix[3].Set(0.5f, 0.5f, 0.5f, 1.0f); Matrix4x4 texture_matrix = inv_view_matrix * light_view_matrix * light_projection_matrix * uv_offset_matrix; Matrix4x4 indent_matrix = Matrix4x4::IdentityMatrix(); // 設定轉換矩陣 device->SetTransform(D3DTS_TEXTURE0, (D3DMATRIX *) &texture_matrix); device->SetTransform(D3DTS_WORLD, (D3DMATRIX *) &indent_matrix); // 開啟自動產生貼圖座標功能 device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION); device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4 | D3DTTFF_PROJECTED); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); device->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); device->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); // 畫出地表 g_Terrain_DX9.Render(0); device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0); } // 宣告所有的繪圖指令都下完了 device->EndScene(); // 把背景backbuffer的畫面呈現出來 device->Present( NULL, NULL, NULL, NULL ); }
INT CrenderTarget::CreateRenderSurface() { HRESULT hr=-1; DWORD dMip = 1; LPDIRECT3DSURFACE9 pSfC = NULL; LPDIRECT3DSURFACE9 pSfD = NULL; D3DSURFACE_DESC dscC; D3DSURFACE_DESC dscD; D3DCAPS9 m_Caps; m_pDev->GetRenderTarget(0,&pSfC); m_pDev->GetDepthStencilSurface(&pSfD); pSfC->GetDesc(&dscC); pSfD->GetDesc(&dscD); m_pDev->GetDeviceCaps(&m_Caps); pSfC->Release(); pSfD->Release(); if(m_iW<0) m_iW = dscC.Width; if(m_iH<0) m_iH = dscC.Height; m_dC = dscC.Format; m_dD = dscD.Format; if(LCX_TARGET_HDR16 == m_nType) m_dC = D3DFMT_A16B16G16R16F; if(LCX_TARGET_HDR32 == m_nType) m_dC = D3DFMT_A32B32G32R32F; hr = D3DXCreateRenderToSurface(m_pDev , m_iW , m_iH , (D3DFORMAT)m_dC , TRUE , (D3DFORMAT)m_dD , &m_pRts); if(FAILED(hr)) return -1; hr = D3DXCreateTexture(m_pDev , m_iW , m_iH , dMip , D3DUSAGE_RENDERTARGET , (D3DFORMAT)m_dC , D3DPOOL_DEFAULT , &m_pTxP); if(FAILED(hr)) return -1; hr = m_pTxP->GetSurfaceLevel(0, &m_pSfc); if(FAILED(hr)) return -1; // Clear 0x0 m_pDev->ColorFill(m_pSfc, NULL, 0x0); return hr; }
//----------------------------------------------------------------------------- // 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); }
// 描画 void CDOFText::Draw() { LPDIRECT3DDEVICE9 lpDevice = D3D::GetDevice(); // シーン描画開始 D3D::Begin(); // カメラ、投影設定 m_Camera.SetCameraPos( &D3DXVECTOR3( 0, 0, -10 ) ); lpDevice->SetTransform( D3DTS_VIEW, m_Camera.GetMatrix() ); lpDevice->SetTransform( D3DTS_PROJECTION, m_Proj.GetMatrix() ); // バックバッファ保存 lpDevice->GetRenderTarget( 0, &m_lpBackBuffer ); lpDevice->GetDepthStencilSurface( &m_lpZBuffer ); CXfile::SetDrawShader( false ); // パス1 // テクスチャに描画 // レンダーターゲット変更 lpDevice->SetRenderTarget( 0, m_SceneTex.GetSurface() ); lpDevice->SetDepthStencilSurface( m_SceneTex.GetZbuffer() ); lpDevice->Clear( 0,NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0xff, 0xff ), 1.0f, 0); lpDevice->SetRenderState( D3DRS_FOGENABLE, TRUE ); lpDevice->SetRenderState( D3DRS_FOGCOLOR, D3DCOLOR_XRGB( 0, 0x00, 0x80 ) ); lpDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_LINEAR ); lpDevice->SetRenderState( D3DRS_FOGSTART, GetAddress( 0.f ) ); lpDevice->SetRenderState( D3DRS_FOGEND, GetAddress( 100.f ) ); // 描画 CMatrix mRot, mTrans; mRot.SetRotY( timeGetTime() / 2 % 360 ); lpDevice->SetRenderState( D3DRS_LIGHTING, TRUE ); // テストメッシュ for( int i = 0; i < 5; i++ ) { mTrans.SetTrans( -2 + i * 2, 0, -3 + i * 3 ); lpDevice->SetTransform( D3DTS_WORLD, &(mRot * mTrans) ); m_Mesh.Draw( lpDevice ); } lpDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); lpDevice->SetTransform( D3DTS_WORLD, &CMatrix() ); m_Sphere.Draw( lpDevice ); // パス2 // 縮小させる // レンダーターゲット変更 // ぼかし用(ビューポートも変える) D3DVIEWPORT9 Vp = { 0, 0, WINDOW_WIDTH / 4, WINDOW_HEIGHT / 4, 0.f, 1.f }; lpDevice->SetViewport( &Vp ); lpDevice->SetRenderTarget( 0, m_MiniSizeTex.GetSurface() ); lpDevice->SetDepthStencilSurface( m_MiniSizeTex.GetZbuffer() ); lpDevice->Clear( 0,NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0xff, 0xff, 0 ), 1.0f, 0); lpDevice->SetTransform( D3DTS_VIEW, &CMatrix() ); lpDevice->SetTransform( D3DTS_PROJECTION, &CMatrix() ); // テクスチャに描画したものを流す m_BlurShader.SetTexture( "g_Texture", m_SceneTex.GetTexture() ); // 解像度を渡す D3DXVECTOR2 TexPos( 0.5f / Vp.Width, 0.5f / Vp.Height ); m_BlurShader.SetValue( "g_Offset", &TexPos, sizeof(D3DXVECTOR2) ); // シェーダー描画開始 m_BlurShader.Begin(); m_BlurShader.BeginPass( 0 ); m_Scene.SetVertex( 2 ); m_Scene.Draw( lpDevice ); m_BlurShader.EndPass(); m_BlurShader.End(); // パス3 // ぼかし入れる // レンダーターゲット変更 lpDevice->SetRenderTarget( 0, m_BlurTex.GetSurface() ); lpDevice->SetDepthStencilSurface( m_BlurTex.GetZbuffer() ); lpDevice->SetViewport( &Vp ); lpDevice->Clear( 0,NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0xff, 0xff, 0 ), 1.0f, 0); // 縮小画像をシェーダーに流す m_BlurShader.SetTexture( "g_Texture", m_MiniSizeTex.GetTexture() ); // シェーダー描画開始 m_BlurShader.Begin(); m_BlurShader.BeginPass( 1 ); lpDevice->SetFVF( CBoard::FVF_VERTEX ); m_Scene.SetVertex( 2 ); m_Scene.Draw( lpDevice ); m_BlurShader.EndPass(); m_BlurShader.End(); // パス4 // 深度計算を行う // レンダーターゲット変更 // (ビューポートももとに戻す) Vp.Width = WINDOW_WIDTH; Vp.Height = WINDOW_HEIGHT; lpDevice->SetViewport( &Vp ); lpDevice->SetRenderTarget( 0, m_DepthTex.GetSurface() ); lpDevice->SetDepthStencilSurface( m_DepthTex.GetZbuffer() ); lpDevice->Clear( 0,NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0xff, 0xff ), 1.0f, 0); // シェーダーに値を渡す lpDevice->SetTransform( D3DTS_VIEW, m_Camera.GetMatrix() ); lpDevice->SetTransform( D3DTS_PROJECTION, m_Proj.GetMatrix() ); m_DepthShader.SetValue( "g_Proj", m_Proj.GetMatrix(), sizeof(D3DXMATRIX) ); m_DepthShader.SetValue( "g_View", m_Camera.GetMatrix(), sizeof(D3DXMATRIX) ); // シェーダー描画開始 m_DepthShader.Begin(); m_DepthShader.BeginPass( 0 ); CXfile::SetDrawShader( true ); //CXfile::SetShaderDevice( &m_DepthShader, "g_mWVP" ); for( int i = 0; i < 5; i++ ) { mTrans.SetTrans( -2 + i * 2, 0, -3 + i * 3 ); lpDevice->SetTransform( D3DTS_WORLD, &(mRot * mTrans) ); m_Mesh.Draw( lpDevice ); } //m_DepthShader.SetValue( "g_World", &CMatrix(), sizeof(D3DXMATRIX) ); lpDevice->SetTransform( D3DTS_WORLD, &CMatrix() ); m_Sphere.Draw( lpDevice ); m_DepthShader.EndPass(); m_DepthShader.End(); // パス5 // 被写界深度適用 // バックバッファに描画 // バックバッファにレンダーターゲットを戻す lpDevice->SetRenderTarget( 0, m_lpBackBuffer ); lpDevice->SetDepthStencilSurface( m_lpZBuffer ); Vp.Width = WINDOW_WIDTH; Vp.Height= WINDOW_HEIGHT; lpDevice->SetViewport( &Vp ); lpDevice->Clear( 0,NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0xff, 0, 0xff ), 1.0f, 0); // シェーダーに値を渡す m_DOFShader.SetTexture( "g_SceneTex", m_SceneTex.GetTexture() ); // シーン m_DOFShader.SetTexture( "g_BlurTex", m_BlurTex.GetTexture() ); // ぼかし m_DOFShader.SetTexture( "g_DepthTex", m_DepthTex.GetTexture() ); // 深度 lpDevice->SetTransform( D3DTS_VIEW, &CMatrix() ); lpDevice->SetTransform( D3DTS_PROJECTION, &CMatrix() ); lpDevice->SetTransform( D3DTS_WORLD, &CMatrix() ); // 板ポリゴンに描画 m_DOFShader.Begin(); m_DOFShader.BeginPass( 0 ); m_Scene.SetVertex( 2 ); m_Scene.Draw( lpDevice ); m_DOFShader.EndPass(); m_DOFShader.End(); /*Vp.Y = 300.f; Vp.Height = 600.f; lpDevice->SetViewport( &Vp );*/ DrawSceneByPolygon(lpDevice); SAFE_RELEASE( m_lpBackBuffer ); SAFE_RELEASE( m_lpZBuffer ); // シーン終了 D3D::End(); }
/////////////////////////////////////////////////////////////////////// /// /// Generate a shadow map of the scene /// void VCND3D9EffectCore::GenerateShadowMap() { GPU_PROFILE_BLOCK_NAME( VCNTXT("Shadow Map") ); HRESULT hr = S_FALSE; // Notify that we have start to render shadow map mGeneratingShadowMap = true; VCND3D9* renderer = static_cast<VCND3D9*>(VCNRenderCore::GetInstance()); LPDIRECT3DDEVICE9 device = renderer->GetD3DDevice(); // Keep track of the previous back buffer CComPtr<IDirect3DSurface9> d3dBackBuffer; hr = device->GetRenderTarget(0, &d3dBackBuffer); // Set shadow map surface and start rendering of scene using the shadow map effect CComPtr<IDirect3DSurface9> shadowMapSurface; hr = mShadowMapTexture->GetSurfaceLevel(0, &shadowMapSurface); VCN_ASSERT( SUCCEEDED(hr) ); hr = device->SetRenderTarget(0, shadowMapSurface); VCN_ASSERT( SUCCEEDED(hr) ); // Set the map viewport D3DSURFACE_DESC d3dSurfaceDesc; shadowMapSurface->GetDesc( &d3dSurfaceDesc ); VCNRenderCore::GetInstance()->SetViewport(d3dSurfaceDesc.Width, d3dSurfaceDesc.Height); // Clear the shadow map for what's coming device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0, 1.0f, 0); // Start rendering to the map hr = device->BeginScene(); VCN_ASSERT( SUCCEEDED(hr) ); device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO); // Backup current view settings const Matrix4 oldView = VCNXformCore::GetInstance()->GetViewMatrix(); const Matrix4 oldProj = VCNXformCore::GetInstance()->GetProjectionMatrix(); const VCNProjectionType oldProjType = VCNXformCore::GetInstance()->GetProjectionType(); // Set the view as the light view when generating the shadow map. VCNLight* shadowLightSource = VCNLightingCore::GetInstance()->GetLight(0); const Matrix4 lightProj = shadowLightSource->GetProjectionMatrix(); const Matrix4 lightView = shadowLightSource->GetViewMatrix(); VCNXformCore::GetInstance()->SetViewLookAt( lightView ); VCNXformCore::GetInstance()->SetProjectionMatrix( lightProj ); // Draw scene // TODO: Generate map for each light VCNNodeCore::GetInstance()->GetRootNode()->Render(); // Restore view VCNXformCore::GetInstance()->SetProjectionType( oldProjType, oldProj ); VCNXformCore::GetInstance()->SetViewLookAt( oldView ); // Restore color writes device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE); hr = device->EndScene(); VCN_ASSERT( SUCCEEDED(hr) ); // Set shadow map texture for effects that needs it // TODO: Effects that need the shadow map should pull it from the effect core shadow maps pool mEffectMap[eidLitTextured]->Cast<VCND3D9_LitTexturedFX>()->SetShadowMapTexture( mShadowMapTexture ); mEffectMap[eidSkinned]->Cast<VCND3D9_LitTexturedSkinnedFX>()->SetShadowMapTexture( mShadowMapTexture ); mEffectMap[eidTerrain]->Cast<VCND3D9_TerrainFX>()->SetShadowMapTexture( mShadowMapTexture ); // Restore back buffer device->SetRenderTarget(0, d3dBackBuffer); // Restore back buffer viewport VCNRenderCore::GetInstance()->RestoreFullViewport(); // We are done generating shadow map mGeneratingShadowMap = false; }