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