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