0001: //-------------------------------------------------------------
0002: // File: main.cpp
0003: //
0004: // Desc: Gaussian フィルタ
0005: //-------------------------------------------------------------
0006: #define STRICT
0007: #include <windows.h>
0008: #include <commctrl.h>
0009: #include <commdlg.h>
0010: #include <basetsd.h>
0011: #include <math.h>
0012: #include <stdio.h>
0013: #include <d3dx9.h>
0014: #include <dxerr9.h>
0015: #include <tchar.h>
0016: #include "DXUtil.h"
0017: #include "D3DEnumeration.h"
0018: #include "D3DSettings.h"
0019: #include "D3DApp.h"
0020: #include "D3DFont.h"
0021: #include "D3DFile.h"
0022: #include "D3DUtil.h"
0023: #include "resource.h"
0024: #include "main.h"
0025: 
0026: #define MAP_WIDTH   512
0027: #define MAP_HEIGHT  512
0028: 
0029: 
0030: // 長いから短縮形を作ってみた
0031: #define RS   m_pd3dDevice->SetRenderState
0032: #define TSS  m_pd3dDevice->SetTextureStageState
0033: #define SAMP m_pd3dDevice->SetSamplerState
0034: 
0035: 
0036: //-------------------------------------------------------------
0037: // 頂点の構造体
0038: //-------------------------------------------------------------
0039: typedef struct {
0040:     FLOAT       p[4];
0041:     FLOAT       tu, tv;
0042: } TVERTEX;
0043: 
0044: //-------------------------------------------------------------
0045: // グローバル変数
0046: //-------------------------------------------------------------
0047: CMyD3DApplication* g_pApp  = NULL;
0048: HINSTANCE          g_hInst = NULL;
0049: 
0050: 
0051: //-------------------------------------------------------------
0052: // Name: WinMain()
0053: // Desc: メイン関数
0054: //-------------------------------------------------------------
0055: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0056: {
0057:     CMyD3DApplication d3dApp;
0058: 
0059:     g_pApp  = &d3dApp;
0060:     g_hInst = hInst;
0061: 
0062:     InitCommonControls();
0063:     if( FAILED( d3dApp.Create( hInst ) ) )
0064:         return 0;
0065: 
0066:     return d3dApp.Run();
0067: }
0068: 
0069: 
0070: 
0071: 
0072: //-------------------------------------------------------------
0073: // Name: CMyD3DApplication()
0074: // Desc: アプリケーションのコンストラクタ
0075: //-------------------------------------------------------------
0076: CMyD3DApplication::CMyD3DApplication()
0077: {
0078:     m_dispersion_sq             = 5.0f;
0079: 
0080:     m_pMesh                     = new CD3DMesh();
0081:     m_pMeshBg                   = new CD3DMesh();
0082: 
0083:     m_pMapZ                     = NULL;
0084:     m_pOriginalMap              = NULL;
0085:     m_pOriginalMapSurf          = NULL;
0086:     m_pXMap                     = NULL;
0087:     m_pXMapSurf                 = NULL;
0088:     m_pXYMap                    = NULL;
0089:     m_pXYMapSurf                = NULL;
0090: 
0091:     m_pEffect                   = NULL;
0092:     m_hTechnique                = NULL;
0093:     m_hafWeight                 = NULL;
0094:     m_htSrcMap                  = NULL;
0095: 
0096:     m_fWorldRotX                = -0.41271535f;
0097:     m_fWorldRotY                = 0.0f;
0098:     m_fViewZoom                 = 5.0f;
0099: 
0100:     m_dwCreationWidth           = 512;
0101:     m_dwCreationHeight          = 512;
0102:     m_strWindowTitle            = TEXT( "main" );
0103:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0104:     m_bStartFullscreen          = false;
0105:     m_bShowCursorWhenFullscreen = false;
0106: 
0107:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0108:     m_bLoadingApp               = TRUE;
0109: 
0110:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0111: }
0112: 
0113: 
0114: 
0115: 
0116: //-------------------------------------------------------------
0117: // Name: ~CMyD3DApplication()
0118: // Desc: デストラクタ
0119: //-------------------------------------------------------------
0120: CMyD3DApplication::~CMyD3DApplication()
0121: {
0122: }
0123: 
0124: 
0125: 
0126: 
0127: //-------------------------------------------------------------
0128: // Name: OneTimeSceneInit()
0129: // Desc: 一度だけ行う初期化
0130: //      ウィンドウの初期化やIDirect3D9の初期化は終わってます。
0131: //      ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。
0132: //-------------------------------------------------------------
0133: HRESULT CMyD3DApplication::OneTimeSceneInit()
0134: {
0135:     // ローディングメッセージを表示する
0136:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0137: 
0138:     m_bLoadingApp = FALSE;
0139: 
0140:     return S_OK;
0141: }
0142: 
0143: 
0144: 
0145: 
0146: //-------------------------------------------------------------
0147: // Name: ConfirmDevice()
0148: // Desc: 初期化の時に呼ばれます。必要な能力をチェックします。
0149: //-------------------------------------------------------------
0150: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0151:                      DWORD dwBehavior,    D3DFORMAT Format )
0152: {
0153:     UNREFERENCED_PARAMETER( Format );
0154:     UNREFERENCED_PARAMETER( dwBehavior );
0155:     UNREFERENCED_PARAMETER( pCaps );
0156:     
0157: 
0158:     // ピクセルシェーダバージョンチェック
0159:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0160:         return E_FAIL;
0161: 
0162:     // 頂点シェーダバージョンが上位かソフトウェア頂点処理
0163:     if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0164:     &&  0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0165:             return E_FAIL;
0166: 
0167:     return S_OK;
0168: }
0169: 
0170: 
0171: 
0172: 
0173: //-------------------------------------------------------------
0174: // Name: UpdateWeight()
0175: // Desc: 重みの計算
0176: //-------------------------------------------------------------
0177: VOID CMyD3DApplication::UpdateWeight( FLOAT dispersion )
0178: {
0179:     DWORD i;
0180: 
0181:     FLOAT total=0;
0182:     for( i=0; i<WEIGHT_MUN; i++ ){
0183:         m_tbl[i] = expf(-0.5f*(FLOAT)(i*i)/dispersion);
0184:         if(0==i){
0185:             total += m_tbl[i];
0186:         }else{
0187:             // 中心以外は、2回同じ係数を使うので2倍
0188:             total += 2.0f*m_tbl[i];
0189:         }
0190:     }
0191:     // 規格化
0192:     for( i=0; i<WEIGHT_MUN; i++ ) m_tbl[i] /= total;
0193: 
0194:     if(m_pEffect) m_pEffect->SetFloatArray(m_hafWeight
0195:                                         , m_tbl, WEIGHT_MUN);
0196: 
0197: }
0198: 
0199: 
0200: 
0201: 
0202: //-------------------------------------------------------------
0203: // Name: InitDeviceObjects()
0204: // Desc: デバイスが生成された後の初期化をします。
0205: //      フレームバッファフォーマットやデバイスの種類が変わった
0206: //      後に通過します。
0207: //      ここで確保したメモリはDeleteDeviceObjects()で開放します
0208: //-------------------------------------------------------------
0209: HRESULT CMyD3DApplication::InitDeviceObjects()
0210: {
0211:     HRESULT hr;
0212: 
0213:     // 車の読み込み
0214:     if(FAILED(hr=m_pMesh  ->Create( m_pd3dDevice, _T("ufo.x"))))
0215:         return DXTRACE_ERR( "LoadCar", hr );
0216:     // 地面の読み込み
0217:     if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, _T("map.x"))))
0218:         return DXTRACE_ERR( "Load BG", hr );
0219:         
0220:     // シェーダの読み込み
0221:     LPD3DXBUFFER pErr;
0222:     if( FAILED( hr = D3DXCreateEffectFromFile(
0223:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0224:                 D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0225:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0226:                     , "ERROR", MB_OK);
0227:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0228:     }
0229:     m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0230:     m_hafWeight = m_pEffect->GetParameterByName( NULL, "weight" );
0231:     m_htSrcMap  = m_pEffect->GetParameterByName( NULL, "SrcMap" );
0232:     hr = m_pEffect->SetFloat("MAP_WIDTH",  MAP_WIDTH);  // 幅の設定
0233:     hr = m_pEffect->SetFloat("MAP_HEIGHT", MAP_HEIGHT); // 高さの設定
0234: 
0235:     // 重みを設定
0236:     this->UpdateWeight(m_dispersion_sq*m_dispersion_sq);
0237: 
0238:     // フォント
0239:     m_pFont->InitDeviceObjects( m_pd3dDevice );
0240: 
0241:     return S_OK;
0242: }
0243: 
0244: //-------------------------------------------------------------
0245: // Name: RestoreDeviceObjects()
0246: // Desc: 画面のサイズが変更された時等に呼ばれます。
0247: //      確保したメモリはInvalidateDeviceObjects()で開放します。
0248: //-------------------------------------------------------------
0249: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0250: {
0251:     // メッシュ
0252:     m_pMesh  ->RestoreDeviceObjects( m_pd3dDevice );
0253:     m_pMeshBg->RestoreDeviceObjects( m_pd3dDevice );
0254: 
0255:     // 質感の設定
0256:     D3DMATERIAL9 mtrl;
0257:     D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0258:     m_pd3dDevice->SetMaterial( &mtrl );
0259: 
0260: 
0261:     // レンダリング状態の設定
0262:     RS( D3DRS_DITHERENABLE,   FALSE );
0263:     RS( D3DRS_SPECULARENABLE, FALSE );
0264:     RS( D3DRS_ZENABLE,        TRUE );
0265:     RS( D3DRS_AMBIENT,        0x000F0F0F );
0266:     
0267:     TSS( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0268:     TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0269:     TSS( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0270:     TSS( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
0271:     TSS( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
0272:     SAMP( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0273:     SAMP( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0274:     SAMP( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0275:     SAMP( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0276: 
0277:     // ワールド行列
0278:     D3DXMATRIX matIdentity;
0279:     D3DXMatrixIdentity( &m_mWorld );
0280: 
0281:     // ビュー行列
0282:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0283:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0284:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0285:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0286: 
0287:     // 射影行列
0288:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0289:     D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0290: 
0291:     // フォント
0292:     m_pFont->RestoreDeviceObjects();
0293: 
0294:     // シャドウマップの生成
0295:     if (FAILED(m_pd3dDevice->CreateDepthStencilSurface(MAP_WIDTH, MAP_HEIGHT, 
0296:         D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pMapZ, NULL)))
0297:         return E_FAIL;
0298:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_WIDTH, MAP_HEIGHT, 1, 
0299:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pOriginalMap, NULL)))
0300:         return E_FAIL;
0301:     if (FAILED(m_pOriginalMap->GetSurfaceLevel(0, &m_pOriginalMapSurf)))
0302:         return E_FAIL;
0303:     // エッジ
0304:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_WIDTH, MAP_HEIGHT, 1, 
0305:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pXMap, NULL)))
0306:         return E_FAIL;
0307:     if (FAILED(m_pXMap->GetSurfaceLevel(0, &m_pXMapSurf)))
0308:         return E_FAIL;
0309:     // エッジをぼかしたマップ
0310:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_WIDTH, MAP_HEIGHT, 1, 
0311:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pXYMap, NULL)))
0312:         return E_FAIL;
0313:     if (FAILED(m_pXYMap->GetSurfaceLevel(0, &m_pXYMapSurf)))
0314:         return E_FAIL;
0315: 
0316:     m_pEffect->OnResetDevice();
0317: 
0318:     return S_OK;
0319: }
0320: 
0321: 
0322: 
0323: 
0324: //-------------------------------------------------------------
0325: // Name: FrameMove()
0326: // Desc: 毎フレーム呼ばれます。アニメの処理などを行います。
0327: //-------------------------------------------------------------
0328: HRESULT CMyD3DApplication::FrameMove()
0329: {
0330:     // 入力データの更新
0331:     UpdateInput( &m_UserInput );
0332: 
0333:     //---------------------------------------------------------
0334:     // 入力に応じて座標系を更新する
0335:     //---------------------------------------------------------
0336:     // 回転
0337:     D3DXMATRIX matRotY;
0338:     D3DXMATRIX matRotX;
0339: 
0340:     if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0341:         m_fWorldRotY += m_fElapsedTime;
0342:     else
0343:     if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0344:         m_fWorldRotY -= m_fElapsedTime;
0345: 
0346:     if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0347:         m_fWorldRotX += m_fElapsedTime;
0348:     else
0349:     if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0350:         m_fWorldRotX -= m_fElapsedTime;
0351: 
0352:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0353:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0354: 
0355:     D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0356:     
0357:     //---------------------------------------------------------
0358:     // ビュー行列の設定
0359:     //---------------------------------------------------------
0360:     // ズーム
0361:     if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0362:         m_fViewZoom += m_fElapsedTime;
0363:     else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0364:         m_fViewZoom -= m_fElapsedTime;
0365: 
0366:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0367:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0368:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0369:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0370: 
0371: 
0372:     if( m_UserInput.bDispersionUp && !m_UserInput.bDispersionDown ){
0373:         m_dispersion_sq += m_fElapsedTime;
0374:         this->UpdateWeight(m_dispersion_sq*m_dispersion_sq);
0375:     } else
0376:     if( m_UserInput.bDispersionDown && !m_UserInput.bDispersionUp ){
0377:         m_dispersion_sq -= m_fElapsedTime;
0378:         this->UpdateWeight(m_dispersion_sq*m_dispersion_sq);
0379:     }
0380: 
0381: 
0382:     return S_OK;
0383: }
0384: //-------------------------------------------------------------
0385: // Name: UpdateInput()
0386: // Desc: 入力データを更新する
0387: //-------------------------------------------------------------
0388: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0389: {
0390:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0391:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0392:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0393:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0394:     
0395:     pUserInput->bZoomIn      = ( m_bActive && (GetAsyncKeyState( 'Z'     )  & 0x8000) == 0x8000 );
0396:     pUserInput->bZoomOut     = ( m_bActive && (GetAsyncKeyState( 'X'      ) & 0x8000) == 0x8000 );
0397: 
0398:     pUserInput->bDispersionUp   = ( m_bActive && (GetAsyncKeyState( VK_PRIOR) & 0x8000) == 0x8000 );
0399:     pUserInput->bDispersionDown = ( m_bActive && (GetAsyncKeyState( VK_NEXT ) & 0x8000) == 0x8000 );
0400: }
0401: 
0402: 
0403: //-------------------------------------------------------------
0404: // Name: Render()
0405: // Desc: 画面を描画する.
0406: //-------------------------------------------------------------
0407: HRESULT CMyD3DApplication::Render()
0408: {
0409:     D3DXMATRIX m, mT, mR, mView, mProj;
0410:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0411:     D3DVIEWPORT9 oldViewport;
0412: 
0413:     //---------------------------------------------------------
0414:     // 描画
0415:     //---------------------------------------------------------
0416:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0417:     {
0418:         //-------------------------------------------------
0419:         // レンダリングターゲットの保存
0420:         //-------------------------------------------------
0421:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0422:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0423:         m_pd3dDevice->GetViewport(&oldViewport);
0424: 
0425:         if( m_pEffect != NULL ) 
0426:         {
0427:             //-------------------------------------------------
0428:             // レンダリングターゲットの変更
0429:             //-------------------------------------------------
0430:             m_pd3dDevice->SetRenderTarget(0, m_pOriginalMapSurf);
0431:             m_pd3dDevice->SetDepthStencilSurface(m_pMapZ);
0432:             // ビューポートの変更
0433:             D3DVIEWPORT9 viewport = {0,0      // 左上の座標
0434:                             , MAP_WIDTH  // 幅
0435:                             , MAP_HEIGHT // 高さ
0436:                             , 0.0f,1.0f};     // 前面、後面
0437:             m_pd3dDevice->SetViewport(&viewport);
0438: 
0439:             // シャドウマップのクリア
0440:             m_pd3dDevice->Clear(0L, NULL
0441:                             , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0442:                             , 0xffffffff, 1.0f, 0L);
0443: 
0444:             //-------------------------------------------------
0445:             // 1パス目:レンダリングターゲットの作成
0446:             //-------------------------------------------------
0447:             m_pd3dDevice->SetTransform( D3DTS_WORLD,  &m_mWorld);
0448:             m_pd3dDevice->SetTransform( D3DTS_VIEW,  &m_mView );
0449:             m_pd3dDevice->SetTransform( D3DTS_PROJECTION,  &m_mProj );
0450: 
0451:             TSS( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
0452:             TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0453:             m_pMeshBg->Render( m_pd3dDevice );
0454:             
0455:             // 飛行モデルの描画
0456:             TSS( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
0457:             TSS( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );
0458:             RS( D3DRS_LIGHTING, TRUE );
0459:             RS( D3DRS_AMBIENT, 0 );
0460:             m_pd3dDevice->LightEnable( 0, TRUE );
0461:             D3DXMatrixTranslation( &m, 1.0f, 0.0f ,0.0f );
0462:             D3DXMatrixRotationY( &mR,  m_fTime );
0463:             D3DXMatrixTranslation( &mT, 1.0f, 1.0f ,0.0f );
0464:             m = m * mR * mT * m_mWorld;
0465:             m_pd3dDevice->SetTransform( D3DTS_WORLD,  &m);
0466:             m_pMesh  ->Render( m_pd3dDevice );
0467: 
0468:             //-------------------------------------------------
0469:             // シェーダの設定
0470:             //-------------------------------------------------
0471:             m_pEffect->SetTechnique( m_hTechnique );
0472:             m_pEffect->Begin( NULL, 0 );
0473: 
0474:             //-------------------------------------------------
0475:             // ぼかしx
0476:             //-------------------------------------------------
0477:             m_pd3dDevice->SetRenderTarget(0, m_pXMapSurf);
0478:             m_pEffect->Pass( 0 );
0479: 
0480:             RS( D3DRS_ZENABLE, FALSE );
0481:             RS( D3DRS_LIGHTING, FALSE );
0482:             TSS(0,D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
0483:             TSS(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0484:             TSS(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0485: 
0486:             TVERTEX Vertex1[4] = {
0487:                 //   x         y         z     w     tu tv
0488:                 {     0.0f,       0.0f, 0.1f, 1.0f,   0, 0,},
0489:                 {MAP_WIDTH,       0.0f, 0.1f, 1.0f,   1, 0,},
0490:                 {MAP_WIDTH, MAP_HEIGHT, 0.1f, 1.0f,   1, 1,},
0491:                 {     0.0f, MAP_HEIGHT, 0.1f, 1.0f,   0, 1,},
0492:             };
0493:             m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0494:             m_pEffect->SetTexture(m_htSrcMap, m_pOriginalMap);
0495:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0496:                             , 2, Vertex1, sizeof( TVERTEX ) );
0497: 
0498:             //-------------------------------------------------
0499:             // ぼかしy
0500:             //-------------------------------------------------
0501:             m_pd3dDevice->SetRenderTarget(0, m_pXYMapSurf);
0502:             m_pEffect->Pass( 1 );
0503: 
0504:             m_pEffect->SetTexture(m_htSrcMap, m_pXMap);
0505:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0506:                             , 2, Vertex1, sizeof( TVERTEX ) );
0507: 
0508:             m_pEffect->End();
0509:         }
0510: 
0511:         //-----------------------------------------------------
0512:         // レンダリングターゲットを元に戻す
0513:         //-----------------------------------------------------
0514:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0515:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0516:         m_pd3dDevice->SetViewport(&oldViewport);
0517:         pOldBackBuffer->Release();
0518:         pOldZBuffer->Release();
0519: 
0520:         // バッファのクリア
0521:         m_pd3dDevice->Clear( 0L, NULL
0522:                         , D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER
0523:                         , 0x00404080, 1.0f, 0L );
0524: 
0525:         //-----------------------------------------------------
0526:         // そのまま張る
0527:         //-----------------------------------------------------
0528:         FLOAT w = (FLOAT)oldViewport.Width;
0529:         FLOAT h = (FLOAT)oldViewport.Height;
0530:         TVERTEX Vertex1[4] = {
0531:             //x  y   z    rhw  tu tv
0532:             { 0, 0, 0.1f, 1.0f, 0, 0,},
0533:             { w, 0, 0.1f, 1.0f, 1, 0,},
0534:             { w, h, 0.1f, 1.0f, 1, 1,},
0535:             { 0, h, 0.1f, 1.0f, 0, 1,},
0536:         };
0537:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0538:         m_pd3dDevice->SetTexture(0, m_pOriginalMap);
0539:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0540:                             , 2, Vertex1, sizeof( TVERTEX ) );
0541: 
0542:         //-----------------------------------------------------
0543:         // ブラーしたものを張る
0544:         //-----------------------------------------------------
0545: 
0546:         TVERTEX Vertex2[4] = {
0547:             //   x    y   z    rhw    tu    tv
0548:             { 0.0f,   0, 0.1f, 1.0f, 0.0f, 0.0f,},
0549:             { 0.5f*w, 0, 0.1f, 1.0f, 0.5f, 0.0f,},
0550:             { 0.5f*w, h, 0.1f, 1.0f, 0.5f, 1.0f,},
0551:             { 0.0f,   h, 0.1f, 1.0f, 0.0f, 1.0f,},
0552:         };
0553:         m_pd3dDevice->SetTexture(0, m_pXYMap);
0554:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0555:                             , 2, Vertex2, sizeof( TVERTEX ) );
0556: 
0557:         RS( D3DRS_ZENABLE, TRUE );
0558:         RS( D3DRS_LIGHTING, TRUE );
0559: 
0560:         // ヘルプの表示
0561:         RenderText();
0562: 
0563: #if 0 // デバッグ用にテクスチャを表示する
0564:         {
0565:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0566:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0567:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0568:         m_pd3dDevice->SetVertexShader(NULL);
0569:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0570:         m_pd3dDevice->SetPixelShader(0);
0571:         float scale = 128.0f;
0572:         for(DWORD i=0; i<3; i++){
0573:             TVERTEX Vertex[4] = {
0574:                 // x  y  z rhw tu tv
0575:                 {    0,(i+0)*scale,0, 1, 0, 0,},
0576:                 {scale,(i+0)*scale,0, 1, 1, 0,},
0577:                 {scale,(i+1)*scale,0, 1, 1, 1,},
0578:                 {    0,(i+1)*scale,0, 1, 0, 1,},
0579:             };
0580:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pOriginalMap );
0581:             if(1==i) m_pd3dDevice->SetTexture( 0, m_pXMap );
0582:             if(2==i) m_pd3dDevice->SetTexture( 0, m_pXYMap );
0583:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0584:         }
0585:         }
0586: #endif      
0587: 
0588:         // 描画の終了
0589:         m_pd3dDevice->EndScene();
0590:     }
0591: 
0592:     return S_OK;
0593: }
0594: 
0595: 
0596: 
0597: 
0598: //-------------------------------------------------------------
0599: // Name: RenderText()
0600: // Desc: 状態やヘルプを画面に表示する
0601: //-------------------------------------------------------------
0602: HRESULT CMyD3DApplication::RenderText()
0603: {
0604:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
0605:     TCHAR szMsg[MAX_PATH] = TEXT("");
0606: 
0607:     FLOAT fNextLine = 40.0f; // 表示する高さ
0608: 
0609:     // 操作法やパラメータを表示する
0610:     fNextLine = (FLOAT) m_d3dsdBackBuffer.Height; 
0611:     sprintf( szMsg, TEXT("Use Page Up/Down keys to change dispersion (Now %f^2)"), m_dispersion_sq );
0612:     fNextLine -= 20.0f;
0613:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0614:     lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
0615:     fNextLine -= 20.0f;
0616:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0617: 
0618:     lstrcpy( szMsg, m_strDeviceStats );
0619:     fNextLine -= 20.0f;
0620:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0621:     lstrcpy( szMsg, m_strFrameStats );
0622:     fNextLine -= 20.0f;
0623:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0624:     
0625:     return S_OK;
0626: }
0627: 
0628: 
0629: 
0630: 
0631: //-------------------------------------------------------------
0632: // Name: MsgProc()
0633: // Desc: WndProc をオーバーライドしたもの
0634: //-------------------------------------------------------------
0635: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
0636:                                  WPARAM wParam, LPARAM lParam )
0637: {
0638:     switch( msg )
0639:     {
0640:         case WM_PAINT:
0641:         {
0642:             if( m_bLoadingApp )
0643:             {
0644:                 // ロード中
0645:                 HDC hDC = GetDC( hWnd );
0646:                 TCHAR strMsg[MAX_PATH];
0647:                 wsprintf(strMsg, TEXT("Loading... Please wait"));
0648:                 RECT rct;
0649:                 GetClientRect( hWnd, &rct );
0650:                 DrawText( hDC, strMsg, -1, &rct
0651:                         , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0652:                 ReleaseDC( hWnd, hDC );
0653:             }
0654:             break;
0655:         }
0656: 
0657:     }
0658: 
0659:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0660: }
0661: 
0662: 
0663: 
0664: 
0665: //-------------------------------------------------------------
0666: // Name: InvalidateDeviceObjects()
0667: // Desc: RestoreDeviceObjects() で作成したオブジェクトの開放
0668: //-------------------------------------------------------------
0669: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0670: {
0671:     // シャドウマップ
0672:     SAFE_RELEASE(m_pXYMapSurf);
0673:     SAFE_RELEASE(m_pXYMap);
0674:     SAFE_RELEASE(m_pXMapSurf);
0675:     SAFE_RELEASE(m_pXMap);
0676:     SAFE_RELEASE(m_pOriginalMapSurf);
0677:     SAFE_RELEASE(m_pOriginalMap);
0678:     SAFE_RELEASE(m_pMapZ);
0679: 
0680:     m_pMesh  ->InvalidateDeviceObjects(); // メッシュ
0681:     m_pMeshBg->InvalidateDeviceObjects();
0682: 
0683:     m_pFont->InvalidateDeviceObjects(); // フォント
0684: 
0685:     // シェーダ
0686:     if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
0687: 
0688:     return S_OK;
0689: }
0690: 
0691: 
0692: 
0693: 
0694: //-------------------------------------------------------------
0695: // Name: DeleteDeviceObjects()
0696: // Desc: InitDeviceObjects() で作成したオブジェクトを開放する
0697: //-------------------------------------------------------------
0698: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0699: {
0700:     // シェーダ
0701:     SAFE_RELEASE( m_pEffect );
0702:     
0703:     // メッシュ
0704:     m_pMesh  ->Destroy();
0705:     m_pMeshBg->Destroy();
0706: 
0707:     // フォント
0708:     m_pFont->DeleteDeviceObjects();
0709: 
0710:     return S_OK;
0711: }
0712: 
0713: 
0714: 
0715: 
0716: //-------------------------------------------------------------
0717: // Name: FinalCleanup()
0718: // Desc: 終了する直線に呼ばれる
0719: //-------------------------------------------------------------
0720: HRESULT CMyD3DApplication::FinalCleanup()
0721: {
0722:     SAFE_DELETE( m_pMeshBg ); // メッシュ
0723:     SAFE_DELETE( m_pMesh );
0724: 
0725:     SAFE_DELETE( m_pFont ); // フォント
0726: 
0727:     return S_OK;
0728: }
0729: 
0730: 
0731: 
0732: 
0733: