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