// Glare.cpp: implementation of the CGlare class. // ////////////////////////////////////////////////////////////////////// #include "d3dx8.h" #include "dxutil.h" #include "BaseGraphicsLayer.h" #include "Glare.h" #include "SceneStateMgr.h" #define __CENTER -0.5f const DWORD GLAREDISPLAY_VERTEX::FVF = D3DFVF_XYZRHW | D3DFVF_TEX1 | D3DFVF_DIFFUSE; CGlareScene::CGlareScene() { m_pd3dDevice = NULL; m_pOldRenderTarget = NULL; m_pOldZSurface = NULL; ZeroMemory( m_RenderTarget256, sizeof(m_RenderTarget256) ); ZeroMemory( m_RenderTarget128, sizeof(m_RenderTarget128) ); ZeroMemory( m_RenderTarget64, sizeof(m_RenderTarget64) ); ZeroMemory( m_RenderTarget32, sizeof(m_RenderTarget32) ); ZeroMemory( m_RenderTarget16, sizeof(m_RenderTarget16) ); } CGlareScene::~CGlareScene() { Destroy(); } HRESULT CGlareScene::Create( LPDIRECT3DDEVICE8 Device ) { m_pd3dDevice = Device; HRESULT hr = S_OK; for( int i = 0; i < NUM_256_TEXTURES; i++ ) { if( FAILED(hr=CreateRenderTarget(&m_RenderTarget256[i], GLARETEXTURE_HEIGHT, GLARETEXTURE_WIDTH)) ) return hr; } for( i = 0; i < NUM_128_TEXTURES; i++ ) { if( FAILED(hr= CreateRenderTarget(&m_RenderTarget128[i], GLARETEXTURE_HEIGHT>>1, GLARETEXTURE_HEIGHT>>1)) ) return hr; } for( i = 0; i < NUM_64_TEXTURES; i++ ) { if( FAILED(hr=CreateRenderTarget(&m_RenderTarget64[i], GLARETEXTURE_HEIGHT>>2, GLARETEXTURE_HEIGHT>>2)) ) return hr; } for( i = 0; i < NUM_32_TEXTURES; i++ ) { if( FAILED(hr=CreateRenderTarget(&m_RenderTarget32[i], GLARETEXTURE_HEIGHT>>3, GLARETEXTURE_HEIGHT>>3)) ) return hr; } for( i = 0; i < NUM_16_TEXTURES; i++ ) { if( FAILED(hr=CreateRenderTarget(&m_RenderTarget16[i], GLARETEXTURE_HEIGHT>>4, GLARETEXTURE_HEIGHT>>4)) ) return hr; } m_pd3dDevice->GetRenderTarget( &m_pOldRenderTarget ); m_pd3dDevice->GetDepthStencilSurface( &m_pOldZSurface ); for (DWORD t=0; t<3; t++) { CSceneStateMgr::_SetD3DTextureStageState (t, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); CSceneStateMgr::_SetD3DTextureStageState (t, D3DTSS_MINFILTER, D3DTEXF_LINEAR); } return S_OK; } bool CGlareScene::BeginRenderToTexture() { m_pd3dDevice->GetViewport( &m_OldViewport ); m_pd3dDevice->GetRenderTarget( &m_pOldRenderTarget ); m_pd3dDevice->GetDepthStencilSurface( &m_pOldZSurface ); D3DVIEWPORT8 NewViewport; NewViewport.X = 0; NewViewport.Y = 0; NewViewport.MaxZ = 1.0f; NewViewport.MinZ = 0.0f; NewViewport.Width = GLARETEXTURE_WIDTH; NewViewport.Height = GLARETEXTURE_HEIGHT; //RenderTarget PrimaryBlurImage if( FAILED(m_pd3dDevice->SetRenderTarget(m_RenderTarget256[0].pSurface, m_RenderTarget256[0].pZSurface)) ) return false; if( FAILED(m_pd3dDevice->SetViewport(&NewViewport)) ) return false; m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0 ); //Render to Texture m_pd3dDevice->EndScene(); m_pd3dDevice->BeginScene(); return true; } bool CGlareScene::EndRenderToTexture() { m_pd3dDevice->EndScene(); m_pd3dDevice->BeginScene(); //Return Old State m_pd3dDevice->SetRenderTarget( m_pOldRenderTarget, m_pOldZSurface ); m_pd3dDevice->SetViewport( &m_OldViewport ); SAFE_RELEASE( m_pOldRenderTarget ); SAFE_RELEASE( m_pOldZSurface ); return true; } void CGlareScene::Destroy() { SAFE_RELEASE(m_pOldRenderTarget); SAFE_RELEASE(m_pOldZSurface); for( int i = 0; i < NUM_256_TEXTURES; i++ ) { SAFE_RELEASE(m_RenderTarget256[i].pTexture); SAFE_RELEASE(m_RenderTarget256[i].pSurface); SAFE_RELEASE(m_RenderTarget256[i].pZSurface); } for( i = 0; i < NUM_128_TEXTURES; i++ ) { SAFE_RELEASE(m_RenderTarget128[i].pTexture); SAFE_RELEASE(m_RenderTarget128[i].pSurface); SAFE_RELEASE(m_RenderTarget128[i].pZSurface); } for( i = 0; i < NUM_64_TEXTURES; i++ ) { SAFE_RELEASE(m_RenderTarget64[i].pTexture); SAFE_RELEASE(m_RenderTarget64[i].pSurface); SAFE_RELEASE(m_RenderTarget64[i].pZSurface); } for( i = 0; i < NUM_32_TEXTURES; i++ ) { SAFE_RELEASE(m_RenderTarget32[i].pTexture); SAFE_RELEASE(m_RenderTarget32[i].pSurface); SAFE_RELEASE(m_RenderTarget32[i].pZSurface); } for( i = 0; i < NUM_16_TEXTURES; i++ ) { SAFE_RELEASE(m_RenderTarget16[i].pTexture); SAFE_RELEASE(m_RenderTarget16[i].pSurface); SAFE_RELEASE(m_RenderTarget16[i].pZSurface); } } void CGlareScene::SetDiffuseColorShader() { CSceneStateMgr::_SetD3DTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE); CSceneStateMgr::_SetD3DTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); CSceneStateMgr::_SetD3DTextureStageState (0, D3DTSS_COLOROP, D3DTOP_SELECTARG2); CSceneStateMgr::_SetD3DTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_pd3dDevice->SetTexture (1, NULL); CSceneStateMgr::_SetD3DTextureStageState (1, D3DTSS_COLOROP, D3DTOP_DISABLE); CSceneStateMgr::_SetD3DTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); } void CGlareScene::SetTextureShader( LPDIRECT3DTEXTURE8 pTexture ) { m_pd3dDevice->SetTexture (0, pTexture); CSceneStateMgr::_SetD3DTextureStageState (0, D3DTSS_COLORARG1, D3DTA_TEXTURE); CSceneStateMgr::_SetD3DTextureStageState (0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); CSceneStateMgr::_SetD3DTextureStageState (0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); CSceneStateMgr::_SetD3DTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_DISABLE); m_pd3dDevice->SetTexture (1, NULL); CSceneStateMgr::_SetD3DTextureStageState (1, D3DTSS_COLOROP, D3DTOP_DISABLE); CSceneStateMgr::_SetD3DTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); } void CGlareScene::SetTextureAlphaFactorShader( LPDIRECT3DTEXTURE8 pTexture, D3DCOLOR color ) { CSceneStateMgr::_SetD3DRenderState( D3DRS_TEXTUREFACTOR, color ); m_pd3dDevice->SetTexture ( 0, pTexture ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_TFACTOR ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ); m_pd3dDevice->SetTexture( 1, NULL ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); } void CGlareScene::SetColorFactorShader( D3DCOLOR color ) { CSceneStateMgr::_SetD3DRenderState( D3DRS_TEXTUREFACTOR, color ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_TFACTOR ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); m_pd3dDevice->SetTexture (1, NULL); CSceneStateMgr::_SetD3DTextureStageState (1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); CSceneStateMgr::_SetD3DTextureStageState (1, D3DTSS_COLOROP, D3DTOP_DISABLE); } void CGlareScene::AddAttenuateThreeTextureShader( LPDIRECT3DTEXTURE8 pTex1, LPDIRECT3DTEXTURE8 pTex2, LPDIRECT3DTEXTURE8 pTex3, D3DCOLOR color ) { CSceneStateMgr::_SetD3DRenderState( D3DRS_TEXTUREFACTOR, color ); m_pd3dDevice->SetTexture( 0, pTex1 ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_TFACTOR ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); m_pd3dDevice->SetTexture( 1, pTex2 ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_CURRENT ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_MODULATEALPHA_ADDCOLOR ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_ALPHAARG2, D3DTA_TFACTOR ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); m_pd3dDevice->SetTexture( 2, pTex3 ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_CURRENT ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_MODULATEALPHA_ADDCOLOR ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); } void CGlareScene::Set3StageAccumAdderShader( LPDIRECT3DTEXTURE8 pTex1, LPDIRECT3DTEXTURE8 pTex2, LPDIRECT3DTEXTURE8 pTex3 ) { CSceneStateMgr::_SetD3DRenderState( D3DRS_TEXTUREFACTOR, 0x55ffffff ); //a = 0.333 m_pd3dDevice->SetTexture( 0, pTex1 ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); // Since diffuse alpha from stage 0 is 0.5, this stage computes // 0.5*sample1 + 0.5*sample2. m_pd3dDevice->SetTexture( 1, pTex2 ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BLENDCURRENTALPHA ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_ALPHAARG2, D3DTA_CURRENT ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); // Computes 0.333*(sample3) + 0.666*(0.5*sample1+0.5*sample2) using // texture alpha factor. m_pd3dDevice->SetTexture( 2, pTex2 ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_CURRENT ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_BLENDFACTORALPHA ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_ALPHAARG2, D3DTA_TFACTOR ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2 ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); m_pd3dDevice->SetTexture (3, NULL); CSceneStateMgr::_SetD3DTextureStageState (3, D3DTSS_COLOROP, D3DTOP_DISABLE); CSceneStateMgr::_SetD3DTextureStageState (3, D3DTSS_ALPHAOP, D3DTOP_DISABLE); } void CGlareScene::DownImage( RenderTarget* pSrc, RenderTarget* pDst, float offsetU, float offsetV ) { HRESULT hr = S_OK; D3DVIEWPORT8 NewViewport; NewViewport.X = 0; NewViewport.Y = 0; NewViewport.Height = pDst->nHeight; NewViewport.Width = pDst->nWidth; NewViewport.MinZ = 0; NewViewport.MaxZ = 1; //============ //Set State //============ m_pd3dDevice->GetViewport( &m_OldViewport ); m_pd3dDevice->GetRenderTarget( &m_pOldRenderTarget ); m_pd3dDevice->GetDepthStencilSurface( &m_pOldZSurface ); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DRenderState( D3DRS_ZENABLE, FALSE ); CSceneStateMgr::_SetD3DRenderState( D3DRS_DITHERENABLE, TRUE); CSceneStateMgr::_SetD3DRenderState( D3DRS_FOGENABLE, FALSE ); float ooWidth = 1.0f/(float)NewViewport.Width; float ooHeight = 1.0f/(float)NewViewport.Height; float width = (float)NewViewport.Width; float height = (float)NewViewport.Height; GLAREDISPLAY_VERTEX pVertex[4]; //1 pVertex[0].x = __CENTER; pVertex[0].y = __CENTER; pVertex[0].z = 0.5f; pVertex[0].rhw = 1.0f; pVertex[0].u = offsetU * ooWidth; pVertex[0].v = offsetV * ooHeight; //2 pVertex[1].x = width + __CENTER; pVertex[1].y = __CENTER; pVertex[1].z = 0.5f; pVertex[1].rhw = 1.0f; pVertex[1].u = 1.0f + offsetU*ooWidth; pVertex[1].v = offsetV * ooHeight; //3 pVertex[2].x = __CENTER; pVertex[2].y = height + __CENTER; pVertex[2].z = 0.5f; pVertex[2].rhw = 1.0f; pVertex[2].u = offsetU * ooWidth; pVertex[2].v = 1.0f + offsetV * ooHeight; //4 pVertex[3].x = width + __CENTER; pVertex[3].y = height + __CENTER; pVertex[3].z = 0.5f; pVertex[3].rhw = 1.0f; pVertex[3].u = 1.0f + offsetU * ooWidth; pVertex[3].v = 1.0f + offsetV * ooHeight; //Change RenderTarget m_pd3dDevice->SetRenderTarget( pDst->pSurface, pDst->pZSurface ); m_pd3dDevice->SetViewport( &NewViewport ); m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0L ); //Begin Render to TextureQuad m_pd3dDevice->EndScene(); m_pd3dDevice->BeginScene(); SetTextureShader( pSrc->pTexture ); m_pd3dDevice->SetVertexShader( GLAREDISPLAY_VERTEX::FVF ); m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, pVertex, sizeof(GLAREDISPLAY_VERTEX) ); //End Render m_pd3dDevice->EndScene(); m_pd3dDevice->BeginScene(); //============= //Back State //============= m_pd3dDevice->SetRenderTarget( m_pOldRenderTarget, m_pOldZSurface ); m_pd3dDevice->SetViewport( &m_OldViewport ); SAFE_RELEASE( m_pOldRenderTarget ); SAFE_RELEASE( m_pOldZSurface ); CSceneStateMgr::_SetD3DRenderState( D3DRS_ZENABLE, TRUE ); CSceneStateMgr::_SetD3DRenderState (D3DRS_DITHERENABLE, FALSE); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DRenderState( D3DRS_FOGENABLE, TRUE ); } HRESULT CGlareScene::CreateRenderTarget( RenderTarget* pTarget, UINT nHeight, UINT nWidth ) { HRESULT hr = S_OK; pTarget->nHeight = nHeight; pTarget->nWidth = nWidth; /*if(FAILED(hr=D3DXCreateTexture(m_pd3dDevice, nWidth, nHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pTarget->pTexture))) return hr; if(FAILED(hr=m_pd3dDevice->CreateDepthStencilSurface( nWidth, nHeight, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, &pTarget->pZSurface))) return hr;*/ if(FAILED(hr=D3DXCreateTexture(m_pd3dDevice, nWidth, nHeight, 1, D3DUSAGE_RENDERTARGET, BaseGraphicsLayer::m_d3dpp.BackBufferFormat, D3DPOOL_DEFAULT, &pTarget->pTexture))) return hr; if(FAILED(hr=m_pd3dDevice->CreateDepthStencilSurface( nWidth, nHeight, BaseGraphicsLayer::m_d3dpp.AutoDepthStencilFormat, D3DMULTISAMPLE_NONE, &pTarget->pZSurface))) return hr; if(FAILED(hr=pTarget->pTexture->GetSurfaceLevel(0, &pTarget->pSurface))) return hr; return hr; } void CGlareScene::AccumulateImage( RenderTarget* pResult, RenderTarget* pSrc1, RenderTarget* pSrc2, RenderTarget* pSrc3, RenderTarget* pSrc4 ) { D3DVIEWPORT8 NewViewport; NewViewport.X = 0; NewViewport.Y = 0; NewViewport.MinZ = 0.0f; NewViewport.MaxZ = 1.0f; NewViewport.Width = pResult->nWidth; NewViewport.Height = pResult->nHeight; //============ //Set State //============ m_pd3dDevice->GetViewport( &m_OldViewport ); m_pd3dDevice->GetRenderTarget( &m_pOldRenderTarget ); m_pd3dDevice->GetDepthStencilSurface( &m_pOldZSurface ); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DRenderState( D3DRS_ZENABLE, FALSE ); CSceneStateMgr::_SetD3DRenderState( D3DRS_DITHERENABLE, TRUE); CSceneStateMgr::_SetD3DRenderState( D3DRS_FOGENABLE, FALSE ); GLAREDISPLAY_VERTEX pVertex[4]; //1 pVertex[0].x = __CENTER; pVertex[0].y = __CENTER; pVertex[0].z = 0.5f; pVertex[0].rhw = 1.0f; pVertex[0].color = 0x80ffffff; pVertex[0].u = 0.0f; pVertex[0].v = 0.0f; //2 pVertex[1].x = (float)pResult->nWidth + __CENTER; pVertex[1].y = __CENTER; pVertex[1].z = 0.5f; pVertex[1].rhw = 1.0f; pVertex[1].color = 0x80ffffff; pVertex[1].u = 1.0f; pVertex[1].v = 0.0f; //3 pVertex[2].x = __CENTER; pVertex[2].y = (float)pResult->nHeight + __CENTER; pVertex[2].z = 0.5f; pVertex[2].rhw = 1.0f; pVertex[2].color = 0x80ffffff; pVertex[2].u = 0.0f; pVertex[2].v = 1.0f; //4 pVertex[3].x = (float)pResult->nWidth + __CENTER; pVertex[3].y = (float)pResult->nHeight + __CENTER; pVertex[3].z = 0.5f; pVertex[3].rhw = 1.0f; pVertex[3].color = 0x80ffffff; pVertex[3].u = 1.0f; pVertex[3].v = 1.0f; m_pd3dDevice->SetRenderTarget( pResult->pSurface, pResult->pZSurface ); m_pd3dDevice->SetViewport( &NewViewport ); m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L ); //Begin Render To texture m_pd3dDevice->EndScene(); m_pd3dDevice->BeginScene(); //1 m_pd3dDevice->SetVertexShader( GLAREDISPLAY_VERTEX::FVF ); Set3StageAccumAdderShader( pSrc1->pTexture, pSrc2->pTexture, pSrc3->pTexture ); m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, pVertex, sizeof(GLAREDISPLAY_VERTEX) ); //2 if( pSrc4 ) { CSceneStateMgr::_SetD3DRenderState (D3DRS_ALPHABLENDENABLE, TRUE ); CSceneStateMgr::_SetD3DRenderState (D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); CSceneStateMgr::_SetD3DRenderState (D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); // Select texture as color arg and factor alpha as alpha arg. // Alpha is set at 0.25 to blend (0.25*Src)+(0.75*Dest) SetTextureAlphaFactorShader( pSrc4->pTexture, 0x40ffffff ); m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, pVertex, sizeof(GLAREDISPLAY_VERTEX) ); CSceneStateMgr::_SetD3DRenderState ( D3DRS_ALPHABLENDENABLE, FALSE ); } m_pd3dDevice->EndScene(); m_pd3dDevice->BeginScene(); //============= //Back State //============= m_pd3dDevice->SetRenderTarget( m_pOldRenderTarget, m_pOldZSurface ); m_pd3dDevice->SetViewport( &m_OldViewport ); SAFE_RELEASE( m_pOldRenderTarget ); SAFE_RELEASE( m_pOldZSurface ); CSceneStateMgr::_SetD3DRenderState( D3DRS_ZENABLE, TRUE ); CSceneStateMgr::_SetD3DRenderState (D3DRS_DITHERENABLE, FALSE); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DRenderState( D3DRS_FOGENABLE, TRUE ); } void CGlareScene::ShakeGlareImage( float weight16, float weight32, float weight64, float weight128 ) { //DownImage Texture 256x256 -> 128x128 DownImage( &m_RenderTarget256[0], &m_RenderTarget128[0], 0.0f, 0.0f ); DownImage( &m_RenderTarget256[0], &m_RenderTarget128[1], OFFSET1U, OFFSET1V ); DownImage( &m_RenderTarget256[0], &m_RenderTarget128[2], OFFSET2U, OFFSET2V ); DownImage( &m_RenderTarget256[0], &m_RenderTarget128[3], OFFSET3U, OFFSET3V ); DownImage( &m_RenderTarget256[0], &m_RenderTarget128[4], OFFSET4U, OFFSET4V ); DownImage( &m_RenderTarget256[0], &m_RenderTarget128[5], OFFSET5U, OFFSET5V ); AccumulateImage( &m_RenderTarget128[6], &m_RenderTarget128[2], &m_RenderTarget128[5], &m_RenderTarget128[3], &m_RenderTarget128[4] ); AccumulateImage( &m_RenderTarget128[7], &m_RenderTarget128[0], &m_RenderTarget128[1], &m_RenderTarget128[6] ); //DownImage Texture 128x128 -> 64x64 DownImage( &m_RenderTarget128[0], &m_RenderTarget64[0], 0.0f, 0.0f ); DownImage( &m_RenderTarget128[0], &m_RenderTarget64[1], OFFSET1U, OFFSET1V ); DownImage( &m_RenderTarget128[0], &m_RenderTarget64[2], OFFSET2U, OFFSET2V ); DownImage( &m_RenderTarget128[0], &m_RenderTarget64[3], OFFSET3U, OFFSET3V ); DownImage( &m_RenderTarget128[0], &m_RenderTarget64[4], OFFSET4U, OFFSET4V ); DownImage( &m_RenderTarget128[0], &m_RenderTarget64[5], OFFSET5U, OFFSET5V ); AccumulateImage( &m_RenderTarget64[6], &m_RenderTarget64[2], &m_RenderTarget64[5], &m_RenderTarget64[3], &m_RenderTarget64[4] ); AccumulateImage( &m_RenderTarget64[7], &m_RenderTarget64[0], &m_RenderTarget64[1], &m_RenderTarget64[6] ); //DownImage Texture 64x64 -> 32x32 DownImage( &m_RenderTarget64[0], &m_RenderTarget32[0], 0.0f, 0.0f ); DownImage( &m_RenderTarget64[0], &m_RenderTarget32[1], OFFSET1U, OFFSET1V ); DownImage( &m_RenderTarget64[0], &m_RenderTarget32[2], OFFSET2U, OFFSET2V ); DownImage( &m_RenderTarget64[0], &m_RenderTarget32[3], OFFSET3U, OFFSET3V ); DownImage( &m_RenderTarget64[0], &m_RenderTarget32[4], OFFSET4U, OFFSET4V ); DownImage( &m_RenderTarget64[0], &m_RenderTarget32[5], OFFSET5U, OFFSET5V ); AccumulateImage( &m_RenderTarget32[6], &m_RenderTarget32[2], &m_RenderTarget32[5], &m_RenderTarget32[3], &m_RenderTarget32[4] ); AccumulateImage( &m_RenderTarget32[7], &m_RenderTarget32[0], &m_RenderTarget32[1], &m_RenderTarget32[6] ); DownImage( &m_RenderTarget32[0], &m_RenderTarget16[0], 0.0f, 0.0f ); DownImage( &m_RenderTarget32[0], &m_RenderTarget16[1], OFFSET1U, OFFSET1V ); DownImage( &m_RenderTarget32[0], &m_RenderTarget16[2], OFFSET2U, OFFSET2V ); DownImage( &m_RenderTarget32[0], &m_RenderTarget16[3], OFFSET3U, OFFSET3V ); DownImage( &m_RenderTarget32[0], &m_RenderTarget16[4], OFFSET4U, OFFSET4V ); DownImage( &m_RenderTarget32[0], &m_RenderTarget16[5], OFFSET5U, OFFSET5V ); AccumulateImage( &m_RenderTarget16[6], &m_RenderTarget16[2], &m_RenderTarget16[5], &m_RenderTarget16[3], &m_RenderTarget16[4] ); AccumulateImage( &m_RenderTarget16[7], &m_RenderTarget16[0], &m_RenderTarget16[1], &m_RenderTarget16[6] ); //Result Image AddBlurImage( &m_RenderTarget256[1], &m_RenderTarget16[7], &m_RenderTarget32[7], &m_RenderTarget64[7], &m_RenderTarget128[7], weight16, weight32, weight64, weight128 ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_DISABLE ); CSceneStateMgr::_SetD3DTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE ); CSceneStateMgr::_SetD3DTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE ); CSceneStateMgr::_SetD3DTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); } void CGlareScene::AddBlurImage( RenderTarget* pResult, RenderTarget* pSrc1, RenderTarget* pSrc2, RenderTarget* pSrc3, RenderTarget* pSrc4, float weight1, float weight2, float weight3, float weight4 ) { // Pack weights into tfactor. D3DCOLOR d3dcWeightColor = D3DCOLOR_COLORVALUE(weight1, weight1, weight1, weight3); D3DVIEWPORT8 NewViewport; NewViewport.X = 0; NewViewport.Y = 0; NewViewport.Width = pResult->nWidth; NewViewport.Height = pResult->nHeight; NewViewport.MinZ = 0.0f; NewViewport.MaxZ = 1.0f; //============ //Set State //============ m_pd3dDevice->GetViewport( &m_OldViewport ); m_pd3dDevice->GetRenderTarget( &m_pOldRenderTarget ); m_pd3dDevice->GetDepthStencilSurface( &m_pOldZSurface ); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP); CSceneStateMgr::_SetD3DRenderState( D3DRS_ZENABLE, FALSE ); CSceneStateMgr::_SetD3DRenderState( D3DRS_DITHERENABLE, TRUE); CSceneStateMgr::_SetD3DRenderState( D3DRS_FOGENABLE, FALSE ); GLAREDISPLAY_VERTEX pVertex[4]; pVertex[0].x = __CENTER; pVertex[0].y = __CENTER; pVertex[0].z = 0.5f; pVertex[0].rhw = 1.0f; pVertex[0].u = 0.0f; pVertex[0].v = 0.0f; pVertex[0].color = D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0f, weight2); pVertex[1].x = (float)GLARETEXTURE_WIDTH + __CENTER; pVertex[1].y = __CENTER; pVertex[1].z = 0.5f; pVertex[1].rhw = 1.0f; pVertex[1].u = 1.0f; pVertex[1].v = 0.0f; pVertex[1].color = D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0f, weight2); pVertex[2].x = __CENTER; pVertex[2].y = (float)GLARETEXTURE_HEIGHT + __CENTER; pVertex[2].z = 0.5f; pVertex[2].rhw = 1.0f; pVertex[2].u = 0.0f; pVertex[2].v = 1.0f; pVertex[2].color = D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0f, weight2); pVertex[3].x = (float)GLARETEXTURE_WIDTH + __CENTER; pVertex[3].y = (float)GLARETEXTURE_HEIGHT + __CENTER; pVertex[3].z = 0.5f; pVertex[3].rhw = 1.0f; pVertex[3].u = 1.0f; pVertex[3].v = 1.0f; pVertex[3].color = D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.0f, weight2); m_pd3dDevice->SetRenderTarget( pResult->pSurface, pResult->pZSurface ); m_pd3dDevice->SetViewport( &NewViewport ); m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0L ); m_pd3dDevice->EndScene(); m_pd3dDevice->BeginScene (); AddAttenuateThreeTextureShader( pSrc1->pTexture, pSrc2->pTexture, pSrc3->pTexture, d3dcWeightColor ); m_pd3dDevice->SetVertexShader( GLAREDISPLAY_VERTEX::FVF ); m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, pVertex, sizeof(GLAREDISPLAY_VERTEX) ); if( pSrc4 ) { CSceneStateMgr::_SetD3DRenderState (D3DRS_ALPHABLENDENABLE, TRUE); CSceneStateMgr::_SetD3DRenderState (D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); CSceneStateMgr::_SetD3DRenderState (D3DRS_DESTBLEND, D3DBLEND_ONE); D3DCOLOR weight4Factor = D3DCOLOR_COLORVALUE( 1.0f, 1.0f, 1.0f, weight4 ); SetTextureAlphaFactorShader(pSrc4->pTexture, weight4Factor); m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, pVertex, sizeof(GLAREDISPLAY_VERTEX) ); CSceneStateMgr::_SetD3DRenderState (D3DRS_ALPHABLENDENABLE, FALSE); } m_pd3dDevice->EndScene(); m_pd3dDevice->BeginScene(); //============= //Back State //============= m_pd3dDevice->SetRenderTarget( m_pOldRenderTarget, m_pOldZSurface ); m_pd3dDevice->SetViewport( &m_OldViewport ); SAFE_RELEASE( m_pOldRenderTarget ); SAFE_RELEASE( m_pOldZSurface ); CSceneStateMgr::_SetD3DRenderState( D3DRS_ZENABLE, TRUE ); CSceneStateMgr::_SetD3DRenderState (D3DRS_DITHERENABLE, FALSE); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DRenderState( D3DRS_FOGENABLE, TRUE ); }