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