0001: //-------------------------------------------------------------
0002: // File: main.cpp
0003: //
0004: // Desc: Ashikhminモデル
0005: //-------------------------------------------------------------
0006: #define STRICT
0007: #include <windows.h>
0008: #include <commctrl.h>
0009: #include <commdlg.h>
0010: #include <basetsd.h>
0011: #include <math.h>
0012: #include <stdio.h>
0013: #include <d3dx9.h>
0014: #include <dxerr9.h>
0015: #include <tchar.h>
0016: #include "DXUtil.h"
0017: #include "D3DEnumeration.h"
0018: #include "D3DSettings.h"
0019: #include "D3DApp.h"
0020: #include "D3DFont.h"
0021: #include "D3DFile.h"
0022: #include "D3DUtil.h"
0023: #include "resource.h"
0024: #include "main.h"
0025: 
0026: #define MAP_SIZE    256
0027: 
0028: // 長いから短縮形を作ってみた
0029: #define RS   m_pd3dDevice->SetRenderState
0030: #define TSS  m_pd3dDevice->SetTextureStageState
0031: #define SAMP m_pd3dDevice->SetSamplerState
0032: 
0033: 
0034: //-------------------------------------------------------------
0035: // 頂点の構造体
0036: //-------------------------------------------------------------
0037: typedef struct {
0038:     FLOAT       p[4];
0039:     FLOAT       tu, tv;
0040: } TVERTEX;
0041: 
0042: // 頂点宣言
0043: D3DVERTEXELEMENT9 decl[] =
0044: {
0045:     {0,  0, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
0046:     {0, 12, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   0},
0047:     {0, 24, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0},
0048:     {0, 36, D3DDECLTYPE_FLOAT3,   D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT,  0},
0049:     D3DDECL_END()
0050: };
0051: typedef struct  {
0052:     D3DXVECTOR3 p;
0053:     D3DXVECTOR3 n, b, t;
0054: } MESH_VERTEX;
0055: 
0056: //-------------------------------------------------------------
0057: // グローバル変数
0058: //-------------------------------------------------------------
0059: CMyD3DApplication* g_pApp  = NULL;
0060: HINSTANCE          g_hInst = NULL;
0061: 
0062: 
0063: //-------------------------------------------------------------
0064: // Name: WinMain()
0065: // Desc: メイン関数
0066: //-------------------------------------------------------------
0067: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0068: {
0069:     CMyD3DApplication d3dApp;
0070: 
0071:     g_pApp  = &d3dApp;
0072:     g_hInst = hInst;
0073: 
0074:     InitCommonControls();
0075:     if( FAILED( d3dApp.Create( hInst ) ) )
0076:         return 0;
0077: 
0078:     return d3dApp.Run();
0079: }
0080: 
0081: 
0082: 
0083: 
0084: //-------------------------------------------------------------
0085: // Name: Diffuse()
0086: // Desc: Diffuse テクスチャの作成
0087: //-------------------------------------------------------------
0088: VOID WINAPI Diffuse (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord, 
0089: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0090: {
0091:     FLOAT u = pTexCoord->x;
0092:     FLOAT v = pTexCoord->y;
0093:     
0094:     u = 1-powf(1-u/2, 5);
0095:     v = 1-powf(1-v/2, 5);
0096: 
0097:     pOut->x = pOut->y = pOut->z = u*v;
0098: }
0099: 
0100: //-------------------------------------------------------------
0101: // Name: gs()
0102: // Desc: gs テクスチャの作成
0103: //-------------------------------------------------------------
0104: VOID WINAPI gs (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord, 
0105: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0106: {
0107:     FLOAT u = pTexCoord->x;
0108:     FLOAT v = pTexCoord->y;
0109:     FLOAT g;
0110: 
0111:     if(1.0f-1.0f/256.0f<u){
0112:         g = expf(-5.0f*5.0f*(v-0.5f)*(v-0.5f)/2.0f );
0113:     }else{
0114:         g = powf(u, 5.0f*5.0f*(v-0.5f)*(v-0.5f)/(1.0f-u*u));
0115:     }
0116:     pOut->x = pOut->y = pOut->z = g;
0117: }
0118: 
0119: //-------------------------------------------------------------
0120: // Name: Fresnel
0121: // Desc: Fresnel項の計算
0122: //-------------------------------------------------------------
0123: FLOAT Fresnel(FLOAT n)
0124: {
0125:     FLOAT Rs = 0.5f;// 鏡面反射率
0126: 
0127:     return Rs+(1-Rs)*(1-powf(n,5));
0128: }
0129: 
0130: //-------------------------------------------------------------
0131: // Name: gp()
0132: // Desc: gp テクスチャの作成
0133: //-------------------------------------------------------------
0134: VOID WINAPI gp (D3DXVECTOR4* pOut, CONST D3DXVECTOR2* pTexCoord, 
0135: CONST D3DXVECTOR2* pTexelSize, LPVOID pData)
0136: {
0137:     FLOAT u = pTexCoord->x;
0138:     FLOAT v = pTexCoord->y;
0139:     FLOAT g;
0140:     
0141:     g = Fresnel(u)/(1.0f/256.0f+u*v);
0142: 
0143:     pOut->x = pOut->y = pOut->z = g;
0144: }
0145: 
0146: 
0147: 
0148: 
0149: //-------------------------------------------------------------
0150: // Name: CMyD3DApplication()
0151: // Desc: アプリケーションのコンストラクタ
0152: //-------------------------------------------------------------
0153: CMyD3DApplication::CMyD3DApplication()
0154: {
0155:     m_pMesh                     = new CD3DMesh();
0156:     m_pTex[0]                   = NULL;
0157:     m_pTex[1]                   = NULL;
0158:     m_pTex[2]                   = NULL;
0159: 
0160:     m_pEffect                   = NULL;
0161:     m_hTechnique                = NULL;
0162:     m_hmWVP                     = NULL;
0163:     m_hvCol                     = NULL;
0164:     m_hvLightPos                = NULL;
0165:     m_hvCamPos                  = NULL;
0166:     m_pDecl                     = NULL;
0167: 
0168:     m_fWorldRotX                = -0.41271535f;
0169:     m_fWorldRotY                = 0.5f * D3DX_PI;
0170:     m_fViewZoom                 = 2.5f;
0171: 
0172:     m_dwCreationWidth           = 512;
0173:     m_dwCreationHeight          = 512;
0174:     m_strWindowTitle            = TEXT( "main" );
0175:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0176:     m_bStartFullscreen          = false;
0177:     m_bShowCursorWhenFullscreen = false;
0178: 
0179:     m_LightPos                  = D3DXVECTOR4( 3.0f, 3.0f, -3.0f, 0.0f);
0180: //  m_LightPos                  = D3DXVECTOR4(-0.6f, 2.2f, -2.0f, 0.0f);
0181: 
0182:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0183:     m_bLoadingApp               = TRUE;
0184: 
0185:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0186: }
0187: 
0188: 
0189: 
0190: 
0191: //-------------------------------------------------------------
0192: // Name: ~CMyD3DApplication()
0193: // Desc: デストラクタ
0194: //-------------------------------------------------------------
0195: CMyD3DApplication::~CMyD3DApplication()
0196: {
0197: }
0198: 
0199: 
0200: 
0201: 
0202: //-------------------------------------------------------------
0203: // Name: OneTimeSceneInit()
0204: // Desc: 一度だけ行う初期化
0205: //      ウィンドウの初期化やIDirect3D9の初期化は終わってます。
0206: //      ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。
0207: //-------------------------------------------------------------
0208: HRESULT CMyD3DApplication::OneTimeSceneInit()
0209: {
0210:     // ローディングメッセージを表示する
0211:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0212: 
0213:     m_bLoadingApp = FALSE;
0214: 
0215:     return S_OK;
0216: }
0217: 
0218: 
0219: 
0220: 
0221: //-------------------------------------------------------------
0222: // Name: ConfirmDevice()
0223: // Desc: 初期化の時に呼ばれます。必要な能力をチェックします。
0224: //-------------------------------------------------------------
0225: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0226:                      DWORD dwBehavior,    D3DFORMAT Format )
0227: {
0228:     UNREFERENCED_PARAMETER( Format );
0229:     UNREFERENCED_PARAMETER( dwBehavior );
0230:     UNREFERENCED_PARAMETER( pCaps );
0231:     
0232: 
0233:     // 頂点シェーダバージョンが上位かソフトウェア頂点処理
0234:     if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0235:     &&  0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0236:             return E_FAIL;
0237: 
0238:     // ピクセルシェーダバージョンチェック
0239:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0240:         return E_FAIL;
0241: 
0242:     return S_OK;
0243: }
0244: 
0245: 
0246: // ---------------------------------------------------------------------------
0247: // 生成
0248: // ---------------------------------------------------------------------------
0249: HRESULT CreateMesh( LPDIRECT3DDEVICE9 pd3dDevice, CD3DMesh *pSrcMesh )
0250: {
0251:     HRESULT ret = S_OK;
0252:     MESH_VERTEX* pVertices;
0253:     LPD3DXMESH pMesh;
0254:     DWORD i;
0255: 
0256:     if( FAILED( pSrcMesh->m_pSysMemMesh->CloneMesh(
0257:                         D3DXMESH_SYSTEMMEM,
0258:                         decl,
0259:                         pd3dDevice, &pMesh ) ) )
0260:         return E_FAIL;
0261:     DWORD dwNumVertices    = pMesh->GetNumVertices();
0262: 
0263:     // バッファを専有
0264:     pMesh->LockVertexBuffer( 0L, (LPVOID*)&pVertices );
0265:     
0266:     D3DXVECTOR3 up = D3DXVECTOR3(0,1,0);
0267:     for(i=0;i<dwNumVertices;i++){
0268:         D3DXVec3Cross( &pVertices[i].t, &pVertices[i].n, &up );
0269:         D3DXVec3Normalize( &pVertices[i].t, &pVertices[i].t );
0270:         D3DXVec3Cross( &pVertices[i].b, &pVertices[i].n, &pVertices[i].t );
0271:         D3DXVec3Normalize( &pVertices[i].b, &pVertices[i].b );
0272:     }
0273:     
0274:     // バッファの専有を解除
0275:     pMesh->UnlockVertexBuffer();
0276:     
0277:     SAFE_RELEASE( pSrcMesh->m_pSysMemMesh );
0278:     pSrcMesh->m_pSysMemMesh = pMesh;
0279: 
0280:     return ret;
0281: }
0282: 
0283: //-------------------------------------------------------------
0284: // Name: InitDeviceObjects()
0285: // Desc: デバイスが生成された後の初期化をします。
0286: //      フレームバッファフォーマットやデバイスの種類が変わった
0287: //      後に通過します。
0288: //      ここで確保したメモリはDeleteDeviceObjects()で開放します
0289: //-------------------------------------------------------------
0290: HRESULT CMyD3DApplication::InitDeviceObjects()
0291: {
0292:     HRESULT hr;
0293:     D3DXVECTOR4 offset;
0294: 
0295:     // 頂点宣言のオブジェクトの生成
0296:     if( FAILED( hr = m_pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl ) ) )
0297:         return DXTRACE_ERR( "CreateVertexDeclaration", hr );
0298:     
0299:     // モデルの読み込み
0300:     if(FAILED(hr=m_pMesh  ->Create( m_pd3dDevice, _T("ball.x"))))
0301: //  if(FAILED(hr=m_pMesh  ->Create( m_pd3dDevice, _T("dog.x"))))
0302:         return DXTRACE_ERR( "LoadCar", hr );
0303:     m_pMesh->UseMeshMaterials(FALSE);
0304: 
0305:     CreateMesh( m_pd3dDevice, m_pMesh );
0306: 
0307:     // シェーダの読み込み
0308:     LPD3DXBUFFER pErr;
0309:     if( FAILED( hr = D3DXCreateEffectFromFile(
0310:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0311:                 D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0312:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0313:                     , "ERROR", MB_OK);
0314:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0315:     }
0316:     m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0317:     m_hmWVP = m_pEffect->GetParameterByName( NULL, "mWVP" );
0318:     m_hvCol  = m_pEffect->GetParameterByName( NULL, "vCol" );
0319:     m_hvLightPos  = m_pEffect->GetParameterByName( NULL, "vLightPos" );
0320:     m_hvCamPos  = m_pEffect->GetParameterByName( NULL, "vCamPos" );
0321:     
0322:     // フォント
0323:     m_pFont->InitDeviceObjects( m_pd3dDevice );
0324: 
0325:     return S_OK;
0326: }
0327: 
0328: //-------------------------------------------------------------
0329: // Name: RestoreDeviceObjects()
0330: // Desc: 画面のサイズが変更された時等に呼ばれます。
0331: //      確保したメモリはInvalidateDeviceObjects()で開放します。
0332: //-------------------------------------------------------------
0333: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0334: {
0335:     HRESULT hr;
0336:     
0337:     // 拡散光用
0338:     if( FAILED(hr = m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1
0339:                           , D3DUSAGE_RENDERTARGET, D3DFMT_R16F
0340:                           , D3DPOOL_DEFAULT, &m_pTex[0], NULL)))
0341:         return hr;
0342:     if( FAILED(hr = D3DXFillTexture(m_pTex[0], Diffuse, NULL)))
0343:         return hr;
0344:     m_pEffect->SetTexture( "tDiffuse", m_pTex[0] );
0345:     
0346:     // 異方性
0347:     if( FAILED(hr = m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1
0348:                           , D3DUSAGE_RENDERTARGET, D3DFMT_R16F
0349:                           , D3DPOOL_DEFAULT, &m_pTex[1], NULL)))
0350:         return hr;
0351:     if( FAILED(hr = D3DXFillTexture(m_pTex[1], gp, NULL)))
0352:         return hr;
0353:     m_pEffect->SetTexture( "tGp", m_pTex[1] );
0354:     // フレネル、エネルギー非増幅
0355:     if( FAILED(hr = m_pd3dDevice->CreateTexture(MAP_SIZE, MAP_SIZE, 1
0356:                           , D3DUSAGE_RENDERTARGET, D3DFMT_R16F
0357:                           , D3DPOOL_DEFAULT, &m_pTex[2], NULL)))
0358:         return hr;
0359:     if( FAILED(hr = D3DXFillTexture(m_pTex[2], gs, NULL)))
0360:         return hr;
0361:     m_pEffect->SetTexture( "tGs", m_pTex[2] );
0362: 
0363: 
0364:     // メッシュ
0365:     if( NULL == m_pMesh->m_pSysMemMesh )        return E_FAIL;
0366:     if( FAILED( m_pMesh->m_pSysMemMesh->CloneMesh( 0L, decl,
0367:                    m_pd3dDevice, &m_pMesh->m_pLocalMesh ) ) )
0368:         return E_FAIL;
0369: 
0370:     // レンダリング状態の設定
0371:     RS( D3DRS_DITHERENABLE,   FALSE );
0372:     RS( D3DRS_SPECULARENABLE, FALSE );
0373:     RS( D3DRS_ZENABLE,        TRUE );
0374:     RS( D3DRS_AMBIENT,        0x000F0F0F );
0375:     RS( D3DRS_LIGHTING,       FALSE );
0376:     
0377:     TSS( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0378:     TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0379:     TSS( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0380:     TSS( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
0381:     TSS( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
0382:     SAMP( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0383:     SAMP( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0384:     SAMP( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0385:     SAMP( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0386: 
0387:     // ワールド行列
0388:     D3DXMATRIX matIdentity;
0389:     D3DXMatrixIdentity( &m_mWorld );
0390: 
0391:     // ビュー行列
0392:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0393:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0394:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0395:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0396: 
0397:     // 射影行列
0398:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0399:     D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0400: 
0401:     // フォント
0402:     m_pFont->RestoreDeviceObjects();
0403: 
0404:     m_pEffect->OnResetDevice();
0405: 
0406:     return S_OK;
0407: }
0408: 
0409: 
0410: 
0411: 
0412: //-------------------------------------------------------------
0413: // Name: FrameMove()
0414: // Desc: 毎フレーム呼ばれます。アニメの処理などを行います。
0415: //-------------------------------------------------------------
0416: HRESULT CMyD3DApplication::FrameMove()
0417: {
0418:     // 入力データの更新
0419:     UpdateInput( &m_UserInput );
0420: 
0421:     //---------------------------------------------------------
0422:     // 入力に応じて座標系を更新する
0423:     //---------------------------------------------------------
0424:     // 回転
0425:     D3DXMATRIX matRotY;
0426:     D3DXMATRIX matRotX;
0427: 
0428:     if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0429:         m_fWorldRotY += m_fElapsedTime;
0430:     else
0431:     if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0432:         m_fWorldRotY -= m_fElapsedTime;
0433: 
0434:     if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0435:         m_fWorldRotX += m_fElapsedTime;
0436:     else
0437:     if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0438:         m_fWorldRotX -= m_fElapsedTime;
0439: 
0440:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0441:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0442: 
0443:     D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0444:     
0445:     //---------------------------------------------------------
0446:     // ビュー行列の設定
0447:     //---------------------------------------------------------
0448:     // ズーム
0449:     if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0450:         m_fViewZoom += m_fElapsedTime;
0451:     else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0452:         m_fViewZoom -= m_fElapsedTime;
0453: 
0454:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0455:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0456:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0457:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0458: 
0459:     return S_OK;
0460: }
0461: //-------------------------------------------------------------
0462: // Name: UpdateInput()
0463: // Desc: 入力データを更新する
0464: //-------------------------------------------------------------
0465: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0466: {
0467:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0468:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0469:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0470:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0471:     
0472:     pUserInput->bZoomIn      = ( m_bActive && (GetAsyncKeyState( 'Z'     )  & 0x8000) == 0x8000 );
0473:     pUserInput->bZoomOut     = ( m_bActive && (GetAsyncKeyState( 'X'      ) & 0x8000) == 0x8000 );
0474: }
0475: 
0476: 
0477: //-------------------------------------------------------------
0478: // Name: Render()
0479: // Desc: 画面を描画する.
0480: //-------------------------------------------------------------
0481: HRESULT CMyD3DApplication::Render()
0482: {
0483:     D3DXMATRIX m, mW;
0484:     D3DXVECTOR4 v;
0485: 
0486:     //---------------------------------------------------------
0487:     // 描画
0488:     //---------------------------------------------------------
0489:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0490:     {
0491:         m_pd3dDevice->Clear(0L, NULL
0492:                         , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0493:                         , 0x404080, 1.0f, 0L);
0494: 
0495:         if(m_pEffect != NULL)
0496:         {
0497:             m_pEffect->SetTechnique( m_hTechnique );
0498:             m_pEffect->Begin( NULL, 0 );
0499:             m_pEffect->Pass( 0 );
0500:             
0501:             D3DXMatrixTranslation ( &mW, 0, -0.0f, 0 );
0502: //          D3DXMatrixTranslation ( &mW, 0, -0.5f, 0 );
0503:             mW = mW * m_mWorld;
0504:             m = mW * m_mView * m_mProj;
0505:             m_pEffect->SetMatrix( m_hmWVP, &m );
0506: 
0507:             // ライト
0508:             D3DXMatrixInverse( &m, NULL, &mW);
0509:             D3DXVec4Transform( &v, &m_LightPos, &m );
0510:             m_pEffect->SetVector( m_hvLightPos, &v );
0511:             
0512:             // 視点
0513:             v = D3DXVECTOR4( 0.0f, 0.0f, -m_fViewZoom, 1.0f );
0514:             D3DXVec4Transform( &v, &v, &m );
0515:             m_pEffect->SetVector( m_hvCamPos, &v );
0516:             
0517:             TSS( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
0518:             TSS( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );
0519: 
0520:             //-------------------------------------------------
0521:             // 描画
0522:             //-------------------------------------------------
0523:             m_pd3dDevice->SetVertexDeclaration(m_pDecl);
0524: 
0525:             D3DMATERIAL9 *pMtrl = m_pMesh->m_pMaterials;
0526:             for( DWORD i=0; i<m_pMesh->m_dwNumMaterials; i++ ) {
0527:                 // メッシュの色
0528:                 v.x = pMtrl->Diffuse.r;
0529:                 v.y = pMtrl->Diffuse.g;
0530:                 v.z = pMtrl->Diffuse.b;
0531:                 v.w = pMtrl->Diffuse.a;
0532:                 m_pEffect->SetVector( m_hvCol, &v );
0533:                 // テクスチャ
0534:                 m_pMesh->m_pLocalMesh->DrawSubset( i ); // 描画
0535:                 pMtrl++;
0536:             }
0537: 
0538:             m_pEffect->End();
0539:         }
0540: 
0541: #if 1 // デバッグ用にテクスチャを表示する
0542:     m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0543:     m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0544:     m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0545:     m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0546:     m_pd3dDevice->SetVertexShader(NULL);
0547:     m_pd3dDevice->SetPixelShader(0);
0548:     for(DWORD l = 0; l < 3; l++){
0549:         const float scale =128.0f;
0550:         typedef struct {FLOAT p[4]; FLOAT tu, tv;} TVERTEX;
0551:         FLOAT X = (FLOAT)this->m_rcWindowClient.right;
0552:         FLOAT Y = (FLOAT)this->m_rcWindowClient.bottom;
0553:         TVERTEX Vertex[4] = {
0554:             //  x       y    z rhw tu tv
0555:             {X-(l+1)*scale,Y-scale,0, 1, 0.0f, 0.0f,},
0556:             {X-(l+0)*scale,Y-scale,0, 1, 1.0f, 0.0f,},
0557:             {X-(l+0)*scale,Y      ,0, 1, 1.0f, 1.0f,},
0558:             {X-(l+1)*scale,Y      ,0, 1, 0.0f, 1.0f,},
0559:         };
0560:         switch(l){
0561:         case 0: m_pd3dDevice->SetTexture( 0, m_pTex[0] ); break;
0562:         case 1: m_pd3dDevice->SetTexture( 0, m_pTex[1] ); break;
0563:         case 2: m_pd3dDevice->SetTexture( 0, m_pTex[2] ); break;
0564:         }
0565:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0566:     }
0567: #endif      
0568:         // ヘルプの表示
0569:         RenderText();
0570: 
0571:         // 描画の終了
0572:         m_pd3dDevice->EndScene();
0573:     }
0574: 
0575:     return S_OK;
0576: }
0577: 
0578: 
0579: 
0580: 
0581: //-------------------------------------------------------------
0582: // Name: RenderText()
0583: // Desc: 状態やヘルプを画面に表示する
0584: //-------------------------------------------------------------
0585: HRESULT CMyD3DApplication::RenderText()
0586: {
0587:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
0588:     TCHAR szMsg[MAX_PATH] = TEXT("");
0589: 
0590:     FLOAT fNextLine = 40.0f; // 表示する高さ
0591: 
0592:     // 操作法やパラメータを表示する
0593:     fNextLine = (FLOAT) m_d3dsdBackBuffer.Height; 
0594:     lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
0595:     fNextLine -= 20.0f;
0596:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0597:     
0598:     lstrcpy( szMsg, m_strDeviceStats );
0599:     fNextLine -= 20.0f;
0600:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0601:     lstrcpy( szMsg, m_strFrameStats );
0602:     fNextLine -= 20.0f;
0603:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0604:     
0605:     return S_OK;
0606: }
0607: 
0608: 
0609: 
0610: 
0611: //-------------------------------------------------------------
0612: // Name: MsgProc()
0613: // Desc: WndProc をオーバーライドしたもの
0614: //-------------------------------------------------------------
0615: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
0616:                                  WPARAM wParam, LPARAM lParam )
0617: {
0618:     switch( msg )
0619:     {
0620:         case WM_PAINT:
0621:         {
0622:             if( m_bLoadingApp )
0623:             {
0624:                 // ロード中
0625:                 HDC hDC = GetDC( hWnd );
0626:                 TCHAR strMsg[MAX_PATH];
0627:                 wsprintf(strMsg, TEXT("Loading... Please wait"));
0628:                 RECT rct;
0629:                 GetClientRect( hWnd, &rct );
0630:                 DrawText( hDC, strMsg, -1, &rct
0631:                         , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0632:                 ReleaseDC( hWnd, hDC );
0633:             }
0634:             break;
0635:         }
0636: 
0637:     }
0638: 
0639:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0640: }
0641: 
0642: 
0643: 
0644: 
0645: //-------------------------------------------------------------
0646: // Name: InvalidateDeviceObjects()
0647: // Desc: RestoreDeviceObjects() で作成したオブジェクトの開放
0648: //-------------------------------------------------------------
0649: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0650: {
0651:     SAFE_RELEASE(m_pTex[2]);
0652:     SAFE_RELEASE(m_pTex[1]);
0653:     SAFE_RELEASE(m_pTex[0]);
0654: 
0655:     m_pMesh  ->InvalidateDeviceObjects(); // メッシュ
0656: 
0657:     m_pFont->InvalidateDeviceObjects(); // フォント
0658: 
0659:     // シェーダ
0660:     if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
0661: 
0662:     return S_OK;
0663: }
0664: 
0665: 
0666: 
0667: 
0668: //-------------------------------------------------------------
0669: // Name: DeleteDeviceObjects()
0670: // Desc: InitDeviceObjects() で作成したオブジェクトを開放する
0671: //-------------------------------------------------------------
0672: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0673: {
0674:     // シェーダ
0675:     SAFE_RELEASE( m_pEffect );
0676:     SAFE_RELEASE( m_pDecl );
0677:     
0678:     // メッシュ
0679:     m_pMesh  ->Destroy();
0680: 
0681:     // フォント
0682:     m_pFont->DeleteDeviceObjects();
0683: 
0684:     return S_OK;
0685: }
0686: 
0687: 
0688: 
0689: 
0690: //-------------------------------------------------------------
0691: // Name: FinalCleanup()
0692: // Desc: 終了する直線に呼ばれる
0693: //-------------------------------------------------------------
0694: HRESULT CMyD3DApplication::FinalCleanup()
0695: {
0696:     SAFE_DELETE( m_pMesh );
0697: 
0698:     SAFE_DELETE( m_pFont ); // フォント
0699: 
0700:     return S_OK;
0701: }
0702: 
0703: 
0704: 
0705: 
0706: