0001: //-------------------------------------------------------------
0002: // File: main.cpp
0003: //
0004: // Desc: 重心計算
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: 
0030: #define frand() ((float)rand()/(float)RAND_MAX)
0031: 
0032: //-------------------------------------------------------------
0033: // Globals variables and definitions(グローバル変数と定義)
0034: //-------------------------------------------------------------
0035: CMyD3DApplication* g_pApp  = NULL;
0036: HINSTANCE          g_hInst = NULL;
0037: 
0038: typedef struct {
0039:     FLOAT       p[4];
0040:     FLOAT       tu, tv;
0041: } TVERTEX;
0042: 
0043: //-------------------------------------------------------------
0044: // Name: WinMain()
0045: // Desc: Entry point to the program. Initializes everything, and goes into a
0046: //       message-processing loop. Idle time is used to render the scene.
0047: //       (メイン関数)
0048: //-------------------------------------------------------------
0049: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0050: {
0051:     CMyD3DApplication d3dApp;
0052: 
0053:     g_pApp  = &d3dApp;
0054:     g_hInst = hInst;
0055: 
0056:     InitCommonControls();
0057:     if( FAILED( d3dApp.Create( hInst ) ) )
0058:         return 0;
0059: 
0060:     return d3dApp.Run();
0061: }
0062: 
0063: 
0064: 
0065: 
0066: //-------------------------------------------------------------
0067: // Name: CMyD3DApplication()
0068: // Desc: Application constructor.   Paired with ~CMyD3DApplication()
0069: //       Member variables should be initialized to a known state here.  
0070: //       The application window has not yet been created and no Direct3D device 
0071: //       has been created, so any initialization that depends on a window or 
0072: //       Direct3D should be deferred to a later stage. 
0073: //       (アプリケーションのコンストラクタ)
0074: //-------------------------------------------------------------
0075: CMyD3DApplication::CMyD3DApplication()
0076: {
0077:     time_t t;
0078:     
0079:     time( &t );
0080:     srand( t );
0081: 
0082:     m_bDrop = 0;
0083:     m_fDrop_x = 0;
0084:     m_fDrop_y = 0;
0085: 
0086:     m_pMapZ                     = NULL;
0087:     m_pBackBufferTex            = NULL;
0088:     m_pBackBufferSurf           = NULL;
0089:     m_pDotTex                   = NULL;
0090:     m_pTargetTex                = NULL;
0091:     m_pTex[0]                   = NULL;
0092:     m_pTex[1]                   = NULL;
0093:     m_pTex[2]                   = NULL;
0094:     m_pTex[3]                   = NULL;
0095:     m_pTex[4]                   = NULL;
0096:     m_pSurf[0]                  = NULL;
0097:     m_pSurf[1]                  = NULL;
0098:     m_pSurf[2]                  = NULL;
0099:     m_pSurf[3]                  = NULL;
0100:     m_pSurf[4]                  = NULL;
0101: 
0102:     m_pEffect                   = NULL;
0103:     m_hTechnique                = NULL;
0104: 
0105:     m_fWorldRotX                = -0.0f;
0106:     m_fWorldRotY                = D3DX_PI*0.5f;
0107:     m_fViewZoom                 = 27.0f;
0108: 
0109:     m_dwCreationWidth           = 512;
0110:     m_dwCreationHeight          = 512;
0111:     m_strWindowTitle            = TEXT( "main" );
0112:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0113:     m_bStartFullscreen          = false;
0114:     m_bShowCursorWhenFullscreen = false;
0115:  
0116:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0117:     m_bLoadingApp               = TRUE;
0118: 
0119:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0120: }
0121: 
0122: 
0123: 
0124: 
0125: //-------------------------------------------------------------
0126: // Name: ~CMyD3DApplication()
0127: // Desc: Application destructor.  Paired with CMyD3DApplication()
0128: //       (デストラクタ)
0129: //-------------------------------------------------------------
0130: CMyD3DApplication::~CMyD3DApplication()
0131: {
0132: }
0133: 
0134: 
0135: 
0136: 
0137: //-------------------------------------------------------------
0138: // Name: OneTimeSceneInit()
0139: // Desc: Paired with FinalCleanup().
0140: //       The window has been created and the IDirect3D9 interface has been
0141: //       created, but the device has not been created yet.  Here you can
0142: //       perform application-related initialization and cleanup that does
0143: //       not depend on a device.
0144: //      (一度だけ行う初期化
0145: //      ウィンドウの初期化やIDirect3D9の初期化は終わってます。
0146: //      ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。)
0147: //-------------------------------------------------------------
0148: HRESULT CMyD3DApplication::OneTimeSceneInit()
0149: {
0150:     // Drawing loading status message until app finishes loading
0151:     // (ローディングメッセージを表示する)
0152:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0153: 
0154:     m_bLoadingApp = FALSE;
0155: 
0156:     return S_OK;
0157: }
0158: 
0159: 
0160: 
0161: 
0162: //-------------------------------------------------------------
0163: // Name: ConfirmDevice()
0164: // Desc: Called during device initialization, this code checks the device
0165: //       for some minimum set of capabilities
0166: //       (初期化の時に呼ばれます。必要な能力をチェックします。)
0167: //-------------------------------------------------------------
0168: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0169:                      DWORD dwBehavior,    D3DFORMAT Format )
0170: {
0171:     UNREFERENCED_PARAMETER( Format );
0172:     UNREFERENCED_PARAMETER( dwBehavior );
0173:     UNREFERENCED_PARAMETER( pCaps );
0174:     
0175: 
0176:     // No fallback, so need ps2.0
0177:     // (ピクセルシェーダバージョンチェック)
0178:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0179:         return E_FAIL;
0180: 
0181:     // If device doesn't support 1.1 vertex shaders in HW, switch to SWVP.
0182:     // 頂点シェーダバージョンが上位かソフトウェア頂点処理
0183:     if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0184:     &&  0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0185:             return E_FAIL;
0186: 
0187:     return S_OK;
0188: }
0189: 
0190: 
0191: //-------------------------------------------------------------
0192: // Name: InitDeviceObjects()
0193: // Desc: Paired with DeleteDeviceObjects()
0194: //       The device has been created.  Resources that are not lost on
0195: //       Reset() can be created here -- resources in D3DPOOL_MANAGED,
0196: //       D3DPOOL_SCRATCH, or D3DPOOL_SYSTEMMEM.  Image surfaces created via
0197: //       CreateImageSurface are never lost and can be created here.  Vertex
0198: //       shaders and pixel shaders can also be created here as they are not
0199: //       lost on Reset().
0200: //      (デバイスが生成された後の初期化をします。
0201: //      フレームバッファフォーマットやデバイスの種類が変わった後に通過します。
0202: //      ここで確保したメモリはDeleteDeviceObjects()で開放します)
0203: //-------------------------------------------------------------
0204: HRESULT CMyD3DApplication::InitDeviceObjects()
0205: {
0206:     HRESULT hr;
0207: 
0208:     // Load objects (モデルの読み込み)
0209:     D3DXCreateTextureFromFile(m_pd3dDevice, "dot.bmp",      &m_pDotTex);
0210:     D3DXCreateTextureFromFile(m_pd3dDevice, "target.tga",   &m_pTargetTex);
0211: 
0212:     // Create textures
0213:     // Create the effect(シェーダの読み込み)
0214:     LPD3DXBUFFER pErr;
0215:     if( FAILED( hr = D3DXCreateEffectFromFile(
0216:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0217:                 D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0218:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0219:                     , "ERROR", MB_OK);
0220:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0221:     }
0222:     m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0223: 
0224:     // Init the font(フォント)
0225:     m_pFont->InitDeviceObjects( m_pd3dDevice );
0226: 
0227:     return S_OK;
0228: }
0229: 
0230: //-------------------------------------------------------------
0231: // Name: RestoreDeviceObjects()
0232: // Desc: Paired with InvalidateDeviceObjects()
0233: //       The device exists, but may have just been Reset().  Resources in
0234: //       D3DPOOL_DEFAULT and any other device state that persists during
0235: //       rendering should be set here.  Render states, matrices, textures,
0236: //       etc., that don't change during rendering can be set once here to
0237: //       avoid redundant state setting during Render() or FrameMove().
0238: //       (画面のサイズが変更された時等に呼ばれます。
0239: //       確保したメモリはInvalidateDeviceObjects()で開放します。)
0240: //-------------------------------------------------------------
0241: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0242: {
0243:     DWORD i;
0244: 
0245:     // Setup a material (質感の設定)
0246:     D3DMATERIAL9 mtrl;
0247:     D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0248:     m_pd3dDevice->SetMaterial( &mtrl );
0249: 
0250:     // Set miscellaneous render states(レンダリング環境の設定)
0251:     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   FALSE );
0252:     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
0253:     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
0254:     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0x000F0F0F );
0255:     
0256:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0257:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0258:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0259:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
0260:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
0261:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
0262:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0263:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0264:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0265:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0266: 
0267:     // World transform to identity (ワールド行列)
0268:     D3DXMATRIX matIdentity;
0269:     D3DXMatrixIdentity( &m_mWorld );
0270: 
0271:     // Set up the view parameters for the camera(ビュー行列)
0272:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0273:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0274:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0275:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0276: 
0277:     // Set the camera projection matrix(射影行列)
0278:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0279:     D3DXMatrixPerspectiveFovLH( &m_mProj, 0.47108996f, fAspect, 1.0f, 100.0f );
0280:     
0281: 
0282:     // Tetrahedron Matrix
0283:     D3DXVECTOR3 lookat[4];
0284:     D3DXVECTOR3 center;
0285:     D3DXMATRIX mRot;
0286:     float theta = 109.0f * D3DX_PI / 180.0f;
0287:     center = D3DXVECTOR3(0,0,0);
0288:     lookat[0] = D3DXVECTOR3(0,0,1);
0289:     lookat[1] = D3DXVECTOR3(sinf(theta)*cosf(0.0*D3DX_PI/3.0f),sinf(theta)*sinf(0.0*D3DX_PI/3.0f), cosf(theta));
0290:     lookat[2] = D3DXVECTOR3(sinf(theta)*cosf(2.0*D3DX_PI/3.0f),sinf(theta)*sinf(2.0*D3DX_PI/3.0f), cosf(theta));
0291:     lookat[3] = D3DXVECTOR3(sinf(theta)*cosf(4.0*D3DX_PI/3.0f),sinf(theta)*sinf(4.0*D3DX_PI/3.0f), cosf(theta));
0292:     
0293:     // Create the stencil buffer to be used with the paraboloid textures(深度マップの生成)
0294:     if (FAILED(m_pd3dDevice->CreateDepthStencilSurface(MAP_SIZE, MAP_SIZE, 
0295:         D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pMapZ, NULL)))
0296:         return E_FAIL;
0297: 
0298:     if (FAILED(m_pd3dDevice->CreateTexture( 512, 512, 1, 
0299:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pBackBufferTex, NULL)))
0300:         return E_FAIL;
0301:     if (FAILED(m_pBackBufferTex->GetSurfaceLevel(0, &m_pBackBufferSurf)))
0302:         return E_FAIL;
0303:     m_bClear = TRUE;
0304: 
0305:     DWORD size = 256;
0306:     for( i = 0; i < 5; i++ )
0307:     {
0308:         if (FAILED(m_pd3dDevice->CreateTexture( size, size, 1, 
0309:             D3DUSAGE_RENDERTARGET, D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &m_pTex[i], NULL)))
0310:             return E_FAIL;
0311:         if (FAILED(m_pTex[i]->GetSurfaceLevel(0, &m_pSurf[i])))
0312:             return E_FAIL;
0313:         size /= 4;
0314:     }
0315: 
0316:     // Restore effect object
0317:     m_pEffect->OnResetDevice();
0318: 
0319:     // Restore the font(フォント)
0320:     m_pFont->RestoreDeviceObjects();
0321: 
0322:     return S_OK;
0323: }
0324: 
0325: 
0326: //-------------------------------------------------------------
0327: // Name: FrameMove()
0328: // Desc: Called once per frame, the call is the entry point for animating
0329: //       the scene.
0330: //       (毎フレーム呼ばれます。アニメの処理などを行います。)
0331: //-------------------------------------------------------------
0332: HRESULT CMyD3DApplication::FrameMove()
0333: {
0334:     // Update user input state(入力データの更新)
0335:     UpdateInput( &m_UserInput );
0336: 
0337:     //---------------------------------------------------------
0338:     // Update the world state according to user input(入力に応じて座標系を更新する)
0339:     //---------------------------------------------------------
0340:     // Rotation(回転)
0341:     D3DXMATRIX matRotY;
0342:     D3DXMATRIX matRotX;
0343: 
0344:     if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0345:         m_fWorldRotY += m_fElapsedTime;
0346:     else
0347:     if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0348:         m_fWorldRotY -= m_fElapsedTime;
0349: 
0350:     if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0351:         m_fWorldRotX += m_fElapsedTime;
0352:     else
0353:     if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0354:         m_fWorldRotX -= m_fElapsedTime;
0355: 
0356:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0357:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0358: 
0359:     D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0360:     
0361:     //---------------------------------------------------------
0362:     // Update view matrix(ビュー行列の設定)
0363:     //---------------------------------------------------------
0364:     // Zoom(ズーム)
0365:     if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0366:         m_fViewZoom += m_fElapsedTime;
0367:     else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0368:         m_fViewZoom -= m_fElapsedTime;
0369: 
0370:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0371:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.3f, 0.0f );
0372:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0373:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0374: 
0375:     return S_OK;
0376: }
0377: //-------------------------------------------------------------
0378: // Name: UpdateInput()
0379: // Desc: Update the user input.  Called once per frame 
0380: //       (入力データを更新する)
0381: //-------------------------------------------------------------
0382: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0383: {
0384:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0385:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0386:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0387:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0388:     
0389: }
0390: 
0391: 
0392: 
0393: 
0394: 
0395: //-------------------------------------------------------------
0396: // Name: Render()
0397: // Desc: Called once per frame, the call is the entry point for 3d
0398: //       rendering. This function sets up render states, clears the
0399: //       viewport, and renders the scene.
0400: //       (画面を描画する)
0401: //-------------------------------------------------------------
0402: HRESULT CMyD3DApplication::Render()
0403: {
0404:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0405:     D3DVIEWPORT9 oldViewport;
0406:     D3DXMATRIX m, mT, mR, mW, mView, mProj;
0407:     DWORD i;
0408:     D3DXVECTOR4 v;
0409:     D3DMATERIAL9 *pMtrl;
0410: 
0411:     // Begin the scene(描画開始)
0412:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0413:     {
0414:         D3DVIEWPORT9 viewport = {0, 0   // x,y(左上の座標)
0415:                         , 512, 512          // width, height(幅,高さ)
0416:                         , 0.0f,1.0f};   // near, far(前面、後面)
0417: 
0418:         //-------------------------------------------------
0419:         // Backup rendering target(レンダリングターゲットの保存)
0420:         //-------------------------------------------------
0421:         m_pd3dDevice->GetRenderTarget( 0, &pOldBackBuffer );
0422:         m_pd3dDevice->GetDepthStencilSurface( &pOldZBuffer );
0423:         m_pd3dDevice->GetViewport( &oldViewport );
0424: 
0425:         m_pd3dDevice->SetRenderTarget( 0, m_pBackBufferSurf );
0426:         m_pd3dDevice->SetDepthStencilSurface( NULL );
0427:         m_pd3dDevice->SetViewport( &viewport );
0428: 
0429:         // Clear the scene
0430:         if(m_bClear)
0431:         {
0432:             m_bClear = FALSE;
0433:             m_pd3dDevice->Clear(0L, NULL
0434:                 , D3DCLEAR_TARGET
0435:                 , 0x00000000, 1.0f, 0L);
0436:         }
0437: 
0438:         if(m_bDrop)
0439:         {// 点を打つ
0440:             m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
0441:             m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
0442:             m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_ONE);
0443:             m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0444:             m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0445:             m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0446:             m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0447:             m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0448:             m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0449:             float r = 16;
0450:             TVERTEX Vertex[4] = {
0451:                 //    x          y         z rhw tu tv
0452:                 {m_fDrop_x-r, m_fDrop_y-r, 0, 1, 0, 0,},
0453:                 {m_fDrop_x+r, m_fDrop_y-r, 0, 1, 1, 0,},
0454:                 {m_fDrop_x+r, m_fDrop_y+r, 0, 1, 1, 1,},
0455:                 {m_fDrop_x-r, m_fDrop_y+r, 0, 1, 0, 1,},
0456:             };
0457:             m_pd3dDevice->SetTexture( 0, m_pDotTex );
0458:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0459:             m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
0460:         }
0461:         
0462:         if( m_pEffect != NULL ) 
0463:         {
0464:             m_pEffect->SetTechnique( m_hTechnique );
0465:             m_pEffect->Begin( NULL, 0 );
0466: 
0467:             // 0 へ
0468:             m_pEffect->Pass( 0 );
0469:             m_pd3dDevice->SetRenderTarget( 0, m_pSurf[0] );
0470:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0471:             TVERTEX Vertex[4] = {
0472:                 // x   y  z      tu            tv
0473:                 { -1,  1, 0,  0+1.0/512.0, 0+1.0/512.0,},
0474:                 {  1,  1, 0,  1+1.0/512.0, 0+1.0/512.0,},
0475:                 {  1, -1, 0,  1+1.0/512.0, 1+1.0/512.0,},
0476:                 { -1, -1, 0,  0+1.0/512.0, 1+1.0/512.0,},
0477:             };
0478:             m_pEffect->SetTexture( "LinearTex",  m_pBackBufferTex );
0479:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0480: 
0481:             // 1から3へ
0482:             m_pEffect->Pass( 1 );
0483:             float size = 256.0;
0484:             for( i = 1; i < 5; i++ )
0485:             {
0486:                 m_pEffect->SetFloat("inv_size", 1.0/size);
0487:                 m_pd3dDevice->SetRenderTarget( 0, m_pSurf[i] );
0488:                 TVERTEX Vertex[4] = {
0489:                     // x   y  z   tu tv
0490:                     { -1,  1, 0,  0, 0,},
0491:                     {  1,  1, 0,  1, 0,},
0492:                     {  1, -1, 0,  1, 1,},
0493:                     { -1, -1, 0,  0, 1,},
0494:                 };
0495:                 m_pEffect->SetTexture( "PointTex",  m_pTex[i-1] );
0496:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0497: 
0498:                 size /= 4.0;
0499:             }
0500:             m_pEffect->End();
0501:         }
0502: 
0503:         //-----------------------------------------------------
0504:         // Restore render target
0505:         // (レンダリングターゲットを元に戻す)
0506:         //-----------------------------------------------------
0507:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
0508:         m_pd3dDevice->SetRenderTarget(1, NULL);
0509:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
0510:         m_pd3dDevice->SetViewport(&oldViewport);
0511:         pOldBackBuffer->Release();
0512:         pOldZBuffer->Release();
0513: 
0514: 
0515:         {// 全画面表示
0516:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0517:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0518:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0519:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0520:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
0521:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0522:         TVERTEX Vertex[4] = {
0523:             //    x                             y         z rhw tu tv
0524:             {  0, 0, 0, 1, 0, 0,},
0525:             {(FLOAT)m_d3dsdBackBuffer.Width, 0, 0, 1, 1, 0,},
0526:             {(FLOAT)m_d3dsdBackBuffer.Width, (FLOAT)m_d3dsdBackBuffer.Height, 0, 1, 1, 1,},
0527:             {  0, (FLOAT)m_d3dsdBackBuffer.Height, 0, 1, 0, 1,},
0528:         };
0529:         m_pd3dDevice->SetTexture( 0, m_pBackBufferTex );
0530:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0531:         }
0532:         
0533:         // 重心の表示
0534:         if( m_pEffect != NULL ) 
0535:         {
0536:             m_pEffect->SetTechnique( m_hTechnique );
0537:             m_pEffect->Begin( NULL, 0 );
0538:             m_pEffect->Pass( 2 );
0539: 
0540:             m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
0541:             m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
0542:             m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
0543: 
0544:             m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0545:             TVERTEX Vertex[4] = {
0546:             //                  x                                  y          z rhw tu tv
0547:             {                             0,                               0, 0, 1, 0.5,        0.5,},
0548:             {(FLOAT)m_d3dsdBackBuffer.Width,                               0, 0, 1, 0.5+512/32, 0.5,},
0549:             {(FLOAT)m_d3dsdBackBuffer.Width, (FLOAT)m_d3dsdBackBuffer.Height, 0, 1, 0.5+512/32, 0.5+512/32,},
0550:             {                             0, (FLOAT)m_d3dsdBackBuffer.Height, 0, 1, 0.5,        0.5+512/32,},
0551:             };
0552:             m_pEffect->SetTexture( "PointTex",  m_pTex[4] );
0553:             m_pEffect->SetTexture( "LinearTex", m_pTargetTex );
0554:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0555: 
0556:             m_pEffect->End();
0557:             m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
0558:         }
0559: 
0560: 
0561: 
0562:         // Render stats and help text(ヘルプの表示)
0563:         RenderText();
0564: 
0565: #ifdef _DEBUG // Textures are displaying when debugging, (デバッグ用にテクスチャを表示する)
0566:         {
0567:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0568:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0569:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0570:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0571:         m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
0572:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0573:         float scale = 64.0f;
0574:         for(DWORD i=0; i<5; i++){
0575:             TVERTEX Vertex[4] = {
0576:                 //    x                             y         z rhw tu tv
0577:                 {(i+0)*scale, (FLOAT)m_d3dsdBackBuffer.Height-scale, 0, 1, 0, 0,},
0578:                 {(i+1)*scale, (FLOAT)m_d3dsdBackBuffer.Height-scale, 0, 1, 1, 0,},
0579:                 {(i+1)*scale, (FLOAT)m_d3dsdBackBuffer.Height-    0, 0, 1, 1, 1,},
0580:                 {(i+0)*scale, (FLOAT)m_d3dsdBackBuffer.Height-    0, 0, 1, 0, 1,},
0581:             };
0582:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pTex[0] );
0583:             if(1==i) m_pd3dDevice->SetTexture( 0, m_pTex[1] );
0584:             if(2==i) m_pd3dDevice->SetTexture( 0, m_pTex[2] );
0585:             if(3==i) m_pd3dDevice->SetTexture( 0, m_pTex[3] );
0586:             if(4==i) m_pd3dDevice->SetTexture( 0, m_pTex[4] );
0587: 
0588:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0589:         }
0590:         }
0591: #endif      
0592: 
0593:         // End the scene.(描画の終了)
0594:         m_pd3dDevice->EndScene();
0595:     }
0596: 
0597:     return S_OK;
0598: }
0599: 
0600: 
0601: 
0602: 
0603: //-------------------------------------------------------------
0604: // Name: RenderText()
0605: // Desc: Draw the help & statistics for running sample
0606: //       (状態やヘルプを画面に表示する)
0607: //-------------------------------------------------------------
0608: HRESULT CMyD3DApplication::RenderText()
0609: {
0610:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
0611:     TCHAR szMsg[MAX_PATH] = TEXT("");
0612:     FLOAT fNextLine;
0613: 
0614:     // Draw help
0615:     fNextLine = 0; 
0616: 
0617:     lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
0618:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0619:     fNextLine += 20.0f;
0620: 
0621:     lstrcpy( szMsg, TEXT("Press 'A' to change shader") );
0622:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0623:     fNextLine += 20.0f;
0624: 
0625:     // Output statistics
0626:     lstrcpy( szMsg, m_strDeviceStats );
0627:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0628:     fNextLine += 20.0f;
0629:     lstrcpy( szMsg, m_strFrameStats );
0630:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0631:     fNextLine += 20.0f;
0632:     
0633:     return S_OK;
0634: }
0635: 
0636: 
0637: // ----------------------------------------------------------------------------
0638: void CMyD3DApplication::SetDrop(float x , float y)
0639: {
0640:     m_bDrop = 100;
0641:     m_fDrop_x = x;
0642:     m_fDrop_y = y;
0643: }
0644: // ----------------------------------------------------------------------------
0645: void CMyD3DApplication::ResetDrop()
0646: {
0647:     m_bDrop = 0;
0648: }
0649: // ----------------------------------------------------------------------------
0650: int CMyD3DApplication::IsDrop()
0651: {
0652:     return m_bDrop;
0653: }
0654: 
0655: 
0656: //-------------------------------------------------------------
0657: // Name: MsgProc()
0658: // Desc: Overrrides the main WndProc, so the sample can do custom message
0659: //       handling (e.g. processing mouse, keyboard, or menu commands).
0660: //       (WndProc をオーバーライドしたもの)
0661: //-------------------------------------------------------------
0662: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
0663:                                  WPARAM wParam, LPARAM lParam )
0664: {
0665:     long x, y;
0666:     switch( msg )
0667:     {
0668:         case WM_PAINT:
0669:         {
0670:             if( m_bLoadingApp )
0671:             {
0672:                 // Loading message(ロード中)
0673:                 HDC hDC = GetDC( hWnd );
0674:                 TCHAR strMsg[MAX_PATH];
0675:                 wsprintf(strMsg, TEXT("Loading... Please wait"));
0676:                 RECT rct;
0677:                 GetClientRect( hWnd, &rct );
0678:                 DrawText( hDC, strMsg, -1, &rct
0679:                         , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0680:                 ReleaseDC( hWnd, hDC );
0681:             }
0682:             break;
0683:         }
0684:         case WM_LBUTTONDOWN:
0685:             x = (lParam << 16) >> 16;
0686:             y = lParam >> 16;
0687:             SetDrop((float)x , (float)y);
0688:             break;
0689:         case WM_MOUSEMOVE:
0690:             x = (lParam << 16) >> 16;
0691:             y = lParam >> 16;
0692:             if(IsDrop())SetDrop((float)x , (float)y);
0693:             break;
0694:         case WM_LBUTTONUP:
0695:             ResetDrop();
0696:             break;
0697:         case WM_KEYDOWN:
0698:             m_bClear = true;
0699:             break;
0700:     }
0701: 
0702:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0703: }
0704: 
0705: 
0706: 
0707: 
0708: //-------------------------------------------------------------
0709: // Name: InvalidateDeviceObjects()
0710: // Desc: Invalidates device objects.  Paired with RestoreDeviceObjects()
0711: //       (RestoreDeviceObjects() で作成したオブジェクトの開放)
0712: //-------------------------------------------------------------
0713: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0714: {
0715:     // font(フォント)
0716:     m_pFont->InvalidateDeviceObjects();
0717: 
0718:     SAFE_RELEASE(m_pSurf[4]);
0719:     SAFE_RELEASE(m_pSurf[3]);
0720:     SAFE_RELEASE(m_pSurf[2]);
0721:     SAFE_RELEASE(m_pSurf[1]);
0722:     SAFE_RELEASE(m_pSurf[0]);
0723:     SAFE_RELEASE(m_pTex[4]);
0724:     SAFE_RELEASE(m_pTex[3]);
0725:     SAFE_RELEASE(m_pTex[2]);
0726:     SAFE_RELEASE(m_pTex[1]);
0727:     SAFE_RELEASE(m_pTex[0]);
0728: 
0729:     SAFE_RELEASE(m_pBackBufferSurf);
0730:     SAFE_RELEASE(m_pBackBufferTex);
0731:     SAFE_RELEASE(m_pMapZ);
0732: 
0733:     // Shaders(シェーダ)
0734:     if( m_pEffect    != NULL ) m_pEffect   ->OnLostDevice();
0735: 
0736:     return S_OK;
0737: }
0738: 
0739: 
0740: 
0741: 
0742: //-------------------------------------------------------------
0743: // Name: DeleteDeviceObjects()
0744: // Desc: Paired with InitDeviceObjects()
0745: //       Called when the app is exiting, or the device is being changed,
0746: //       this function deletes any device dependent objects.  
0747: //       (InitDeviceObjects() で作成したオブジェクトを開放する)
0748: //-------------------------------------------------------------
0749: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0750: {
0751:     // font(フォント)
0752:     m_pFont->DeleteDeviceObjects();
0753: 
0754:     // Shaders(シェーダ)
0755:     SAFE_RELEASE( m_pEffect );
0756:     
0757:     // シェーダ
0758:     SAFE_RELEASE( m_pTargetTex );
0759:     SAFE_RELEASE( m_pDotTex );
0760: 
0761:     return S_OK;
0762: }
0763: 
0764: 
0765: 
0766: 
0767: //-------------------------------------------------------------
0768: // Name: FinalCleanup()
0769: // Desc: Paired with OneTimeSceneInit()
0770: //       Called before the app exits, this function gives the app the chance
0771: //       to cleanup after itself.
0772: //       (終了する直前に呼ばれる)
0773: //-------------------------------------------------------------
0774: HRESULT CMyD3DApplication::FinalCleanup()
0775: {
0776:     // font(フォント)
0777:     SAFE_DELETE( m_pFont );
0778: 
0779:     return S_OK;
0780: }
0781: 
0782: 
0783: 
0784: 
0785: