0001: //-------------------------------------------------------------
0002: // File: main.cpp
0003: //
0004: // Desc: PR
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        256
0027: #define DIFFUSE_SIZE    64
0028: 
0029: 
0030: // 長いから短縮形を作ってみた
0031: #define RS   m_pd3dDevice->SetRenderState
0032: #define TSS  m_pd3dDevice->SetTextureStageState
0033: #define SAMP m_pd3dDevice->SetSamplerState
0034: 
0035: 
0036: //-------------------------------------------------------------
0037: // 頂点の構造体
0038: //-------------------------------------------------------------
0039: typedef struct {
0040:     FLOAT       p[4];
0041:     FLOAT       tu, tv;
0042: } TVERTEX;
0043: 
0044: typedef struct {
0045:     FLOAT       p[4];
0046:     FLOAT       t[4][2];
0047: } T4VERTEX;
0048: 
0049: //-------------------------------------------------------------
0050: // グローバル変数
0051: //-------------------------------------------------------------
0052: CMyD3DApplication* g_pApp  = NULL;
0053: HINSTANCE          g_hInst = NULL;
0054: 
0055: //-------------------------------------------------------------
0056: // Name: WinMain()
0057: // Desc: メイン関数
0058: //-------------------------------------------------------------
0059: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0060: {
0061:     CMyD3DApplication d3dApp;
0062: 
0063:     g_pApp  = &d3dApp;
0064:     g_hInst = hInst;
0065: 
0066:     InitCommonControls();
0067:     if( FAILED( d3dApp.Create( hInst ) ) )
0068:         return 0;
0069: 
0070:     return d3dApp.Run();
0071: }
0072: 
0073: 
0074: 
0075: 
0076: //-------------------------------------------------------------
0077: // Name: CMyD3DApplication()
0078: // Desc: アプリケーションのコンストラクタ
0079: //-------------------------------------------------------------
0080: CMyD3DApplication::CMyD3DApplication()
0081: {
0082:     m_iState = -1;
0083:     m_Shader = 0;
0084: 
0085:     m_pMeshBg                   = new CD3DMesh();
0086: 
0087:     m_pMapZ                     = NULL;
0088:     m_pPosTex                   = NULL;
0089:     m_pPosSurf                  = NULL;
0090:     m_pPosLockTex               = NULL;
0091:     m_pPosLockSurf              = NULL;
0092:     m_pNormalTex                = NULL;
0093:     m_pNormalSurf               = NULL;
0094:     m_pNormalLockTex            = NULL;
0095:     m_pNormalLockSurf           = NULL;
0096:     m_pDiffuseTex               = NULL;
0097:     m_pDiffuseSurf              = NULL;
0098:     m_pFinalTex                 = NULL;
0099:     m_pFinalSurf                = NULL;
0100:     for(int i=0;i<REDUCTION_MAPS;i++){
0101:         m_pReductionTex[i]          = NULL;
0102:         m_pReductionSurf[i]         = NULL;
0103:     }
0104: 
0105:     m_pEffect                   = NULL;
0106:     m_hTechnique                = NULL;
0107:     m_hmWVP                     = NULL;
0108:     m_htSrcTex                  = NULL;
0109: 
0110:     m_fWorldRotX                = -0.5f;
0111:     m_fWorldRotY                = 0.0f;
0112:     m_fViewZoom                 = 13.0f;
0113: 
0114:     m_LighPos                   = D3DXVECTOR3( -5.0f, 5.0f,-2.0f );
0115: 
0116:     m_dwCreationWidth           = 512;
0117:     m_dwCreationHeight          = 512;
0118:     m_strWindowTitle            = TEXT( "main" );
0119:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0120:     m_bStartFullscreen          = false;
0121:     m_bShowCursorWhenFullscreen = false;
0122: 
0123:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0124:     m_bLoadingApp               = TRUE;
0125: 
0126:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0127: }
0128: 
0129: 
0130: 
0131: 
0132: //-------------------------------------------------------------
0133: // Name: ~CMyD3DApplication()
0134: // Desc: デストラクタ
0135: //-------------------------------------------------------------
0136: CMyD3DApplication::~CMyD3DApplication()
0137: {
0138: }
0139: 
0140: 
0141: 
0142: 
0143: //-------------------------------------------------------------
0144: // Name: OneTimeSceneInit()
0145: // Desc: 一度だけ行う初期化
0146: //      ウィンドウの初期化やIDirect3D9の初期化は終わってます。
0147: //      ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。
0148: //-------------------------------------------------------------
0149: HRESULT CMyD3DApplication::OneTimeSceneInit()
0150: {
0151:     // ローディングメッセージを表示する
0152:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0153: 
0154:     m_bLoadingApp = FALSE;
0155: 
0156:     return S_OK;
0157: }
0158: 
0159: 
0160: 
0161: 
0162: //-------------------------------------------------------------
0163: // Name: ConfirmDevice()
0164: // Desc: 初期化の時に呼ばれます。必要な能力をチェックします。
0165: //-------------------------------------------------------------
0166: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0167:                      DWORD dwBehavior,    D3DFORMAT Format )
0168: {
0169:     UNREFERENCED_PARAMETER( Format );
0170:     UNREFERENCED_PARAMETER( dwBehavior );
0171:     UNREFERENCED_PARAMETER( pCaps );
0172:     
0173: 
0174:     // ピクセルシェーダバージョンチェック
0175:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0176:         return E_FAIL;
0177: 
0178:     // 頂点シェーダバージョンが上位かソフトウェア頂点処理
0179:     if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0180:     &&  0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0181:             return E_FAIL;
0182: 
0183:     return S_OK;
0184: }
0185: 
0186: 
0187: //-------------------------------------------------------------
0188: // Name: InitDeviceObjects()
0189: // Desc: デバイスが生成された後の初期化をします。
0190: //      フレームバッファフォーマットやデバイスの種類が変わった
0191: //      後に通過します。
0192: //      ここで確保したメモリはDeleteDeviceObjects()で開放します
0193: //-------------------------------------------------------------
0194: HRESULT CMyD3DApplication::InitDeviceObjects()
0195: {
0196:     HRESULT hr;
0197: 
0198:     // 地面の読み込み
0199:     if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, _T("map.x"))))
0200:         return DXTRACE_ERR( "Load Model", hr );
0201:     m_pMeshBg->UseMeshMaterials(FALSE);// レンダリング時にテクスチャの設定をしない
0202:         
0203:     // シェーダの読み込み
0204:     LPD3DXBUFFER pErr;
0205:     if( FAILED( hr = D3DXCreateEffectFromFile(
0206:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0207:                 D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0208:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0209:                     , "ERROR", MB_OK);
0210:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0211:     }
0212:     m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0213:     m_hmWVP      = m_pEffect->GetParameterByName( NULL, "mWVP" );
0214:     m_htSrcTex   = m_pEffect->GetParameterByName( NULL, "SrcTex" );
0215: 
0216:     // フォント
0217:     m_pFont->InitDeviceObjects( m_pd3dDevice );
0218: 
0219:     return S_OK;
0220: }
0221: 
0222: //-------------------------------------------------------------
0223: // 真ん中が白い絵を作る
0224: //-------------------------------------------------------------
0225: VOID WINAPI FillTex (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord, 
0226: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0227: {
0228:     FLOAT x = 2.0f*(pTexCoord->x-0.5f);
0229:     FLOAT y = 2.0f*(pTexCoord->y-0.5f);
0230:     FLOAT col = (x*x+y*y<(1.0f-2.0f/DIFFUSE_SIZE)*(1.0f-2.0f/DIFFUSE_SIZE))
0231:                 ? 1.0f : 0.0f;
0232:     
0233:     pOut->x = pOut->y = pOut->z = pOut->w = col;
0234: }
0235: 
0236: //-------------------------------------------------------------
0237: // Name: RestoreDeviceObjects()
0238: // Desc: 画面のサイズが変更された時等に呼ばれます。
0239: //      確保したメモリはInvalidateDeviceObjects()で開放します。
0240: //-------------------------------------------------------------
0241: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0242: {
0243:     m_iState = -1;
0244:     m_iChangeState = TRUE;
0245: 
0246:     DWORD i;
0247: 
0248:     // メッシュ
0249:     m_pMeshBg->RestoreDeviceObjects( m_pd3dDevice );
0250: 
0251:     // 質感の設定
0252:     D3DMATERIAL9 mtrl;
0253:     D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0254:     m_pd3dDevice->SetMaterial( &mtrl );
0255: 
0256: 
0257:     // レンダリング状態の設定
0258:     RS( D3DRS_DITHERENABLE,   FALSE );
0259:     RS( D3DRS_SPECULARENABLE, FALSE );
0260:     RS( D3DRS_ZENABLE,        TRUE );
0261:     RS( D3DRS_AMBIENT,        0x000F0F0F );
0262:     
0263:     TSS( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0264:     TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0265:     TSS( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0266:     TSS( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
0267:     TSS( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
0268:     SAMP( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0269:     SAMP( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0270:     SAMP( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0271:     SAMP( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0272: 
0273:     // ワールド行列
0274:     D3DXMATRIX matIdentity;
0275:     D3DXMatrixIdentity( &m_mWorld );
0276: 
0277:     // ビュー行列
0278:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0279:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0280:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0281:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0282: 
0283:     // 射影行列
0284:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0285:     D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0286: 
0287:     // フォント
0288:     m_pFont->RestoreDeviceObjects();
0289: 
0290:     // レンダリングターゲットの生成
0291:     if (FAILED(m_pd3dDevice->CreateDepthStencilSurface(MAP_SIZE, MAP_SIZE, 
0292:         D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pMapZ, NULL)))
0293:         return E_FAIL;
0294:     // 位置マップ
0295:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0296:         D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pPosTex, NULL)))
0297:         return E_FAIL;
0298:     if (FAILED(m_pPosTex->GetSurfaceLevel(0, &m_pPosSurf)))
0299:         return E_FAIL;
0300:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0301:         0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pPosLockTex, NULL)))
0302:         return E_FAIL;
0303:     if (FAILED(m_pPosLockTex->GetSurfaceLevel(0, &m_pPosLockSurf)))
0304:         return E_FAIL;
0305:     // 法線マップ
0306:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0307:         D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pNormalTex, NULL)))
0308:         return E_FAIL;
0309:     if (FAILED(m_pNormalTex->GetSurfaceLevel(0, &m_pNormalSurf)))
0310:         return E_FAIL;
0311:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0312:         0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pNormalLockTex, NULL)))
0313:         return E_FAIL;
0314:     if (FAILED(m_pNormalLockTex->GetSurfaceLevel(0, &m_pNormalLockSurf)))
0315:         return E_FAIL;
0316:     // 拡散マップ
0317:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0318:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pDiffuseTex, NULL)))
0319:         return E_FAIL;
0320:     if (FAILED(m_pDiffuseTex->GetSurfaceLevel(0, &m_pDiffuseSurf)))
0321:         return E_FAIL;
0322:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0323:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pFinalTex, NULL)))
0324:         return E_FAIL;
0325:     if (FAILED(m_pFinalTex->GetSurfaceLevel(0, &m_pFinalSurf)))
0326:         return E_FAIL;
0327: 
0328:     // 縮小バッファ
0329:     for(i=0;i<REDUCTION_MAPS;i++){
0330:         int size = 1<<(3*(REDUCTION_MAPS-i));
0331:         if (FAILED(m_pd3dDevice->CreateTexture(size, size, 1, 
0332:             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pReductionTex[i], NULL)))
0333:             return E_FAIL;
0334:         if (FAILED(m_pReductionTex[i]->GetSurfaceLevel(0, &m_pReductionSurf[i])))
0335:             return E_FAIL;
0336:     }
0337: 
0338:     // マスク用のテクスチャの生成
0339:     if( FAILED(m_pd3dDevice->CreateTexture(DIFFUSE_SIZE, DIFFUSE_SIZE, 1
0340:                           , D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8
0341:                           , D3DPOOL_DEFAULT, &m_pMaskTex, NULL)))
0342:         return E_FAIL;
0343:     if( FAILED(D3DXFillTexture(m_pMaskTex, FillTex, NULL)))
0344:         return E_FAIL;
0345: 
0346:     m_pEffect->OnResetDevice();
0347: 
0348:     return S_OK;
0349: }
0350: 
0351: 
0352: //-------------------------------------------------------------
0353: // Name: FrameMoveCreateMap()
0354: // Desc: 位置、法線マップを作成する。
0355: //-------------------------------------------------------------
0356: int CMyD3DApplication::FrameMoveCreateMap()
0357: {
0358:     // 2回目の以降は次の処理に移る
0359:     if(1<m_iCount) return 1;
0360: 
0361:     return 0;
0362: }
0363: //-------------------------------------------------------------
0364: // Name: RenderCreateMap()
0365: // Desc: 位置、法線マップを作成する。
0366: //-------------------------------------------------------------
0367: void CMyD3DApplication::RenderCreateMap()
0368: {
0369:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0370:     D3DVIEWPORT9 oldViewport;
0371: 
0372:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0373:     {
0374:         //-------------------------------------------------
0375:         // レンダリングターゲットの保存
0376:         //-------------------------------------------------
0377:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0378:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0379:         m_pd3dDevice->GetViewport(&oldViewport);
0380: 
0381:         //-------------------------------------------------
0382:         // レンダリングターゲットの変更
0383:         //-------------------------------------------------
0384:         m_pd3dDevice->SetRenderTarget(0, m_pPosSurf);
0385:         m_pd3dDevice->SetRenderTarget(1, m_pNormalSurf);
0386:         m_pd3dDevice->SetDepthStencilSurface(NULL);
0387:         // ビューポートの変更
0388:         D3DVIEWPORT9 viewport_height = {0,0      // 左上の座標
0389:                         , MAP_SIZE  // 幅
0390:                         , MAP_SIZE // 高さ
0391:                         , 0.0f,1.0f};     // 前面、後面
0392:         m_pd3dDevice->SetViewport(&viewport_height);
0393: 
0394:         //-------------------------------------------------
0395:         // フレームバッファのクリア
0396:         //-------------------------------------------------
0397:         m_pd3dDevice->Clear(0L, NULL
0398:                         , D3DCLEAR_TARGET
0399:                         , 0x00000000, 1.0f, 0L);
0400: 
0401:         if( m_pEffect != NULL ) 
0402:         {
0403:             //-------------------------------------------------
0404:             // シェーダの設定
0405:             //-------------------------------------------------
0406:             m_pEffect->SetTechnique( m_hTechnique );
0407:             m_pEffect->Begin( NULL, 0 );
0408:             m_pEffect->Pass( 0 );
0409: 
0410:             RS( D3DRS_ZENABLE, FALSE );
0411:             RS( D3DRS_CULLMODE, D3DCULL_NONE );
0412: 
0413:             //-------------------------------------------------
0414:             // 背景の描画
0415:             //-------------------------------------------------
0416:             m_pMeshBg->Render(m_pd3dDevice);
0417: 
0418:             RS( D3DRS_ZENABLE, TRUE );
0419:             RS( D3DRS_CULLMODE, D3DCULL_CCW );
0420: 
0421:             m_pEffect->End();
0422:         }
0423: 
0424:         //-----------------------------------------------------
0425:         // レンダリングターゲットを元に戻す
0426:         //-----------------------------------------------------
0427:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0428:         m_pd3dDevice->SetRenderTarget(1, NULL);
0429:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0430:         m_pd3dDevice->SetViewport(&oldViewport);
0431:         pOldBackBuffer->Release();
0432:         pOldZBuffer->Release();
0433: 
0434:         //-----------------------------------------------------
0435:         // ごまかしの画面
0436:         //-----------------------------------------------------
0437:         m_pd3dDevice->Clear(0L, NULL
0438:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0439:                         , 0xff000000, 1.0f, 0L);
0440: 
0441:         m_pd3dDevice->EndScene();
0442:     }
0443: }
0444: 
0445: 
0446: //-------------------------------------------------------------
0447: // Name: FrameMoveCreateMap()
0448: // Desc: 位置、法線マップを作成する。
0449: //-------------------------------------------------------------
0450: static int ix=0;
0451: static int iy=0;
0452: static float pos[MAP_SIZE][MAP_SIZE][3];
0453: static float normal[MAP_SIZE][MAP_SIZE][3];
0454: int CMyD3DApplication::FrameMoveFinalGathering()
0455: {
0456:     ix = this->m_iCount % MAP_SIZE;
0457:     iy = this->m_iCount / MAP_SIZE;
0458:     
0459:     if(0==this->m_iCount){
0460:         D3DLOCKED_RECT d3dlr;
0461:         float *p;
0462:         m_pd3dDevice->GetRenderTargetData(m_pPosSurf,m_pPosLockSurf);
0463:         m_pPosLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY); //サーフェイス上の矩形をロック    
0464:         p = (float *)d3dlr.pBits;
0465:         for(int y=0;y<MAP_SIZE;y++){
0466:         for(int x=0;x<MAP_SIZE;x++){
0467:             pos[x][y][0]=p[0];
0468:             pos[x][y][1]=p[1];
0469:             pos[x][y][2]=p[2];
0470:             p+=4;
0471:         }
0472:         }
0473:         m_pPosLockSurf->UnlockRect();
0474: 
0475:         m_pd3dDevice->GetRenderTargetData(m_pNormalSurf,m_pNormalLockSurf);
0476:         m_pNormalLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY); //サーフェイス上の矩形をロック    
0477:         p = (float *)d3dlr.pBits;        
0478:         for(int y=0;y<MAP_SIZE;y++){
0479:         for(int x=0;x<MAP_SIZE;x++){
0480:             normal[x][y][0]=p[0];
0481:             normal[x][y][1]=p[1];
0482:             normal[x][y][2]=p[2];
0483:             p+=4;
0484:         }
0485:         }
0486:         m_pNormalLockSurf->UnlockRect();
0487:     }
0488: 
0489:     // 終了条件
0490:     if(MAP_SIZE-1==ix && MAP_SIZE-1==iy) return 1;
0491: 
0492:     return 0;
0493: }
0494: //-------------------------------------------------------------
0495: // Name: RenderFinalGathering()
0496: // Desc: それぞれの位置からのラディアンスを計算する。
0497: //-------------------------------------------------------------
0498: void CMyD3DApplication::RenderFinalGathering()
0499: {
0500:     D3DXMATRIX m, mView;
0501:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0502:     D3DVIEWPORT9 oldViewport;
0503:     D3DSURFACE_DESC d3dsd;
0504: 
0505:     D3DVIEWPORT9 viewport = {0,0      // 左上の座標
0506:                     , 1, 1  // 幅,高さ
0507:                     , 0.0f,1.0f};     // 前面、後面
0508: 
0509:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0510:     {
0511:         RS( D3DRS_ZENABLE, FALSE );
0512:         RS( D3DRS_CULLMODE, D3DCULL_NONE );
0513: 
0514:         //-------------------------------------------------
0515:         // レンダリングターゲットの保存
0516:         //-------------------------------------------------
0517:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0518:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0519:         m_pd3dDevice->GetViewport(&oldViewport);
0520: 
0521:         //-------------------------------------------------
0522:         //-------------------------------------------------
0523:         // 各点から見たラディアンスの描画
0524:         //-------------------------------------------------
0525:         //-------------------------------------------------
0526: 
0527:         //-------------------------------------------------
0528:         // レンダリングターゲットの変更
0529:         //-------------------------------------------------
0530:         m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[0]);
0531:         m_pd3dDevice->SetDepthStencilSurface(NULL);
0532:         // ビューポートの変更
0533:         m_pReductionSurf[0]->GetDesc(&d3dsd);
0534:         viewport.Height = d3dsd.Width;
0535:         viewport.Width  = d3dsd.Height;
0536:         m_pd3dDevice->SetViewport(&viewport);
0537: 
0538:         //-------------------------------------------------
0539:         // 重みを持つ半球の部分だけ描画
0540:         //-------------------------------------------------
0541:         {
0542:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0543:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0544:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0545:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,    D3DTOP_SELECTARG1);
0546:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1,  D3DTA_TEXTURE);
0547:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_ALPHAOP,    D3DTOP_DISABLE);
0548:         m_pd3dDevice->SetVertexShader(NULL);
0549:         m_pd3dDevice->SetPixelShader(0);
0550:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0551:         TVERTEX Vertex[4] = {
0552:             // x  y  z rhw tu tv
0553:             {          0,           0,0, 1, 0, 0,},
0554:             {d3dsd.Width,           0,0, 1, 1, 0,},
0555:             {d3dsd.Width,d3dsd.Height,0, 1, 1, 1,},
0556:             {          0,d3dsd.Height,0, 1, 0, 1,},
0557:         };
0558:         m_pd3dDevice->SetTexture( 0, m_pMaskTex );
0559:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0560:         }
0561: 
0562:         if( m_pEffect != NULL ) 
0563:         {
0564:             //-------------------------------------------------
0565:             // シェーダの設定
0566:             //-------------------------------------------------
0567:             m_pEffect->SetTechnique( m_hTechnique );
0568:             m_pEffect->Begin( NULL, 0 );
0569:             m_pEffect->Pass( 1 );
0570:             
0571:             // アルファは書き込まない
0572:             RS(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED |D3DCOLORWRITEENABLE_GREEN |D3DCOLORWRITEENABLE_BLUE );
0573: 
0574:             //-------------------------------------------------
0575:             // 背景の描画
0576:             //-------------------------------------------------
0577:             float x[3] = {pos[ix][iy][0], pos[ix][iy][1], pos[ix][iy][2]};
0578:             float n[3] = {normal[ix][iy][0], normal[ix][iy][1], normal[ix][iy][2]};
0579:             D3DXVECTOR3 vFromPt   = D3DXVECTOR3( x[0], x[1], x[2] ) + 0.05f*D3DXVECTOR3( n[0], n[1], n[2] );
0580:             D3DXVECTOR3 vLookatPt = D3DXVECTOR3( n[0], n[1], n[2] ) + vFromPt;
0581:             D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0582:             D3DXMatrixLookAtLH( &mView, &vFromPt, &vLookatPt, &vUpVec );
0583: 
0584:             m_pEffect->SetMatrix( m_hmWVP, &mView );
0585: 
0586:             m_pMeshBg->Render(m_pd3dDevice);
0587: 
0588: 
0589:             RS(D3DRS_COLORWRITEENABLE, 0xf );
0590:             m_pEffect->End();
0591:         }
0592:         //-------------------------------------------------
0593:         //-------------------------------------------------
0594:         // ラディアンスをミップマップの要領で小さくする
0595:         //-------------------------------------------------
0596:         //-------------------------------------------------
0597:         m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[1]);
0598:         m_pd3dDevice->SetDepthStencilSurface(NULL);
0599:         // ビューポートの変更
0600:         m_pReductionSurf[1]->GetDesc(&d3dsd);
0601:         viewport.Height = d3dsd.Width;
0602:         viewport.Width  = d3dsd.Height;
0603:         m_pd3dDevice->SetViewport(&viewport);
0604: 
0605:         TSS(0,D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
0606:         TSS(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
0607:         TSS(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);
0608:         
0609:         if( m_pEffect != NULL ) {
0610:             //-------------------------------------------------
0611:             // シェーダの設定
0612:             //-------------------------------------------------
0613:             m_pEffect->SetTechnique( m_hTechnique );
0614:             m_pEffect->Begin( NULL, 0 );
0615:             m_pEffect->Pass( 2 );
0616: 
0617:             m_pEffect->SetFloat("MAP_WIDTH",  DIFFUSE_SIZE);
0618:             m_pEffect->SetFloat("MAP_HEIGHT", DIFFUSE_SIZE);
0619: 
0620:             //-------------------------------------------------
0621:             // フィルタリング
0622:             //-------------------------------------------------
0623:             TVERTEX Vertex1[4] = {
0624:                 //   x    y     z    tu tv
0625:                 {-1.0f, +1.0f, 0.1f,  0, 0},
0626:                 {+1.0f, +1.0f, 0.1f,  1, 0},
0627:                 {+1.0f, -1.0f, 0.1f,  1, 1},
0628:                 {-1.0f, -1.0f, 0.1f,  0, 1},
0629:             };
0630:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0631:             m_pEffect->SetTexture("ReductionMap", m_pReductionTex[0]);
0632:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0633:                             , 2, Vertex1, sizeof( TVERTEX ) );
0634: 
0635:             m_pEffect->End();
0636:         }
0637: 
0638:         //-------------------------------------------------
0639:         //-------------------------------------------------
0640:         // ラディアンスをテクスチャの対応する位置に張る
0641:         //-------------------------------------------------
0642:         //-------------------------------------------------
0643:         m_pd3dDevice->SetRenderTarget(0, m_pDiffuseSurf);
0644:         m_pd3dDevice->SetDepthStencilSurface(NULL);
0645:         // ビューポートの変更
0646:         m_pDiffuseSurf->GetDesc(&d3dsd);
0647:         viewport.Height = d3dsd.Width;
0648:         viewport.Width  = d3dsd.Height;
0649:         m_pd3dDevice->SetViewport(&viewport);
0650:         
0651:         // 確認しやすくするために、最初は赤く塗りつぶす
0652:         if(0==ix&&0==iy)m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, 0x00800000, 1.0f, 0L);
0653: 
0654:         if( m_pEffect != NULL ) {
0655:             //-------------------------------------------------
0656:             // シェーダの設定
0657:             //-------------------------------------------------
0658:             m_pEffect->SetTechnique( m_hTechnique );
0659:             m_pEffect->Begin( NULL, 0 );
0660:             m_pEffect->Pass( 2 );
0661: 
0662:             m_pEffect->SetFloat("MAP_WIDTH",  8);
0663:             m_pEffect->SetFloat("MAP_HEIGHT", 8);
0664: 
0665:             //-------------------------------------------------
0666:             // フィルタリング
0667:             //-------------------------------------------------
0668:             float x =  2.0f*((float)ix/(float)MAP_SIZE) - 1.0f;
0669:             float y = -2.0f*((float)iy/(float)MAP_SIZE) + 1.0f;
0670:             TVERTEX Vertex1[4] = {
0671:                 //   x    y     z    tu tv
0672:                 {x,                      y,                     0.1f,  0, 0},
0673:                 {x+2.0f/(float)MAP_SIZE, y,                     0.1f,  1, 0},
0674:                 {x+2.0f/(float)MAP_SIZE, y-2.0f/(float)MAP_SIZE,0.1f,  1, 1},
0675:                 {x,                      y-2.0f/(float)MAP_SIZE,0.1f,  0, 1},
0676:             };
0677:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0678:             m_pEffect->SetTexture("ReductionMap", m_pReductionTex[1]);
0679:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0680:                             , 2, Vertex1, sizeof( TVERTEX ) );
0681: 
0682:             m_pEffect->End();
0683:         }
0684:         
0685:         //-----------------------------------------------------
0686:         // 最終フレームでは、アルファ成分の値で結果を割る(正規化)
0687:         //-----------------------------------------------------
0688:         if(MAP_SIZE-1==ix && MAP_SIZE-1==iy){
0689:             m_pd3dDevice->SetRenderTarget(0, m_pFinalSurf);
0690:             m_pd3dDevice->SetDepthStencilSurface(NULL);
0691:             // ビューポートの変更
0692:             m_pFinalSurf->GetDesc(&d3dsd);
0693:             viewport.Height = d3dsd.Width;
0694:             viewport.Width  = d3dsd.Height;
0695:             m_pd3dDevice->SetViewport(&viewport);
0696: 
0697:             if( m_pEffect != NULL ) {
0698:                 m_pEffect->SetTechnique( m_hTechnique );
0699:                 m_pEffect->Begin( NULL, 0 );
0700:                 m_pEffect->Pass( 3 );
0701: 
0702:                 TVERTEX Vertex[4] = {
0703:                     // x  y  z rhw tu tv
0704:                     {       0,       0,0, 1, 0+0.5f/(FLOAT)MAP_SIZE, 0+0.5f/(FLOAT)MAP_SIZE,},
0705:                     {MAP_SIZE,       0,0, 1, 1+0.5f/(FLOAT)MAP_SIZE, 0+0.5f/(FLOAT)MAP_SIZE,},
0706:                     {MAP_SIZE,MAP_SIZE,0, 1, 1+0.5f/(FLOAT)MAP_SIZE, 1+0.5f/(FLOAT)MAP_SIZE,},
0707:                     {       0,MAP_SIZE,0, 1, 0+0.5f/(FLOAT)MAP_SIZE, 1+0.5f/(FLOAT)MAP_SIZE,},
0708:                 };
0709:                 m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0710:                 m_pEffect->SetTexture(m_htSrcTex, m_pDiffuseTex);
0711:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0712:                                 , 2, Vertex, sizeof( TVERTEX ) );
0713: 
0714:                 m_pEffect->End();
0715:             }
0716:         }
0717: 
0718:         //-----------------------------------------------------
0719:         // レンダリングターゲットを元に戻す
0720:         //-----------------------------------------------------
0721:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0722:         m_pd3dDevice->SetRenderTarget(1, NULL);
0723:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0724:         m_pd3dDevice->SetViewport(&oldViewport);
0725:         pOldBackBuffer->Release();
0726:         pOldZBuffer->Release();
0727: 
0728:         RS( D3DRS_ZENABLE, TRUE );
0729:         RS( D3DRS_CULLMODE, D3DCULL_CCW );
0730: 
0731:         //---------------------------------------------------------
0732:         // 画面の表示
0733:         //---------------------------------------------------------
0734:         m_pd3dDevice->Clear(0L, NULL
0735:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0736:                         , 0x00001020, 1.0f, 0L);
0737: 
0738: #if 1 // デバッグ用にテクスチャを表示する
0739:         {
0740:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0741:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0742:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0743:         m_pd3dDevice->SetVertexShader(NULL);
0744:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0745:         m_pd3dDevice->SetPixelShader(0);
0746:         float scale = 256.0f*2/3;
0747:         for(DWORD i=0; i<3; i++){
0748:             TVERTEX Vertex[4] = {
0749:                 // x  y  z rhw tu tv
0750:                 {    0,(i+0)*scale,0, 1, 0, 0,},
0751:                 {scale,(i+0)*scale,0, 1, 1, 0,},
0752:                 {scale,(i+1)*scale,0, 1, 1, 1,},
0753:                 {    0,(i+1)*scale,0, 1, 0, 1,},
0754:             };
0755:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pDiffuseTex );
0756:             if(1==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[0] );
0757:             if(2==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[1] );
0758:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0759:         }
0760:         }
0761: #endif      
0762: 
0763:         D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
0764:         TCHAR szMsg[MAX_PATH] = TEXT("");
0765: 
0766:         sprintf( szMsg, TEXT("x:%4d\ny:%4d"), ix, iy);
0767:         m_pFont->DrawText( 2, (FLOAT)m_d3dsdBackBuffer.Height-40, fontColor, szMsg );
0768: 
0769:         m_pd3dDevice->EndScene();
0770:     }
0771: }
0772: 
0773: 
0774: //-------------------------------------------------------------
0775: // Name: FrameMove()
0776: // Desc: 毎フレーム呼ばれます。アニメの処理などを行います。
0777: //-------------------------------------------------------------
0778: HRESULT CMyD3DApplication::FrameMove()
0779: {
0780:     m_iCount++;
0781:     
0782:     if(m_iChangeState) {m_iState++;m_iCount=0;m_iChangeState=0;}
0783: 
0784:     switch(m_iState){
0785:     case 0:
0786:         m_iChangeState = this->FrameMoveCreateMap();
0787:         return S_OK;
0788:     case 1:
0789:         m_iChangeState = this->FrameMoveFinalGathering();
0790:         return S_OK;
0791:     default:
0792:         break;
0793:     }
0794: 
0795: 
0796:     // 入力データの更新
0797:     UpdateInput( &m_UserInput );
0798: 
0799:     //---------------------------------------------------------
0800:     // 入力に応じて座標系を更新する
0801:     //---------------------------------------------------------
0802:     // 回転
0803:     D3DXMATRIX matRotY;
0804:     D3DXMATRIX matRotX;
0805: 
0806:     if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0807:         m_fWorldRotY += m_fElapsedTime;
0808:     else
0809:     if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0810:         m_fWorldRotY -= m_fElapsedTime;
0811: 
0812:     if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0813:         m_fWorldRotX += m_fElapsedTime;
0814:     else
0815:     if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0816:         m_fWorldRotX -= m_fElapsedTime;
0817: 
0818:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0819:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0820: 
0821:     D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0822:     
0823:     //---------------------------------------------------------
0824:     // ビュー行列の設定
0825:     //---------------------------------------------------------
0826:     // ズーム
0827:     if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0828:         m_fViewZoom += m_fElapsedTime;
0829:     else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0830:         m_fViewZoom -= m_fElapsedTime;
0831: 
0832:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0833:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.3f, 0.0f );
0834:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0835:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0836: 
0837:     //---------------------------------------------------------
0838:     // シェーダの変更
0839:     //---------------------------------------------------------
0840:     if( m_UserInput.bChangeShader ) m_Shader = (m_Shader + 1) % 3;
0841: 
0842:     return S_OK;
0843: }
0844: //-------------------------------------------------------------
0845: // Name: UpdateInput()
0846: // Desc: 入力データを更新する
0847: //-------------------------------------------------------------
0848: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0849: {
0850:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0851:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0852:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0853:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0854:     
0855:     pUserInput->bZoomIn      = ( m_bActive && (GetAsyncKeyState( 'Z'     )  & 0x8000) == 0x8000 );
0856:     pUserInput->bZoomOut     = ( m_bActive && (GetAsyncKeyState( 'X'      ) & 0x8000) == 0x8000 );
0857:     
0858:     pUserInput->bChangeShader= ( m_bActive && (GetAsyncKeyState( 'A'      ) & 0x8001) == 0x8001 );
0859: }
0860: 
0861: 
0862: //-------------------------------------------------------------
0863: // Name: Render()
0864: // Desc: 画面を描画する.
0865: //-------------------------------------------------------------
0866: HRESULT CMyD3DApplication::Render()
0867: {
0868:     switch(m_iState){
0869:     case 0:
0870:         this->RenderCreateMap();
0871:         return S_OK;
0872:     case 1:
0873:         this->RenderFinalGathering();
0874:         return S_OK;
0875:     default:
0876:         break;
0877:     }
0878: 
0879:     D3DXMATRIX m, mT, mR, mW, mView, mProj;
0880:     DWORD i;
0881:     D3DXVECTOR4 v;
0882:     D3DMATERIAL9 *pMtrl;
0883: 
0884:     //---------------------------------------------------------
0885:     // 描画
0886:     //---------------------------------------------------------
0887:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0888:     {
0889:         // フレームバッファのクリア
0890:         m_pd3dDevice->Clear(0L, NULL
0891:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0892:                         , 0xff000000, 1.0f, 0L);
0893: 
0894:         if( m_pEffect != NULL ) 
0895:         {
0896:             //-------------------------------------------------
0897:             // シェーダの設定
0898:             //-------------------------------------------------
0899:             m_pEffect->SetTechnique( m_hTechnique );
0900:             m_pEffect->Begin( NULL, 0 );
0901:             m_pEffect->Pass( 4+m_Shader );
0902: 
0903:             //-------------------------------------------------
0904:             // シーンの描画
0905:             //-------------------------------------------------
0906:             // 変換行列
0907:             m = m_mWorld * m_mView * m_mProj;
0908:             m_pEffect->SetMatrix( m_hmWVP, &m );
0909:             
0910:             // 背景の描画
0911:             m_pEffect->SetTexture("RadianceTex", this->m_pFinalTex );
0912:             pMtrl = m_pMeshBg->m_pMaterials;
0913:             for( i=0; i<m_pMeshBg->m_dwNumMaterials; i++ ) {
0914:                 m_pEffect->SetTexture(m_htSrcTex, m_pMeshBg->m_pTextures[i] );
0915:                 m_pMeshBg->m_pLocalMesh->DrawSubset( i );   // 描画
0916:                 pMtrl++;
0917:             }
0918: 
0919:             m_pEffect->End();
0920:         }
0921: 
0922:         // ヘルプの表示
0923: //        RenderText();
0924: 
0925: #if 1 // デバッグ用にテクスチャを表示する
0926:         {
0927:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0928:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0929:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0930:         SAMP(0, D3DSAMP_MAGFILTER ,D3DTEXF_POINT  );
0931:         m_pd3dDevice->SetVertexShader(NULL);
0932:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0933:         m_pd3dDevice->SetPixelShader(0);
0934:         float scale = 256.0f;
0935:         for(DWORD i=0; i<3; i++){
0936:             TVERTEX Vertex[4] = {
0937:                 // x  y  z rhw tu tv
0938:                 {(i+0)*scale,    0,0, 1, 0, 0,},
0939:                 {(i+1)*scale,    0,0, 1, 1, 0,},
0940:                 {(i+1)*scale,scale,0, 1, 1, 1,},
0941:                 {(i+0)*scale,scale,0, 1, 0, 1,},
0942:             };
0943:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pPosTex );
0944:             if(1==i) m_pd3dDevice->SetTexture( 0, m_pNormalTex );
0945:             if(2==i) m_pd3dDevice->SetTexture( 0, m_pFinalTex );
0946:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0947:         }
0948:         }
0949: #endif      
0950: 
0951:         // 描画の終了
0952:         m_pd3dDevice->EndScene();
0953:     }
0954: 
0955:     return S_OK;
0956: }
0957: 
0958: 
0959: 
0960: 
0961: //-------------------------------------------------------------
0962: // Name: RenderText()
0963: // Desc: 状態やヘルプを画面に表示する
0964: //-------------------------------------------------------------
0965: HRESULT CMyD3DApplication::RenderText()
0966: {
0967:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
0968:     TCHAR szMsg[MAX_PATH] = TEXT("");
0969: 
0970:     FLOAT fNextLine = 40.0f; // 表示する高さ
0971: 
0972:     // 操作法やパラメータを表示する
0973:     fNextLine = (FLOAT) m_d3dsdBackBuffer.Height; 
0974: 
0975:     lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
0976:     fNextLine -= 20.0f;
0977:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0978: 
0979:     lstrcpy( szMsg, TEXT("Press 'A' to change shader") );
0980:     fNextLine -= 20.0f;
0981:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0982: 
0983:     lstrcpy( szMsg, m_strDeviceStats );
0984:     fNextLine -= 20.0f;
0985:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0986:     lstrcpy( szMsg, m_strFrameStats );
0987:     fNextLine -= 20.0f;
0988:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0989:     
0990:     return S_OK;
0991: }
0992: 
0993: 
0994: 
0995: 
0996: //-------------------------------------------------------------
0997: // Name: MsgProc()
0998: // Desc: WndProc をオーバーライドしたもの
0999: //-------------------------------------------------------------
1000: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
1001:                                  WPARAM wParam, LPARAM lParam )
1002: {
1003:     switch( msg )
1004:     {
1005:         case WM_PAINT:
1006:         {
1007:             if( m_bLoadingApp )
1008:             {
1009:                 // ロード中
1010:                 HDC hDC = GetDC( hWnd );
1011:                 TCHAR strMsg[MAX_PATH];
1012:                 wsprintf(strMsg, TEXT("Loading... Please wait"));
1013:                 RECT rct;
1014:                 GetClientRect( hWnd, &rct );
1015:                 DrawText( hDC, strMsg, -1, &rct
1016:                         , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
1017:                 ReleaseDC( hWnd, hDC );
1018:             }
1019:             break;
1020:         }
1021: 
1022:     }
1023: 
1024:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
1025: }
1026: 
1027: 
1028: 
1029: 
1030: //-------------------------------------------------------------
1031: // Name: InvalidateDeviceObjects()
1032: // Desc: RestoreDeviceObjects() で作成したオブジェクトの開放
1033: //-------------------------------------------------------------
1034: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
1035: {
1036:     // レンダリングターゲット
1037:     SAFE_RELEASE(m_pMaskTex);
1038:     for(int i=0;i<REDUCTION_MAPS;i++){
1039:         SAFE_RELEASE(m_pReductionSurf[i]);
1040:         SAFE_RELEASE(m_pReductionTex[i]);
1041:     }
1042:     SAFE_RELEASE(m_pFinalSurf);
1043:     SAFE_RELEASE(m_pFinalTex);
1044:     SAFE_RELEASE(m_pDiffuseSurf);
1045:     SAFE_RELEASE(m_pDiffuseTex);
1046:     SAFE_RELEASE(m_pNormalLockSurf);
1047:     SAFE_RELEASE(m_pNormalLockTex);
1048:     SAFE_RELEASE(m_pNormalSurf);
1049:     SAFE_RELEASE(m_pNormalTex);
1050:     SAFE_RELEASE(m_pPosLockSurf);
1051:     SAFE_RELEASE(m_pPosLockTex);
1052:     SAFE_RELEASE(m_pPosSurf);
1053:     SAFE_RELEASE(m_pPosTex);
1054:     SAFE_RELEASE(m_pMapZ);
1055: 
1056:     m_pMeshBg->InvalidateDeviceObjects();
1057: 
1058:     m_pFont->InvalidateDeviceObjects(); // フォント
1059: 
1060:     // シェーダ
1061:     if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
1062: 
1063:     return S_OK;
1064: }
1065: 
1066: 
1067: 
1068: 
1069: //-------------------------------------------------------------
1070: // Name: DeleteDeviceObjects()
1071: // Desc: InitDeviceObjects() で作成したオブジェクトを開放する
1072: //-------------------------------------------------------------
1073: HRESULT CMyD3DApplication::DeleteDeviceObjects()
1074: {
1075:     // シェーダ
1076:     SAFE_RELEASE( m_pEffect );
1077:     
1078:     // メッシュ
1079:     m_pMeshBg->Destroy();
1080: 
1081:     // フォント
1082:     m_pFont->DeleteDeviceObjects();
1083: 
1084:     return S_OK;
1085: }
1086: 
1087: 
1088: 
1089: 
1090: //-------------------------------------------------------------
1091: // Name: FinalCleanup()
1092: // Desc: 終了する直線に呼ばれる
1093: //-------------------------------------------------------------
1094: HRESULT CMyD3DApplication::FinalCleanup()
1095: {
1096:     SAFE_DELETE( m_pMeshBg ); // メッシュ
1097: 
1098:     SAFE_DELETE( m_pFont ); // フォント
1099: 
1100:     return S_OK;
1101: }
1102: 
1103: 
1104: 
1105: 
1106: