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_fDiffuse     = 1.0f;
0090:     m_fSpecular    = 0.2f;
0091:     m_fTranslucent = 3.0f;
0092: 
0093:     m_pos = D3DXVECTOR3(0,0,0);
0094:     m_vel = D3DXVECTOR3(frand(),frand(),frand());
0095:     m_rot = D3DXVECTOR3(0,0,0);
0096:     m_omega = D3DXVECTOR3( 0, 1.0, 0 );
0097: //  m_omega = D3DXVECTOR3( frand(), frand(), frand() );
0098:     
0099:     m_EnvRate = 0;
0100:     for(i=0;i<4;i++)
0101:     {
0102:         m_EnvRot[i] = D3DXVECTOR3( 2.0*D3DX_PI*frand(), 2.0*D3DX_PI*frand(), 2.0*D3DX_PI*frand() );
0103:     }
0104:     D3DXMATRIX mx,my,mz;
0105:     D3DXMatrixRotationX( &mx, m_EnvRot[1].x );
0106:     D3DXMatrixRotationY( &my, m_EnvRot[1].y );
0107:     D3DXMatrixRotationZ( &mz, m_EnvRot[1].z );
0108:     m_mEnv = mx*my*mz;
0109: 
0110:     m_pMesh                     = new CD3DMesh();
0111:     m_pMeshBg                   = new CD3DMesh();
0112:     m_pMeshEnv                  = new CD3DMesh();
0113: 
0114:     m_pMapZ                     = NULL;
0115:     m_pPosTex                   = NULL;
0116:     m_pPosSurf                  = NULL;
0117:     m_pPosLockTex               = NULL;
0118:     m_pPosLockSurf              = NULL;
0119:     m_pNormalTex                = NULL;
0120:     m_pNormalSurf               = NULL;
0121:     m_pNormalLockTex            = NULL;
0122:     m_pNormalLockSurf           = NULL;
0123:     m_pParaboloidTex[0]         = NULL;
0124:     m_pParaboloidSurf[0]        = NULL;
0125:     m_pParaboloidTex[1]         = NULL;
0126:     m_pParaboloidSurf[1]        = NULL;
0127:     for(i=0;i<TEX_MAX;i++){
0128:         m_pJacobianTex[i][0]    = NULL;
0129:         m_pJacobianTex[i][1]    = NULL;
0130:     }
0131: 
0132:     for( i = 0; i < REDUCTION_MAPS; i++ )
0133:     {
0134:         m_pReductionTex[i]      = NULL;
0135:         m_pReductionSurf[i]     = NULL;
0136:     }
0137:     for( i=0;i<TEX_MAX;i++){
0138:         m_pFinalTex[i]          = NULL;
0139:         m_pFinalSurf[i]         = NULL;
0140:         m_pFinalSSTex[i]        = NULL;
0141:         m_pFinalSSSurf[i]       = NULL;
0142:     }
0143:     m_p64Tex                    = NULL;
0144:     m_p64Surf                   = NULL;
0145:     m_p64Tex2                   = NULL;
0146:     m_p64Surf2                  = NULL;
0147:     m_p64Z                      = NULL;
0148:     m_p8Tex                     = NULL;
0149:     m_p8Surf                    = NULL;
0150:     m_pMaskTex                  = NULL;
0151: 
0152:     m_pEffect                   = NULL;
0153:     m_hTechnique                = NULL;
0154:     m_hmWV                      = NULL;
0155:     m_hmWVP                     = NULL;
0156:     m_htSrcTex                  = NULL;
0157:     m_fWidth                    = NULL;
0158:     m_fHeight                   = NULL;
0159: 
0160:     m_fWorldRotX                = -0.28f;
0161:     m_fWorldRotY                = D3DX_PI*0.5f;
0162:     m_fViewZoom                 = 7.0f;
0163: 
0164:     m_dwCreationWidth           = 512;
0165:     m_dwCreationHeight          = 512;
0166:     m_strWindowTitle            = TEXT( "main" );
0167:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0168:     m_bStartFullscreen          = false;
0169:     m_bShowCursorWhenFullscreen = false;
0170: 
0171:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0172:     m_bLoadingApp               = TRUE;
0173: 
0174:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0175: }
0176: 
0177: 
0178: 
0179: 
0180: //-------------------------------------------------------------
0181: // Name: ~CMyD3DApplication()
0182: // Desc: Application destructor.  Paired with CMyD3DApplication()
0183: //       (デストラクタ)
0184: //-------------------------------------------------------------
0185: CMyD3DApplication::~CMyD3DApplication()
0186: {
0187: }
0188: 
0189: 
0190: 
0191: 
0192: //-------------------------------------------------------------
0193: // Name: OneTimeSceneInit()
0194: // Desc: Paired with FinalCleanup().
0195: //       The window has been created and the IDirect3D9 interface has been
0196: //       created, but the device has not been created yet.  Here you can
0197: //       perform application-related initialization and cleanup that does
0198: //       not depend on a device.
0199: //      (一度だけ行う初期化
0200: //      ウィンドウの初期化やIDirect3D9の初期化は終わってます。
0201: //      ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。)
0202: //-------------------------------------------------------------
0203: HRESULT CMyD3DApplication::OneTimeSceneInit()
0204: {
0205:     // Drawing loading status message until app finishes loading
0206:     // (ローディングメッセージを表示する)
0207:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0208: 
0209:     m_bLoadingApp = FALSE;
0210: 
0211:     return S_OK;
0212: }
0213: 
0214: 
0215: 
0216: 
0217: //-------------------------------------------------------------
0218: // Name: ConfirmDevice()
0219: // Desc: Called during device initialization, this code checks the device
0220: //       for some minimum set of capabilities
0221: //       (初期化の時に呼ばれます。必要な能力をチェックします。)
0222: //-------------------------------------------------------------
0223: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0224:                      DWORD dwBehavior,    D3DFORMAT Format )
0225: {
0226:     UNREFERENCED_PARAMETER( Format );
0227:     UNREFERENCED_PARAMETER( dwBehavior );
0228:     UNREFERENCED_PARAMETER( pCaps );
0229:     
0230: 
0231:     // No fallback, so need ps2.0
0232:     // (ピクセルシェーダバージョンチェック)
0233:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0234:         return E_FAIL;
0235: 
0236:     // If device doesn't support 1.1 vertex shaders in HW, switch to SWVP.
0237:     // 頂点シェーダバージョンが上位かソフトウェア頂点処理
0238:     if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0239:     &&  0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0240:             return E_FAIL;
0241: 
0242:     return S_OK;
0243: }
0244: 
0245: 
0246: //-------------------------------------------------------------
0247: // SH coefficient of imcomming light
0248: //-------------------------------------------------------------
0249: // Variables send from application
0250: typedef struct {
0251:     int iRank;      // SH dimension
0252:     bool bFront;    // Front or back
0253: }sParaboloid;
0254: 
0255: VOID WINAPI ParaboloidJacobian (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord, 
0256: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0257: {
0258:     UNREFERENCED_PARAMETER( pTexelSize );
0259: 
0260:     sParaboloid *p = (sParaboloid *)pData;
0261: 
0262:     FLOAT x = 2.0f*( pTexCoord->x-0.5f);
0263:     FLOAT y = 2.0f*(-pTexCoord->y+0.5f);
0264:     
0265:     FLOAT r2 = x*x+y*y;
0266:     FLOAT J = 0.5f*(1.0f+r2);
0267: 
0268:     FLOAT col = (r2<(1.0f-1.0f/(FLOAT)MAP_SIZE)*(1.0f-1.0f/(FLOAT)MAP_SIZE)) ? J : 0.0f;
0269:     
0270:     // Normal vector
0271:     FLOAT nz = 0.5f * (1.0f-r2);
0272:     FLOAT nx = x;
0273:     FLOAT ny = y;
0274:     FLOAT n = sqrtf(nx*nx+ny*ny+nz*nz);
0275:     nx /= n;
0276:     ny /= n;
0277:     nz /= n;
0278:     if(p->bFront) nz *= -1;
0279: 
0280:     pOut->x = 0.5f * col * (-nx) + 0.5f;
0281:     pOut->y = 0.5f * col * ( ny) + 0.5f;
0282:     pOut->z = 0.5f * col * (-nz) + 0.5f;
0283:     pOut->w = 0.5f * col         + 0.5f;
0284: }
0285: 
0286: //-------------------------------------------------------------
0287: // 真ん中が白い絵を作る
0288: //-------------------------------------------------------------
0289: VOID WINAPI FillTex (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord, 
0290: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0291: {
0292:     UNREFERENCED_PARAMETER( pData );
0293: 
0294:     FLOAT x = 2.0f*(pTexCoord->x-0.5f);
0295:     FLOAT y = 2.0f*(pTexCoord->y-0.5f);
0296:     FLOAT col = (x*x+y*y<(1.0f-pTexelSize->x)*(1.0f-pTexelSize->y))
0297:                 ? 1.0f : 0.0f;
0298:     
0299:     pOut->x = pOut->y = pOut->z = pOut->w = col;
0300: }
0301: 
0302: //-------------------------------------------------------------
0303: // Name: InitDeviceObjects()
0304: // Desc: Paired with DeleteDeviceObjects()
0305: //       The device has been created.  Resources that are not lost on
0306: //       Reset() can be created here -- resources in D3DPOOL_MANAGED,
0307: //       D3DPOOL_SCRATCH, or D3DPOOL_SYSTEMMEM.  Image surfaces created via
0308: //       CreateImageSurface are never lost and can be created here.  Vertex
0309: //       shaders and pixel shaders can also be created here as they are not
0310: //       lost on Reset().
0311: //      (デバイスが生成された後の初期化をします。
0312: //      フレームバッファフォーマットやデバイスの種類が変わった後に通過します。
0313: //      ここで確保したメモリはDeleteDeviceObjects()で開放します)
0314: //-------------------------------------------------------------
0315: HRESULT CMyD3DApplication::InitDeviceObjects()
0316: {
0317:     HRESULT hr;
0318:     int i;
0319: 
0320:     // Load objects (モデルの読み込み)
0321: #ifdef DX9C
0322:     if(FAILED(hr=m_pMesh->Create( m_pd3dDevice, (LPCWSTR)("t-pot.x"))))
0323:         return DXTRACE_ERR( "Load Model", hr );
0324:         
0325:     if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, (LPCWSTR)("map.x"))))
0326:         return DXTRACE_ERR( "Load Model", hr );
0327:         
0328:     if(FAILED(hr=m_pMeshEnv->Create( m_pd3dDevice, (LPCWSTR)("room.x"))))
0329:         return DXTRACE_ERR( "Load Model", hr );
0330: #else
0331:     if(FAILED(hr=m_pMesh->Create( m_pd3dDevice, _T("t-pot.x"))))
0332:         return DXTRACE_ERR( "Load Model", hr );
0333:         
0334:     if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, _T("map.x"))))
0335:         return DXTRACE_ERR( "Load Model", hr );
0336:         
0337:     if(FAILED(hr=m_pMeshEnv->Create( m_pd3dDevice, _T("room.x"))))
0338:         return DXTRACE_ERR( "Load Model", hr );
0339: #endif
0340:     m_pMesh->UseMeshMaterials(FALSE);// Not set textures when rendering(レンダリング時にテクスチャの設定をしない)
0341:     m_pMeshBg->UseMeshMaterials(FALSE);// Not set textures when rendering(レンダリング時にテクスチャの設定をしない)
0342:     m_pMeshEnv->UseMeshMaterials(FALSE);// Not set textures when rendering(レンダリング時にテクスチャの設定をしない)
0343:         
0344:     // Create textures
0345:     sParaboloid data;
0346:     for( i = 0; i < TEX_MAX; i++ )
0347:     {
0348:         data.iRank = i;
0349:         // SH coefficient of incoming light (重み付けのテクスチャ)
0350:         data.bFront = 0;
0351:         if( FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1
0352:                             , 0, D3DFMT_A8R8G8B8
0353:                             , D3DPOOL_MANAGED, &m_pJacobianTex[i][0], NULL)))
0354:             return E_FAIL;
0355:         if( FAILED(D3DXFillTexture(m_pJacobianTex[i][0], ParaboloidJacobian, &data)))
0356:             return E_FAIL;
0357: 
0358:         data.bFront = 1;
0359:         if( FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1
0360:                             , 0, D3DFMT_A8R8G8B8
0361:                             , D3DPOOL_MANAGED, &m_pJacobianTex[i][1], NULL)))
0362:             return E_FAIL;
0363:         if( FAILED(D3DXFillTexture(m_pJacobianTex[i][1], ParaboloidJacobian, &data)))
0364:             return E_FAIL;
0365:     }
0366: 
0367:     // Create the effect(シェーダの読み込み)
0368:     LPD3DXBUFFER pErr;
0369:     if( FAILED( hr = D3DXCreateEffectFromFile(
0370:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0371:                 D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0372:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0373:                     , "ERROR", MB_OK);
0374:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0375:     }
0376:     m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0377:     m_hmWV       = m_pEffect->GetParameterByName( NULL, "mWV" );
0378:     m_hmWVP      = m_pEffect->GetParameterByName( NULL, "mWVP" );
0379:     m_htSrcTex   = m_pEffect->GetParameterByName( NULL, "SrcTex" );
0380:     m_fWidth     = m_pEffect->GetParameterByName( NULL, "MAP_WIDTH" );
0381:     m_fHeight    = m_pEffect->GetParameterByName( NULL, "MAP_HEIGHT" );
0382: 
0383:     // Init the font(フォント)
0384:     m_pFont->InitDeviceObjects( m_pd3dDevice );
0385: 
0386:     return S_OK;
0387: }
0388: 
0389: //-------------------------------------------------------------
0390: // Name: RestoreDeviceObjects()
0391: // Desc: Paired with InvalidateDeviceObjects()
0392: //       The device exists, but may have just been Reset().  Resources in
0393: //       D3DPOOL_DEFAULT and any other device state that persists during
0394: //       rendering should be set here.  Render states, matrices, textures,
0395: //       etc., that don't change during rendering can be set once here to
0396: //       avoid redundant state setting during Render() or FrameMove().
0397: //       (画面のサイズが変更された時等に呼ばれます。
0398: //       確保したメモリはInvalidateDeviceObjects()で開放します。)
0399: //-------------------------------------------------------------
0400: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0401: {
0402:     DWORD i;
0403: 
0404:     m_iState = -1;
0405:     m_iChangeState = TRUE;
0406:     m_bPause = false;
0407: 
0408:     // Restore meshes(メッシュ)
0409:     m_pMesh->RestoreDeviceObjects( m_pd3dDevice );
0410:     m_pMeshBg->RestoreDeviceObjects( m_pd3dDevice );
0411:     m_pMeshEnv->RestoreDeviceObjects( m_pd3dDevice );
0412: 
0413:     // Setup a material (質感の設定)
0414:     D3DMATERIAL9 mtrl;
0415:     D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0416:     m_pd3dDevice->SetMaterial( &mtrl );
0417: 
0418:     // Set miscellaneous render states(レンダリング環境の設定)
0419:     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   FALSE );
0420:     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
0421:     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
0422:     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0x000F0F0F );
0423:     
0424:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0425:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0426:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0427:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
0428:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
0429:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
0430:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0431:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0432:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0433:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0434: 
0435:     // World transform to identity (ワールド行列)
0436:     D3DXMATRIX matIdentity;
0437:     D3DXMatrixIdentity( &m_mWorld );
0438: 
0439:     // Set up the view parameters for the camera(ビュー行列)
0440:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0441:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0442:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0443:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0444: 
0445:     // Set the camera projection matrix(射影行列)
0446:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0447:     D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0448: 
0449:     // Create the stencil buffer to be used with the paraboloid textures(深度マップの生成)
0450:     if (FAILED(m_pd3dDevice->CreateDepthStencilSurface(MAP_SIZE, MAP_SIZE, 
0451:         D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pMapZ, NULL)))
0452:         return E_FAIL;
0453:     // Create height map(位置マップ)
0454:     if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0455:         D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pPosTex, NULL)))
0456:         return E_FAIL;
0457:     if (FAILED(m_pPosTex->GetSurfaceLevel(0, &m_pPosSurf)))
0458:         return E_FAIL;
0459:     if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0460:         0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pPosLockTex, NULL)))
0461:         return E_FAIL;
0462:     if (FAILED(m_pPosLockTex->GetSurfaceLevel(0, &m_pPosLockSurf)))
0463:         return E_FAIL;
0464:     // Create normal map(法線マップ)
0465:     if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0466:         D3DUSAGE_RENDERTARGET, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &m_pNormalTex, NULL)))
0467:         return E_FAIL;
0468:     if (FAILED(m_pNormalTex->GetSurfaceLevel(0, &m_pNormalSurf)))
0469:         return E_FAIL;
0470:     if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0471:         0, D3DFMT_A32B32G32R32F, D3DPOOL_SYSTEMMEM , &m_pNormalLockTex, NULL)))
0472:         return E_FAIL;
0473:     if (FAILED(m_pNormalLockTex->GetSurfaceLevel(0, &m_pNormalLockSurf)))
0474:         return E_FAIL;
0475:     // Create the paraboloid textures(放物マップ)
0476:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0477:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pParaboloidTex[0], NULL)))
0478:         return E_FAIL;
0479:     if (FAILED(m_pParaboloidTex[0]->GetSurfaceLevel(0, &m_pParaboloidSurf[0])))
0480:         return E_FAIL;
0481:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1, 
0482:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pParaboloidTex[1], NULL)))
0483:         return E_FAIL;
0484:     if (FAILED(m_pParaboloidTex[1]->GetSurfaceLevel(0, &m_pParaboloidSurf[1])))
0485:         return E_FAIL;
0486: 
0487:     // マスク用のテクスチャの生成
0488:     if( FAILED(m_pd3dDevice->CreateTexture( 64, 64, 1
0489:                           , 0, D3DFMT_A8R8G8B8
0490:                           , D3DPOOL_DEFAULT, &m_pMaskTex, NULL)))
0491:         return E_FAIL;
0492:     if( FAILED(D3DXFillTexture(m_pMaskTex, FillTex, NULL)))
0493:         return E_FAIL;
0494: 
0495:     // Create the reduction textures(縮小バッファ)
0496:     int size = 128;
0497:     for( i = 0; i < REDUCTION_MAPS; i++ )
0498:     {
0499:         if (FAILED(m_pd3dDevice->CreateTexture(size, size, 1, 
0500:             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pReductionTex[i], NULL)))
0501:             return E_FAIL;
0502:         if (FAILED(m_pReductionTex[i]->GetSurfaceLevel(0, &m_pReductionSurf[i])))
0503:             return E_FAIL;
0504:         size /= 8;
0505:     }
0506:     if (FAILED(m_pd3dDevice->CreateTexture( 64, 64, 1, 
0507:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_p64Tex, NULL)))
0508:         return E_FAIL;
0509:     if (FAILED(m_p64Tex->GetSurfaceLevel(0, &m_p64Surf)))
0510:         return E_FAIL;
0511:     if (FAILED(m_pd3dDevice->CreateTexture( 64, 64, 1, 
0512:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_p64Tex2, NULL)))
0513:         return E_FAIL;
0514:     if (FAILED(m_p64Tex2->GetSurfaceLevel(0, &m_p64Surf2)))
0515:         return E_FAIL;
0516:     if (FAILED(m_pd3dDevice->CreateDepthStencilSurface( 64, 64, 
0517:         D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_p64Z, NULL)))
0518:         return E_FAIL;
0519: 
0520:     if (FAILED(m_pd3dDevice->CreateTexture( 8, 8, 1, 
0521:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_p8Tex, NULL)))
0522:         return E_FAIL;
0523:     if (FAILED(m_p8Tex->GetSurfaceLevel(0, &m_p8Surf)))
0524:         return E_FAIL;
0525: 
0526:     // 拡散マップ
0527:     for(i=0;i<TEX_MAX;i++){
0528:         if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0529:             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pFinalTex[i], NULL)))
0530:             return E_FAIL;
0531:         if (FAILED(m_pFinalTex[i]->GetSurfaceLevel(0, &m_pFinalSurf[i])))
0532:             return E_FAIL;
0533:         if (FAILED(m_pd3dDevice->CreateTexture(DECALE_MAP_SIZE, DECALE_MAP_SIZE, 1, 
0534:             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pFinalSSTex[i], NULL)))
0535:             return E_FAIL;
0536:         if (FAILED(m_pFinalSSTex[i]->GetSurfaceLevel(0, &m_pFinalSSSurf[i])))
0537:             return E_FAIL;
0538:     }
0539: 
0540:     // Restore effect object
0541:     m_pEffect->OnResetDevice();
0542: 
0543:     // Restore the font(フォント)
0544:     m_pFont->RestoreDeviceObjects();
0545: 
0546:     return S_OK;
0547: }
0548: 
0549: void CatmullRom( D3DXVECTOR3 *v, const D3DXVECTOR3 *v0, const D3DXVECTOR3 *v1, const D3DXVECTOR3 *v2, const D3DXVECTOR3 *v3,FLOAT rate)
0550: {
0551:     const D3DXMATRIX mCatmullRom = D3DXMATRIX(
0552:         -1.0/2.0, 3.0/2.0,-3.0/2.0, 1.0/2.0,
0553:          2.0/2.0,-5.0/2.0, 4.0/2.0,-1.0/2.0,
0554:         -1.0/2.0, 0.0/2.0, 1.0/2.0, 0.0/2.0,
0555:          0.0/2.0, 2.0/2.0, 0.0/2.0, 0.0/2.0);
0556: 
0557:     D3DXVECTOR4 vr = D3DXVECTOR4(rate*rate*rate,rate*rate,rate,1);
0558:     D3DXVECTOR4 v4;
0559: 
0560:     D3DXVec4Transform(&v4, &vr, &mCatmullRom);
0561: 
0562:     *v = v4.x * (*v0) + v4.y * (*v1) + v4.z * (*v2) + v4.w * (*v3);
0563: }
0564: 
0565: //-------------------------------------------------------------
0566: // Name: FrameMove()
0567: // Desc: Called once per frame, the call is the entry point for animating
0568: //       the scene.
0569: //       (毎フレーム呼ばれます。アニメの処理などを行います。)
0570: //-------------------------------------------------------------
0571: HRESULT CMyD3DApplication::FrameMove()
0572: {
0573:     m_iCount++;
0574:     
0575:     if(m_iChangeState) {m_iState++;m_iCount=0;m_iChangeState=0;}
0576: 
0577:     switch(m_iState){
0578:     case 0:
0579:         m_iChangeState = this->FrameMoveCreateMap();
0580:         return S_OK;
0581:     case 1:
0582:         m_iChangeState = this->FrameMoveSSPRT();
0583:         return S_OK;
0584:     case 2:
0585:         m_iChangeState = this->FrameMovePRT();
0586:         return S_OK;
0587:     default:
0588:         break;
0589:     }
0590: 
0591:     // Update user input state(入力データの更新)
0592:     UpdateInput( &m_UserInput );
0593: 
0594:     //---------------------------------------------------------
0595:     // Update the world state according to user input(入力に応じて座標系を更新する)
0596:     //---------------------------------------------------------
0597:     // Rotation(回転)
0598:     D3DXMATRIX matRotY;
0599:     D3DXMATRIX matRotX;
0600: 
0601:     if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0602:         m_fWorldRotY += m_fElapsedTime;
0603:     else
0604:     if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0605:         m_fWorldRotY -= m_fElapsedTime;
0606: 
0607:     if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0608:         m_fWorldRotX += m_fElapsedTime;
0609:     else
0610:     if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0611:         m_fWorldRotX -= m_fElapsedTime;
0612: 
0613:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0614:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0615: 
0616:     D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0617:     
0618:     //---------------------------------------------------------
0619:     // Update view matrix(ビュー行列の設定)
0620:     //---------------------------------------------------------
0621:     // Zoom(ズーム)
0622:     if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0623:         m_fViewZoom += m_fElapsedTime;
0624:     else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0625:         m_fViewZoom -= m_fElapsedTime;
0626: 
0627:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0628:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.3f, 0.0f );
0629:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0630:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0631: 
0632:     //---------------------------------------------------------
0633:     // Change the shader(シェーダの変更)
0634:     //---------------------------------------------------------
0635:     if( m_UserInput.bChangeShader ) m_Shader = (m_Shader + 1) % 3;
0636: 
0637:     //---------------------------------------------------------
0638:     // Pause(ポーズ)
0639:     //---------------------------------------------------------
0640:     if( m_UserInput.bPause ) m_bPause = !m_bPause;
0641: 
0642:     //---------------------------------------------------------
0643:     // Move the ball (玉の移動)
0644:     //---------------------------------------------------------
0645:     if( !m_bPause )
0646:     {
0647:         D3DXVec3Normalize( &m_vel, &m_vel );
0648:         D3DXVec3Scale( &m_vel, &m_vel, 10 );
0649: //      m_pos += m_vel * this->m_fElapsedTime;
0650:         m_rot.y += 1.0 * this->m_fElapsedTime;
0651:         m_rot.y = (float)fmod(m_rot.y, 2.0f*D3DX_PI);
0652: 
0653:         float size = 5.0f - 1.0f;
0654: 
0655:         if( m_pos.x < -size )
0656:         {
0657:             m_pos.x = -size;
0658:             m_vel.x *= -1;
0659:         }
0660:         if( size < m_pos.x )
0661:         {
0662:             m_pos.x = size;
0663:             m_vel.x *= -1;
0664:         }
0665:         if( m_pos.y < -size )
0666:         {
0667:             m_pos.y = -size;
0668:             m_vel.y *= -1;
0669:         }
0670:         if( size < m_pos.y )
0671:         {
0672:             m_pos.y = size;
0673:             m_vel.y *= -1;
0674:         }
0675:         if( m_pos.z < -size )
0676:         {
0677:             m_pos.z = -size;
0678:             m_vel.z *= -1;
0679:         }
0680:         if( size < m_pos.z )
0681:         {
0682:             m_pos.z = size;
0683:             m_vel.z *= -1;
0684:         }
0685:         // rotation of environment
0686:         m_EnvRate += 0.1f * this->m_fElapsedTime;
0687:         while(1.0f<m_EnvRate)
0688:         {
0689:             m_EnvRate -= 1.0f;
0690:             for(DWORD i=0;i<3;i++)
0691:             {
0692:                 m_EnvRot[i] = m_EnvRot[i+1];
0693:             }
0694:             m_EnvRot[3] = D3DXVECTOR3( 2.0*D3DX_PI*frand(),
0695:                                         2.0*D3DX_PI*frand(),
0696:                                         2.0*D3DX_PI*frand() );
0697:         }
0698: 
0699:         D3DXVECTOR3 v;
0700:         CatmullRom( &v, m_EnvRot+0, m_EnvRot+1, m_EnvRot+2, m_EnvRot+3, m_EnvRate);
0701:         D3DXMATRIX mx,my,mz;
0702:         D3DXMatrixRotationX( &mx, v.x );
0703:         D3DXMatrixRotationY( &my, v.y );
0704:         D3DXMatrixRotationZ( &mz, v.z );
0705:         m_mEnv = mx*my*mz;
0706:     }
0707: 
0708:     //---------------------------------------------------------
0709:     // Reflectivity
0710:     //---------------------------------------------------------
0711:     if( m_UserInput.bDiffuseUp && !m_UserInput.bDiffuseDown )
0712:         m_fDiffuse += min(0.1f,m_fElapsedTime);
0713:     else if( m_UserInput.bDiffuseDown && !m_UserInput.bDiffuseUp )
0714:         m_fDiffuse -= min(0.1f,m_fElapsedTime);
0715:     if(m_fDiffuse<0)m_fDiffuse=0;
0716: 
0717:     if( m_UserInput.bSpecularUp && !m_UserInput.bSpecularDown )
0718:         m_fSpecular += min(0.1f,m_fElapsedTime);
0719:     else if( m_UserInput.bSpecularDown && !m_UserInput.bSpecularUp )
0720:         m_fSpecular -= min(0.1f,m_fElapsedTime);
0721:     if(m_fSpecular<0)m_fSpecular=0;
0722: 
0723:     if( m_UserInput.bTranslucentUp && !m_UserInput.bTranslucentDown )
0724:         m_fTranslucent += min(0.1f,m_fElapsedTime);
0725:     else if( m_UserInput.bTranslucentDown && !m_UserInput.bTranslucentUp )
0726:         m_fTranslucent -= min(0.1f,m_fElapsedTime);
0727:     if(m_fTranslucent<0)m_fTranslucent=0;
0728: 
0729:     return S_OK;
0730: }
0731: //-------------------------------------------------------------
0732: // Name: UpdateInput()
0733: // Desc: Update the user input.  Called once per frame 
0734: //       (入力データを更新する)
0735: //-------------------------------------------------------------
0736: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0737: {
0738:     pUserInput->bPause           = ( m_bActive && (GetAsyncKeyState( 'P'      ) & 0x8001) == 0x8001 );
0739:     pUserInput->bChangeShader    = ( m_bActive && (GetAsyncKeyState( 'Q'      ) & 0x8001) == 0x8001 );
0740: 
0741:     pUserInput->bRotateUp        = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0742:     pUserInput->bRotateDown      = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0743:     pUserInput->bRotateLeft      = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0744:     pUserInput->bRotateRight     = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0745:     
0746:     pUserInput->bZoomIn          = ( m_bActive && (GetAsyncKeyState( 'Z'     )  & 0x8000) == 0x8000 );
0747:     pUserInput->bZoomOut         = ( m_bActive && (GetAsyncKeyState( 'X'      ) & 0x8000) == 0x8000 );
0748:     
0749:     pUserInput->bDiffuseUp       = ( m_bActive && (GetAsyncKeyState( 'D'     )  & 0x8000) == 0x8000 );
0750:     pUserInput->bDiffuseDown     = ( m_bActive && (GetAsyncKeyState( 'C'      ) & 0x8000) == 0x8000 );
0751:     pUserInput->bSpecularUp      = ( m_bActive && (GetAsyncKeyState( 'F'     )  & 0x8000) == 0x8000 );
0752:     pUserInput->bSpecularDown    = ( m_bActive && (GetAsyncKeyState( 'V'      ) & 0x8000) == 0x8000 );
0753:     pUserInput->bTranslucentUp   = ( m_bActive && (GetAsyncKeyState( 'G'     )  & 0x8000) == 0x8000 );
0754:     pUserInput->bTranslucentDown = ( m_bActive && (GetAsyncKeyState( 'B'      ) & 0x8000) == 0x8000 );
0755: }
0756: 
0757: 
0758: 
0759: //-------------------------------------------------------------
0760: // Name: FrameMoveCreateMap()
0761: // Desc: Create height map and normal map
0762: //       (位置、法線マップを作成する。)
0763: //-------------------------------------------------------------
0764: int CMyD3DApplication::FrameMoveCreateMap()
0765: {
0766:     // 2回目の以降は次の処理に移る
0767:     if(1<m_iCount) return 1;
0768: 
0769:     return 0;
0770: }
0771: 
0772: //-------------------------------------------------------------
0773: // Name: RenderCreateMap()
0774: // Desc: Create height map and normal map
0775: //       (位置、法線マップを作成する。)
0776: //-------------------------------------------------------------
0777: void CMyD3DApplication::RenderCreateMap()
0778: {
0779:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0780:     D3DVIEWPORT9 oldViewport;
0781: 
0782:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0783:     {
0784:         //-------------------------------------------------
0785:         // レンダリングターゲットの保存
0786:         //-------------------------------------------------
0787:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0788:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0789:         m_pd3dDevice->GetViewport(&oldViewport);
0790: 
0791:         //-------------------------------------------------
0792:         // レンダリングターゲットの変更
0793:         //-------------------------------------------------
0794:         m_pd3dDevice->SetRenderTarget(0, m_pPosSurf);
0795:         m_pd3dDevice->SetRenderTarget(1, m_pNormalSurf);
0796:         m_pd3dDevice->SetDepthStencilSurface(NULL);
0797:         // ビューポートの変更
0798:         D3DVIEWPORT9 viewport_height = {0,0      // 左上の座標
0799:                         , DECALE_MAP_SIZE  // 幅
0800:                         , DECALE_MAP_SIZE // 高さ
0801:                         , 0.0f,1.0f};     // 前面、後面
0802:         m_pd3dDevice->SetViewport(&viewport_height);
0803: 
0804:         //-------------------------------------------------
0805:         // フレームバッファのクリア
0806:         //-------------------------------------------------
0807:         m_pd3dDevice->Clear(0L, NULL
0808:                         , D3DCLEAR_TARGET
0809:                         , 0xffffffff, 1.0f, 0L);
0810: 
0811:         if( m_pEffect != NULL ) 
0812:         {
0813:             //-------------------------------------------------
0814:             // シェーダの設定
0815:             //-------------------------------------------------
0816:             m_pEffect->SetTechnique( m_hTechnique );
0817:             m_pEffect->Begin( NULL, 0 );
0818: 
0819:             m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
0820:             m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
0821: 
0822:             //-------------------------------------------------
0823:             // モデルの描画
0824:             //-------------------------------------------------
0825:             BEGIN_PASS(10);
0826:             m_pMesh->Render(m_pd3dDevice);
0827:             END_PASS();
0828: 
0829:             m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
0830:             m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
0831: 
0832:             m_pEffect->End();
0833:         }
0834: 
0835:         //-----------------------------------------------------
0836:         // レンダリングターゲットを元に戻す
0837:         //-----------------------------------------------------
0838:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0839:         m_pd3dDevice->SetRenderTarget(1, NULL);
0840:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0841:         m_pd3dDevice->SetViewport(&oldViewport);
0842:         pOldBackBuffer->Release();
0843:         pOldZBuffer->Release();
0844: 
0845:         //-----------------------------------------------------
0846:         // ごまかしの画面
0847:         //-----------------------------------------------------
0848:         m_pd3dDevice->Clear(0L, NULL
0849:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0850:                         , 0xff000000, 1.0f, 0L);
0851: 
0852:         m_pd3dDevice->EndScene();
0853:     }
0854: }
0855: 
0856: 
0857: //-------------------------------------------------------------
0858: // Name: FrameMoveSSPRT()
0859: // Desc: 位置、法線マップを作成する。
0860: //-------------------------------------------------------------
0861: static int itex=0;
0862: static int ix=0;
0863: static int iy=0;
0864: static float pos[DECALE_MAP_SIZE][DECALE_MAP_SIZE][3];
0865: static float normal[DECALE_MAP_SIZE][DECALE_MAP_SIZE][3];
0866: int CMyD3DApplication::FrameMoveSSPRT()
0867: {
0868:     itex = this->m_iCount / (DECALE_MAP_SIZE*DECALE_MAP_SIZE);
0869:     ix = this->m_iCount % DECALE_MAP_SIZE;
0870:     iy = (this->m_iCount / DECALE_MAP_SIZE) % DECALE_MAP_SIZE;
0871:     
0872:     if(0==this->m_iCount){
0873:         D3DLOCKED_RECT d3dlr;
0874:         float *p;
0875:         m_pd3dDevice->GetRenderTargetData(m_pPosSurf,m_pPosLockSurf);
0876:         m_pPosLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY); //サーフェイス上の矩形をロック    
0877:         p = (float *)d3dlr.pBits;
0878:         for(int y=0;y<DECALE_MAP_SIZE;y++){
0879:         for(int x=0;x<DECALE_MAP_SIZE;x++){
0880:             pos[x][y][0]=p[0];
0881:             pos[x][y][1]=p[1];
0882:             pos[x][y][2]=p[2];
0883:             p+=4;
0884:         }
0885:         }
0886:         m_pPosLockSurf->UnlockRect();
0887: 
0888:         m_pd3dDevice->GetRenderTargetData(m_pNormalSurf,m_pNormalLockSurf);
0889:         m_pNormalLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY); //サーフェイス上の矩形をロック    
0890:         p = (float *)d3dlr.pBits;        
0891:         for( y=0;y<DECALE_MAP_SIZE;y++){
0892:         for( int x=0;x<DECALE_MAP_SIZE;x++){
0893:             normal[x][y][0]=p[0];
0894:             normal[x][y][1]=p[1];
0895:             normal[x][y][2]=p[2];
0896:             p+=4;
0897:         }
0898:         }
0899:         m_pNormalLockSurf->UnlockRect();
0900:     }
0901: 
0902:     // 終了条件
0903:     if(DECALE_MAP_SIZE-1==ix && DECALE_MAP_SIZE-1==iy && TEX_MAX-1==itex) return 1;
0904: 
0905:     return 0;
0906: }
0907: 
0908: 
0909: //-------------------------------------------------------------
0910: // Name: RenderSSPRT()
0911: // Desc: それぞれの位置からのラディアンスを計算する。
0912: //-------------------------------------------------------------
0913: void CMyD3DApplication::RenderSSPRT()
0914: {
0915:     D3DXMATRIX m, mView;
0916:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0917:     D3DVIEWPORT9 oldViewport;
0918:     D3DSURFACE_DESC d3dsd;
0919: 
0920:     D3DVIEWPORT9 viewport = {0,0      // 左上の座標
0921:                     , 1, 1  // 幅,高さ
0922:                     , 0.0f,1.0f};     // 前面、後面
0923: 
0924:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0925:     {
0926:         //-------------------------------------------------
0927:         // レンダリングターゲットの保存
0928:         //-------------------------------------------------
0929:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0930:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0931:         m_pd3dDevice->GetViewport(&oldViewport);
0932: 
0933:         //-------------------------------------------------
0934:         //-------------------------------------------------
0935:         // 各点から見たラディアンスの描画
0936:         //-------------------------------------------------
0937:         //-------------------------------------------------
0938: 
0939:         //-------------------------------------------------
0940:         // レンダリングターゲットの変更
0941:         //-------------------------------------------------
0942:         // ビューポートの変更
0943:         m_pReductionSurf[0]->GetDesc(&d3dsd);
0944:         viewport.Height = d3dsd.Width;
0945:         viewport.Width  = d3dsd.Height;
0946:         m_pd3dDevice->SetViewport(&viewport);
0947: 
0948:         if( m_pEffect != NULL ) 
0949:         {
0950:             //-------------------------------------------------
0951:             // 重みを持つ半球の部分だけ描画
0952:             //-------------------------------------------------
0953:             float x[3] = {pos[ix][iy][0], pos[ix][iy][1], pos[ix][iy][2]};
0954:             float n[3] = {normal[ix][iy][0], normal[ix][iy][1], normal[ix][iy][2]};
0955:             D3DXVECTOR3 vFromPt   = D3DXVECTOR3( x[0], x[1], x[2] ) - 0.05f*D3DXVECTOR3( n[0], n[1], n[2] );
0956:             D3DXVECTOR3 vLookatPt = D3DXVECTOR3( -n[0], -n[1], -n[2] ) + vFromPt;
0957:             D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0958:             D3DXMatrixLookAtLH( &mView, &vFromPt, &vLookatPt, &vUpVec );
0959:             m_pEffect->SetMatrix( "mWV", &mView );
0960: 
0961:             D3DXMatrixTranspose(&m, &mView);
0962:             m_pEffect->SetMatrix( "mST", &m );
0963: 
0964:             // シェーダの設定
0965:             m_pEffect->SetTechnique( m_hTechnique );
0966:             m_pEffect->Begin( NULL, 0 );
0967: 
0968:             //-------------------------------------------------
0969:             // モデルの描画
0970:             //-------------------------------------------------
0971:             m_pd3dDevice->SetRenderTarget(0, m_p64Surf2 );
0972:             m_pd3dDevice->SetDepthStencilSurface( m_p64Z );
0973:             m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L);
0974:             BEGIN_PASS(16);
0975:             m_pMesh->Render(m_pd3dDevice);
0976:             END_PASS();
0977: 
0978:             //-------------------------------------------------
0979:             // SH係数の描画
0980:             //-------------------------------------------------
0981:             TVERTEX Vertex[4] = {
0982:                 // x  y  z tu tv
0983:                 {-1,-1,0,  0, 0,},
0984:                 {+1,-1,0,  1, 0,},
0985:                 {+1,+1,0,  1, 1,},
0986:                 {-1,+1,0,  0, 1,},
0987:             };
0988:             m_pd3dDevice->SetRenderTarget(0, m_p64Surf );
0989:             m_pd3dDevice->SetDepthStencilSurface(NULL);
0990:             m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
0991:             m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
0992: 
0993:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0994:             m_pEffect->SetTexture(m_htSrcTex, m_pMaskTex);
0995:             m_pEffect->SetTexture( "SSTex", m_p64Tex2 );
0996:             BEGIN_PASS( 17 + itex );
0997:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0998:             END_PASS();
0999: 
1000:             m_pEffect->End();
1001:         }
1002:         //-------------------------------------------------
1003:         //-------------------------------------------------
1004:         // ラディアンスをミップマップの要領で小さくする
1005:         //-------------------------------------------------
1006:         //-------------------------------------------------
1007:         m_pd3dDevice->SetRenderTarget( 0, m_p8Surf );
1008:         m_pd3dDevice->SetDepthStencilSurface(NULL);
1009:         // ビューポートの変更
1010:         m_p8Surf->GetDesc(&d3dsd);
1011:         viewport.Height = d3dsd.Width;
1012:         viewport.Width  = d3dsd.Height;
1013:         m_pd3dDevice->SetViewport(&viewport);
1014: 
1015:         if( m_pEffect != NULL ) {
1016:             //-------------------------------------------------
1017:             // シェーダの設定
1018:             //-------------------------------------------------
1019:             m_pEffect->SetTechnique( m_hTechnique );
1020:             m_pEffect->Begin( NULL, 0 );
1021: 
1022:             m_pEffect->SetFloat( m_fWidth,  64);
1023:             m_pEffect->SetFloat( m_fHeight, 64);
1024: 
1025:             //-------------------------------------------------
1026:             // フィルタリング
1027:             //-------------------------------------------------
1028:             TVERTEX Vertex1[4] = {
1029:                 //   x    y     z    tu tv
1030:                 {-1.0f, +1.0f, 0.1f,  0, 0},
1031:                 {+1.0f, +1.0f, 0.1f,  1, 0},
1032:                 {+1.0f, -1.0f, 0.1f,  1, 1},
1033:                 {-1.0f, -1.0f, 0.1f,  0, 1},
1034:             };
1035:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1036:             m_pEffect->SetTexture( m_htSrcTex, m_p64Tex );
1037:             BEGIN_PASS( 3 );
1038:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1039:                             , 2, Vertex1, sizeof( TVERTEX ) );
1040:             END_PASS();
1041: 
1042:             m_pEffect->End();
1043:         }
1044: 
1045:         //-------------------------------------------------
1046:         //-------------------------------------------------
1047:         // ラディアンスをテクスチャの対応する位置に張る
1048:         //-------------------------------------------------
1049:         //-------------------------------------------------
1050:         m_pd3dDevice->SetRenderTarget(0, m_pFinalSSSurf[itex]);
1051:         m_pd3dDevice->SetDepthStencilSurface(NULL);
1052:         // ビューポートの変更
1053:         m_pFinalSSSurf[itex]->GetDesc(&d3dsd);
1054:         viewport.Height = d3dsd.Width;
1055:         viewport.Width  = d3dsd.Height;
1056:         m_pd3dDevice->SetViewport(&viewport);
1057:         
1058:         // 最初は灰色で塗りつぶす
1059:         if(0==ix&&0==iy)m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, 0x80808080, 1.0f, 0L);
1060: 
1061:         if( m_pEffect != NULL ) {
1062:             //-------------------------------------------------
1063:             // シェーダの設定
1064:             //-------------------------------------------------
1065:             m_pEffect->SetTechnique( m_hTechnique );
1066:             m_pEffect->Begin( NULL, 0 );
1067: 
1068:             m_pEffect->SetFloat( m_fWidth,  8);
1069:             m_pEffect->SetFloat( m_fHeight, 8);
1070: 
1071:             //-------------------------------------------------
1072:             // フィルタリング
1073:             //-------------------------------------------------
1074:             float x =  2.0f*((float)ix/(float)DECALE_MAP_SIZE) - 1.0f;
1075:             float y = -2.0f*((float)iy/(float)DECALE_MAP_SIZE) + 1.0f;
1076:             TVERTEX Vertex1[4] = {
1077:                 //   x    y     z    tu tv
1078:                 {x,                      y,                     0.1f,  0, 0},
1079:                 {x+2.0f/(float)DECALE_MAP_SIZE, y,                     0.1f,  1, 0},
1080:                 {x+2.0f/(float)DECALE_MAP_SIZE, y-2.0f/(float)DECALE_MAP_SIZE,0.1f,  1, 1},
1081:                 {x,                      y-2.0f/(float)DECALE_MAP_SIZE,0.1f,  0, 1},
1082:             };
1083:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1084:             m_pEffect->SetTexture( m_htSrcTex, m_p8Tex );
1085:             BEGIN_PASS(3);
1086:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1087:                             , 2, Vertex1, sizeof( TVERTEX ) );
1088:             END_PASS();
1089: 
1090:             m_pEffect->End();
1091:         }
1092: 
1093:         
1094:         //-----------------------------------------------------
1095:         // レンダリングターゲットを元に戻す
1096:         //-----------------------------------------------------
1097:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
1098:         m_pd3dDevice->SetRenderTarget(1, NULL);
1099:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
1100:         m_pd3dDevice->SetViewport(&oldViewport);
1101:         pOldBackBuffer->Release();
1102:         pOldZBuffer->Release();
1103: 
1104:         m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
1105:         m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
1106: 
1107:         //---------------------------------------------------------
1108:         // 画面の表示
1109:         //---------------------------------------------------------
1110:         m_pd3dDevice->Clear(0L, NULL
1111:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1112:                         , 0x00001020, 1.0f, 0L);
1113: 
1114: #if 1 // デバッグ用にテクスチャを表示する
1115:         {
1116:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
1117:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
1118:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
1119:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
1120:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
1121:         m_pd3dDevice->SetVertexShader(NULL);
1122:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1123:         m_pd3dDevice->SetPixelShader(0);
1124:         float scale = 256.0f*2/3;
1125:         for(DWORD i=0; i<3; i++){
1126:             TVERTEX Vertex[4] = {
1127:                 // x  y  z rhw tu tv
1128:                 {    0,(i+0)*scale,0, 1, 0, 0,},
1129:                 {scale,(i+0)*scale,0, 1, 1, 0,},
1130:                 {scale,(i+1)*scale,0, 1, 1, 1,},
1131:                 {    0,(i+1)*scale,0, 1, 0, 1,},
1132:             };
1133:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pFinalSSTex[itex] );
1134:             if(1==i) m_pd3dDevice->SetTexture( 0, m_p64Tex );
1135:             if(2==i) m_pd3dDevice->SetTexture( 0, m_p8Tex );
1136:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1137:         }
1138:         }
1139: #endif      
1140: 
1141:         D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
1142:         TCHAR szMsg[MAX_PATH] = TEXT("");
1143: 
1144:         sprintf( szMsg, TEXT("Precomputing 1/2\nx:%4d\ny:%4d"), ix, iy);
1145:         m_pFont->DrawText( 2, (FLOAT)m_d3dsdBackBuffer.Height-60, fontColor, szMsg );
1146: 
1147:         m_pd3dDevice->EndScene();
1148:     }
1149: }
1150: 
1151: 
1152: //-------------------------------------------------------------
1153: // Name: FrameMovePRT()
1154: // Desc: 位置、法線マップを作成する。
1155: //-------------------------------------------------------------
1156: int CMyD3DApplication::FrameMovePRT()
1157: {
1158:     itex = this->m_iCount / (DECALE_MAP_SIZE*DECALE_MAP_SIZE);
1159:     ix = this->m_iCount % DECALE_MAP_SIZE;
1160:     iy = (this->m_iCount / DECALE_MAP_SIZE) % DECALE_MAP_SIZE;
1161:     
1162:     if(0==this->m_iCount){
1163: #if 0
1164:         D3DLOCKED_RECT d3dlr;
1165:         float *p;
1166:         m_pd3dDevice->GetRenderTargetData(m_pPosSurf,m_pPosLockSurf);
1167:         m_pPosLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY); //サーフェイス上の矩形をロック    
1168:         p = (float *)d3dlr.pBits;
1169:         for(int y=0;y<DECALE_MAP_SIZE;y++){
1170:         for(int x=0;x<DECALE_MAP_SIZE;x++){
1171:             pos[x][y][0]=p[0];
1172:             pos[x][y][1]=p[1];
1173:             pos[x][y][2]=p[2];
1174:             p+=4;
1175:         }
1176:         }
1177:         m_pPosLockSurf->UnlockRect();
1178: 
1179:         m_pd3dDevice->GetRenderTargetData(m_pNormalSurf,m_pNormalLockSurf);
1180:         m_pNormalLockSurf->LockRect(&d3dlr,NULL,D3DLOCK_READONLY); //サーフェイス上の矩形をロック    
1181:         p = (float *)d3dlr.pBits;        
1182:         for( y=0;y<DECALE_MAP_SIZE;y++){
1183:         for( int x=0;x<DECALE_MAP_SIZE;x++){
1184:             normal[x][y][0]=p[0];
1185:             normal[x][y][1]=p[1];
1186:             normal[x][y][2]=p[2];
1187:             p+=4;
1188:         }
1189:         }
1190:         m_pNormalLockSurf->UnlockRect();
1191: #endif
1192:     }
1193: 
1194:     // 終了条件
1195:     if(DECALE_MAP_SIZE-1==ix && DECALE_MAP_SIZE-1==iy && TEX_MAX-1==itex) return 1;
1196: 
1197:     return 0;
1198: }
1199: 
1200: 
1201: //-------------------------------------------------------------
1202: // Name: RenderPRT()
1203: // Desc: それぞれの位置からのラディアンスを計算する。
1204: //-------------------------------------------------------------
1205: void CMyD3DApplication::RenderPRT()
1206: {
1207:     D3DXMATRIX m, mView;
1208:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
1209:     D3DVIEWPORT9 oldViewport;
1210:     D3DSURFACE_DESC d3dsd;
1211: 
1212:     D3DVIEWPORT9 viewport = {0,0      // 左上の座標
1213:                     , 1, 1  // 幅,高さ
1214:                     , 0.0f,1.0f};     // 前面、後面
1215: 
1216:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
1217:     {
1218:         m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
1219:         m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
1220: 
1221:         //-------------------------------------------------
1222:         // レンダリングターゲットの保存
1223:         //-------------------------------------------------
1224:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
1225:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
1226:         m_pd3dDevice->GetViewport(&oldViewport);
1227: 
1228:         //-------------------------------------------------
1229:         //-------------------------------------------------
1230:         // 各点から見たラディアンスの描画
1231:         //-------------------------------------------------
1232:         //-------------------------------------------------
1233: 
1234:         //-------------------------------------------------
1235:         // レンダリングターゲットの変更
1236:         //-------------------------------------------------
1237:         m_pd3dDevice->SetRenderTarget(0, m_p64Surf );
1238:         m_pd3dDevice->SetDepthStencilSurface(NULL);
1239:         // ビューポートの変更
1240:         m_p64Surf->GetDesc(&d3dsd);
1241:         viewport.Height = d3dsd.Width;
1242:         viewport.Width  = d3dsd.Height;
1243:         m_pd3dDevice->SetViewport(&viewport);
1244: 
1245:         if( m_pEffect != NULL ) 
1246:         {
1247:             //-------------------------------------------------
1248:             // 重みを持つ半球の部分だけ描画
1249:             //-------------------------------------------------
1250:             float x[3] = {pos[ix][iy][0], pos[ix][iy][1], pos[ix][iy][2]};
1251:             float n[3] = {normal[ix][iy][0], normal[ix][iy][1], normal[ix][iy][2]};
1252:             D3DXVECTOR3 vFromPt   = D3DXVECTOR3( x[0], x[1], x[2] ) + 0.01f*D3DXVECTOR3( n[0], n[1], n[2] );
1253:             D3DXVECTOR3 vLookatPt = D3DXVECTOR3( n[0], n[1], n[2] ) + vFromPt;
1254:             D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
1255:             D3DXMatrixLookAtLH( &mView, &vFromPt, &vLookatPt, &vUpVec );
1256:             m_pEffect->SetMatrix( "mWV", &mView );
1257: 
1258:             D3DXMatrixTranspose(&m, &mView);
1259:             m_pEffect->SetMatrix( "mST", &m );
1260: 
1261:             // シェーダの設定
1262:             m_pEffect->SetTechnique( "TShader" );
1263:             m_pEffect->Begin( NULL, 0 );
1264: 
1265:             TVERTEX Vertex[4] = {
1266:                 // x  y  z tu tv
1267:                 {-1,-1,0,  0, 0,},
1268:                 {+1,-1,0,  1, 0,},
1269:                 {+1,+1,0,  1, 1,},
1270:                 {-1,+1,0,  0, 1,},
1271:             };
1272:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1273:             m_pEffect->SetTexture(m_htSrcTex, m_pMaskTex);
1274:             BEGIN_PASS( 11 + itex );
1275:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1276:             END_PASS();
1277: 
1278:             //-------------------------------------------------
1279:             // モデルの描画
1280:             //-------------------------------------------------
1281:             BEGIN_PASS( 15 );
1282:             m_pMesh->Render(m_pd3dDevice);
1283:             END_PASS();
1284:             m_pEffect->End();
1285:         }
1286:         //-------------------------------------------------
1287:         //-------------------------------------------------
1288:         // ラディアンスをミップマップの要領で小さくする
1289:         //-------------------------------------------------
1290:         //-------------------------------------------------
1291:         m_pd3dDevice->SetRenderTarget( 0, m_p8Surf );
1292:         m_pd3dDevice->SetDepthStencilSurface(NULL);
1293:         // ビューポートの変更
1294:         m_p8Surf->GetDesc(&d3dsd);
1295:         viewport.Height = d3dsd.Width;
1296:         viewport.Width  = d3dsd.Height;
1297:         m_pd3dDevice->SetViewport(&viewport);
1298: 
1299:         if( m_pEffect != NULL ) {
1300:             //-------------------------------------------------
1301:             // シェーダの設定
1302:             //-------------------------------------------------
1303:             m_pEffect->SetTechnique( m_hTechnique );
1304:             m_pEffect->Begin( NULL, 0 );
1305: 
1306:             m_pEffect->SetFloat( m_fWidth,  64);
1307:             m_pEffect->SetFloat( m_fHeight, 64);
1308: 
1309:             //-------------------------------------------------
1310:             // フィルタリング
1311:             //-------------------------------------------------
1312:             TVERTEX Vertex1[4] = {
1313:                 //   x    y     z    tu tv
1314:                 {-1.0f, +1.0f, 0.1f,  0, 0},
1315:                 {+1.0f, +1.0f, 0.1f,  1, 0},
1316:                 {+1.0f, -1.0f, 0.1f,  1, 1},
1317:                 {-1.0f, -1.0f, 0.1f,  0, 1},
1318:             };
1319:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1320:             m_pEffect->SetTexture( m_htSrcTex, m_p64Tex );
1321:             BEGIN_PASS( 3 );
1322:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1323:                             , 2, Vertex1, sizeof( TVERTEX ) );
1324:             END_PASS();
1325: 
1326:             m_pEffect->End();
1327:         }
1328: 
1329:         //-------------------------------------------------
1330:         //-------------------------------------------------
1331:         // ラディアンスをテクスチャの対応する位置に張る
1332:         //-------------------------------------------------
1333:         //-------------------------------------------------
1334:         m_pd3dDevice->SetRenderTarget(0, m_pFinalSurf[itex]);
1335:         m_pd3dDevice->SetDepthStencilSurface(NULL);
1336:         // ビューポートの変更
1337:         m_pFinalSurf[itex]->GetDesc(&d3dsd);
1338:         viewport.Height = d3dsd.Width;
1339:         viewport.Width  = d3dsd.Height;
1340:         m_pd3dDevice->SetViewport(&viewport);
1341:         
1342:         // 最初は灰色で塗りつぶす
1343:         if(0==ix&&0==iy)m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET, 0x80808080, 1.0f, 0L);
1344: 
1345:         if( m_pEffect != NULL ) {
1346:             //-------------------------------------------------
1347:             // シェーダの設定
1348:             //-------------------------------------------------
1349:             m_pEffect->SetTechnique( m_hTechnique );
1350:             m_pEffect->Begin( NULL, 0 );
1351: 
1352:             m_pEffect->SetFloat( m_fWidth,  8);
1353:             m_pEffect->SetFloat( m_fHeight, 8);
1354: 
1355:             //-------------------------------------------------
1356:             // フィルタリング
1357:             //-------------------------------------------------
1358:             float x =  2.0f*((float)ix/(float)DECALE_MAP_SIZE) - 1.0f;
1359:             float y = -2.0f*((float)iy/(float)DECALE_MAP_SIZE) + 1.0f;
1360:             TVERTEX Vertex1[4] = {
1361:                 //   x    y     z    tu tv
1362:                 {x,                      y,                     0.1f,  0, 0},
1363:                 {x+2.0f/(float)DECALE_MAP_SIZE, y,                     0.1f,  1, 0},
1364:                 {x+2.0f/(float)DECALE_MAP_SIZE, y-2.0f/(float)DECALE_MAP_SIZE,0.1f,  1, 1},
1365:                 {x,                      y-2.0f/(float)DECALE_MAP_SIZE,0.1f,  0, 1},
1366:             };
1367:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1368:             m_pEffect->SetTexture( m_htSrcTex, m_p8Tex );
1369:             BEGIN_PASS( 3 );
1370:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1371:                             , 2, Vertex1, sizeof( TVERTEX ) );
1372:             END_PASS();
1373: 
1374:             m_pEffect->End();
1375:         }
1376: 
1377:         
1378:         //-----------------------------------------------------
1379:         // レンダリングターゲットを元に戻す
1380:         //-----------------------------------------------------
1381:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
1382:         m_pd3dDevice->SetRenderTarget(1, NULL);
1383:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
1384:         m_pd3dDevice->SetViewport(&oldViewport);
1385:         pOldBackBuffer->Release();
1386:         pOldZBuffer->Release();
1387: 
1388:         m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
1389:         m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
1390: 
1391:         //---------------------------------------------------------
1392:         // 画面の表示
1393:         //---------------------------------------------------------
1394:         m_pd3dDevice->Clear(0L, NULL
1395:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1396:                         , 0x00001020, 1.0f, 0L);
1397: 
1398: #if 1 // デバッグ用にテクスチャを表示する
1399:         {
1400:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
1401:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
1402:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
1403:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
1404:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
1405:         m_pd3dDevice->SetVertexShader(NULL);
1406:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1407:         m_pd3dDevice->SetPixelShader(0);
1408:         float scale = 256.0f*2/3;
1409:         for(DWORD i=0; i<3; i++){
1410:             TVERTEX Vertex[4] = {
1411:                 // x  y  z rhw tu tv
1412:                 {    0,(i+0)*scale,0, 1, 0, 0,},
1413:                 {scale,(i+0)*scale,0, 1, 1, 0,},
1414:                 {scale,(i+1)*scale,0, 1, 1, 1,},
1415:                 {    0,(i+1)*scale,0, 1, 0, 1,},
1416:             };
1417:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pFinalTex[itex] );
1418:             if(1==i) m_pd3dDevice->SetTexture( 0, m_p64Tex );
1419:             if(2==i) m_pd3dDevice->SetTexture( 0, m_p8Tex );
1420:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1421:         }
1422:         }
1423: #endif      
1424: 
1425:         D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
1426:         TCHAR szMsg[MAX_PATH] = TEXT("");
1427: 
1428:         sprintf( szMsg, TEXT("Precomputing 2/2\nx:%4d\ny:%4d"), ix, iy);
1429:         m_pFont->DrawText( 2, (FLOAT)m_d3dsdBackBuffer.Height-60, fontColor, szMsg );
1430: 
1431:         m_pd3dDevice->EndScene();
1432:     }
1433: }
1434: 
1435: 
1436: //-------------------------------------------------------------
1437: // Name: RenderParaboloidMap()
1438: //-------------------------------------------------------------
1439: void CMyD3DApplication::RenderParaboloidMap()
1440: {
1441:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
1442:     D3DVIEWPORT9 oldViewport;
1443:     D3DMATERIAL9 *pMtrl;
1444:     int i, j, k;
1445:     D3DXMATRIX m, mT, mR, mW, mView, mProj;
1446:     D3DXVECTOR3 vFrom;
1447:     D3DXVECTOR3 vLookat;
1448:     D3DXVECTOR3 vUpVec;
1449: 
1450:     D3DVIEWPORT9 viewport = {0, 0   // x,y(左上の座標)
1451:                     , 1, 1          // width, height(幅,高さ)
1452:                     , 0.0f,1.0f};   // near, far(前面、後面)
1453: 
1454:     //-------------------------------------------------
1455:     // Backup rendering target(レンダリングターゲットの保存)
1456:     //-------------------------------------------------
1457:     m_pd3dDevice->GetRenderTarget( 0, &pOldBackBuffer );
1458:     m_pd3dDevice->GetDepthStencilSurface( &pOldZBuffer );
1459:     m_pd3dDevice->GetViewport( &oldViewport );
1460:     
1461:     //-------------------------------------------------
1462:     // Create environment map(環境マップの作成)
1463:     //-------------------------------------------------
1464:     if( m_pEffect != NULL ) 
1465:     {
1466:         // Set shader
1467:         m_pEffect->SetTechnique( m_hTechnique );
1468:         m_pEffect->Begin( NULL, 0 );
1469: 
1470:         for( i = 0; i < 2; i++ )
1471:         {
1472:             // Chenge rendering target
1473:             m_pd3dDevice->SetRenderTarget( 0, m_pParaboloidSurf[i] );
1474:             m_pd3dDevice->SetDepthStencilSurface( m_pMapZ );
1475:             viewport.Width  = viewport.Height = MAP_SIZE;
1476:             m_pd3dDevice->SetViewport( &viewport );
1477:             
1478:             // Clear the scene
1479:             m_pd3dDevice->Clear(0L, NULL
1480:                 , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1481:                 , 0xff000000, 1.0f, 0L);
1482:             
1483:             // Create view matrix
1484: 
1485:             // Set matrixs(行列の設定)
1486:             D3DXMatrixRotationX( &mW, m_rot.x );
1487:             D3DXMatrixRotationY( &mR, m_rot.y );
1488:             mW = mW * mR;
1489:             D3DXMatrixRotationZ( &mR, m_rot.z );
1490:             mW = mW * mR;
1491:             D3DXMatrixTranslation( &mT, m_pos.x, m_pos.y, m_pos.z );
1492:             mW = mW * mT;
1493: 
1494:             vFrom   = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
1495:             D3DXVec3TransformCoord( &vFrom, &vFrom, &mW);
1496:             switch(i)
1497:             {
1498:             case 0:
1499:                 // Front of view(前面)
1500:                 vLookat = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
1501:                 break;
1502:             case 1:
1503:                 // Back of view(後面)
1504:                 vLookat = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
1505:                 break;
1506:             }
1507:             D3DXVec3TransformCoord( &vLookat, &vLookat, &mW);
1508:             vUpVec  = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
1509:             D3DXVec3TransformCoord( &vUpVec, &vUpVec, &mW);
1510:             vUpVec -= vFrom;
1511:             D3DXMatrixLookAtLH( &mView, &vFrom, &vLookat, &vUpVec );
1512: 
1513:             // Render ground(地面の描画)
1514:             m = mView;
1515:             D3DXMatrixTranslation( &mT, 0, -3, 0 );
1516:             m = mT * m;
1517: //D3DXMatrixTranslation( &mT, 0, -2, 0 );
1518: //D3DXMatrixRotationZ( &m, 0.5*D3DX_PI );
1519: //m = mT * m * mView;
1520:             m_pEffect->SetMatrix( m_hmWV, &m );
1521: 
1522:             pMtrl = m_pMeshBg->m_pMaterials;
1523:             for( unsigned int s=0; s<m_pMeshBg->m_dwNumMaterials; s++ ) {
1524:                 m_pEffect->SetTexture(m_htSrcTex, m_pMeshBg->m_pTextures[s] );
1525:                 BEGIN_PASS( 4 );
1526:                 m_pMeshBg->m_pLocalMesh->DrawSubset( s );
1527:                 END_PASS();
1528:                 pMtrl++;
1529:             }
1530:             
1531:             // Render back sphere(天球の描画)
1532:             D3DXMatrixScaling ( &m, 100, 100, 100 );
1533:             m = m_mEnv * m * mView;
1534:             m_pEffect->SetMatrix( m_hmWV, &m );
1535:             
1536:             pMtrl = m_pMeshEnv->m_pMaterials;
1537:             for( s=0; s<m_pMeshEnv->m_dwNumMaterials; s++ ) {
1538:                 m_pEffect->SetTexture(m_htSrcTex, m_pMeshEnv->m_pTextures[s] );
1539:                 BEGIN_PASS( 4 );
1540:                 m_pMeshEnv->m_pLocalMesh->DrawSubset( s );
1541:                 END_PASS();
1542:                 pMtrl++;
1543:             }
1544:         }
1545:         //-------------------------------------------------
1546:         // Reduction in the way of a mip-map.(ミップマップの要領で小さくする)
1547:         //-------------------------------------------------
1548: 
1549:         m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[0]);
1550:         m_pd3dDevice->SetDepthStencilSurface(NULL);
1551:         viewport.Width  = viewport.Height = 128;
1552:         m_pd3dDevice->SetViewport(&viewport);
1553:         
1554:         // Reduces to the screen divided into four by 1/2,
1555:         // and both sides are compounded by multiplying the SH coefficient
1556:         // (4分割した画面に1/2縮小して両面をSH係数で合成する)
1557:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX2 );
1558:         m_pEffect->SetTexture( "ParaboloidFrontTex", m_pParaboloidTex[0] );
1559:         m_pEffect->SetTexture( "ParaboloidBackTex",  m_pParaboloidTex[1] );
1560:         m_pEffect->SetTexture( "WeightTexFront", m_pJacobianTex[0][0] );
1561:         m_pEffect->SetTexture( "WeightTexBack", m_pJacobianTex[0][1] );
1562:         float scale = 64;
1563:         int pass = 6;
1564:         for( j = 0; j < 2; j++)
1565:         {
1566:             for( k = 0; k < 2; k++)
1567:             {
1568:                 TVERTEX Vertex[4] = {
1569:                     //    x           y       z rhw  tu       tv
1570:                     {(j+0)*scale,(k+0)*scale, 0, 1, 0+1/128, 0+1/128 },
1571:                     {(j+1)*scale,(k+0)*scale, 0, 1, 1+1/128, 0+1/128 },
1572:                     {(j+1)*scale,(k+1)*scale, 0, 1, 1+1/128, 1+1/128 },
1573:                     {(j+0)*scale,(k+1)*scale, 0, 1, 0+1/128, 1+1/128 },
1574:                 };
1575:                 BEGIN_PASS( pass++ );
1576:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1577:                 END_PASS();
1578:             }
1579:         }
1580: 
1581: 
1582:         // 2nd reduction (16x16) (2段階)
1583:         m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[1]);
1584:         viewport.Width  = viewport.Height = 16;
1585:         m_pd3dDevice->SetViewport(&viewport);
1586: 
1587:         TVERTEX Vertex1[4] = {
1588:             //   x    y     z    tu tv
1589:             {-1.0f, +1.0f, 0.5f,  0, 0},
1590:             {+1.0f, +1.0f, 0.5f,  1, 0},
1591:             {+1.0f, -1.0f, 0.5f,  1, 1},
1592:             {-1.0f, -1.0f, 0.5f,  0, 1},
1593:         };
1594:         m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
1595: 
1596:         m_pEffect->SetFloat( m_fWidth,  128 );
1597:         m_pEffect->SetFloat( m_fHeight, 128 );
1598:         m_pEffect->SetTexture( m_htSrcTex, m_pReductionTex[0] );
1599:         BEGIN_PASS( 3 );
1600:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1601:                         , 2, Vertex1, sizeof( TVERTEX ) );
1602:         END_PASS();
1603: 
1604: 
1605:         // 3rd reduction (2x2) (3段階)
1606:         m_pd3dDevice->SetRenderTarget(0, m_pReductionSurf[2]);
1607:         viewport.Width  = viewport.Height = 2;
1608:         m_pd3dDevice->SetViewport(&viewport);
1609: 
1610:         m_pEffect->SetFloat( m_fWidth,  16 );
1611:         m_pEffect->SetFloat( m_fHeight, 16 );
1612:         m_pEffect->SetTexture(m_htSrcTex, m_pReductionTex[1]);
1613:         BEGIN_PASS( 3 );
1614:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1615:                         , 2, Vertex1, sizeof( TVERTEX ) );
1616:         END_PASS();
1617: 
1618:         m_pEffect->End();
1619:     }
1620: 
1621: 
1622:     //-----------------------------------------------------
1623:     // Restore render target
1624:     // (レンダリングターゲットを元に戻す)
1625:     //-----------------------------------------------------
1626:     m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
1627:     m_pd3dDevice->SetRenderTarget(1, NULL);
1628:     m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
1629:     m_pd3dDevice->SetViewport(&oldViewport);
1630:     pOldBackBuffer->Release();
1631:     pOldZBuffer->Release();
1632: 
1633: }
1634: //-------------------------------------------------------------
1635: // Name: Render()
1636: // Desc: Called once per frame, the call is the entry point for 3d
1637: //       rendering. This function sets up render states, clears the
1638: //       viewport, and renders the scene.
1639: //       (画面を描画する)
1640: //-------------------------------------------------------------
1641: HRESULT CMyD3DApplication::Render()
1642: {
1643:     D3DXMATRIX m, mT, mR, mView, mProj;
1644:     DWORD i;
1645:     D3DXVECTOR4 v;
1646:     D3DMATERIAL9 *pMtrl;
1647: 
1648:     switch(m_iState){
1649:     case 0:
1650:         this->RenderCreateMap();
1651:         return S_OK;
1652:     case 1:
1653:         this->RenderSSPRT();
1654:         return S_OK;
1655:     case 2:
1656:         this->RenderPRT();
1657:         return S_OK;
1658:     default:
1659:         break;
1660:     }
1661: 
1662: 
1663:     // Begin the scene(描画開始)
1664:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
1665:     {
1666:         //---------------------------------------------------------
1667:         // Create maps(テクスチャの生成)
1668:         //---------------------------------------------------------
1669:         RenderParaboloidMap();
1670: 
1671:         // Clear the render buffers(フレームバッファのクリア)
1672:         m_pd3dDevice->Clear(0L, NULL
1673:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
1674:                         , 0xff000000, 1.0f, 0L);
1675: 
1676:         if( m_pEffect != NULL ) 
1677:         {
1678:             //-------------------------------------------------
1679:             // Set effect(シェーダの設定)
1680:             //-------------------------------------------------
1681:             m_pEffect->SetTechnique( m_hTechnique );
1682:             m_pEffect->Begin( NULL, 0 );
1683: 
1684:             //-------------------------------------------------
1685:             // Render model (モデルの描画)
1686:             //-------------------------------------------------
1687:             // Set matrixs(行列の設定)
1688:             D3DXMatrixRotationX( &mR, m_rot.x );
1689:             D3DXMatrixRotationY( &m, m_rot.y );
1690:             mR = mR * m;
1691:             D3DXMatrixRotationZ( &m, m_rot.z );
1692:             mR = mR * m;
1693:             D3DXMatrixTranslation( &mT, m_pos.x, m_pos.y, m_pos.z );
1694:             m = mR * mT * m_mWorld * m_mView;
1695:             m_pEffect->SetMatrix( m_hmWV, &m );
1696:             m = m * m_mProj;
1697:             m_pEffect->SetMatrix( m_hmWVP, &m );
1698: 
1699:             // View point in the local coordinate system(ローカルでの視点)
1700:             m = mR * mT * m_mWorld * m_mView;
1701:             D3DXMatrixInverse( &m, NULL, &m);
1702:             v = D3DXVECTOR4( 0, 0, 0, 1 );
1703:             D3DXVec4Transform( &v, &v, &m);
1704:             m_pEffect->SetVector( "vEye", &v );
1705: 
1706:             m_pEffect->SetFloat( "diffuse", m_fDiffuse );
1707:             m_pEffect->SetFloat( "specular", m_fSpecular );
1708:             m_pEffect->SetFloat( "translucent", m_fTranslucent );
1709: 
1710:             // Set the texture and render
1711:             m_pEffect->SetTexture( "ParaboloidFrontTex", m_pParaboloidTex[0] );
1712:             m_pEffect->SetTexture( "ParaboloidBackTex",  m_pParaboloidTex[1] );
1713:             m_pEffect->SetTexture( m_htSrcTex, m_pReductionTex[2]);
1714: 
1715:             m_pEffect->SetTexture( "PRTTex",  m_pFinalTex[0] );
1716:             m_pEffect->SetTexture( "SSTex",   m_pFinalSSTex[0] );
1717:             
1718:             m_pEffect->SetTexture( "DecaleTex",  m_pMesh->m_pTextures[0] );
1719:             BEGIN_PASS( 0+m_Shader );
1720:             m_pMesh->Render( m_pd3dDevice );
1721:             END_PASS();
1722: 
1723:             //-------------------------------------------------
1724:             // Render ground(地形の描画)
1725:             //-------------------------------------------------
1726:             // World+view+projection matrix(ローカル-射影行列)
1727:             m = m_mWorld * m_mView * m_mProj;
1728:             D3DXMatrixTranslation( &mT, 0, -3, 0 );
1729:             m = mT * m;
1730: //D3DXMatrixTranslation( &mT, 0, -2, 0 );
1731: //D3DXMatrixRotationZ( &m, 0.5*D3DX_PI );
1732: //m = mT * m * m_mWorld * m_mView * m_mProj;
1733:             m_pEffect->SetMatrix( m_hmWVP, &m );
1734:             
1735:             // Set the texture and render(テクスチャを設定して描画)
1736:             pMtrl = m_pMeshBg->m_pMaterials;
1737:             for( i=0; i<m_pMeshBg->m_dwNumMaterials; i++ ) {
1738:                 m_pEffect->SetTexture(m_htSrcTex, m_pMeshBg->m_pTextures[i] );
1739:                 BEGIN_PASS( 5 );
1740:                 m_pMeshBg->m_pLocalMesh->DrawSubset( i );
1741:                 END_PASS();
1742:                 pMtrl++;
1743:             }
1744: 
1745:             //-------------------------------------------------
1746:             // Render back sphere(天球の描画)
1747:             //-------------------------------------------------
1748:             // World+view+projection matrix(ローカル-射影行列)
1749:             D3DXMatrixScaling ( &m, 100, 100, 100 );
1750:             m = m * m_mEnv * m_mWorld * m_mView * m_mProj;
1751:             m_pEffect->SetMatrix( m_hmWVP, &m );
1752:             
1753:             // Set the texture and render(テクスチャを設定して描画)
1754:             pMtrl = m_pMeshEnv->m_pMaterials;
1755:             for( i=0; i<m_pMeshEnv->m_dwNumMaterials; i++ ) {
1756:                 m_pEffect->SetTexture(m_htSrcTex, m_pMeshEnv->m_pTextures[i] );
1757:                 BEGIN_PASS( 5 );
1758:                 m_pMeshEnv->m_pLocalMesh->DrawSubset( i );
1759:                 END_PASS();
1760:                 pMtrl++;
1761:             }
1762: 
1763:             m_pEffect->End();
1764:         }
1765: 
1766: #ifdef _DEBUG // Textures are displaying when debugging, (デバッグ用にテクスチャを表示する)
1767:         {
1768:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
1769:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
1770:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
1771:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
1772:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
1773:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1774:         float scale = 512.0f/11;
1775:         for(DWORD i=0; i<11; i++){
1776:             TVERTEX Vertex[4] = {
1777:                 //    x                             y         z rhw tu tv
1778:                 {(i+0)*scale, (FLOAT)m_d3dsdBackBuffer.Height-scale, 0, 1, 0, 0,},
1779:                 {(i+1)*scale, (FLOAT)m_d3dsdBackBuffer.Height-scale, 0, 1, 1, 0,},
1780:                 {(i+1)*scale, (FLOAT)m_d3dsdBackBuffer.Height-    0, 0, 1, 1, 1,},
1781:                 {(i+0)*scale, (FLOAT)m_d3dsdBackBuffer.Height-    0, 0, 1, 0, 1,},
1782:             };
1783:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pParaboloidTex[0] );
1784:             if(1==i) m_pd3dDevice->SetTexture( 0, m_pParaboloidTex[1] );
1785:             if(2==i) m_pd3dDevice->SetTexture( 0, m_pJacobianTex[0][0] );
1786:             if(3==i) m_pd3dDevice->SetTexture( 0, m_pJacobianTex[0][1] );
1787:             if(4==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[0] );
1788:             if(5==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[1] );
1789:             if(6==i) m_pd3dDevice->SetTexture( 0, m_pReductionTex[2] );
1790:             if(7==i) m_pd3dDevice->SetTexture( 0, m_pPosTex );
1791:             if(8==i) m_pd3dDevice->SetTexture( 0, m_pNormalTex );
1792:             if(9==i) m_pd3dDevice->SetTexture( 0, m_pFinalSSTex[0] );
1793:             if(10==i) m_pd3dDevice->SetTexture( 0, m_pFinalTex[0] );
1794: 
1795:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1796:         }
1797:         }
1798: #endif      
1799: 
1800:         // Render stats and help text(ヘルプの表示)
1801:         RenderText();
1802: 
1803:         // End the scene.(描画の終了)
1804:         m_pd3dDevice->EndScene();
1805:     }
1806: 
1807:     return S_OK;
1808: }
1809: 
1810: 
1811: 
1812: 
1813: //-------------------------------------------------------------
1814: // Name: RenderText()
1815: // Desc: Draw the help & statistics for running sample
1816: //       (状態やヘルプを画面に表示する)
1817: //-------------------------------------------------------------
1818: HRESULT CMyD3DApplication::RenderText()
1819: {
1820:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
1821:     TCHAR szMsg[MAX_PATH] = TEXT("");
1822:     FLOAT fNextLine;
1823: 
1824:     // Draw help
1825:     fNextLine = 0; 
1826: 
1827:     static const TCHAR *shader_str[]=
1828:     {
1829:         "Diffuse+Translucent+Specular",
1830:         "Diffuse",
1831:         "Translucent",
1832:     };
1833: 
1834:     sprintf( szMsg, TEXT("Press 'Q' to change shader[%s]"), shader_str[m_Shader] );
1835:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1836:     fNextLine += 20.0f;
1837: 
1838:     sprintf( szMsg, TEXT("Diffuse[D-C] : %f"), m_fDiffuse );
1839:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1840:     fNextLine += 20.0f;
1841: 
1842:     sprintf( szMsg, TEXT("Specular[F-V] : %f"), m_fSpecular );
1843:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1844:     fNextLine += 20.0f;
1845: 
1846:     sprintf( szMsg, TEXT("Translucent[G-B] : %f"), m_fTranslucent );
1847:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1848:     fNextLine += 20.0f;
1849: 
1850:     // Output statistics
1851:     lstrcpy( szMsg, m_strDeviceStats );
1852:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1853:     fNextLine += 20.0f;
1854:     lstrcpy( szMsg, m_strFrameStats );
1855:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1856:     fNextLine += 20.0f;
1857:     
1858: #ifndef _DEBUG
1859:     fNextLine = (FLOAT) m_d3dsdBackBuffer.Height-20; 
1860:     m_pFont->DrawText( 6, fNextLine+2, 0xff000000, TEXT("t-pot.com") );
1861:     m_pFont->DrawText( 4, fNextLine,   0xffff8040, TEXT("t-pot.com") );
1862: #endif //!_DEBUG
1863: 
1864:     return S_OK;
1865: }
1866: 
1867: 
1868: 
1869: 
1870: //-------------------------------------------------------------
1871: // Name: MsgProc()
1872: // Desc: Overrrides the main WndProc, so the sample can do custom message
1873: //       handling (e.g. processing mouse, keyboard, or menu commands).
1874: //       (WndProc をオーバーライドしたもの)
1875: //-------------------------------------------------------------
1876: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
1877:                                  WPARAM wParam, LPARAM lParam )
1878: {
1879:     switch( msg )
1880:     {
1881:         case WM_PAINT:
1882:         {
1883:             if( m_bLoadingApp )
1884:             {
1885:                 // Loading message(ロード中)
1886:                 HDC hDC = GetDC( hWnd );
1887:                 TCHAR strMsg[MAX_PATH];
1888:                 wsprintf(strMsg, TEXT("Loading... Please wait"));
1889:                 RECT rct;
1890:                 GetClientRect( hWnd, &rct );
1891:                 DrawText( hDC, strMsg, -1, &rct
1892:                         , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
1893:                 ReleaseDC( hWnd, hDC );
1894:             }
1895:             break;
1896:         }
1897: 
1898:     }
1899: 
1900:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
1901: }
1902: 
1903: 
1904: 
1905: 
1906: //-------------------------------------------------------------
1907: // Name: InvalidateDeviceObjects()
1908: // Desc: Invalidates device objects.  Paired with RestoreDeviceObjects()
1909: //       (RestoreDeviceObjects() で作成したオブジェクトの開放)
1910: //-------------------------------------------------------------
1911: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
1912: {
1913:     int i;
1914: 
1915:     // font(フォント)
1916:     m_pFont->InvalidateDeviceObjects();
1917: 
1918:     // Rendering targets (レンダリングターゲット)
1919:     for(int j=0;j<TEX_MAX;j++){
1920:         SAFE_RELEASE(m_pFinalSSSurf[j]);
1921:         SAFE_RELEASE(m_pFinalSSTex[j]);
1922:         SAFE_RELEASE(m_pFinalSurf[j]);
1923:         SAFE_RELEASE(m_pFinalTex[j]);
1924:     }
1925:     SAFE_RELEASE( m_p8Surf );
1926:     SAFE_RELEASE( m_p8Tex );
1927:     SAFE_RELEASE( m_p64Z );
1928:     SAFE_RELEASE( m_p64Surf2 );
1929:     SAFE_RELEASE( m_p64Tex2 );
1930:     SAFE_RELEASE( m_p64Surf );
1931:     SAFE_RELEASE( m_p64Tex );
1932:     for( i = 0; i < REDUCTION_MAPS; i++ )
1933:     {
1934:         SAFE_RELEASE(m_pReductionSurf[i]);
1935:         SAFE_RELEASE(m_pReductionTex[i]);
1936:     }
1937:     SAFE_RELEASE(m_pMaskTex);
1938:     SAFE_RELEASE(m_pParaboloidSurf[1]);
1939:     SAFE_RELEASE(m_pParaboloidTex[1]);
1940:     SAFE_RELEASE(m_pParaboloidSurf[0]);
1941:     SAFE_RELEASE(m_pParaboloidTex[0]);
1942:     SAFE_RELEASE(m_pNormalLockSurf);
1943:     SAFE_RELEASE(m_pNormalLockTex);
1944:     SAFE_RELEASE(m_pNormalSurf);
1945:     SAFE_RELEASE(m_pNormalTex);
1946:     SAFE_RELEASE(m_pPosLockSurf);
1947:     SAFE_RELEASE(m_pPosLockTex);
1948:     SAFE_RELEASE(m_pPosSurf);
1949:     SAFE_RELEASE(m_pPosTex);
1950:     SAFE_RELEASE(m_pMapZ);
1951: 
1952:     // Models(モデル)
1953:     m_pMeshEnv->InvalidateDeviceObjects();
1954:     m_pMeshBg->InvalidateDeviceObjects();
1955:     m_pMesh->InvalidateDeviceObjects();
1956:     
1957:     // Shaders(シェーダ)
1958:     if( m_pEffect    != NULL ) m_pEffect   ->OnLostDevice();
1959: 
1960:     return S_OK;
1961: }
1962: 
1963: 
1964: 
1965: 
1966: //-------------------------------------------------------------
1967: // Name: DeleteDeviceObjects()
1968: // Desc: Paired with InitDeviceObjects()
1969: //       Called when the app is exiting, or the device is being changed,
1970: //       this function deletes any device dependent objects.  
1971: //       (InitDeviceObjects() で作成したオブジェクトを開放する)
1972: //-------------------------------------------------------------
1973: HRESULT CMyD3DApplication::DeleteDeviceObjects()
1974: {
1975:     // font(フォント)
1976:     m_pFont->DeleteDeviceObjects();
1977: 
1978:     // textures (テクスチャ)
1979:     for( int i = 0; i < TEX_MAX; i++ )
1980:     {
1981:         SAFE_RELEASE(m_pJacobianTex[i][0]);
1982:         SAFE_RELEASE(m_pJacobianTex[i][1]);
1983:     }
1984: 
1985:     // Shaders(シェーダ)
1986:     SAFE_RELEASE( m_pEffect );
1987:     
1988:     // Models(モデル)
1989:     m_pMeshEnv->Destroy();
1990:     m_pMeshBg->Destroy();
1991:     m_pMesh->Destroy();
1992: 
1993:     return S_OK;
1994: }
1995: 
1996: 
1997: 
1998: 
1999: //-------------------------------------------------------------
2000: // Name: FinalCleanup()
2001: // Desc: Paired with OneTimeSceneInit()
2002: //       Called before the app exits, this function gives the app the chance
2003: //       to cleanup after itself.
2004: //       (終了する直前に呼ばれる)
2005: //-------------------------------------------------------------
2006: HRESULT CMyD3DApplication::FinalCleanup()
2007: {
2008:     // font(フォント)
2009:     SAFE_DELETE( m_pFont );
2010: 
2011:     // Models(モデル)
2012:     SAFE_DELETE( m_pMeshEnv );
2013:     SAFE_DELETE( m_pMeshBg );
2014:     SAFE_DELETE( m_pMesh );
2015: 
2016:     return S_OK;
2017: }
2018: 
2019: 
2020: 
2021: 
2022: