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