0001: //-----------------------------------------------------------------------------
0002: // File: main.cpp
0003: //
0004: // Desc: DirectX window application created by the DirectX AppWizard
0005: //-----------------------------------------------------------------------------
0006: #define STRICT
0007: #include <windows.h>
0008: #include <commctrl.h>
0009: #include <commdlg.h>
0010: #include <basetsd.h>
0011: #include <math.h>
0012: #include <stdio.h>
0013: #include <d3dx9.h>
0014: #include <dxerr9.h>
0015: #include <tchar.h>
0016: #include "DXUtil.h"
0017: #include "D3DEnumeration.h"
0018: #include "D3DSettings.h"
0019: #include "D3DApp.h"
0020: #include "D3DFont.h"
0021: #include "D3DFile.h"
0022: #include "D3DUtil.h"
0023: #include "resource.h"
0024: #include "main.h"
0025: 
0026: 
0027: #define PARTICLEMAP_WIDTH  64
0028: #define PARTICLEMAP_HEIGHT 4
0029: 
0030: 
0031: typedef struct {
0032:     FLOAT p[4];
0033:     D3DCOLOR c;
0034: } LVERTEX;
0035: 
0036: typedef struct {
0037:     FLOAT p[3];     // 位置座標
0038:     FLOAT n[3];     // 法線
0039:     FLOAT tu, tv;   // テクスチャ
0040:     FLOAT du, dv;   // ディスプレースメント
0041: } MESH_VERTEX;
0042: 
0043: //-----------------------------------------------------------------------------
0044: // グローバル変数
0045: //-----------------------------------------------------------------------------
0046: CMyD3DApplication* g_pApp  = NULL;
0047: HINSTANCE          g_hInst = NULL;
0048: 
0049: 
0050: 
0051: 
0052: //-----------------------------------------------------------------------------
0053: // Name: WinMain()
0054: // Desc: Entry point to the program. Initializes everything, and goes into a
0055: //       message-processing loop. Idle time is used to render the scene.
0056: //-----------------------------------------------------------------------------
0057: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0058: {
0059:     CMyD3DApplication d3dApp;
0060: 
0061:     g_pApp  = &d3dApp;
0062:     g_hInst = hInst;
0063: 
0064:     InitCommonControls();
0065:     if( FAILED( d3dApp.Create( hInst ) ) )
0066:         return 0;
0067: 
0068:     return d3dApp.Run();
0069: }
0070: 
0071: 
0072: // ----------------------------------------------------------------------------
0073: // ----------------------------------------------------------------------------
0074: // パーティクルクラス
0075: // ----------------------------------------------------------------------------
0076: // ----------------------------------------------------------------------------
0077: void CParticle::CreateMesh(LPDIRECT3DDEVICE9 pd3dDevice, TCHAR *filename)
0078: {
0079:     pMesh->Create( pd3dDevice, filename );
0080: }
0081: // ----------------------------------------------------------------------------
0082: void CParticle::RestoreDeviceObjects(LPDIRECT3DDEVICE9 pd3dDevice)
0083: {
0084:     if( pMesh->m_pSysMemMesh ){
0085:         if( FAILED( pMesh->m_pSysMemMesh->CloneMesh(
0086:                         D3DXMESH_NPATCHES, decl,
0087:                         pd3dDevice, &pMesh->m_pLocalMesh ) ) )
0088:             return;
0089:         D3DXComputeNormals( pMesh->m_pLocalMesh, NULL );
0090:         
0091:         // ディスプレースメントマップの座標を指定する
0092:         MESH_VERTEX* pVertices;
0093:         pMesh->m_pLocalMesh->LockVertexBuffer( 0L, (LPVOID*)&pVertices );
0094:         for(DWORD i=0;i<pMesh->m_pLocalMesh->GetNumVertices();i++){
0095:             pVertices[i].du = (0.5f+(FLOAT)id)/PARTICLEMAP_WIDTH;
0096:             pVertices[i].dv = 0.5f/PARTICLEMAP_HEIGHT;
0097:         }
0098:         pMesh->m_pLocalMesh->UnlockVertexBuffer();
0099:     }
0100: }
0101: 
0102: 
0103: 
0104: //-----------------------------------------------------------------------------
0105: // Name: InitParticleMap()
0106: // Desc: テクスチャの初期化
0107: //-----------------------------------------------------------------------------
0108: void CMyD3DApplication::InitParticleMap()
0109: {
0110:     int i, j;
0111:     D3DXVECTOR4 v;
0112:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0113:     D3DVIEWPORT9 oldViewport;
0114: 
0115:     m_pd3dDevice->BeginScene();
0116: 
0117:     //-------------------------------------------------------------------------
0118:     // バックバッファの退避
0119:     //-------------------------------------------------------------------------
0120:     m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0121:     m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0122:     m_pd3dDevice->GetViewport(&oldViewport);
0123: 
0124:     //-------------------------------------------------------------------------
0125:     // レンダリングターゲットの変更
0126:     //-------------------------------------------------------------------------
0127:     m_pd3dDevice->SetRenderTarget(0, m_pParticleSurf);
0128:     m_pd3dDevice->SetDepthStencilSurface(NULL);
0129:     D3DVIEWPORT9 map_viewport = {0,0, PARTICLEMAP_WIDTH, PARTICLEMAP_HEIGHT, 0.0f,1.0f};
0130:     m_pd3dDevice->SetViewport(&map_viewport);
0131:     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, 0x808080, 1.0f, 0L );
0132:     
0133:     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
0134:     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
0135:     m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0136:     m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_DIFFUSE);
0137:     m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);
0138:     m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_DIFFUSE );
0139: 
0140:     //-------------------------------------------------------------------------
0141:     // 位置、速度の初期化
0142:     //-------------------------------------------------------------------------
0143:     LVERTEX Vertex[4] = {
0144:         //        x                  y           z
0145:         {{                0,                  0,0.5,1},},
0146:         {{PARTICLEMAP_WIDTH,                  0,0.5,1},},
0147:         {{                0, PARTICLEMAP_HEIGHT,0.5,1},},
0148:         {{PARTICLEMAP_WIDTH, PARTICLEMAP_HEIGHT,0.5,1},},
0149:     };
0150: 
0151:     m_pEffect->SetTechnique( m_hTechnique );
0152:     m_pEffect->Begin( NULL, 0 );
0153:     m_pEffect->Pass( 1 );
0154:     for(i=0;i<PARTICLEMAP_WIDTH;i++){
0155:     for(j=0;j<2;j++){
0156:         v.x = 0.1f*(frand()-0.5f)+0.5f;
0157:         v.y = 0.1f*(frand()-0.5f)+0.5f;
0158:         v.z = 0.1f*(frand()-0.5f)+0.5f;
0159:         v.w = 1.0f;
0160: 
0161:         Vertex[0].p[0] = (float)i    ;
0162:         Vertex[1].p[0] = (float)(i+1);
0163:         Vertex[2].p[0] = (float)i    ;
0164:         Vertex[3].p[0] = (float)(i+1);
0165:         Vertex[0].p[1] = (float)j    ;
0166:         Vertex[1].p[1] = (float)j    ;
0167:         Vertex[2].p[1] = (float)(j+1);
0168:         Vertex[3].p[1] = (float)(j+1);
0169: 
0170:         m_pEffect->SetVector(m_hvVector, &v );
0171:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, Vertex, sizeof( LVERTEX ) );
0172:     }
0173:     }
0174:     m_pEffect->End();
0175: 
0176: 
0177:     //-------------------------------------------------------------------------
0178:     // 元に戻す
0179:     //-------------------------------------------------------------------------
0180:     m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0181:     m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0182:     m_pd3dDevice->SetViewport(&oldViewport);
0183:     pOldBackBuffer->Release();
0184:     pOldZBuffer->Release();
0185: 
0186:     m_pd3dDevice->EndScene();
0187: }
0188: //-----------------------------------------------------------------------------
0189: // Name: UpdateParticleMap()
0190: // Desc: 
0191: //-----------------------------------------------------------------------------
0192: void CMyD3DApplication::UpdateParticleMap()
0193: {
0194:     D3DXVECTOR4 v;
0195: 
0196:     //-----------------------------------------------------
0197:     // レンダリングターゲットの変更
0198:     //-----------------------------------------------------
0199:     m_pd3dDevice->SetRenderTarget(0, m_pParticleSurf);
0200:     m_pd3dDevice->SetDepthStencilSurface(NULL);
0201:     D3DVIEWPORT9 viewport = {0,0
0202:                     , PARTICLEMAP_WIDTH
0203:                     , PARTICLEMAP_HEIGHT
0204:                     , 0.0f,1.0f};
0205:     m_pd3dDevice->SetViewport(&viewport);
0206: 
0207:     //-----------------------------------------------------
0208:     // 移動
0209:     //-----------------------------------------------------
0210:     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
0211:     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
0212:     m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0213:     m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0214:     m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);
0215: 
0216:     m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX2 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1) );
0217:     m_pEffect->SetTechnique( m_hTechnique );
0218:     m_pEffect->Begin( NULL, 0 );
0219:     m_pEffect->Pass( 2 );
0220:     m_pEffect->SetTexture("ParticleMap", m_pParticleTex );
0221: 
0222:     typedef struct {FLOAT p[4]; FLOAT tu0, tv0; FLOAT tu1, tv1;} T2VERTEX;
0223:     T2VERTEX TVertex[4] = {
0224:         //         x         y  z rhw             tu1                     tv1                        tu2                     tv2
0225:         {{                0, 0, 0, 1}, 0+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 0+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0226:         {{PARTICLEMAP_WIDTH, 0, 0, 1}, 1+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 1+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0227:         {{                0, 2, 0, 1}, 0+0.5f/PARTICLEMAP_WIDTH, 2.5f/PARTICLEMAP_HEIGHT, 0+0.5f/PARTICLEMAP_WIDTH, 3.5f/PARTICLEMAP_HEIGHT,},
0228:         {{PARTICLEMAP_WIDTH, 2, 0, 1}, 1+0.5f/PARTICLEMAP_WIDTH, 2.5f/PARTICLEMAP_HEIGHT, 1+0.5f/PARTICLEMAP_WIDTH, 3.5f/PARTICLEMAP_HEIGHT,},
0229:     };
0230:     m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, TVertex, sizeof( T2VERTEX ) );
0231: 
0232:     //-----------------------------------------------------
0233:     // 跳ね返り
0234:     //-----------------------------------------------------
0235: 
0236:     // 速度調整
0237:     T2VERTEX TVertexV[4] = {
0238:         //         x         y  z rhw             tu1                     tv1                        tu2                     tv2
0239:         {{                0, 1, 0, 1}, 0+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 0+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0240:         {{PARTICLEMAP_WIDTH, 1, 0, 1}, 1+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 1+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0241:         {{                0, 2, 0, 1}, 0+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 0+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0242:         {{PARTICLEMAP_WIDTH, 2, 0, 1}, 1+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 1+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0243:     };
0244:     m_pEffect->Pass( 3 );
0245:     m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, TVertexV, sizeof( T2VERTEX ) );
0246:     // 位置調整
0247:     T2VERTEX TVertexX[4] = {
0248:         //         x         y  z rhw             tu1                     tv1                        tu2                     tv2
0249:         {{                0, 0, 0, 1}, 0+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 0+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0250:         {{PARTICLEMAP_WIDTH, 0, 0, 1}, 1+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 1+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0251:         {{                0, 1, 0, 1}, 0+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 0+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0252:         {{PARTICLEMAP_WIDTH, 1, 0, 1}, 1+0.5f/PARTICLEMAP_WIDTH, 0.5f/PARTICLEMAP_HEIGHT, 1+0.5f/PARTICLEMAP_WIDTH, 1.5f/PARTICLEMAP_HEIGHT,},
0253:     };
0254:     m_pEffect->Pass( 4 );
0255:     m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, TVertexX, sizeof( T2VERTEX ) );
0256: 
0257:     m_pEffect->End();
0258: 
0259:     //-----------------------------------------------------
0260:     // 加速度の再設定
0261:     //-----------------------------------------------------
0262:     m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0263:     m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_DIFFUSE);
0264:     m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);
0265: 
0266:     m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_DIFFUSE );
0267:     LVERTEX ClearVertex[4] = {
0268:         //       x          y z rhw   color
0269:         {{                0,2,0, 1}, 0x807c80,},
0270:         {{PARTICLEMAP_WIDTH,2,0, 1}, 0x807c80,},
0271:         {{                0,3,0, 1}, 0x807c80,},
0272:         {{PARTICLEMAP_WIDTH,3,0, 1}, 0x807c80,},
0273:     };
0274:     m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 2, ClearVertex, sizeof( LVERTEX ) );
0275: 
0276:     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
0277: }
0278: 
0279: //-----------------------------------------------------------------------------
0280: // Name: CMyD3DApplication()
0281: // Desc: Application constructor.   Paired with ~CMyD3DApplication()
0282: //       Member variables should be initialized to a known state here.  
0283: //       The application window has not yet been created and no Direct3D device 
0284: //       has been created, so any initialization that depends on a window or 
0285: //       Direct3D should be deferred to a later stage. 
0286: //-----------------------------------------------------------------------------
0287: CMyD3DApplication::CMyD3DApplication()
0288: {
0289:     m_pDecl                     = NULL;
0290:     m_pMeshBg                   = new CD3DMesh();   
0291:     m_pParticle                 = new CParticleMgr();   
0292:     m_TexWorldScale             = 1.0f;
0293: 
0294:     m_pEffect                   = NULL;
0295:     m_hTechnique                = NULL;
0296:     m_hmWVP                     = NULL;
0297:     m_hvVector                  = NULL;
0298: 
0299:     m_pParticleTex              = NULL;
0300:     m_pParticleSurf             = NULL;
0301:     
0302:     m_dwCreationWidth           = 500;
0303:     m_dwCreationHeight          = 375;
0304:     m_strWindowTitle            = TEXT( "main" );
0305:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0306:     m_bStartFullscreen          = false;
0307:     m_bShowCursorWhenFullscreen = false;
0308: 
0309:     // Create a D3D font using d3dfont.cpp
0310:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0311:     m_bLoadingApp               = TRUE;
0312: 
0313:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0314:     m_fWorldRotX                = -0.10f*D3DX_PI;
0315:     m_fWorldRotY                =  0.25f*D3DX_PI;
0316:     m_zoom                      =  4.5f;
0317: }
0318: 
0319: 
0320: 
0321: 
0322: //-----------------------------------------------------------------------------
0323: // Name: ~CMyD3DApplication()
0324: // Desc: Application destructor.  Paired with CMyD3DApplication()
0325: //-----------------------------------------------------------------------------
0326: CMyD3DApplication::~CMyD3DApplication()
0327: {
0328: }
0329: 
0330: 
0331: 
0332: 
0333: //-----------------------------------------------------------------------------
0334: // Name: OneTimeSceneInit()
0335: // Desc: Paired with FinalCleanup().
0336: //       The window has been created and the IDirect3D9 interface has been
0337: //       created, but the device has not been created yet.  Here you can
0338: //       perform application-related initialization and cleanup that does
0339: //       not depend on a device.
0340: //-----------------------------------------------------------------------------
0341: HRESULT CMyD3DApplication::OneTimeSceneInit()
0342: {
0343:     // TODO: perform one time initialization
0344: 
0345:     // Drawing loading status message until app finishes loading
0346:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0347: 
0348:     m_bLoadingApp = FALSE;
0349: 
0350:     return S_OK;
0351: }
0352: 
0353: 
0354: 
0355: 
0356: 
0357: //-----------------------------------------------------------------------------
0358: // Name: ConfirmDevice()
0359: // Desc: Called during device initialization, this code checks the display device
0360: //       for some minimum set of capabilities
0361: //-----------------------------------------------------------------------------
0362: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
0363: D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat )
0364: {
0365:     UNREFERENCED_PARAMETER( adapterFormat );
0366:     UNREFERENCED_PARAMETER( backBufferFormat );
0367:     UNREFERENCED_PARAMETER( dwBehavior );
0368:     UNREFERENCED_PARAMETER( pCaps );
0369:     
0370:     // 頂点シェーダ
0371:     if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0372:     &&  0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0373:             return E_FAIL;
0374: 
0375:     // ピクセルシェーダ
0376:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0377:         return E_FAIL;
0378:     if(!(pCaps->DevCaps2 & D3DDEVCAPS2_DMAPNPATCH ) ||
0379:        FAILED( m_pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal
0380:                                         , pCaps->DeviceType
0381:                                         ,  adapterFormat
0382:                                         ,  D3DUSAGE_RENDERTARGET|D3DUSAGE_DMAP
0383:                                         ,  D3DRTYPE_TEXTURE
0384:                                         ,  D3DFMT_A32B32G32R32F ) ))
0385:         return E_FAIL;
0386: 
0387:     return S_OK;
0388: }
0389: 
0390: 
0391: 
0392: 
0393: //-----------------------------------------------------------------------------
0394: // Name: InitDeviceObjects()
0395: // Desc: Paired with DeleteDeviceObjects()
0396: //       The device has been created.  Resources that are not lost on
0397: //       Reset() can be created here -- resources in D3DPOOL_MANAGED,
0398: //       D3DPOOL_SCRATCH, or D3DPOOL_SYSTEMMEM.  Image surfaces created via
0399: //       CreateImageSurface are never lost and can be created here.  Vertex
0400: //       shaders and pixel shaders can also be created here as they are not
0401: //       lost on Reset().
0402: //-----------------------------------------------------------------------------
0403: HRESULT CMyD3DApplication::InitDeviceObjects()
0404: {
0405:     HRESULT hr;
0406: 
0407:     // 頂点宣言
0408:     if( FAILED( hr = m_pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl ) ) )
0409:         return DXTRACE_ERR( "CreateVertexDeclaration", hr );
0410:     
0411:     // 飛ぶやつ
0412:     m_pParticle->CreateMesh( m_pd3dDevice, "star.x" );
0413: 
0414:     // 背景
0415:     if( FAILED( hr = m_pMeshBg->Create(m_pd3dDevice, "InBox.x" )))
0416:         return DXTRACE_ERR( "Load Mesh", hr );
0417: 
0418:     // シェーダ
0419:     LPD3DXBUFFER pErr;
0420:     if( FAILED( hr = D3DXCreateEffectFromFile(
0421:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0422:                 0, NULL, &m_pEffect, &pErr ))){
0423:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0424:                     , "ERROR", MB_OK);
0425:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0426:     }
0427:     m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0428:     m_hmWVP = m_pEffect->GetParameterByName( NULL, "mWVP" );
0429:     m_hvVector = m_pEffect->GetParameterByName( NULL, "vVector" );
0430: 
0431:     // Init the font
0432:     m_pFont->InitDeviceObjects( m_pd3dDevice );
0433: 
0434:     return S_OK;
0435: }
0436: 
0437: 
0438: 
0439: 
0440: //-----------------------------------------------------------------------------
0441: // Name: RestoreDeviceObjects()
0442: // Desc: Paired with InvalidateDeviceObjects()
0443: //       The device exists, but may have just been Reset().  Resources in
0444: //       D3DPOOL_DEFAULT and any other device state that persists during
0445: //       rendering should be set here.  Render states, matrices, textures,
0446: //       etc., that don't change during rendering can be set once here to
0447: //       avoid redundant state setting during Render() or FrameMove().
0448: //-----------------------------------------------------------------------------
0449: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0450: {
0451:     D3DXMATRIX m;
0452:     
0453:     if( m_pEffect != NULL ) m_pEffect->OnResetDevice(); // シェーダ
0454:     m_pMeshBg->RestoreDeviceObjects(m_pd3dDevice);      // メッシュ
0455:     m_pParticle->RestoreDeviceObjects(m_pd3dDevice);    // メッシュ
0456:     m_pFont->RestoreDeviceObjects();                    // フォント
0457: 
0458:     //-------------------------------------------------------------------------
0459:     // テクスチャの生成
0460:     //-------------------------------------------------------------------------
0461:     // particle map
0462:     if (FAILED(m_pd3dDevice->CreateTexture(
0463:                       PARTICLEMAP_WIDTH, PARTICLEMAP_HEIGHT, 1
0464:                     , D3DUSAGE_RENDERTARGET|D3DUSAGE_DMAP, D3DFMT_A32B32G32R32F
0465:                     , D3DPOOL_DEFAULT, &m_pParticleTex, NULL)))
0466:         return E_FAIL;
0467:     if (FAILED(m_pParticleTex->GetSurfaceLevel(0, &m_pParticleSurf)))
0468:         return E_FAIL;
0469:     
0470:     this->InitParticleMap();    // 初期化
0471: 
0472:     
0473:     // ライト
0474:     D3DLIGHT9 light;
0475:     D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, -0.5f, -1.0f, 2.0f );
0476:     m_pd3dDevice->SetLight( 0, &light );
0477:     m_pd3dDevice->LightEnable( 0, TRUE );
0478:     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
0479: 
0480:     // テクスチャ
0481:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0482:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0483:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0484:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
0485:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
0486:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
0487:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0488:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0489: 
0490:     // レンダリングステート
0491:     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   FALSE );
0492:     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
0493:     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        D3DZB_TRUE );
0494:     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0x000F0F0F );
0495: 
0496:     // ワールド行列
0497:     D3DXMATRIX matIdentity;
0498:     D3DXMatrixIdentity( &matIdentity );
0499:     m_pd3dDevice->SetTransform( D3DTS_WORLD,  &matIdentity );
0500: 
0501:     // ビュー行列
0502:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_zoom );
0503:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0504:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0505:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0506:     m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_mView );
0507: 
0508:     // 射影行列
0509:     D3DXMATRIX matProj;
0510:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0511:     D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0512:     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_mProj );
0513: 
0514:     return S_OK;
0515: }
0516: 
0517: 
0518: 
0519: 
0520: //-----------------------------------------------------------------------------
0521: // Name: FrameMove()
0522: // Desc: Called once per frame, the call is the entry point for animating
0523: //       the scene.
0524: //-----------------------------------------------------------------------------
0525: HRESULT CMyD3DApplication::FrameMove()
0526: {
0527:     D3DXMATRIX m;
0528:     D3DXMATRIX mCamera;
0529:     D3DXMATRIX matRotY;
0530:     D3DXMATRIX matRotX;
0531: 
0532:     // 入力データの更新
0533:     UpdateInput( &m_UserInput );
0534: 
0535:     // ズーム
0536:     if(m_UserInput.bZ && !m_UserInput.bX)
0537:         m_zoom += 0.01f;
0538:     else if(m_UserInput.bX && !m_UserInput.bZ)
0539:         m_zoom -= 0.01f;
0540: 
0541:     // カメラを動かす
0542:     if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0543:         m_fWorldRotY += m_fElapsedTime;
0544:     else if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0545:         m_fWorldRotY -= m_fElapsedTime;
0546: 
0547:     if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0548:         m_fWorldRotX += m_fElapsedTime;
0549:     else if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0550:         m_fWorldRotX -= m_fElapsedTime;
0551: 
0552:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0553:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0554:     D3DXMatrixMultiply( &mCamera, &matRotY, &matRotX );
0555: 
0556:     // ビュー行列の再設定
0557:     D3DXVECTOR3 vEye    = D3DXVECTOR3( 0.0f, 0.0f, -m_zoom );
0558:     D3DXVECTOR3 vLookat = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0559:     D3DXVECTOR3 vUp     = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0560:     D3DXMatrixLookAtLH( &m, &vEye, &vLookat, &vUp );
0561: 
0562:     m_mView = mCamera * m;
0563: 
0564:     return S_OK;
0565: }
0566: 
0567: 
0568: 
0569: 
0570: //-----------------------------------------------------------------------------
0571: // Name: UpdateInput()
0572: // Desc: Update the user input.  Called once per frame 
0573: //-----------------------------------------------------------------------------
0574: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0575: {
0576:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0577:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0578:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0579:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0580: 
0581:     pUserInput->bZ = ( m_bActive && (GetAsyncKeyState( 'Z' ) & 0x8000) == 0x8000 );
0582:     pUserInput->bX = ( m_bActive && (GetAsyncKeyState( 'X' ) & 0x8000) == 0x8000 );
0583: }
0584: 
0585: 
0586: 
0587: //-----------------------------------------------------------------------------
0588: // Name: Render()
0589: // Desc: Called once per frame, the call is the entry point for 3d
0590: //       rendering. This function sets up render states, clears the
0591: //       viewport, and renders the scene.
0592: //-----------------------------------------------------------------------------
0593: HRESULT CMyD3DApplication::Render()
0594: {
0595:     D3DXMATRIX matIdentity;
0596:     D3DXMATRIX m, mVP;
0597:     D3DXMATRIX mW;
0598:     D3DXMATRIX mVisibleProj;
0599:     D3DXVECTOR4 v;
0600:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0601:     D3DVIEWPORT9 oldViewport;
0602: 
0603:     // 描画開始
0604:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0605:     {
0606:         //---------------------------------------------------------------------
0607:         // バックバッファの退避
0608:         //---------------------------------------------------------------------
0609:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0610:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0611:         m_pd3dDevice->GetViewport(&oldViewport);
0612: 
0613:         //---------------------------------------------------------------------
0614:         // パーティクルの移動
0615:         //---------------------------------------------------------------------
0616:         UpdateParticleMap( );
0617: 
0618:         //-------------------------------------------------
0619:         // バックバッファを元に戻す
0620:         //-------------------------------------------------
0621:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0622:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0623:         m_pd3dDevice->SetViewport(&oldViewport);
0624:         pOldBackBuffer->Release();
0625:         pOldZBuffer->Release();
0626: 
0627:         m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
0628:                             0x404080, 1.0f, 0L );
0629: 
0630:         //---------------------------------------------------------------------
0631:         // 背景の描画
0632:         //---------------------------------------------------------------------
0633:         m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_USEW );
0634:         m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
0635:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0636:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_DIFFUSE);
0637: 
0638:         D3DXMatrixIdentity( &matIdentity );
0639:         m_pd3dDevice->SetTransform( D3DTS_WORLD,  &matIdentity );
0640:         m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_mView );
0641:         m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_mProj );
0642: 
0643:         m_pMeshBg->Render( m_pd3dDevice );
0644: 
0645:         m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
0646:         m_pd3dDevice->SetSamplerState (0,D3DSAMP_MAGFILTER ,    D3DTEXF_LINEAR );
0647:         m_pd3dDevice->SetSamplerState (0,D3DSAMP_MINFILTER ,    D3DTEXF_LINEAR );
0648:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0649:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0650: 
0651:         //-------------------------------------------------------------------------
0652:         // ディスプレースメントマッピングを使ってパーティクルを描画する
0653:         //-------------------------------------------------------------------------
0654:         m_pEffect->SetTechnique( m_hTechnique );
0655:         m_pEffect->Begin( NULL, 0 );
0656:         m_pEffect->Pass( 0 );
0657: 
0658:         // 変換行列
0659:         m = m_mView * m_mProj;
0660:         m_pEffect->SetMatrix( m_hmWVP, &m );
0661:         // テクスチャ
0662:         m_pEffect->SetTexture("DecaleMap", m_pParticle->GetObj(0)->GetMesh()->m_pTextures[0] );
0663: 
0664:         m_pd3dDevice->SetNPatchMode(1); // ディスプレースメントマップを使用する
0665:         m_pd3dDevice->SetVertexDeclaration( m_pDecl );
0666:         m_pd3dDevice->SetTexture(D3DDMAPSAMPLER, m_pParticleTex);
0667:         for(DWORD i=0;i<PARTICLE_MAX;i++){
0668:             m_pParticle->GetObj(i)->GetMesh()->Render( m_pd3dDevice );
0669:         }
0670:         m_pd3dDevice->SetNPatchMode(0); // ディスプレースメントをやめる
0671:         m_pEffect->End();
0672: 
0673: #if 1 // デバッグ用にテクスチャを見る
0674:         {
0675:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0676:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0677:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0678:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
0679:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
0680:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0681:         m_pd3dDevice->SetVertexShader(NULL);
0682:         m_pd3dDevice->SetPixelShader(0);
0683:         for(DWORD l = 0; l < 1; l++){
0684:             const float scale =128.0f;
0685:             FLOAT X = (FLOAT)this->m_rcWindowClient.right;
0686:             FLOAT Y = (FLOAT)this->m_rcWindowClient.bottom;
0687:             typedef struct {FLOAT p[4]; FLOAT tu, tv;} TVERTEX;
0688:             TVERTEX Vertex[4] = {
0689:                 //  x       y    z rhw tu tv
0690:                 {X-(l+1)*scale,Y-scale,0, 1, 0.0f, 0.0f,},
0691:                 {X-(l+0)*scale,Y-scale,0, 1, 1.0f, 0.0f,},
0692:                 {X-(l+0)*scale,Y      ,0, 1, 1.0f, 1.0f,},
0693:                 {X-(l+1)*scale,Y      ,0, 1, 0.0f, 1.0f,},
0694:             };
0695:             switch(l){
0696:             case 0: m_pd3dDevice->SetTexture( 0, m_pParticleTex );  break;
0697:             }
0698:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0699:         }
0700:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0701:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0702:         }
0703: #endif
0704: 
0705:         // Render stats and help text  
0706:         RenderText();
0707: 
0708:         // End the scene.
0709:         m_pd3dDevice->EndScene();
0710:     }
0711: 
0712:     return S_OK;
0713: }
0714: 
0715: 
0716: 
0717: 
0718: //-----------------------------------------------------------------------------
0719: // Name: RenderText()
0720: // Desc: Renders stats and help text to the scene.
0721: //-----------------------------------------------------------------------------
0722: HRESULT CMyD3DApplication::RenderText()
0723: {
0724:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
0725:     TCHAR szMsg[MAX_PATH] = TEXT("");
0726: 
0727:     // Output display stats
0728:     FLOAT fNextLine = 40.0f; 
0729: 
0730:     lstrcpy( szMsg, m_strDeviceStats );
0731:     fNextLine -= 20.0f;
0732:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0733: 
0734:     lstrcpy( szMsg, m_strFrameStats );
0735:     fNextLine -= 20.0f;
0736:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0737: 
0738:     return S_OK;
0739: }
0740: 
0741: 
0742: 
0743: 
0744: //-----------------------------------------------------------------------------
0745: // Name: MsgProc()
0746: // Desc: Overrrides the main WndProc, so the sample can do custom message
0747: //       handling (e.g. processing mouse, keyboard, or menu commands).
0748: //-----------------------------------------------------------------------------
0749: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg, WPARAM wParam,
0750:                                     LPARAM lParam )
0751: {
0752:     switch( msg )
0753:     {
0754:         case WM_PAINT:
0755:         {
0756:             if( m_bLoadingApp )
0757:             {
0758:                 // Draw on the window tell the user that the app is loading
0759:                 // TODO: change as needed
0760:                 HDC hDC = GetDC( hWnd );
0761:                 TCHAR strMsg[MAX_PATH];
0762:                 wsprintf( strMsg, TEXT("Loading... Please wait") );
0763:                 RECT rct;
0764:                 GetClientRect( hWnd, &rct );
0765:                 DrawText( hDC, strMsg, -1, &rct, DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0766:                 ReleaseDC( hWnd, hDC );
0767:             }
0768:             break;
0769:         }
0770:     }
0771: 
0772:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0773: }
0774: 
0775: 
0776: 
0777: 
0778: //-----------------------------------------------------------------------------
0779: // Name: InvalidateDeviceObjects()
0780: // Desc: Invalidates device objects.  Paired with RestoreDeviceObjects()
0781: //-----------------------------------------------------------------------------
0782: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0783: {
0784:     // TODO: Cleanup any objects created in RestoreDeviceObjects()
0785:     SAFE_RELEASE(m_pParticleSurf);
0786:     SAFE_RELEASE(m_pParticleTex);
0787: 
0788:     // mesh
0789:     m_pMeshBg->InvalidateDeviceObjects();
0790:     m_pParticle->InvalidateDeviceObjects();
0791: 
0792:     // Shader
0793:     if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
0794: 
0795:     m_pFont->InvalidateDeviceObjects();
0796: 
0797:     return S_OK;
0798: }
0799: 
0800: 
0801: 
0802: 
0803: //-----------------------------------------------------------------------------
0804: // Name: DeleteDeviceObjects()
0805: // Desc: Paired with InitDeviceObjects()
0806: //       Called when the app is exiting, or the device is being changed,
0807: //       this function deletes any device dependent objects.  
0808: //-----------------------------------------------------------------------------
0809: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0810: {
0811:     // TODO: Cleanup any objects created in InitDeviceObjects()
0812: 
0813:     // Mesh
0814:     m_pMeshBg->Destroy();
0815:     m_pParticle->Destroy();
0816:     
0817:     SAFE_RELEASE( m_pDecl );
0818: 
0819:     SAFE_RELEASE( m_pEffect );    // Shader
0820: 
0821:     m_pFont->DeleteDeviceObjects();
0822: 
0823:     return S_OK;
0824: }
0825: 
0826: 
0827: 
0828: 
0829: //-----------------------------------------------------------------------------
0830: // Name: FinalCleanup()
0831: // Desc: Paired with OneTimeSceneInit()
0832: //       Called before the app exits, this function gives the app the chance
0833: //       to cleanup after itself.
0834: //-----------------------------------------------------------------------------
0835: HRESULT CMyD3DApplication::FinalCleanup()
0836: {
0837:     // TODO: Perform any final cleanup needed
0838: 
0839:     // Mesh
0840:     SAFE_DELETE( m_pMeshBg );
0841:     
0842:     SAFE_DELETE( m_pParticle );
0843: 
0844:     // Cleanup D3D font
0845:     SAFE_DELETE( m_pFont );
0846: 
0847:     return S_OK;
0848: }
0849: 
0850: 
0851: 
0852: 
0853: