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