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