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