0001: //-------------------------------------------------------------
0002: // File: main.cpp
0003: //
0004: // Desc: Real-Time Global Illumination
0005: // Copyright (c) 2004 IMAGIRE Takashi. All rights reserved.
0006: //-------------------------------------------------------------
0007: #define STRICT
0008: #include <windows.h>
0009: #include <time.h>
0010: #include <commctrl.h>
0011: #include <commdlg.h>
0012: #include <basetsd.h>
0013: #include <math.h>
0014: #include <stdio.h>
0015: #include <d3dx9.h>
0016: #include <dxerr9.h>
0017: #include <tchar.h>
0018: #include "DXUtil.h"
0019: #include "D3DEnumeration.h"
0020: #include "D3DSettings.h"
0021: #include "D3DApp.h"
0022: #include "D3DFont.h"
0023: #include "D3DFile.h"
0024: #include "D3DUtil.h"
0025: #include "resource.h"
0026: #include "main.h"
0027: 
0028: #define MAP_SIZE            128
0029: #define DECALE_MAP_SIZE     512
0030: 
0031: #define frand() ((float)rand()/(float)RAND_MAX)
0032: 
0033: //-------------------------------------------------------------
0034: // Globals variables and definitions(グローバル変数と定義)
0035: //-------------------------------------------------------------
0036: CMyD3DApplication* g_pApp  = NULL;
0037: HINSTANCE          g_hInst = NULL;
0038: 
0039: typedef struct {
0040:     FLOAT       p[4];
0041:     FLOAT       tu, tv;
0042: } TVERTEX;
0043: 
0044: //-------------------------------------------------------------
0045: // Name: WinMain()
0046: // Desc: Entry point to the program. Initializes everything, and goes into a
0047: //       message-processing loop. Idle time is used to render the scene.
0048: //       (メイン関数)
0049: //-------------------------------------------------------------
0050: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0051: {
0052:     CMyD3DApplication d3dApp;
0053: 
0054:     g_pApp  = &d3dApp;
0055:     g_hInst = hInst;
0056: 
0057:     InitCommonControls();
0058:     if( FAILED( d3dApp.Create( hInst ) ) )
0059:         return 0;
0060: 
0061:     return d3dApp.Run();
0062: }
0063: 
0064: 
0065: 
0066: 
0067: //-------------------------------------------------------------
0068: // Name: CMyD3DApplication()
0069: // Desc: Application constructor.   Paired with ~CMyD3DApplication()
0070: //       Member variables should be initialized to a known state here.  
0071: //       The application window has not yet been created and no Direct3D device 
0072: //       has been created, so any initialization that depends on a window or 
0073: //       Direct3D should be deferred to a later stage. 
0074: //       (アプリケーションのコンストラクタ)
0075: //-------------------------------------------------------------
0076: CMyD3DApplication::CMyD3DApplication()
0077: {
0078:     int i;
0079:     time_t t;
0080:     
0081:     time( &t );
0082:     srand( t );
0083: 
0084:     m_iState = -1;
0085:     m_bPause = false;
0086: 
0087:     m_Shader = 0;
0088: 
0089:     m_pos = D3DXVECTOR3(0,0,0);
0090:     m_vel = D3DXVECTOR3(frand(),frand(),frand());
0091:     m_rot = D3DXVECTOR3(0,0,0);
0092:     m_omega = D3DXVECTOR3( frand(), frand(), frand() );
0093: 
0094:     m_pMesh                     = new CD3DMesh();
0095:     m_pMeshBg                   = new CD3DMesh();
0096:     m_pMeshEnv                  = new CD3DMesh();
0097: 
0098:     m_pMapZ                     = NULL;
0099:     m_pPosTex                   = NULL;
0100:     m_pPosSurf                  = NULL;
0101:     m_pPosLockTex               = NULL;
0102:     m_pPosLockSurf              = NULL;
0103:     m_pNormalTex                = NULL;
0104:     m_pNormalSurf               = NULL;
0105:     m_pNormalLockTex            = NULL;
0106:     m_pNormalLockSurf           = NULL;
0107:     m_pParaboloidTex[0]         = NULL;
0108:     m_pParaboloidSurf[0]        = NULL;
0109:     m_pParaboloidTex[1]         = NULL;
0110:     m_pParaboloidSurf[1]        = NULL;
0111:     for(i=0;i<TEX_MAX;i++){
0112:         m_pJacobianTex[i][0]    = NULL;
0113:         m_pJacobianTex[i][1]    = NULL;
0114:     }
0115: 
0116:     for( i = 0; i < REDUCTION_MAPS; i++ )
0117:     {
0118:         m_pReductionTex[i]      = NULL;
0119:         m_pReductionSurf[i]     = NULL;
0120:     }
0121:     for( i=0;i<TEX_MAX;i++){
0122:         m_pFinalTex[i]          = NULL;
0123:         m_pFinalSurf[i]         = NULL;
0124:     }
0125:     m_p64Tex                    = NULL;
0126:     m_p64Surf                   = NULL;
0127:     m_p8Tex                 = NULL;
0128:     m_p8Surf                    = NULL;
0129:     m_pMaskTex                  = NULL;
0130: #ifdef NV3X
0131:     m_pPrtTex                   = NULL;
0132: #endif // NV3X
0133: 
0134:     m_pEffect                   = NULL;
0135:     m_hTechnique                = NULL;
0136:     m_hmWV                      = NULL;
0137:     m_hmWVP                     = NULL;
0138:     m_htSrcTex                  = NULL;
0139:     m_fWidth                    = NULL;
0140:     m_fHeight                   = NULL;
0141: 
0142:     m_fWorldRotX                = -0.0f;
0143:     m_fWorldRotY                = D3DX_PI*0.5f;
0144:     m_fViewZoom                 = 18.0f;
0145: 
0146:     m_dwCreationWidth           = 512;
0147:     m_dwCreationHeight          = 512;
0148:     m_strWindowTitle            = TEXT( "main" );
0149:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0150:     m_bStartFullscreen          = false;
0151:     m_bShowCursorWhenFullscreen = false;
0152: 
0153:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0154:     m_bLoadingApp               = TRUE;
0155: 
0156:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0157: }
0158: 
0159: 
0160: 
0161: 
0162: //-------------------------------------------------------------
0163: // Name: ~CMyD3DApplication()
0164: // Desc: Application destructor.  Paired with CMyD3DApplication()
0165: //       (デストラクタ)
0166: //-------------------------------------------------------------
0167: CMyD3DApplication::~CMyD3DApplication()
0168: {
0169: }
0170: 
0171: 
0172: 
0173: 
0174: //-------------------------------------------------------------
0175: // Name: OneTimeSceneInit()
0176: // Desc: Paired with FinalCleanup().
0177: //       The window has been created and the IDirect3D9 interface has been
0178: //       created, but the device has not been created yet.  Here you can
0179: //       perform application-related initialization and cleanup that does
0180: //       not depend on a device.
0181: //      (一度だけ行う初期化
0182: //      ウィンドウの初期化やIDirect3D9の初期化は終わってます。
0183: //      ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。)
0184: //-------------------------------------------------------------
0185: HRESULT CMyD3DApplication::OneTimeSceneInit()
0186: {
0187:     // Drawing loading status message until app finishes loading
0188:     // (ローディングメッセージを表示する)
0189:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0190: 
0191:     m_bLoadingApp = FALSE;
0192: 
0193:     return S_OK;
0194: }
0195: 
0196: 
0197: 
0198: 
0199: //-------------------------------------------------------------
0200: // Name: ConfirmDevice()
0201: // Desc: Called during device initialization, this code checks the device
0202: //       for some minimum set of capabilities
0203: //       (初期化の時に呼ばれます。必要な能力をチェックします。)
0204: //-------------------------------------------------------------
0205: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0206:                      DWORD dwBehavior,    D3DFORMAT Format )
0207: {
0208:     UNREFERENCED_PARAMETER( Format );
0209:     UNREFERENCED_PARAMETER( dwBehavior );
0210:     UNREFERENCED_PARAMETER( pCaps );
0211:     
0212: 
0213:     // No fallback, so need ps2.0
0214:     // (ピクセルシェーダバージョンチェック)
0215:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0216:         return E_FAIL;
0217: 
0218:     // If device doesn't support 1.1 vertex shaders in HW, switch to SWVP.
0219:     // 頂点シェーダバージョンが上位かソフトウェア頂点処理
0220:     if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0221:     &&  0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0222:             return E_FAIL;
0223: 
0224:     return S_OK;
0225: }
0226: 
0227: 
0228: //-------------------------------------------------------------
0229: // SH coefficient of imcomming light
0230: //-------------------------------------------------------------
0231: // Variables send from application
0232: typedef struct {
0233:     int iRank;      // SH dimension
0234:     bool bFront;    // Front or back
0235: }sParaboloid;
0236: 
0237: VOID WINAPI ParaboloidJacobian (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord, 
0238: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0239: {
0240:     UNREFERENCED_PARAMETER( pTexelSize );
0241: 
0242:     sParaboloid *p = (sParaboloid *)pData;
0243: 
0244:     FLOAT x = 2.0f*( pTexCoord->x-0.5f);
0245:     FLOAT y = 2.0f*(-pTexCoord->y+0.5f);
0246: 
0247:     FLOAT r2 = x*x+y*y;
0248:     FLOAT J = 0.5f*(1.0f+r2);
0249: 
0250:     FLOAT col = (r2<(1.0f-1.0f/MAP_SIZE)*(1.0f-1.0f/MAP_SIZE)) ? J : 0.0f;
0251:     
0252:     // Normal vector
0253:     FLOAT nz = 0.5f * (1.0f-r2);
0254:     FLOAT nx = x;
0255:     FLOAT ny = y;
0256:     FLOAT n = sqrtf(nx*nx+ny*ny+nz*nz);
0257:     nx /= n;
0258:     ny /= n;
0259:     nz /= n;
0260:     if(p->bFront) nz *= -1;
0261: 
0262:     pOut->x = 0.5f * col * (-nx) + 0.5f;
0263:     pOut->y = 0.5f * col * ( ny) + 0.5f;
0264:     pOut->z = 0.5f * col * (-nz) + 0.5f;
0265:     pOut->w = 0.5f * col         + 0.5f;
0266: }
0267: 
0268: //-------------------------------------------------------------
0269: // 真ん中が白い絵を作る
0270: //-------------------------------------------------------------
0271: VOID WINAPI FillTex (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord, 
0272: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0273: {
0274:     UNREFERENCED_PARAMETER( pData );
0275: 
0276:     FLOAT x = 2.0f*(pTexCoord->x-0.5f);
0277:     FLOAT y = 2.0f*(pTexCoord->y-0.5f);
0278:     FLOAT col = (x*x+y*y<(1.0f-pTexelSize->x)*(1.0f-pTexelSize->y))
0279:                 ? 1.0f : 0.0f;
0280:     
0281:     pOut->x = pOut->y = pOut->z = pOut->w = col;
0282: }
0283: 
0284: //-------------------------------------------------------------
0285: // Name: InitDeviceObjects()
0286: // Desc: Paired with DeleteDeviceObjects()
0287: //       The device has been created.  Resources that are not lost on
0288: //       Reset() can be created here -- resources in D3DPOOL_MANAGED,
0289: //       D3DPOOL_SCRATCH, or D3DPOOL_SYSTEMMEM.  Image surfaces created via
0290: //       CreateImageSurface are never lost and can be created here.  Vertex
0291: //       shaders and pixel shaders can also be created here as they are not
0292: //       lost on Reset().
0293: //      (デバイスが生成された後の初期化をします。
0294: //      フレームバッファフォーマットやデバイスの種類が変わった後に通過します。
0295: //      ここで確保したメモリはDeleteDeviceObjects()で開放します)
0296: //-------------------------------------------------------------
0297: HRESULT CMyD3DApplication::InitDeviceObjects()
0298: {
0299:     HRESULT hr;
0300:     int i;
0301: 
0302:     // Load objects (モデルの読み込み)
0303:     if(FAILED(hr=m_pMesh->Create( m_pd3dDevice, _T("t-pot.x"))))
0304:         return DXTRACE_ERR( "Load Model", hr );
0305:     m_pMesh->UseMeshMaterials(FALSE);// Not set textures when rendering(レンダリング時にテクスチャの設定をしない)
0306:         
0307:     if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, _T("map.x"))))
0308:         return DXTRACE_ERR( "Load Model", hr );
0309:     m_pMeshBg->UseMeshMaterials(FALSE);// Not set textures when rendering(レンダリング時にテクスチャの設定をしない)
0310:         
0311:     if(FAILED(hr=m_pMeshEnv->Create( m_pd3dDevice, _T("room.x"))))
0312:         return DXTRACE_ERR( "Load Model", hr );
0313:     m_pMeshEnv->UseMeshMaterials(FALSE);// Not set textures when rendering(レンダリング時にテクスチャの設定をしない)
0314:         
0315: #ifdef NV3X
0316:     if(FAILED(hr=D3DXCreateTextureFromFile(
0317:                 m_pd3dDevice, _T("PRT.bmp"), &m_pPrtTex)))
0318:         return DXTRACE_ERR( "Load Texture", hr );
0319: #endif // NV3X
0320: 
0321:     // Create textures
0322:     sParaboloid data;
0323:     for( i = 0; i < TEX_MAX; i++ )
0324:     {
0325:         data.iRank = i;
0326:         // SH coefficient of incoming light (重み付けのテクスチャ)
0327:         data.bFront = 0;
0328:         if( FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1
0329:                             , 0, D3DFMT_A8R8G8B8
0330:                             , D3DPOOL_MANAGED, &m_pJacobianTex[i][0], NULL)))
0331:             return E_FAIL;
0332:         if( FAILED(D3DXFillTexture(m_pJacobianTex[i][0], ParaboloidJacobian, &data)))
0333:             return E_FAIL;
0334: 
0335:         data.bFront = 1;
0336:         if( FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1
0337:                             , 0, D3DFMT_A8R8G8B8
0338:                             , D3DPOOL_MANAGED, &m_pJacobianTex[i][1], NULL)))
0339:             return E_FAIL;
0340:         if( FAILED(D3DXFillTexture(m_pJacobianTex[i][1], ParaboloidJacobian, &data)))
0341:             return E_FAIL;
0342:     }
0343: 
0344:     // Create the effect(シェーダの読み込み)
0345:     LPD3DXBUFFER pErr;
0346:     if( FAILED( hr = D3DXCreateEffectFromFile(
0347:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0348:                 D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0349:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0350:                     , "ERROR", MB_OK);
0351:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0352:     }
0353:     m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0354:     m_hmWV       = m_pEffect->GetParameterByName( NULL, "mWV" );
0355:     m_hmWVP      = m_pEffect->GetParameterByName( NULL, "mWVP" );
0356:     m_htSrcTex   = m_pEffect->GetParameterByName( NULL, "SrcTex" );
0357:     m_fWidth     = m_pEffect->GetParameterByName( NULL, "MAP_WIDTH" );
0358:     m_fHeight    = m_pEffect->GetParameterByName( NULL, "MAP_HEIGHT" );
0359: 
0360:     // Init the font(フォント)
0361:     m_pFont->InitDeviceObjects( m_pd3dDevice );
0362: 
0363:     return S_OK;
0364: }
0365: 
0366: //-------------------------------------------------------------
0367: // Name: RestoreDeviceObjects()
0368: // Desc: Paired with InvalidateDeviceObjects()
0369: //       The device exists, but may have just been Reset().  Resources in
0370: //       D3DPOOL_DEFAULT and any other device state that persists during
0371: //       rendering should be set here.  Render states, matrices, textures,
0372: //       etc., that don't change during rendering can be set once here to
0373: //       avoid redundant state setting during Render() or FrameMove().
0374: //       (画面のサイズが変更された時等に呼ばれます。
0375: //       確保したメモリはInvalidateDeviceObjects()で開放します。)
0376: //-------------------------------------------------------------
0377: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0378: {
0379:     DWORD i;
0380: 
0381:     m_iState = -1;
0382:     m_iChangeState = TRUE;
0383:     m_bPause = false;
0384: 
0385:     // Restore meshes(メッシュ)
0386:     m_pMesh->RestoreDeviceObjects( m_pd3dDevice );
0387:     m_pMeshBg->RestoreDeviceObjects( m_pd3dDevice );
0388:     m_pMeshEnv->RestoreDeviceObjects( m_pd3dDevice );
0389: 
0390:     // Setup a material (質感の設定)
0391:     D3DMATERIAL9 mtrl;
0392:     D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0393:     m_pd3dDevice->SetMaterial( &mtrl );
0394: 
0395:     // Set miscellaneous render states(レンダリング環境の設定)
0396:     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   FALSE );
0397:     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
0398:     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
0399:     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0x000F0F0F );
0400:     
0401:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0402:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0403:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0404:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
0405:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
0406:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
0407:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0408:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0409:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0410:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0411: 
0412:     // World transform to identity (ワールド行列)
0413:     D3DXMATRIX matIdentity;
0414:     D3DXMatrixIdentity( &m_mWorld );
0415: 
0416:     // Set up the view parameters for the camera(ビュー行列)
0417:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0418:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0419:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0420:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0421: 
0422:     // Set the camera projection matrix(射影行列)
0423:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0424:     D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0425: 
0426:     // Create the stencil buffer to be used with the paraboloid textures(深度マップの生成)
0427:     if (FAILED(m_pd3dDevice->CreateDepthStencilSurface(MAP_SIZE, MAP_SIZE, 
0428:         D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pMapZ, NULL)))
0429:         return E_FAIL;
0430: #ifndef NV3X
0431:     // Create height map(位置マップ)
0432:     if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0433:         D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pPosTex, NULL)))
0434:         return E_FAIL;
0435:     if (FAILED(m_pPosTex->GetSurfaceLevel(0, &m_pPosSurf)))
0436:         return E_FAIL;
0437:     if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0438:         0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pPosLockTex, NULL)))
0439:         return E_FAIL;
0440:     if (FAILED(m_pPosLockTex->GetSurfaceLevel(0, &m_pPosLockSurf)))
0441:         return E_FAIL;
0442:     // Create normal map(法線マップ)
0443:     if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0444:         D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pNormalTex, NULL)))
0445:         return E_FAIL;
0446:     if (FAILED(m_pNormalTex->GetSurfaceLevel(0, &m_pNormalSurf)))
0447:         return E_FAIL;
0448:     if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0449:         0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pNormalLockTex, NULL)))
0450:         return E_FAIL;
0451:     if (FAILED(m_pNormalLockTex->GetSurfaceLevel(0, &m_pNormalLockSurf)))
0452:         return E_FAIL;
0453: #endif // !NV3X
0454:     // Create the paraboloid textures(放物マップ)
0455:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0456:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pParaboloidTex[0], NULL)))
0457:         return E_FAIL;
0458:     if (FAILED(m_pParaboloidTex[0]->GetSurfaceLevel(0, &m_pParaboloidSurf[0])))
0459:         return E_FAIL;
0460:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0461:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pParaboloidTex[1], NULL)))
0462:         return E_FAIL;
0463:     if (FAILED(m_pParaboloidTex[1]->GetSurfaceLevel(0, &m_pParaboloidSurf[1])))
0464:         return E_FAIL;
0465: 
0466:     // マスク用のテクスチャの生成
0467:     if( FAILED(m_pd3dDevice->CreateTexture( 64, 64, 1
0468:                           , 0, D3DFMT_A8R8G8B8
0469:                           , D3DPOOL_DEFAULT, &m_pMaskTex, NULL)))
0470:         return E_FAIL;
0471:     if( FAILED(D3DXFillTexture(m_pMaskTex, FillTex, NULL)))
0472:         return E_FAIL;
0473: 
0474:     // Create the reduction textures(縮小バッファ)
0475:     int size = 128;
0476:     for( i = 0; i < REDUCTION_MAPS; i++ )
0477:     {
0478:         if (FAILED(m_pd3dDevice->CreateTexture(size, size, 1, 
0479:             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pReductionTex[i], NULL)))
0480:             return E_FAIL;
0481:         if (FAILED(m_pReductionTex[i]->GetSurfaceLevel(0, &m_pReductionSurf[i])))
0482:             return E_FAIL;
0483:         size /= 8;
0484:     }
0485:     if (FAILED(m_pd3dDevice->CreateTexture( 64, 64, 1, 
0486:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_p64Tex, NULL)))
0487:         return E_FAIL;
0488:     if (FAILED(m_p64Tex->GetSurfaceLevel(0, &m_p64Surf)))
0489:         return E_FAIL;
0490:     if (FAILED(m_pd3dDevice->CreateTexture( 8, 8, 1, 
0491:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_p8Tex, NULL)))
0492:         return E_FAIL;
0493:     if (FAILED(m_p8Tex->GetSurfaceLevel(0, &m_p8Surf)))
0494:         return E_FAIL;
0495: 
0496:     // 拡散マップ
0497:     for(i=0;i<TEX_MAX;i++){
0498:         if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0499:             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pFinalTex[i], NULL)))
0500:             return E_FAIL;
0501:         if (FAILED(m_pFinalTex[i]->GetSurfaceLevel(0, &m_pFinalSurf[i])))
0502:             return E_FAIL;
0503:     }
0504: 
0505:     // Restore effect object
0506:     m_pEffect->OnResetDevice();
0507: 
0508:     // Restore the font(フォント)
0509:     m_pFont->RestoreDeviceObjects();
0510: 
0511:     return S_OK;
0512: }
0513: 
0514: 
0515: //-------------------------------------------------------------
0516: // Name: FrameMove()
0517: // Desc: Called once per frame, the call is the entry point for animating
0518: //       the scene.
0519: //       (毎フレーム呼ばれます。アニメの処理などを行います。)
0520: //-------------------------------------------------------------
0521: HRESULT CMyD3DApplication::FrameMove()
0522: {
0523:     m_iCount++;
0524:     
0525:     if(m_iChangeState) {m_iState++;m_iCount=0;m_iChangeState=0;}
0526: 
0527: #ifdef NV3X
0528:     m_iState = 2; // Do not need to Compute PRT texture
0529: #endif // NV3X
0530: 
0531:     switch(m_iState){
0532:     case 0:
0533:         m_iChangeState = this->FrameMoveCreateMap();
0534:         return S_OK;
0535:     case 1:
0536:         m_iChangeState = this->FrameMovePRT();
0537:         return S_OK;
0538:     default:
0539:         break;
0540:     }
0541: 
0542:     // Update user input state(入力データの更新)
0543:     UpdateInput( &m_UserInput );
0544: 
0545:     //---------------------------------------------------------
0546:     // Update the world state according to user input(入力に応じて座標系を更新する)
0547:     //---------------------------------------------------------
0548:     // Rotation(回転)
0549:     D3DXMATRIX matRotY;
0550:     D3DXMATRIX matRotX;
0551: 
0552:     if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0553:         m_fWorldRotY += m_fElapsedTime;
0554:     else
0555:     if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0556:         m_fWorldRotY -= m_fElapsedTime;
0557: 
0558:     if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0559:         m_fWorldRotX += m_fElapsedTime;
0560:     else
0561:     if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0562:         m_fWorldRotX -= m_fElapsedTime;
0563: 
0564:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0565:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0566: 
0567:     D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0568:     
0569:     //---------------------------------------------------------
0570:     // Update view matrix(ビュー行列の設定)
0571:     //---------------------------------------------------------
0572:     // Zoom(ズーム)
0573:     if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0574:         m_fViewZoom += m_fElapsedTime;
0575:     else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0576:         m_fViewZoom -= m_fElapsedTime;
0577: 
0578:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0579:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.3f, 0.0f );
0580:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0581:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0582: 
0583:     //---------------------------------------------------------
0584:     // Change the shader(シェーダの変更)
0585:     //---------------------------------------------------------
0586:     if( m_UserInput.bChangeShader ) m_Shader = (m_Shader + 1) % 3;
0587: 
0588:     //---------------------------------------------------------
0589:     // Pause(ポーズ)
0590:     //---------------------------------------------------------
0591:     if( m_UserInput.bPause ) m_bPause = !m_bPause;
0592: 
0593:     //---------------------------------------------------------
0594:     // Move the ball (玉の移動)
0595:     //---------------------------------------------------------
0596:     if( !m_bPause )
0597:     {
0598:         D3DXVec3Normalize( &m_vel, &m_vel );
0599:         D3DXVec3Scale( &m_vel, &m_vel, 10 );
0600:         m_pos += m_vel * this->m_fElapsedTime;
0601:         m_rot += m_omega * this->m_fElapsedTime;
0602:         m_rot.x = (float)fmod(m_rot.x, 2.0f*D3DX_PI);
0603:         m_rot.y = (float)fmod(m_rot.y, 2.0f*D3DX_PI);
0604:         m_rot.z = (float)fmod(m_rot.z, 2.0f*D3DX_PI);
0605: 
0606:         float size = 5.0f - 1.0f;
0607: 
0608:         if( m_pos.x < -size )
0609:         {
0610:             m_pos.x = -size;
0611:             m_vel.x *= -1;
0612:         }
0613:         if( size < m_pos.x )
0614:         {
0615:             m_pos.x = size;
0616:             m_vel.x *= -1;
0617:         }
0618:         if( m_pos.y < -size )
0619:         {
0620:             m_pos.y = -size;
0621:             m_vel.y *= -1;
0622:         }
0623:         if( size < m_pos.y )
0624:         {
0625:             m_pos.y = size;
0626:             m_vel.y *= -1;
0627:         }
0628:         if( m_pos.z < -size )
0629:         {
0630:             m_pos.z = -size;
0631:             m_vel.z *= -1;
0632:         }
0633:         if( size < m_pos.z )
0634:         {
0635:             m_pos.z = size;
0636:             m_vel.z *= -1;
0637:         }
0638:     }
0639: 
0640:     return S_OK;
0641: }
0642: //-------------------------------------------------------------
0643: // Name: UpdateInput()
0644: // Desc: Update the user input.  Called once per frame 
0645: //       (入力データを更新する)
0646: //-------------------------------------------------------------
0647: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0648: {
0649:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0650:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0651:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0652:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0653:     
0654:     pUserInput->bZoomIn      = ( m_bActive && (GetAsyncKeyState( 'Z'     )  & 0x8000) == 0x8000 );
0655:     pUserInput->bZoomOut     = ( m_bActive && (GetAsyncKeyState( 'X'      ) & 0x8000) == 0x8000 );
0656:     
0657:     pUserInput->bChangeShader= ( m_bActive && (GetAsyncKeyState( 'A'      ) & 0x8001) == 0x8001 );
0658:     pUserInput->bPause       = ( m_bActive && (GetAsyncKeyState( 'P'      ) & 0x8001) == 0x8001 );
0659: }
0660: 
0661: 
0662: 
0663: //-------------------------------------------------------------
0664: // Name: FrameMoveCreateMap()
0665: // Desc: Create height map and normal map
0666: //       (位置、法線マップを作成する。)
0667: //-------------------------------------------------------------
0668: int CMyD3DApplication::FrameMoveCreateMap()
0669: {
0670:     // 2回目の以降は次の処理に移る
0671:     if(1<m_iCount) return 1;
0672: 
0673:     return 0;
0674: }
0675: 
0676: //-------------------------------------------------------------
0677: // Name: RenderCreateMap()
0678: // Desc: Create height map and normal map
0679: //       (位置、法線マップを作成する。)
0680: //-------------------------------------------------------------
0681: void CMyD3DApplication::RenderCreateMap()
0682: {
0683:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0684:     D3DVIEWPORT9 oldViewport;
0685: 
0686:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0687:     {
0688:         //-------------------------------------------------
0689:         // レンダリングターゲットの保存
0690:         //-------------------------------------------------
0691:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0692:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0693:         m_pd3dDevice->GetViewport(&oldViewport);
0694: 
0695:         //-------------------------------------------------
0696:         // レンダリングターゲットの変更
0697:         //-------------------------------------------------
0698:         m_pd3dDevice->SetRenderTarget(0, m_pPosSurf);
0699:         m_pd3dDevice->SetRenderTarget(1, m_pNormalSurf);
0700:         m_pd3dDevice->SetDepthStencilSurface(NULL);
0701:         // ビューポートの変更
0702:         D3DVIEWPORT9 viewport_height = {0,0      // 左上の座標
0703:                         , DECALE_MAP_SIZE  // 幅
0704:                         , DECALE_MAP_SIZE // 高さ
0705:                         , 0.0f,1.0f};     // 前面、後面
0706:         m_pd3dDevice->SetViewport(&viewport_height);
0707: 
0708:         //-------------------------------------------------
0709:         // フレームバッファのクリア
0710:         //-------------------------------------------------
0711:         m_pd3dDevice->Clear(0L, NULL
0712:                         , D3DCLEAR_TARGET
0713:                         , 0xffffffff, 1.0f, 0L);
0714: 
0715:         if( m_pEffect != NULL ) 
0716:         {
0717:             //-------------------------------------------------
0718:             // シェーダの設定
0719:             //-------------------------------------------------
0720:             m_pEffect->SetTechnique( m_hTechnique );
0721:             m_pEffect->Begin( NULL, 0 );
0722:             m_pEffect->Pass( 10 );
0723: 
0724:             m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
0725:             m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
0726: 
0727:             //-------------------------------------------------
0728:             // モデルの描画
0729:             //-------------------------------------------------
0730:             m_pMesh->Render(m_pd3dDevice);
0731: 
0732:             m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
0733:             m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
0734: 
0735:             m_pEffect->End();
0736:         }
0737: 
0738:         //-----------------------------------------------------
0739:         // レンダリングターゲットを元に戻す
0740:         //-----------------------------------------------------
0741:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0742:         m_pd3dDevice->SetRenderTarget(1, NULL);
0743:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0744:         m_pd3dDevice->SetViewport(&oldViewport);
0745:         pOldBackBuffer->Release();
0746:         pOldZBuffer->Release();
0747: 
0748:         //-----------------------------------------------------
0749:         // ごまかしの画面
0750:         //-----------------------------------------------------
0751:         m_pd3dDevice->Clear(0L, NULL
0752:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0753:                         , 0xff000000, 1.0f, 0L);
0754: 
0755:         m_pd3dDevice->EndScene();
0756:     }
0757: }
0758: 
0759: 
0760: //-------------------------------------------------------------
0761: // Name: FrameMovePRT()
0762: // Desc: 位置、法線マップを作成する。
0763: //-------------------------------------------------------------
0764: static int itex=0;
0765: static int ix=0;
0766: static int iy=0;
0767: static float pos[DECALE_MAP_SIZE][DECALE_MAP_SIZE][3];
0768: static float normal[DECALE_MAP_SIZE][DECALE_MAP_SIZE][3];
0769: int CMyD3DApplication::FrameMovePRT()
0770: {
0771:     itex = this->m_iCount / (DECALE_MAP_SIZE*DECALE_MAP_SIZE);
0772:     ix = this->m_iCount % DECALE_MAP_SIZE;
0773:     iy = (this->m_iCount / DECALE_MAP_SIZE) % DECALE_MAP_SIZE;
0774:     
0775:     if(0==this->m_iCount){
0776:         D3DLOCKED_RECT d3dlr;
0777:         float *p;
0778:         m_pd3dDevice->GetRenderTargetData(m_pPosSurf,m_pPosLockSurf);
0779:         m_pPosLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY); //サーフェイス上の矩形をロック    
0780:         p = (float *)d3dlr.pBits;
0781:         for(int y=0;y<DECALE_MAP_SIZE;y++){
0782:         for(int x=0;x<DECALE_MAP_SIZE;x++){
0783:             pos[x][y][0]=p[0];
0784:             pos[x][y][1]=p[1];
0785:             pos[x][y][2]=p[2];
0786:             p+=4;
0787:         }
0788:         }
0789:         m_pPosLockSurf->UnlockRect();
0790: 
0791:         m_pd3dDevice->GetRenderTargetData(m_pNormalSurf,m_pNormalLockSurf);
0792:         m_pNormalLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY); //サーフェイス上の矩形をロック    
0793:         p = (float *)d3dlr.pBits;        
0794:         for( y=0;y<DECALE_MAP_SIZE;y++){
0795:         for( int x=0;x<DECALE_MAP_SIZE;x++){
0796:             normal[x][y][0]=p[0];
0797:             normal[x][y][1]=p[1];
0798:             normal[x][y][2]=p[2];
0799:             p+=4;
0800:         }
0801:         }
0802:         m_pNormalLockSurf->UnlockRect();
0803:     }
0804: 
0805:     // 終了条件
0806:     if(DECALE_MAP_SIZE-1==ix && DECALE_MAP_SIZE-1==iy && TEX_MAX-1==itex) return 1;
0807: 
0808:     return 0;
0809: }
0810: 
0811: 
0812: //-------------------------------------------------------------
0813: // Name: RenderPRT()
0814: // Desc: それぞれの位置からのラディアンスを計算する。
0815: //-------------------------------------------------------------
0816: void CMyD3DApplication::RenderPRT()
0817: {
0818:     D3DXMATRIX m, mView;
0819:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0820:     D3DVIEWPORT9 oldViewport;
0821:     D3DSURFACE_DESC d3dsd;
0822: 
0823:     D3DVIEWPORT9 viewport = {0,0      // 左上の座標
0824:                     , 1, 1  // 幅,高さ
0825:                     , 0.0f,1.0f};     // 前面、後面
0826: 
0827:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0828:     {
0829:         m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
0830:         m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
0831: 
0832:         //-------------------------------------------------
0833:         // レンダリングターゲットの保存
0834:         //-------------------------------------------------
0835:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0836:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0837:         m_pd3dDevice->GetViewport(&oldViewport);
0838: 
0839:         //-------------------------------------------------
0840:         //-------------------------------------------------
0841:         // 各点から見たラディアンスの描画
0842:         //-------------------------------------------------
0843:         //-------------------------------------------------
0844: 
0845:         //-------------------------------------------------
0846:         // レンダリングターゲットの変更
0847:         //-------------------------------------------------
0848:         m_pd3dDevice->SetRenderTarget(0, m_p64Surf );
0849:         m_pd3dDevice->SetDepthStencilSurface(NULL);
0850:         // ビューポートの変更
0851:         m_pReductionSurf[0]->GetDesc(&d3dsd);
0852:         viewport.Height = d3dsd.Width;
0853:         viewport.Width  = d3dsd.Height;
0854:         m_pd3dDevice->SetViewport(&viewport);
0855: 
0856:         if( m_pEffect != NULL ) 
0857:         {
0858:             //-------------------------------------------------
0859:             // 重みを持つ半球の部分だけ描画
0860:             //-------------------------------------------------
0861:             float x[3] = {pos[ix][iy][0], pos[ix][iy][1], pos[ix][iy][2]};
0862:             float n[3] = {normal[ix][iy][0], normal[ix][iy][1], normal[ix][iy][2]};
0863:             D3DXVECTOR3 vFromPt   = D3DXVECTOR3( x[0], x[1], x[2] ) + 0.01f*D3DXVECTOR3( n[0], n[1], n[2] );
0864:             D3DXVECTOR3 vLookatPt = D3DXVECTOR3( n[0], n[1], n[2] ) + vFromPt;
0865:             D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0866:             D3DXMatrixLookAtLH( &mView, &vFromPt, &vLookatPt, &vUpVec );
0867:             m_pEffect->SetMatrix( "mWV", &mView );
0868: 
0869:             D3DXMatrixTranspose(&m, &mView);
0870:             m_pEffect->SetMatrix( "mST", &m );
0871: 
0872:             // シェーダの設定
0873:             m_pEffect->SetTechnique( "TShader" );
0874:             m_pEffect->Begin( NULL, 0 );
0875: 
0876:             TVERTEX Vertex[4] = {
0877:                 // x  y  z tu tv
0878:                 {-1,-1,0,  0, 0,},
0879:                 {+1,-1,0,  1, 0,},
0880:                 {+1,+1,0,  1, 1,},
0881:                 {-1,+1,0,  0, 1,},
0882:             };
0883:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0884:             m_pEffect->SetTexture(m_htSrcTex, m_pMaskTex);
0885:             m_pEffect->Pass( 11 + itex );
0886:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0887: 
0888:             //-------------------------------------------------
0889:             // モデルの描画
0890:             //-------------------------------------------------
0891:             m_pEffect->Pass( 15 );
0892:             m_pMesh->Render(m_pd3dDevice);
0893:             m_pEffect->End();
0894:         }
0895:         //-------------------------------------------------
0896:         //-------------------------------------------------
0897:         // ラディアンスをミップマップの要領で小さくする
0898:         //-------------------------------------------------
0899:         //-------------------------------------------------
0900:         m_pd3dDevice->SetRenderTarget( 0, m_p8Surf );
0901:         m_pd3dDevice->SetDepthStencilSurface(NULL);
0902:         // ビューポートの変更
0903:         m_pReductionSurf[1]->GetDesc(&d3dsd);
0904:         viewport.Height = d3dsd.Width;
0905:         viewport.Width  = d3dsd.Height;
0906:         m_pd3dDevice->SetViewport(&viewport);
0907: 
0908:         if( m_pEffect != NULL ) {
0909:             //-------------------------------------------------
0910:             // シェーダの設定
0911:             //-------------------------------------------------
0912:             m_pEffect->SetTechnique( m_hTechnique );
0913:             m_pEffect->Begin( NULL, 0 );
0914:             m_pEffect->Pass( 3 );
0915: 
0916:             m_pEffect->SetFloat( m_fWidth,  64);
0917:             m_pEffect->SetFloat( m_fHeight, 64);
0918: 
0919:             //-------------------------------------------------
0920:             // フィルタリング
0921:             //-------------------------------------------------
0922:             TVERTEX Vertex1[4] = {
0923:                 //   x    y     z    tu tv
0924:                 {-1.0f, +1.0f, 0.1f,  0, 0},
0925:                 {+1.0f, +1.0f, 0.1f,  1, 0},
0926:                 {+1.0f, -1.0f, 0.1f,  1, 1},
0927:                 {-1.0f, -1.0f, 0.1f,  0, 1},
0928:             };
0929:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0930:             m_pEffect->SetTexture( m_htSrcTex, m_p64Tex );
0931:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0932:                             , 2, Vertex1, sizeof( TVERTEX ) );
0933: 
0934:             m_pEffect->End();
0935:         }
0936: 
0937:         //-------------------------------------------------
0938:         //-------------------------------------------------
0939:         // ラディアンスをテクスチャの対応する位置に張る
0940:         //-------------------------------------------------
0941:         //-------------------------------------------------
0942:         m_pd3dDevice->SetRenderTarget(0, m_pFinalSurf[itex]);
0943:         m_pd3dDevice->SetDepthStencilSurface(NULL);
0944:         // ビューポートの変更
0945:         m_pFinalSurf[itex]->GetDesc(&d3dsd);
0946:         viewport.Height = d3dsd.Width;
0947:         viewport.Width  = d3dsd.Height;
0948:         m_pd3dDevice->SetViewport(&viewport);
0949:         
0950:         // 最初は灰色で塗りつぶす
0951:         if(0==ix&&0==iy)m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, 0x80808080, 1.0f, 0L);
0952: 
0953:         if( m_pEffect != NULL ) {
0954:             //-------------------------------------------------
0955:             // シェーダの設定
0956:             //-------------------------------------------------
0957:             m_pEffect->SetTechnique( m_hTechnique );
0958:             m_pEffect->Begin( NULL, 0 );
0959:             m_pEffect->Pass( 3 );
0960: 
0961:             m_pEffect->SetFloat( m_fWidth,  8);
0962:             m_pEffect->SetFloat( m_fHeight, 8);
0963: 
0964:             //-------------------------------------------------
0965:             // フィルタリング
0966:             //-------------------------------------------------
0967:             float x =  2.0f*((float)ix/(float)DECALE_MAP_SIZE) - 1.0f;
0968:             float y = -2.0f*((float)iy/(float)DECALE_MAP_SIZE) + 1.0f;
0969:             TVERTEX Vertex1[4] = {
0970:                 //   x    y     z    tu tv
0971:                 {x,                      y,                     0.1f,  0, 0},
0972:                 {x+2.0f/(float)DECALE_MAP_SIZE, y,                     0.1f,  1, 0},
0973:                 {x+2.0f/(float)DECALE_MAP_SIZE, y-2.0f/(float)DECALE_MAP_SIZE,0.1f,  1, 1},
0974:                 {x,                      y-2.0f/(float)DECALE_MAP_SIZE,0.1f,  0, 1},
0975:             };
0976:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0977:             m_pEffect->SetTexture( m_htSrcTex, m_p8Tex );
0978:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0979:                             , 2, Vertex1, sizeof( TVERTEX ) );
0980: 
0981:             m_pEffect->End();
0982:         }
0983: 
0984:         
0985:         //-----------------------------------------------------
0986:         // レンダリングターゲットを元に戻す
0987:         //-----------------------------------------------------
0988:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0989:         m_pd3dDevice->SetRenderTarget(1, NULL);
0990:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0991:         m_pd3dDevice->SetViewport(&oldViewport);
0992:         pOldBackBuffer->Release();
0993:         pOldZBuffer->Release();
0994: 
0995:         m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
0996:         m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
0997: 
0998:         //---------------------------------------------------------
0999:         // 画面の表示
1000:         //---------------------------------------------------------
1001:         m_pd3dDevice->Clear(0L, NULL
1002:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1003:                         , 0x00001020, 1.0f, 0L);
1004: 
1005: #if 1 // デバッグ用にテクスチャを表示する
1006:         {
1007:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
1008:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
1009:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
1010:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
1011:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
1012:         m_pd3dDevice->SetVertexShader(NULL);
1013:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1014:         m_pd3dDevice->SetPixelShader(0);
1015:         float scale = 256.0f*2/3;
1016:         for(DWORD i=0; i<3; i++){
1017:             TVERTEX Vertex[4] = {
1018:                 // x  y  z rhw tu tv
1019:                 {    0,(i+0)*scale,0, 1, 0, 0,},
1020:                 {scale,(i+0)*scale,0, 1, 1, 0,},
1021:                 {scale,(i+1)*scale,0, 1, 1, 1,},
1022:                 {    0,(i+1)*scale,0, 1, 0, 1,},
1023:             };
1024:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pFinalTex[itex] );
1025:             if(1==i) m_pd3dDevice->SetTexture( 0, m_p64Tex );
1026:             if(2==i) m_pd3dDevice->SetTexture( 0, m_p8Tex );
1027:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1028:         }
1029:         }
1030: #endif      
1031: 
1032:         D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
1033:         TCHAR szMsg[MAX_PATH] = TEXT("");
1034: 
1035:         sprintf( szMsg, TEXT("x:%4d\ny:%4d"), ix, iy);
1036:         m_pFont->DrawText( 2, (FLOAT)m_d3dsdBackBuffer.Height-40, fontColor, szMsg );
1037: 
1038:         m_pd3dDevice->EndScene();
1039:     }
1040: }
1041: 
1042: 
1043: //-------------------------------------------------------------
1044: // Name: RenderParaboloidMap()
1045: //-------------------------------------------------------------
1046: void CMyD3DApplication::RenderParaboloidMap()
1047: {
1048:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
1049:     D3DVIEWPORT9 oldViewport;
1050:     D3DMATERIAL9 *pMtrl;
1051:     int i, j, k;
1052:     D3DXMATRIX m, mT, mR, mW, mView, mProj;
1053:     D3DXVECTOR3 vFrom;
1054:     D3DXVECTOR3 vLookat;
1055:     D3DXVECTOR3 vUpVec;
1056: 
1057:     D3DVIEWPORT9 viewport = {0, 0   // x,y(左上の座標)
1058:                     , 1, 1          // width, height(幅,高さ)
1059:                     , 0.0f,1.0f};   // near, far(前面、後面)
1060: 
1061:     //-------------------------------------------------
1062:     // Backup rendering target(レンダリングターゲットの保存)
1063:     //-------------------------------------------------
1064:     m_pd3dDevice->GetRenderTarget( 0, &pOldBackBuffer );
1065:     m_pd3dDevice->GetDepthStencilSurface( &pOldZBuffer );
1066:     m_pd3dDevice->GetViewport( &oldViewport );
1067:     
1068:     //-------------------------------------------------
1069:     // Create environment map(環境マップの作成)
1070:     //-------------------------------------------------
1071:     if( m_pEffect != NULL ) 
1072:     {
1073:         // Set shader
1074:         m_pEffect->SetTechnique( m_hTechnique );
1075:         m_pEffect->Begin( NULL, 0 );
1076:         m_pEffect->Pass( 4 );
1077: 
1078:         for( i = 0; i < 2; i++ )
1079:         {
1080:             // Chenge rendering target
1081:             m_pd3dDevice->SetRenderTarget( 0, m_pParaboloidSurf[i] );
1082:             m_pd3dDevice->SetDepthStencilSurface( m_pMapZ );
1083:             viewport.Width  = viewport.Height = MAP_SIZE;
1084:             m_pd3dDevice->SetViewport( &viewport );
1085:             
1086:             // Clear the scene
1087:             m_pd3dDevice->Clear(0L, NULL
1088:                 , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1089:                 , 0xff000000, 1.0f, 0L);
1090:             
1091:             // Create view matrix
1092: 
1093:             // Set matrixs(行列の設定)
1094:             D3DXMatrixRotationX( &mW, m_rot.x );
1095:             D3DXMatrixRotationY( &mR, m_rot.y );
1096:             mW = mW * mR;
1097:             D3DXMatrixRotationZ( &mR, m_rot.z );
1098:             mW = mW * mR;
1099:             D3DXMatrixTranslation( &mT, m_pos.x, m_pos.y, m_pos.z );
1100:             mW = mW * mT;
1101: 
1102:             vFrom   = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
1103:             D3DXVec3TransformCoord( &vFrom, &vFrom, &mW);
1104:             switch(i)
1105:             {
1106:             case 0:
1107:                 // Front of view(前面)
1108:                 vLookat = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
1109:                 break;
1110:             case 1:
1111:                 // Back of view(後面)
1112:                 vLookat = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
1113:                 break;
1114:             }
1115:             D3DXVec3TransformCoord( &vLookat, &vLookat, &mW);
1116:             vUpVec  = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
1117:             D3DXVec3TransformCoord( &vUpVec, &vUpVec, &mW);
1118:             vUpVec -= vFrom;
1119:             D3DXMatrixLookAtLH( &mView, &vFrom, &vLookat, &vUpVec );
1120: 
1121:             // Render ground(地面の描画)
1122:             m = mView;
1123:             D3DXMatrixTranslation( &mT, 0, -5, 0 );
1124:             m = mT * m;
1125: //D3DXMatrixTranslation( &mT, 0, -2, 0 );
1126: //D3DXMatrixRotationZ( &m, 0.5*D3DX_PI );
1127: //m = mT * m * mView;
1128:             m_pEffect->SetMatrix( m_hmWV, &m );
1129: 
1130:             pMtrl = m_pMeshBg->m_pMaterials;
1131:             for( unsigned int s=0; s<m_pMeshBg->m_dwNumMaterials; s++ ) {
1132:                 m_pEffect->SetTexture(m_htSrcTex, m_pMeshBg->m_pTextures[s] );
1133:                 m_pMeshBg->m_pLocalMesh->DrawSubset( s );
1134:                 pMtrl++;
1135:             }
1136:             
1137:             // Render back sphere(天球の描画)
1138:             D3DXMatrixScaling ( &m, 10, 10, 10 );
1139:             m = m * mView;
1140:             m_pEffect->SetMatrix( m_hmWV, &m );
1141:             
1142:             pMtrl = m_pMeshEnv->m_pMaterials;
1143:             for( s=0; s<m_pMeshEnv->m_dwNumMaterials; s++ ) {
1144:                 m_pEffect->SetTexture(m_htSrcTex, m_pMeshEnv->m_pTextures[s] );
1145:                 m_pMeshEnv->m_pLocalMesh->DrawSubset( s );
1146:                 pMtrl++;
1147:             }
1148:         }
1149: 
1150:         //-------------------------------------------------
1151:         // Reduction in the way of a mip-map.(ミップマップの要領で小さくする)
1152:         //-------------------------------------------------
1153:         m_pEffect->End();
1154:         m_pEffect->SetTechnique( m_hTechnique );
1155:         m_pEffect->Begin( NULL, 0 );
1156: 
1157:         m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[0]);
1158:         m_pd3dDevice->SetDepthStencilSurface(NULL);
1159:         viewport.Width  = viewport.Height = 128;
1160:         m_pd3dDevice->SetViewport(&viewport);
1161:         
1162:         // Reduces to the screen divided into four by 1/2,
1163:         // and both sides are compounded by multiplying the SH coefficient
1164:         // (4分割した画面に1/2縮小して両面をSH係数で合成する)
1165:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX2 );
1166:         m_pEffect->SetTexture( "ParaboloidFrontTex", m_pParaboloidTex[0] );
1167:         m_pEffect->SetTexture( "ParaboloidBackTex",  m_pParaboloidTex[1] );
1168:         m_pEffect->SetTexture( "WeightTexFront", m_pJacobianTex[0][0] );
1169:         m_pEffect->SetTexture( "WeightTexBack", m_pJacobianTex[0][1] );
1170:         float scale = 64;
1171:         int pass = 6;
1172:         for( j = 0; j < 2; j++)
1173:         {
1174:             for( k = 0; k < 2; k++)
1175:             {
1176:                 m_pEffect->Pass( pass++ );
1177:                 TVERTEX Vertex[4] = {
1178:                     //    x           y       z rhw  tu       tv
1179:                     {(j+0)*scale,(k+0)*scale, 0, 1, 0+1/128, 0+1/128 },
1180:                     {(j+1)*scale,(k+0)*scale, 0, 1, 1+1/128, 0+1/128 },
1181:                     {(j+1)*scale,(k+1)*scale, 0, 1, 1+1/128, 1+1/128 },
1182:                     {(j+0)*scale,(k+1)*scale, 0, 1, 0+1/128, 1+1/128 },
1183:                 };
1184:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1185:             }
1186:         }
1187: 
1188: 
1189:         // 2nd reduction (16x16) (2段階)
1190:         m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[1]);
1191:         viewport.Width  = viewport.Height = 16;
1192:         m_pd3dDevice->SetViewport(&viewport);
1193:         m_pEffect->Pass( 3 );
1194: 
1195:         TVERTEX Vertex1[4] = {
1196:             //   x    y     z    tu tv
1197:             {-1.0f, +1.0f, 0.5f,  0, 0},
1198:             {+1.0f, +1.0f, 0.5f,  1, 0},
1199:             {+1.0f, -1.0f, 0.5f,  1, 1},
1200:             {-1.0f, -1.0f, 0.5f,  0, 1},
1201:         };
1202:         m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1203: 
1204:         m_pEffect->SetFloat( m_fWidth,  128 );
1205:         m_pEffect->SetFloat( m_fHeight, 128 );
1206:         m_pEffect->SetTexture( m_htSrcTex, m_pReductionTex[0] );
1207:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1208:                         , 2, Vertex1, sizeof( TVERTEX ) );
1209: 
1210: 
1211:         // 3rd reduction (2x2) (3段階)
1212:         m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[2]);
1213:         viewport.Width  = viewport.Height = 2;
1214:         m_pd3dDevice->SetViewport(&viewport);
1215: 
1216:         m_pEffect->SetFloat( m_fWidth,  16 );
1217:         m_pEffect->SetFloat( m_fHeight, 16 );
1218:         m_pEffect->SetTexture(m_htSrcTex, m_pReductionTex[1]);
1219:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1220:                         , 2, Vertex1, sizeof( TVERTEX ) );
1221: 
1222:         m_pEffect->End();
1223:     }
1224: 
1225: 
1226:     //-----------------------------------------------------
1227:     // Restore render target
1228:     // (レンダリングターゲットを元に戻す)
1229:     //-----------------------------------------------------
1230:     m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
1231:     m_pd3dDevice->SetRenderTarget(1, NULL);
1232:     m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
1233:     m_pd3dDevice->SetViewport(&oldViewport);
1234:     pOldBackBuffer->Release();
1235:     pOldZBuffer->Release();
1236: 
1237: }
1238: //-------------------------------------------------------------
1239: // Name: Render()
1240: // Desc: Called once per frame, the call is the entry point for 3d
1241: //       rendering. This function sets up render states, clears the
1242: //       viewport, and renders the scene.
1243: //       (画面を描画する)
1244: //-------------------------------------------------------------
1245: HRESULT CMyD3DApplication::Render()
1246: {
1247:     D3DXMATRIX m, mT, mR, mView, mProj;
1248:     DWORD i;
1249:     D3DXVECTOR4 v;
1250:     D3DMATERIAL9 *pMtrl;
1251: 
1252:     switch(m_iState){
1253:     case 0:
1254:         this->RenderCreateMap();
1255:         return S_OK;
1256:     case 1:
1257:         this->RenderPRT();
1258:         return S_OK;
1259:     default:
1260:         break;
1261:     }
1262: 
1263: 
1264:     // Begin the scene(描画開始)
1265:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
1266:     {
1267:         //---------------------------------------------------------
1268:         // Create maps(テクスチャの生成)
1269:         //---------------------------------------------------------
1270:         RenderParaboloidMap();
1271: 
1272:         // Clear the render buffers(フレームバッファのクリア)
1273:         m_pd3dDevice->Clear(0L, NULL
1274:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1275:                         , 0xff000000, 1.0f, 0L);
1276: 
1277:         if( m_pEffect != NULL ) 
1278:         {
1279:             //-------------------------------------------------
1280:             // Set effect(シェーダの設定)
1281:             //-------------------------------------------------
1282:             m_pEffect->SetTechnique( m_hTechnique );
1283:             m_pEffect->Begin( NULL, 0 );
1284: 
1285:             //-------------------------------------------------
1286:             // Render model (モデルの描画)
1287:             //-------------------------------------------------
1288:             m_pEffect->Pass( 0+m_Shader );
1289: 
1290:             // Set matrixs(行列の設定)
1291:             D3DXMatrixRotationX( &mR, m_rot.x );
1292:             D3DXMatrixRotationY( &m, m_rot.y );
1293:             mR = mR * m;
1294:             D3DXMatrixRotationZ( &m, m_rot.z );
1295:             mR = mR * m;
1296:             D3DXMatrixTranslation( &mT, m_pos.x, m_pos.y, m_pos.z );
1297:             m = mR * mT * m_mWorld * m_mView;
1298:             m_pEffect->SetMatrix( m_hmWV, &m );
1299:             m = m * m_mProj;
1300:             m_pEffect->SetMatrix( m_hmWVP, &m );
1301: 
1302:             // View point in the local coordinate system(ローカルでの視点)
1303:             m = mR * mT * m_mWorld * m_mView;
1304:             D3DXMatrixInverse( &m, NULL, &m);
1305:             v = D3DXVECTOR4( 0, 0, 0, 1 );
1306:             D3DXVec4Transform( &v, &v, &m);
1307:             m_pEffect->SetVector( "vEye", &v );
1308: 
1309:             // Set the texture and render
1310:             m_pEffect->SetTexture( "ParaboloidFrontTex", m_pParaboloidTex[0] );
1311:             m_pEffect->SetTexture( "ParaboloidBackTex",  m_pParaboloidTex[1] );
1312:             m_pEffect->SetTexture( m_htSrcTex, m_pReductionTex[2]);
1313: 
1314: #ifdef NV3X
1315:             m_pEffect->SetTexture( "PRTTex",  m_pPrtTex );
1316: #else // NV3X
1317:             m_pEffect->SetTexture( "PRTTex",  m_pFinalTex[0] );
1318: #endif // NV3X
1319:             
1320:             m_pEffect->SetTexture( "DecaleTex",  m_pMesh->m_pTextures[0] );
1321:             m_pMesh->Render( m_pd3dDevice );
1322: 
1323:             //-------------------------------------------------
1324:             // Render ground(地形の描画)
1325:             //-------------------------------------------------
1326:             // Set shader(シェーダの設定)
1327:             m_pEffect->Pass( 5 );
1328: 
1329:             // World+view+projection matrix(ローカル-射影行列)
1330:             m = m_mWorld * m_mView * m_mProj;
1331:             D3DXMatrixTranslation( &mT, 0, -5, 0 );
1332:             m = mT * m;
1333: //D3DXMatrixTranslation( &mT, 0, -2, 0 );
1334: //D3DXMatrixRotationZ( &m, 0.5*D3DX_PI );
1335: //m = mT * m * m_mWorld * m_mView * m_mProj;
1336:             m_pEffect->SetMatrix( m_hmWVP, &m );
1337:             
1338:             // Set the texture and render(テクスチャを設定して描画)
1339:             pMtrl = m_pMeshBg->m_pMaterials;
1340:             for( i=0; i<m_pMeshBg->m_dwNumMaterials; i++ ) {
1341:                 m_pEffect->SetTexture(m_htSrcTex, m_pMeshBg->m_pTextures[i] );
1342:                 m_pMeshBg->m_pLocalMesh->DrawSubset( i );
1343:                 pMtrl++;
1344:             }
1345: 
1346:             //-------------------------------------------------
1347:             // Render back sphere(天球の描画)
1348:             //-------------------------------------------------
1349:             // World+view+projection matrix(ローカル-射影行列)
1350:             D3DXMatrixScaling ( &m, 10, 10, 10 );
1351:             m = m * m_mWorld * m_mView;
1352:             m = m * m_mProj;
1353:             m_pEffect->SetMatrix( m_hmWVP, &m );
1354:             
1355:             // Set the texture and render(テクスチャを設定して描画)
1356:             pMtrl = m_pMeshEnv->m_pMaterials;
1357:             for( i=0; i<m_pMeshBg->m_dwNumMaterials; i++ ) {
1358:                 m_pEffect->SetTexture(m_htSrcTex, m_pMeshEnv->m_pTextures[i] );
1359:                 m_pMeshEnv->m_pLocalMesh->DrawSubset( i );
1360:                 pMtrl++;
1361:             }
1362: 
1363:             m_pEffect->End();
1364:         }
1365: 
1366:         // Render stats and help text(ヘルプの表示)
1367:         RenderText();
1368: 
1369: #ifdef _DEBUG // Textures are displaying when debugging, (デバッグ用にテクスチャを表示する)
1370:         {
1371:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
1372:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
1373:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
1374:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
1375:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
1376:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1377:         float scale = 64.0f;
1378:         for(DWORD i=0; i<10; i++){
1379:             TVERTEX Vertex[4] = {
1380:                 //    x                             y         z rhw tu tv
1381:                 {(i+0)*scale, (FLOAT)m_d3dsdBackBuffer.Height-scale, 0, 1, 0, 0,},
1382:                 {(i+1)*scale, (FLOAT)m_d3dsdBackBuffer.Height-scale, 0, 1, 1, 0,},
1383:                 {(i+1)*scale, (FLOAT)m_d3dsdBackBuffer.Height-    0, 0, 1, 1, 1,},
1384:                 {(i+0)*scale, (FLOAT)m_d3dsdBackBuffer.Height-    0, 0, 1, 0, 1,},
1385:             };
1386:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pParaboloidTex[0] );
1387:             if(1==i) m_pd3dDevice->SetTexture( 0, m_pParaboloidTex[1] );
1388:             if(2==i) m_pd3dDevice->SetTexture( 0, m_pJacobianTex[0][0] );
1389:             if(3==i) m_pd3dDevice->SetTexture( 0, m_pJacobianTex[0][1] );
1390:             if(4==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[0] );
1391:             if(5==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[1] );
1392:             if(6==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[2] );
1393: #ifdef NV3X
1394:             if(7==i) m_pd3dDevice->SetTexture( 0, m_pPrtTex );
1395:             if(8==i) break;
1396: #else
1397:             if(7==i) m_pd3dDevice->SetTexture( 0, m_pPosTex );
1398:             if(8==i) m_pd3dDevice->SetTexture( 0, m_pNormalTex );
1399:             if(9==i) m_pd3dDevice->SetTexture( 0, m_pFinalTex[0] );
1400: #endif // NV3X
1401: 
1402:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1403:         }
1404:         }
1405: #endif      
1406: 
1407:         // End the scene.(描画の終了)
1408:         m_pd3dDevice->EndScene();
1409:     }
1410: 
1411:     return S_OK;
1412: }
1413: 
1414: 
1415: 
1416: 
1417: //-------------------------------------------------------------
1418: // Name: RenderText()
1419: // Desc: Draw the help & statistics for running sample
1420: //       (状態やヘルプを画面に表示する)
1421: //-------------------------------------------------------------
1422: HRESULT CMyD3DApplication::RenderText()
1423: {
1424:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
1425:     TCHAR szMsg[MAX_PATH] = TEXT("");
1426:     FLOAT fNextLine;
1427: 
1428:     // Draw help
1429:     fNextLine = 0; 
1430: 
1431:     lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
1432:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1433:     fNextLine += 20.0f;
1434: 
1435:     lstrcpy( szMsg, TEXT("Press 'A' to change shader") );
1436:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1437:     fNextLine += 20.0f;
1438: 
1439:     // Output statistics
1440:     lstrcpy( szMsg, m_strDeviceStats );
1441:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1442:     fNextLine += 20.0f;
1443:     lstrcpy( szMsg, m_strFrameStats );
1444:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1445:     fNextLine += 20.0f;
1446:     
1447:     return S_OK;
1448: }
1449: 
1450: 
1451: 
1452: 
1453: //-------------------------------------------------------------
1454: // Name: MsgProc()
1455: // Desc: Overrrides the main WndProc, so the sample can do custom message
1456: //       handling (e.g. processing mouse, keyboard, or menu commands).
1457: //       (WndProc をオーバーライドしたもの)
1458: //-------------------------------------------------------------
1459: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
1460:                                  WPARAM wParam, LPARAM lParam )
1461: {
1462:     switch( msg )
1463:     {
1464:         case WM_PAINT:
1465:         {
1466:             if( m_bLoadingApp )
1467:             {
1468:                 // Loading message(ロード中)
1469:                 HDC hDC = GetDC( hWnd );
1470:                 TCHAR strMsg[MAX_PATH];
1471:                 wsprintf(strMsg, TEXT("Loading... Please wait"));
1472:                 RECT rct;
1473:                 GetClientRect( hWnd, &rct );
1474:                 DrawText( hDC, strMsg, -1, &rct
1475:                         , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
1476:                 ReleaseDC( hWnd, hDC );
1477:             }
1478:             break;
1479:         }
1480: 
1481:     }
1482: 
1483:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
1484: }
1485: 
1486: 
1487: 
1488: 
1489: //-------------------------------------------------------------
1490: // Name: InvalidateDeviceObjects()
1491: // Desc: Invalidates device objects.  Paired with RestoreDeviceObjects()
1492: //       (RestoreDeviceObjects() で作成したオブジェクトの開放)
1493: //-------------------------------------------------------------
1494: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
1495: {
1496:     int i;
1497: 
1498:     // font(フォント)
1499:     m_pFont->InvalidateDeviceObjects();
1500: 
1501:     // Rendering targets (レンダリングターゲット)
1502:     for(int j=0;j<TEX_MAX;j++){
1503:         SAFE_RELEASE(m_pFinalSurf[j]);
1504:         SAFE_RELEASE(m_pFinalTex[j]);
1505:     }
1506:     SAFE_RELEASE( m_p8Surf );
1507:     SAFE_RELEASE( m_p8Tex );
1508:     SAFE_RELEASE( m_p64Surf );
1509:     SAFE_RELEASE( m_p64Tex );
1510:     for( i = 0; i < REDUCTION_MAPS; i++ )
1511:     {
1512:         SAFE_RELEASE(m_pReductionSurf[i]);
1513:         SAFE_RELEASE(m_pReductionTex[i]);
1514:     }
1515:     SAFE_RELEASE(m_pMaskTex);
1516:     SAFE_RELEASE(m_pParaboloidSurf[1]);
1517:     SAFE_RELEASE(m_pParaboloidTex[1]);
1518:     SAFE_RELEASE(m_pParaboloidSurf[0]);
1519:     SAFE_RELEASE(m_pParaboloidTex[0]);
1520:     SAFE_RELEASE(m_pNormalLockSurf);
1521:     SAFE_RELEASE(m_pNormalLockTex);
1522:     SAFE_RELEASE(m_pNormalSurf);
1523:     SAFE_RELEASE(m_pNormalTex);
1524:     SAFE_RELEASE(m_pPosLockSurf);
1525:     SAFE_RELEASE(m_pPosLockTex);
1526:     SAFE_RELEASE(m_pPosSurf);
1527:     SAFE_RELEASE(m_pPosTex);
1528:     SAFE_RELEASE(m_pMapZ);
1529: 
1530:     // Models(モデル)
1531:     m_pMeshEnv->InvalidateDeviceObjects();
1532:     m_pMeshBg->InvalidateDeviceObjects();
1533:     m_pMesh->InvalidateDeviceObjects();
1534:     
1535:     // Shaders(シェーダ)
1536:     if( m_pEffect    != NULL ) m_pEffect   ->OnLostDevice();
1537: 
1538:     return S_OK;
1539: }
1540: 
1541: 
1542: 
1543: 
1544: //-------------------------------------------------------------
1545: // Name: DeleteDeviceObjects()
1546: // Desc: Paired with InitDeviceObjects()
1547: //       Called when the app is exiting, or the device is being changed,
1548: //       this function deletes any device dependent objects.  
1549: //       (InitDeviceObjects() で作成したオブジェクトを開放する)
1550: //-------------------------------------------------------------
1551: HRESULT CMyD3DApplication::DeleteDeviceObjects()
1552: {
1553:     // font(フォント)
1554:     m_pFont->DeleteDeviceObjects();
1555: 
1556:     // textures (テクスチャ)
1557:     for( int i = 0; i < TEX_MAX; i++ )
1558:     {
1559:         SAFE_RELEASE(m_pJacobianTex[i][0]);
1560:         SAFE_RELEASE(m_pJacobianTex[i][1]);
1561:     }
1562: 
1563:     // Shaders(シェーダ)
1564:     SAFE_RELEASE( m_pEffect );
1565:     
1566:     // Models(モデル)
1567:     m_pMeshEnv->Destroy();
1568:     m_pMeshBg->Destroy();
1569:     m_pMesh->Destroy();
1570: 
1571: #ifdef NV3X
1572:     SAFE_RELEASE(m_pPrtTex);
1573: #endif // NV3X
1574: 
1575:     return S_OK;
1576: }
1577: 
1578: 
1579: 
1580: 
1581: //-------------------------------------------------------------
1582: // Name: FinalCleanup()
1583: // Desc: Paired with OneTimeSceneInit()
1584: //       Called before the app exits, this function gives the app the chance
1585: //       to cleanup after itself.
1586: //       (終了する直前に呼ばれる)
1587: //-------------------------------------------------------------
1588: HRESULT CMyD3DApplication::FinalCleanup()
1589: {
1590:     // font(フォント)
1591:     SAFE_DELETE( m_pFont );
1592: 
1593:     // Models(モデル)
1594:     SAFE_DELETE( m_pMeshEnv );
1595:     SAFE_DELETE( m_pMeshBg );
1596:     SAFE_DELETE( m_pMesh );
1597: 
1598:     return S_OK;
1599: }
1600: 
1601: 
1602: 
1603: 
1604: