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 RENDERING_WIDTH 512 0027: #define RENDERING_HEIGHT 512 0028: #define MAP_WIDTH 128 0029: #define MAP_HEIGHT 128 0030: 0031: #define PASS_SCENE 0 0032: #define PASS_REDUCTION 1 0033: #define PASS_X 2 0034: #define PASS_Y 3 0035: #define PASS_FINAL 4 0036: 0037: // 頂点宣言 0038: D3DVERTEXELEMENT9 decl[] = 0039: { 0040: {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, 0041: {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, 0042: D3DDECL_END() 0043: }; 0044: 0045: // 長いから短縮形を作ってみた 0046: #define RS m_pd3dDevice->SetRenderState 0047: #define TSS m_pd3dDevice->SetTextureStageState 0048: #define SAMP m_pd3dDevice->SetSamplerState 0049: 0050: 0051: //------------------------------------------------------------- 0052: // 頂点の構造体 0053: //------------------------------------------------------------- 0054: typedef struct { 0055: FLOAT p[4]; 0056: FLOAT tu, tv; 0057: } TVERTEX; 0058: typedef struct { 0059: FLOAT p[4]; 0060: FLOAT uv1[2]; 0061: FLOAT uv2[2]; 0062: } T2VERTEX; 0063: 0064: //------------------------------------------------------------- 0065: // グローバル変数 0066: //------------------------------------------------------------- 0067: CMyD3DApplication* g_pApp = NULL; 0068: HINSTANCE g_hInst = NULL; 0069: 0070: 0071: //------------------------------------------------------------- 0072: // Name: WinMain() 0073: // Desc: メイン関数 0074: //------------------------------------------------------------- 0075: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) 0076: { 0077: CMyD3DApplication d3dApp; 0078: 0079: g_pApp = &d3dApp; 0080: g_hInst = hInst; 0081: 0082: InitCommonControls(); 0083: if( FAILED( d3dApp.Create( hInst ) ) ) 0084: return 0; 0085: 0086: return d3dApp.Run(); 0087: } 0088: 0089: 0090: 0091: 0092: //------------------------------------------------------------- 0093: // Name: CMyD3DApplication() 0094: // Desc: アプリケーションのコンストラクタ 0095: //------------------------------------------------------------- 0096: CMyD3DApplication::CMyD3DApplication() 0097: { 0098: m_pMesh = new CD3DMesh(); 0099: 0100: m_pMapZ = NULL; 0101: m_pOriginalTex = NULL; 0102: m_pOriginalSurf = NULL; 0103: m_pLumTex = NULL; 0104: m_pLumSurf = NULL; 0105: m_pXMap = NULL; 0106: m_pXMapSurf = NULL; 0107: m_pLargeTex = NULL; 0108: m_pLargeSurf = NULL; 0109: m_pSmallTex = NULL; 0110: m_pSmallSurf = NULL; 0111: 0112: m_pEffect = NULL; 0113: m_hTechnique = NULL; 0114: m_hafWeight = NULL; 0115: m_htSrcMap = NULL; 0116: m_htSrcMap2 = NULL; 0117: m_hmWVP = NULL; 0118: m_hvLightDir = NULL; 0119: m_hvEyePos = NULL; 0120: m_hvCol = NULL; 0121: m_pDecl = NULL; 0122: 0123: m_fWorldRotX = -0.41271535f; 0124: m_fWorldRotY = D3DX_PI/2; 0125: m_fViewZoom = 2.0f; 0126: 0127: m_dwCreationWidth = 512; 0128: m_dwCreationHeight = 512; 0129: m_strWindowTitle = TEXT( "main" ); 0130: m_d3dEnumeration.AppUsesDepthBuffer = TRUE; 0131: m_bStartFullscreen = false; 0132: m_bShowCursorWhenFullscreen = false; 0133: 0134: m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD ); 0135: m_bLoadingApp = TRUE; 0136: 0137: ZeroMemory( &m_UserInput, sizeof(m_UserInput) ); 0138: } 0139: 0140: 0141: 0142: 0143: //------------------------------------------------------------- 0144: // Name: ~CMyD3DApplication() 0145: // Desc: デストラクタ 0146: //------------------------------------------------------------- 0147: CMyD3DApplication::~CMyD3DApplication() 0148: { 0149: } 0150: 0151: 0152: 0153: 0154: //------------------------------------------------------------- 0155: // Name: OneTimeSceneInit() 0156: // Desc: 一度だけ行う初期化 0157: // ウィンドウの初期化やIDirect3D9の初期化は終わってます。 0158: // ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。 0159: //------------------------------------------------------------- 0160: HRESULT CMyD3DApplication::OneTimeSceneInit() 0161: { 0162: // ローディングメッセージを表示する 0163: SendMessage( m_hWnd, WM_PAINT, 0, 0 ); 0164: 0165: m_bLoadingApp = FALSE; 0166: 0167: return S_OK; 0168: } 0169: 0170: 0171: 0172: 0173: //------------------------------------------------------------- 0174: // Name: ConfirmDevice() 0175: // Desc: 初期化の時に呼ばれます。必要な能力をチェックします。 0176: //------------------------------------------------------------- 0177: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, 0178: DWORD dwBehavior, D3DFORMAT Format ) 0179: { 0180: UNREFERENCED_PARAMETER( Format ); 0181: UNREFERENCED_PARAMETER( dwBehavior ); 0182: UNREFERENCED_PARAMETER( pCaps ); 0183: 0184: 0185: // ピクセルシェーダバージョンチェック 0186: if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) ) 0187: return E_FAIL; 0188: 0189: // 頂点シェーダバージョンが上位かソフトウェア頂点処理 0190: if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) 0191: && 0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) ) 0192: return E_FAIL; 0193: 0194: return S_OK; 0195: } 0196: 0197: 0198: 0199: 0200: //------------------------------------------------------------- 0201: // Name: UpdateWeight() 0202: // Desc: 重みの計算 0203: //------------------------------------------------------------- 0204: VOID CMyD3DApplication::UpdateWeight( ) 0205: { 0206: DWORD i, j; 0207: FLOAT size[2][3] = { 0208: // 赤 緑 青 0209: { 30.0f, 17.5f, 9.8f},// 外側 0210: { 20.0f, 11.2f, 7.5f},// 内側 0211: }; 0212: 0213: for(j=0;j<2;j++){ 0214: FLOAT total[3]={0,0,0}; 0215: for( i=0; i<WEIGHT_MUN; i++ ){ 0216: FLOAT pos = 1.0f+2.0f*(FLOAT)i; 0217: // ガウス重みを計算する 0218: m_tbl[j][i].x = expf(-0.5f*(FLOAT)(pos*pos)/size[j][0]); 0219: m_tbl[j][i].y = expf(-0.5f*(FLOAT)(pos*pos)/size[j][1]); 0220: m_tbl[j][i].z = expf(-0.5f*(FLOAT)(pos*pos)/size[j][2]); 0221: // 規格化のための重みの総和を計算する 0222: total[0] += 2.0f*m_tbl[j][i].x; 0223: total[1] += 2.0f*m_tbl[j][i].y; 0224: total[2] += 2.0f*m_tbl[j][i].z; 0225: 0226: m_tbl[j][i].w = 1; 0227: } 0228: // 規格化 0229: for( i=0; i<WEIGHT_MUN; i++ ){ 0230: m_tbl[j][i].x /= total[0]; 0231: m_tbl[j][i].y /= total[1]; 0232: m_tbl[j][i].z /= total[2]; 0233: } 0234: } 0235: } 0236: 0237: 0238: 0239: 0240: //------------------------------------------------------------- 0241: // Name: InitDeviceObjects() 0242: // Desc: デバイスが生成された後の初期化をします。 0243: // フレームバッファフォーマットやデバイスの種類が変わった 0244: // 後に通過します。 0245: // ここで確保したメモリはDeleteDeviceObjects()で開放します 0246: //------------------------------------------------------------- 0247: HRESULT CMyD3DApplication::InitDeviceObjects() 0248: { 0249: HRESULT hr; 0250: D3DXVECTOR4 offset; 0251: 0252: // 車の読み込み 0253: if(FAILED(hr=m_pMesh ->Create( m_pd3dDevice, _T("ufo.x")))) 0254: return DXTRACE_ERR( "LoadCar", hr ); 0255: 0256: // 頂点宣言のオブジェクトの生成 0257: if( FAILED( hr = m_pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl ) ) ) 0258: return DXTRACE_ERR( "CreateVertexDeclaration", hr ); 0259: 0260: // シェーダの読み込み 0261: LPD3DXBUFFER pErr; 0262: if( FAILED( hr = D3DXCreateEffectFromFile( 0263: m_pd3dDevice, "hlsl.fx", NULL, NULL, 0264: D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){ 0265: MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer() 0266: , "ERROR", MB_OK); 0267: return DXTRACE_ERR( "CreateEffectFromFile", hr ); 0268: } 0269: m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" ); 0270: m_hafWeight = m_pEffect->GetParameterByName( NULL, "weight" ); 0271: m_htSrcMap = m_pEffect->GetParameterByName( NULL, "SrcMap" ); 0272: m_htSrcMap2 = m_pEffect->GetParameterByName( NULL, "SrcMap2" ); 0273: m_hmWVP = m_pEffect->GetParameterByName( NULL, "mWVP" ); 0274: m_hvLightDir = m_pEffect->GetParameterByName( NULL, "vLightDir" ); 0275: m_hvEyePos = m_pEffect->GetParameterByName( NULL, "vEyePos" ); 0276: m_hvCol = m_pEffect->GetParameterByName( NULL, "vCol" ); 0277: m_pEffect->SetFloat("MAP_WIDTH", MAP_WIDTH); // 幅の設定 0278: m_pEffect->SetFloat("MAP_HEIGHT", MAP_HEIGHT); // 高さの設定 0279: 0280: // シェーダ内で使う8テクセル先を指定する定数 0281: offset.x = 8.0f/MAP_WIDTH; offset.y = 0.0f/MAP_HEIGHT; 0282: m_pEffect->SetVector("offsetX", &offset); 0283: offset.x = 0.0f/MAP_WIDTH; offset.y = 8.0f/MAP_HEIGHT; 0284: m_pEffect->SetVector("offsetY", &offset); 0285: 0286: // 重みを設定 0287: this->UpdateWeight(); 0288: 0289: // フォント 0290: m_pFont->InitDeviceObjects( m_pd3dDevice ); 0291: 0292: return S_OK; 0293: } 0294: 0295: //------------------------------------------------------------- 0296: // Name: RestoreDeviceObjects() 0297: // Desc: 画面のサイズが変更された時等に呼ばれます。 0298: // 確保したメモリはInvalidateDeviceObjects()で開放します。 0299: //------------------------------------------------------------- 0300: HRESULT CMyD3DApplication::RestoreDeviceObjects() 0301: { 0302: // メッシュ 0303: m_pMesh ->RestoreDeviceObjects( m_pd3dDevice ); 0304: 0305: // レンダリング状態の設定 0306: RS( D3DRS_DITHERENABLE, FALSE ); 0307: RS( D3DRS_SPECULARENABLE, FALSE ); 0308: RS( D3DRS_ZENABLE, TRUE ); 0309: 0310: TSS( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); 0311: TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); 0312: TSS( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); 0313: TSS( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); 0314: TSS( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE ); 0315: SAMP( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); 0316: SAMP( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); 0317: SAMP( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP ); 0318: SAMP( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP ); 0319: 0320: // ワールド行列 0321: D3DXMATRIX matIdentity; 0322: D3DXMatrixIdentity( &m_mWorld ); 0323: 0324: // ビュー行列 0325: D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -5.0f ); 0326: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f ); 0327: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f ); 0328: D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec ); 0329: 0330: // 射影行列 0331: FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height; 0332: D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f ); 0333: 0334: // フォント 0335: m_pFont->RestoreDeviceObjects(); 0336: 0337: // レンダリングターゲットの生成 0338: if (FAILED(m_pd3dDevice->CreateDepthStencilSurface(RENDERING_WIDTH, RENDERING_HEIGHT, 0339: D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pMapZ, NULL))) 0340: return E_FAIL; 0341: if (FAILED(m_pd3dDevice->CreateTexture(RENDERING_WIDTH, RENDERING_HEIGHT, 1, 0342: D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pOriginalTex, NULL))) 0343: return E_FAIL; 0344: // 縮小バッファ 0345: if (FAILED(m_pOriginalTex->GetSurfaceLevel(0, &m_pOriginalSurf))) 0346: return E_FAIL; 0347: if (FAILED(m_pd3dDevice->CreateTexture(MAP_WIDTH, MAP_HEIGHT, 1, 0348: D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pLumTex, NULL))) 0349: return E_FAIL; 0350: if (FAILED(m_pLumTex->GetSurfaceLevel(0, &m_pLumSurf))) 0351: return E_FAIL; 0352: // X軸方向のぼかし 0353: if (FAILED(m_pd3dDevice->CreateTexture(MAP_WIDTH, MAP_HEIGHT, 1, 0354: D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pXMap, NULL))) 0355: return E_FAIL; 0356: if (FAILED(m_pXMap->GetSurfaceLevel(0, &m_pXMapSurf))) 0357: return E_FAIL; 0358: // 大き目の円 0359: if (FAILED(m_pd3dDevice->CreateTexture(MAP_WIDTH, MAP_HEIGHT, 1, 0360: D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pLargeTex, NULL))) 0361: return E_FAIL; 0362: if (FAILED(m_pLargeTex->GetSurfaceLevel(0, &m_pLargeSurf))) 0363: return E_FAIL; 0364: // 小さ目の円 0365: if (FAILED(m_pd3dDevice->CreateTexture(MAP_WIDTH, MAP_HEIGHT, 1, 0366: D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pSmallTex, NULL))) 0367: return E_FAIL; 0368: if (FAILED(m_pSmallTex->GetSurfaceLevel(0, &m_pSmallSurf))) 0369: return E_FAIL; 0370: 0371: m_pEffect->OnResetDevice(); 0372: 0373: return S_OK; 0374: } 0375: 0376: 0377: 0378: 0379: //------------------------------------------------------------- 0380: // Name: FrameMove() 0381: // Desc: 毎フレーム呼ばれます。アニメの処理などを行います。 0382: //------------------------------------------------------------- 0383: HRESULT CMyD3DApplication::FrameMove() 0384: { 0385: // 入力データの更新 0386: UpdateInput( &m_UserInput ); 0387: 0388: //--------------------------------------------------------- 0389: // 入力に応じて座標系を更新する 0390: //--------------------------------------------------------- 0391: // 回転 0392: D3DXMATRIX matRotY; 0393: D3DXMATRIX matRotX; 0394: 0395: if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight ) 0396: m_fWorldRotY += m_fElapsedTime; 0397: else 0398: if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft ) 0399: m_fWorldRotY -= m_fElapsedTime; 0400: 0401: if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown ) 0402: m_fWorldRotX += m_fElapsedTime; 0403: else 0404: if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp ) 0405: m_fWorldRotX -= m_fElapsedTime; 0406: 0407: D3DXMatrixRotationX( &matRotX, m_fWorldRotX ); 0408: D3DXMatrixRotationY( &matRotY, m_fWorldRotY ); 0409: 0410: D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX ); 0411: 0412: //--------------------------------------------------------- 0413: // ビュー行列の設定 0414: //--------------------------------------------------------- 0415: // ズーム 0416: if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut ) 0417: m_fViewZoom += m_fElapsedTime; 0418: else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn ) 0419: m_fViewZoom -= m_fElapsedTime; 0420: 0421: D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom ); 0422: D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f ); 0423: D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f ); 0424: D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec ); 0425: 0426: return S_OK; 0427: } 0428: //------------------------------------------------------------- 0429: // Name: UpdateInput() 0430: // Desc: 入力データを更新する 0431: //------------------------------------------------------------- 0432: void CMyD3DApplication::UpdateInput( UserInput* pUserInput ) 0433: { 0434: pUserInput->bRotateUp = ( m_bActive && (GetAsyncKeyState( VK_UP ) & 0x8000) == 0x8000 ); 0435: pUserInput->bRotateDown = ( m_bActive && (GetAsyncKeyState( VK_DOWN ) & 0x8000) == 0x8000 ); 0436: pUserInput->bRotateLeft = ( m_bActive && (GetAsyncKeyState( VK_LEFT ) & 0x8000) == 0x8000 ); 0437: pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 ); 0438: 0439: pUserInput->bZoomIn = ( m_bActive && (GetAsyncKeyState( 'Z' ) & 0x8000) == 0x8000 ); 0440: pUserInput->bZoomOut = ( m_bActive && (GetAsyncKeyState( 'X' ) & 0x8000) == 0x8000 ); 0441: } 0442: 0443: 0444: //------------------------------------------------------------- 0445: // Name: Render() 0446: // Desc: 画面を描画する. 0447: //------------------------------------------------------------- 0448: HRESULT CMyD3DApplication::Render() 0449: { 0450: D3DXMATRIX m, mL, mR, mView, mProj; 0451: LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer; 0452: D3DVIEWPORT9 oldViewport; 0453: D3DXVECTOR4 v, light_pos; 0454: D3DMATERIAL9 *pMtrl; 0455: DWORD i; 0456: 0457: //--------------------------------------------------------- 0458: // 描画 0459: //--------------------------------------------------------- 0460: if( SUCCEEDED( m_pd3dDevice->BeginScene() ) ) 0461: { 0462: //------------------------------------------------- 0463: // レンダリングターゲットの保存 0464: //------------------------------------------------- 0465: m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer); 0466: m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer); 0467: m_pd3dDevice->GetViewport(&oldViewport); 0468: 0469: if( m_pEffect != NULL ) 0470: { 0471: //------------------------------------------------- 0472: // レンダリングターゲットの変更 0473: //------------------------------------------------- 0474: m_pd3dDevice->SetRenderTarget(0, m_pOriginalSurf); 0475: m_pd3dDevice->SetDepthStencilSurface(m_pMapZ); 0476: // ビューポートの変更 0477: D3DVIEWPORT9 viewport = {0,0 // 左上の座標 0478: , RENDERING_WIDTH // 幅 0479: , RENDERING_HEIGHT // 高さ 0480: , 0.0f,1.0f}; // 前面、後面 0481: m_pd3dDevice->SetViewport(&viewport); 0482: 0483: // レンダリングターゲットのクリア 0484: m_pd3dDevice->Clear(0L, NULL 0485: , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER 0486: , 0x000000, 1.0f, 0L); 0487: 0488: //------------------------------------------------- 0489: // シーンの描画 0490: //------------------------------------------------- 0491: TSS( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); 0492: TSS( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE ); 0493: 0494: m_pEffect->SetTechnique( m_hTechnique ); 0495: m_pEffect->Begin( NULL, 0 ); 0496: m_pEffect->Pass( PASS_SCENE ); 0497: 0498: // 移動 0499: D3DXMatrixTranslation( &m, 0.5f, 0.0f ,0.0f ); 0500: D3DXMatrixRotationY( &mR, m_fTime ); 0501: mL = m * mR * m_mWorld; 0502: // ライトの方向 0503: D3DXMatrixInverse( &m, NULL, &mL); 0504: light_pos = D3DXVECTOR4(1,1,-1,0); 0505: D3DXVec4Transform( &v, &light_pos, &m ); 0506: v.w = 0; 0507: D3DXVec4Normalize( &v, &v ); 0508: m_pEffect->SetVector( m_hvLightDir, &v ); 0509: // 視点 0510: D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom ); 0511: D3DXVec3Transform( &v, &vFromPt, &m ); 0512: m_pEffect->SetVector( m_hvEyePos, &v ); 0513: // ローカル-射影変換行列 0514: m = mL * m_mView * m_mProj; 0515: m_pEffect->SetMatrix( m_hmWVP, &m ); 0516: 0517: pMtrl = m_pMesh->m_pMaterials; 0518: for( i=0; i<m_pMesh->m_dwNumMaterials; i++ ) { 0519: v.x = pMtrl->Diffuse.r; 0520: v.y = pMtrl->Diffuse.g; 0521: v.z = pMtrl->Diffuse.b; 0522: v.w = pMtrl->Diffuse.a; 0523: m_pEffect->SetVector( m_hvCol, &v ); 0524: m_pMesh->m_pLocalMesh->DrawSubset( i ); // 描画 0525: pMtrl++; 0526: } 0527: 0528: m_pEffect->End(); 0529: 0530: //------------------------------------------------- 0531: // 縮小マップの作成 0532: //------------------------------------------------- 0533: m_pd3dDevice->SetRenderTarget(0, m_pLumSurf); 0534: D3DVIEWPORT9 viewport_map = {0,0 // 左上の座標 0535: , MAP_WIDTH // 幅 0536: , MAP_HEIGHT // 高さ 0537: , 0.0f,1.0f}; // 前面、後面 0538: m_pd3dDevice->SetViewport(&viewport_map); 0539: 0540: RS( D3DRS_ZENABLE, FALSE ); 0541: RS( D3DRS_LIGHTING, FALSE ); 0542: TSS(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1); 0543: TSS(0,D3DTSS_COLORARG1, D3DTA_TEXTURE); 0544: TSS(1,D3DTSS_COLOROP, D3DTOP_DISABLE); 0545: 0546: FLOAT w = (FLOAT)MAP_WIDTH; 0547: FLOAT h = (FLOAT)MAP_HEIGHT; 0548: FLOAT du = 0.5f/RENDERING_WIDTH; 0549: FLOAT dv = 0.5f/RENDERING_HEIGHT; 0550: TVERTEX Vertex0[4] = { 0551: //x y z rhw tu tv 0552: { 0, 0, 0.1f, 1.0f, 0+du, 0+dv,}, 0553: { w, 0, 0.1f, 1.0f, 1+du, 0+dv,}, 0554: { w, h, 0.1f, 1.0f, 1+du, 1+dv,}, 0555: { 0, h, 0.1f, 1.0f, 0+du, 1+dv,}, 0556: }; 0557: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 ); 0558: m_pd3dDevice->SetTexture(0, m_pOriginalTex); 0559: 0560: m_pEffect->SetTechnique( m_hTechnique ); 0561: m_pEffect->Begin( NULL, 0 ); 0562: m_pEffect->Pass( PASS_REDUCTION ); 0563: m_pEffect->SetTexture(m_htSrcMap, m_pOriginalTex); 0564: 0565: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN 0566: , 2, Vertex0, sizeof( TVERTEX ) ); 0567: 0568: m_pEffect->End(); 0569: 0570: //------------------------------------------------- 0571: // シェーダの設定 0572: //------------------------------------------------- 0573: m_pEffect->SetTechnique( m_hTechnique ); 0574: m_pEffect->Begin( NULL, 0 ); 0575: 0576: //------------------------------------------------- 0577: // 大きなぼかしx 0578: //------------------------------------------------- 0579: typedef struct {FLOAT p[3];FLOAT tu, tv;} VERTEX; 0580: VERTEX Vertex1[4] = { 0581: // x y z tu tv 0582: {{ 1.0f, -1.0f, 0.1f}, 1, 1,}, 0583: {{ -1.0f, -1.0f, 0.1f}, 0, 1,}, 0584: {{ -1.0f, 1.0f, 0.1f}, 0, 0,}, 0585: {{ 1.0f, 1.0f, 0.1f}, 1, 0,}, 0586: }; 0587: m_pd3dDevice->SetRenderTarget(0, m_pXMapSurf); 0588: m_pEffect->Pass( PASS_X ); 0589: m_pEffect->SetVectorArray(m_hafWeight 0590: , m_tbl[0], WEIGHT_MUN); 0591: 0592: m_pd3dDevice->SetVertexDeclaration( m_pDecl ); 0593: m_pEffect->SetTexture(m_htSrcMap, m_pLumTex); 0594: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN 0595: , 2, Vertex1, sizeof( VERTEX ) ); 0596: 0597: //------------------------------------------------- 0598: // 大きなぼかしy 0599: //------------------------------------------------- 0600: m_pd3dDevice->SetRenderTarget(0, m_pLargeSurf); 0601: m_pEffect->Pass( PASS_Y ); 0602: 0603: m_pEffect->SetTexture(m_htSrcMap, m_pXMap); 0604: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN 0605: , 2, Vertex1, sizeof( VERTEX ) ); 0606: 0607: //------------------------------------------------- 0608: // 小さなぼかしx 0609: //------------------------------------------------- 0610: m_pd3dDevice->SetRenderTarget(0, m_pXMapSurf); 0611: m_pEffect->Pass( PASS_X ); 0612: m_pEffect->SetVectorArray(m_hafWeight 0613: , m_tbl[1], WEIGHT_MUN); 0614: 0615: m_pEffect->SetTexture(m_htSrcMap, m_pLumTex); 0616: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN 0617: , 2, Vertex1, sizeof( VERTEX ) ); 0618: 0619: //------------------------------------------------- 0620: // 小さなぼかしy 0621: //------------------------------------------------- 0622: m_pd3dDevice->SetRenderTarget(0, m_pSmallSurf); 0623: m_pEffect->Pass( PASS_Y ); 0624: 0625: m_pEffect->SetTexture(m_htSrcMap, m_pXMap); 0626: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN 0627: , 2, Vertex1, sizeof( VERTEX ) ); 0628: 0629: m_pEffect->End(); 0630: } 0631: 0632: //----------------------------------------------------- 0633: // レンダリングターゲットを元に戻す 0634: //----------------------------------------------------- 0635: m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer); 0636: m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer); 0637: m_pd3dDevice->SetViewport(&oldViewport); 0638: pOldBackBuffer->Release(); 0639: pOldZBuffer->Release(); 0640: 0641: // バッファのクリア 0642: m_pd3dDevice->Clear( 0L, NULL 0643: , D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER 0644: , 0x00404080, 1.0f, 0L ); 0645: 0646: //----------------------------------------------------- 0647: // そのまま張る 0648: //----------------------------------------------------- 0649: FLOAT w = (FLOAT)oldViewport.Width; 0650: FLOAT h = (FLOAT)oldViewport.Height; 0651: TVERTEX Vertex1[4] = { 0652: //x y z rhw tu tv 0653: { 0, 0, 0.1f, 1.0f, 0+0.5f/RENDERING_WIDTH, 0+0.5f/RENDERING_HEIGHT,}, 0654: { w, 0, 0.1f, 1.0f, 1+0.5f/RENDERING_WIDTH, 0+0.5f/RENDERING_HEIGHT,}, 0655: { w, h, 0.1f, 1.0f, 1+0.5f/RENDERING_WIDTH, 1+0.5f/RENDERING_HEIGHT,}, 0656: { 0, h, 0.1f, 1.0f, 0+0.5f/RENDERING_WIDTH, 1+0.5f/RENDERING_HEIGHT,}, 0657: }; 0658: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 ); 0659: m_pd3dDevice->SetTexture(0, m_pOriginalTex); 0660: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN 0661: , 2, Vertex1, sizeof( TVERTEX ) ); 0662: 0663: //----------------------------------------------------- 0664: // 最終的に合成する 0665: //----------------------------------------------------- 0666: if(m_pEffect){ 0667: FLOAT du = 1.5f/MAP_WIDTH; 0668: FLOAT dv = 1.5f/MAP_HEIGHT; 0669: T2VERTEX Vertex2[4] = { 0670: // x y z rhw tu1 tv1 tu2 tv2 0671: { 0.0f, 0, 0.1f, 1.0f, 0.0f+du, 0.0f+dv, 0.0f+du, 0.0f+dv,}, 0672: { 1.0f*w, 0, 0.1f, 1.0f, 1.0f+du, 0.0f+dv, 1.0f+du, 0.0f+dv,}, 0673: { 1.0f*w, h, 0.1f, 1.0f, 1.0f+du, 1.0f+dv, 1.0f+du, 1.0f+dv,}, 0674: { 0.0f, h, 0.1f, 1.0f, 0.0f+du, 1.0f+dv, 0.0f+du, 1.0f+dv,}, 0675: }; 0676: m_pEffect->SetTechnique( m_hTechnique ); 0677: m_pEffect->Begin( NULL, 0 ); 0678: m_pEffect->Pass( PASS_FINAL ); 0679: 0680: RS(D3DRS_ALPHABLENDENABLE, TRUE); 0681: RS(D3DRS_SRCBLEND , D3DBLEND_ONE); 0682: RS(D3DRS_DESTBLEND , D3DBLEND_ONE); 0683: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX2 ); 0684: m_pEffect->SetTexture(m_htSrcMap, m_pLargeTex); 0685: m_pEffect->SetTexture(m_htSrcMap2, m_pSmallTex); 0686: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN 0687: , 2, Vertex2, sizeof( T2VERTEX ) ); 0688: m_pEffect->End(); 0689: } 0690: 0691: RS( D3DRS_ZENABLE, TRUE ); 0692: RS( D3DRS_LIGHTING, TRUE ); 0693: RS(D3DRS_ALPHABLENDENABLE, FALSE ); 0694: 0695: // ヘルプの表示 0696: RenderText(); 0697: 0698: #if 1 // デバッグ用にテクスチャを表示する 0699: { 0700: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_SELECTARG1); 0701: m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE); 0702: m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_DISABLE); 0703: m_pd3dDevice->SetVertexShader(NULL); 0704: m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 ); 0705: m_pd3dDevice->SetPixelShader(0); 0706: float scale = 100.0f; 0707: for(DWORD i=0; i<5; i++){ 0708: TVERTEX Vertex[4] = { 0709: // x y z rhw tu tv 0710: {(i+0)*scale, 0,0, 1, 0, 0,}, 0711: {(i+1)*scale, 0,0, 1, 1, 0,}, 0712: {(i+1)*scale,scale,0, 1, 1, 1,}, 0713: {(i+0)*scale,scale,0, 1, 0, 1,}, 0714: }; 0715: if(0==i) m_pd3dDevice->SetTexture( 0, m_pOriginalTex ); 0716: if(1==i) m_pd3dDevice->SetTexture( 0, m_pLumTex ); 0717: if(2==i) m_pd3dDevice->SetTexture( 0, m_pXMap ); 0718: if(3==i) m_pd3dDevice->SetTexture( 0, m_pLargeTex ); 0719: if(4==i) m_pd3dDevice->SetTexture( 0, m_pSmallTex ); 0720: m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) ); 0721: } 0722: } 0723: #endif 0724: 0725: // 描画の終了 0726: m_pd3dDevice->EndScene(); 0727: } 0728: 0729: return S_OK; 0730: } 0731: 0732: 0733: 0734: 0735: //------------------------------------------------------------- 0736: // Name: RenderText() 0737: // Desc: 状態やヘルプを画面に表示する 0738: //------------------------------------------------------------- 0739: HRESULT CMyD3DApplication::RenderText() 0740: { 0741: D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0); 0742: TCHAR szMsg[MAX_PATH] = TEXT(""); 0743: 0744: FLOAT fNextLine = 40.0f; // 表示する高さ 0745: 0746: // 操作法やパラメータを表示する 0747: fNextLine = (FLOAT) m_d3dsdBackBuffer.Height; 0748: lstrcpy( szMsg, TEXT("Press 'F2' to configure display") ); 0749: fNextLine -= 20.0f; 0750: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg ); 0751: 0752: lstrcpy( szMsg, m_strDeviceStats ); 0753: fNextLine -= 20.0f; 0754: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg ); 0755: lstrcpy( szMsg, m_strFrameStats ); 0756: fNextLine -= 20.0f; 0757: m_pFont->DrawText( 2, fNextLine, fontColor, szMsg ); 0758: 0759: return S_OK; 0760: } 0761: 0762: 0763: 0764: 0765: //------------------------------------------------------------- 0766: // Name: MsgProc() 0767: // Desc: WndProc をオーバーライドしたもの 0768: //------------------------------------------------------------- 0769: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg, 0770: WPARAM wParam, LPARAM lParam ) 0771: { 0772: switch( msg ) 0773: { 0774: case WM_PAINT: 0775: { 0776: if( m_bLoadingApp ) 0777: { 0778: // ロード中 0779: HDC hDC = GetDC( hWnd ); 0780: TCHAR strMsg[MAX_PATH]; 0781: wsprintf(strMsg, TEXT("Loading... Please wait")); 0782: RECT rct; 0783: GetClientRect( hWnd, &rct ); 0784: DrawText( hDC, strMsg, -1, &rct 0785: , DT_CENTER|DT_VCENTER|DT_SINGLELINE ); 0786: ReleaseDC( hWnd, hDC ); 0787: } 0788: break; 0789: } 0790: 0791: } 0792: 0793: return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam ); 0794: } 0795: 0796: 0797: 0798: 0799: //------------------------------------------------------------- 0800: // Name: InvalidateDeviceObjects() 0801: // Desc: RestoreDeviceObjects() で作成したオブジェクトの開放 0802: //------------------------------------------------------------- 0803: HRESULT CMyD3DApplication::InvalidateDeviceObjects() 0804: { 0805: // シャドウマップ 0806: SAFE_RELEASE(m_pLargeSurf); 0807: SAFE_RELEASE(m_pLargeTex); 0808: SAFE_RELEASE(m_pSmallSurf); 0809: SAFE_RELEASE(m_pSmallTex); 0810: SAFE_RELEASE(m_pXMapSurf); 0811: SAFE_RELEASE(m_pXMap); 0812: SAFE_RELEASE(m_pLumSurf); 0813: SAFE_RELEASE(m_pLumTex); 0814: SAFE_RELEASE(m_pOriginalSurf); 0815: SAFE_RELEASE(m_pOriginalTex); 0816: SAFE_RELEASE(m_pMapZ); 0817: 0818: m_pMesh ->InvalidateDeviceObjects(); // メッシュ 0819: 0820: m_pFont->InvalidateDeviceObjects(); // フォント 0821: 0822: // シェーダ 0823: if( m_pEffect != NULL ) m_pEffect->OnLostDevice(); 0824: 0825: return S_OK; 0826: } 0827: 0828: 0829: 0830: 0831: //------------------------------------------------------------- 0832: // Name: DeleteDeviceObjects() 0833: // Desc: InitDeviceObjects() で作成したオブジェクトを開放する 0834: //------------------------------------------------------------- 0835: HRESULT CMyD3DApplication::DeleteDeviceObjects() 0836: { 0837: // シェーダ 0838: SAFE_RELEASE( m_pEffect ); 0839: SAFE_RELEASE( m_pDecl ); 0840: 0841: // メッシュ 0842: m_pMesh ->Destroy(); 0843: 0844: // フォント 0845: m_pFont->DeleteDeviceObjects(); 0846: 0847: return S_OK; 0848: } 0849: 0850: 0851: 0852: 0853: //------------------------------------------------------------- 0854: // Name: FinalCleanup() 0855: // Desc: 終了する直線に呼ばれる 0856: //------------------------------------------------------------- 0857: HRESULT CMyD3DApplication::FinalCleanup() 0858: { 0859: SAFE_DELETE( m_pMesh ); 0860: 0861: SAFE_DELETE( m_pFont ); // フォント 0862: 0863: return S_OK; 0864: } 0865: 0866: 0867: 0868: 0869: