//-----------------------------------------------------------------------------
// 文件:CFULLWINDOW.Cpp
// 功能:Board 的类实现
//-----------------------------------------------------------------------------
#include "dxstdafx.h"
#include "CFullWindow.h"
CFULLWINDOW::CFULLWINDOW( )
{
m_pVB = NULL;
m_pIB = NULL;
m_dwNumIndices = 0;
m_dwNumVertices = 0;
m_pDecl = NULL;
m_pEffect = NULL;
//
m_pColorTexture = NULL;
m_pTextureFFT = NULL;
//
m_pRTTTextureSrc = NULL;
m_pRTTInterfaceSrc = NULL;
m_pRTTSurfaceSrc = NULL;
//
m_pRTTTextureDest = NULL;
m_pRTTInterfaceDest = NULL;
m_pRTTSurfaceDest = NULL;
//
}
CFULLWINDOW::~CFULLWINDOW( )
{
SAFE_RELEASE( m_pVB );
SAFE_RELEASE( m_pIB );
m_dwNumIndices = 0;
m_dwNumVertices = 0;
SAFE_RELEASE( m_pDecl );
SAFE_RELEASE( m_pEffect );
//
SAFE_RELEASE( m_pColorTexture );
SAFE_RELEASE( m_pTextureFFT );
//
SAFE_RELEASE( m_pRTTTextureSrc );
SAFE_RELEASE( m_pRTTInterfaceSrc );
SAFE_RELEASE( m_pRTTSurfaceSrc );
//
SAFE_RELEASE( m_pRTTTextureDest );
SAFE_RELEASE( m_pRTTInterfaceDest );
SAFE_RELEASE( m_pRTTSurfaceDest );
//
}
//
void CFULLWINDOW::Init( )
{
}
HRESULT CFULLWINDOW::OnCreateDevice( IDirect3DDevice9* pd3dDevice )
{
HRESULT hr = S_OK;
TCHAR szAppPath[MAX_PATH];
GetCurrentDirectory( MAX_PATH, szAppPath );
// 装入纹理
if( FAILED( hr = D3DXCreateTextureFromFileEx( pd3dDevice, L"ColorMap.dds", D3DX_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0,
NULL, NULL, &m_pColorTexture ) )) return hr;
// 检查要变换的纹理是否满足条件
D3DSURFACE_DESC Desc;
if( FAILED( hr = m_pColorTexture->GetLevelDesc( 0 , &Desc ))) return E_FAIL;
int iWidth = Desc.Width ;
int NumButterflies = ( int )( log(( float ) iWidth ) / log( 2.0 ));
m_ButterFly = NumButterflies ;
// 创建 FFT 纹理参数
/*
if( FAILED( hr = D3DXCreateTextureFromFileEx( pd3dDevice , L"FFT_512.dds" , D3DX_DEFAULT , D3DX_DEFAULT ,
D3DX_FROM_FILE , 0 , D3DFMT_UNKNOWN , D3DPOOL_MANAGED , D3DX_DEFAULT , D3DX_DEFAULT , 0 ,
NULL , NULL , &m_pTextureFFT ) )) return hr;
*/
// 给渲染到纹理的纹理大小设置宽高
m_TextureSize = iWidth ;
//
// 初始化纹理数据
if( FAILED( hr = CreateBufferflyIndexTexture( pd3dDevice , NumButterflies ))) return E_FAIL;
// ------------------------ 以新的方式创建顶点流 -------------------------------
LPD3DXMESH pMeshSysMemClone = NULL;
D3DVERTEXELEMENT9 decl[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 16, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, 20, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
};
if( FAILED( hr = pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl ))) return hr;
// ---------------------------------------------------------------------------
LPD3DXBUFFER pCode = NULL;
DWORD dwFlag = D3DXSHADER_DEBUG | D3DXSHADER_SKIPOPTIMIZATION ;
if( FAILED( hr = D3DXCreateEffectFromFile( pd3dDevice, L"FileFX.fx", NULL, NULL,
dwFlag, NULL, &m_pEffect, &pCode )))
{
LPVOID pBufErr = pCode->GetBufferPointer();
char* ErrInfo = ( char* ) pBufErr;
pCode->Release( );
return hr;
}
return hr;
}
HRESULT CFULLWINDOW::OnResetDevice( IDirect3DDevice9* pd3dDevice )
{
HRESULT hr = S_OK;
hr = m_pEffect->SetTexture( "g_texColorTexture" , m_pColorTexture );
hr = m_pEffect->SetTexture( "g_texFFTTexture" , m_pTextureFFT );
//
if( FAILED( hr = CreateSquare( pd3dDevice ))) return hr;
// ------------------- 源纹理 -----------------------------
if( FAILED( hr = D3DXCreateTexture( pd3dDevice , m_TextureSize , m_TextureSize , 1 ,
D3DUSAGE_RENDERTARGET , D3DFMT_A32B32G32R32F , D3DPOOL_DEFAULT , &m_pRTTTextureSrc )))
{
return hr;
}
D3DSURFACE_DESC desc;
hr = m_pRTTTextureSrc->GetSurfaceLevel( 0, &m_pRTTSurfaceSrc );
hr = m_pRTTSurfaceSrc->GetDesc( &desc );
if( FAILED( hr = D3DXCreateRenderToSurface( pd3dDevice , desc.Width , desc.Height ,
desc.Format , false , D3DFMT_A32B32G32R32F , &m_pRTTInterfaceSrc )))
{
return hr;
}
// ----------------- 目的纹理 ------------------------
if( FAILED( hr = D3DXCreateTexture( pd3dDevice , m_TextureSize , m_TextureSize , 1 ,
D3DUSAGE_RENDERTARGET , D3DFMT_A32B32G32R32F , D3DPOOL_DEFAULT , &m_pRTTTextureDest )))
{
return hr;
}
hr = m_pRTTTextureDest->GetSurfaceLevel( 0 , &m_pRTTSurfaceDest );
hr = m_pRTTSurfaceDest->GetDesc( &desc );
if( FAILED( hr = D3DXCreateRenderToSurface( pd3dDevice , desc.Width , desc.Height ,
desc.Format , false , D3DFMT_A32B32G32R32F , &m_pRTTInterfaceDest )))
{
return hr;
}
// 可以返回结果
return hr;
}
void CFULLWINDOW::OnFrameMove( float fTime )
{
//HRESULT hr;
}
void CFULLWINDOW::OnFrameRender( IDirect3DDevice9* pd3dDevice )
{
HRESULT hr;
hr = m_pEffect->SetTexture( "g_texSceneDest" , m_pRTTTextureDest );
hr = m_pEffect->SetTexture( "g_texSceneSrc" , m_pRTTTextureSrc );
hr = pd3dDevice->SetVertexDeclaration( m_pDecl );
hr = pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof( BOARD_D3DVERTEX ));
hr = pd3dDevice->SetIndices( m_pIB );
UINT iPass, cPasses;
// ------------------------- ScreenShader ---------------------------
//hr = m_pEffect->SetTexture( "g_texSceneDest" , m_pRTTTextureDest );
//
hr = m_pEffect->SetTechnique( "TechDefault" );
hr = m_pEffect->Begin( &cPasses, 0 );
for( iPass = 0; iPass < cPasses; iPass++ )
{
hr = m_pEffect->BeginPass( iPass );
hr = pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST , 0 , 0 , m_dwNumVertices , 0 , 2 );
hr = m_pEffect->EndPass( );
}
hr = m_pEffect->End();
}
void CFULLWINDOW::OnLostDevice()
{
if( m_pEffect ) m_pEffect->OnLostDevice();
SAFE_RELEASE( m_pIB );
SAFE_RELEASE( m_pVB );
//
SAFE_RELEASE( m_pRTTTextureSrc );
SAFE_RELEASE( m_pRTTInterfaceSrc );
SAFE_RELEASE( m_pRTTSurfaceSrc );
//
SAFE_RELEASE( m_pRTTTextureDest );
SAFE_RELEASE( m_pRTTInterfaceDest );
SAFE_RELEASE( m_pRTTSurfaceDest );
}
void CFULLWINDOW::OnDestroyDevice()
{
SAFE_RELEASE( m_pEffect );
SAFE_RELEASE( m_pDecl );
//
SAFE_RELEASE( m_pVB );
SAFE_RELEASE( m_pIB );
SAFE_RELEASE( m_pColorTexture );
SAFE_RELEASE( m_pTextureFFT );
//
SAFE_RELEASE( m_pRTTTextureSrc );
SAFE_RELEASE( m_pRTTInterfaceSrc );
SAFE_RELEASE( m_pRTTSurfaceSrc );
//
SAFE_RELEASE( m_pRTTTextureDest );
SAFE_RELEASE( m_pRTTInterfaceDest );
SAFE_RELEASE( m_pRTTSurfaceDest );
//
}
//----------------------------------------------------------------------------
// 函数:CreateSquare
// 功能:创建模型
//----------------------------------------------------------------------------
HRESULT CFULLWINDOW::CreateSquare( LPDIRECT3DDEVICE9 pd3dDevice )
{
HRESULT hr = S_OK;
m_dwNumIndices = 6; // 两个三角形共6个点
m_dwNumVertices = 6; // 6
// ---------------------------- 创建索引缓冲区 ---------------------------
{
WORD* pIndices;
if( FAILED( hr = pd3dDevice->CreateIndexBuffer( m_dwNumIndices * sizeof( WORD ),
0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_pIB, NULL ))) return hr;
if( FAILED( hr