0001: //-----------------------------------------------------------------------------
0002: // File: main.cpp
0003: //
0004: // Desc: アプリケーションソース
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: 
0028: //-----------------------------------------------------------------------------
0029: // グローバル変数
0030: //-----------------------------------------------------------------------------
0031: CMyD3DApplication* g_pApp  = NULL;
0032: HINSTANCE          g_hInst = NULL;
0033: 
0034: //-----------------------------------------------------------------------------
0035: // クラスの静的変数
0036: //-----------------------------------------------------------------------------
0037: char CMyD3DApplication::BRDF_name[BRDF_MAX][256] = {
0038:     "Cornell Garnet Red Duplicolor T-345 Paint",
0039:     "Cornell Krylon Latex Enamel #7205, True Blue",
0040:     "Cornell Cayman Blue Lacquer",
0041:     "Cornell Mystique Lacquer",
0042:     "CURET Velvet",
0043:     "CURET Leather",
0044:     "Analytic Anisotropic Poulin-Fournier \"Satin\"",
0045: };
0046: 
0047: char CMyD3DApplication::BRDF_file[BRDF_MAX][2][256] = {
0048:     {"garnetredp.bmp",  "garnetredq.bmp"},
0049:     {"krylonbluep.bmp", "krylonblueq.bmp"},
0050:     {"caymanp.bmp",     "caymanq.bmp"},
0051:     {"mystiquep.bmp",   "mystiqueq.bmp"},
0052:     {"velvetp.bmp",     "velvetq.bmp"},
0053:     {"leatherp.bmp",    "leatherq.bmp"},
0054:     {"satinp.bmp",      "satinq.bmp"},
0055: };
0056: 
0057: float CMyD3DApplication::BRDF_color[][4]= {
0058:     {30*0.04324800f,   30*0.0725744f,   30*0.0527886f,   0},// Cornell Garnet Red Duplicolor T-345 Paint
0059:     {20*11.4187000f,   20*0.0555595f,   20*0.0841939f,   0},// Cornell Krylon Latex Enamel #7205, True Blue
0060:     {50*2.38849e-05f,  50*0.0185791f,   50*0.0232454f,   0},// Cornell Cayman Blue Lacquer
0061:     {100*0.00096388f, 100*0.1627260f,  100*0.0199128f,   0},// Cornell Mystique Lacquer
0062:     {20*0.03070830f,   20*0.000387719f, 20*9.24145e-07f, 0},//CURET Velvet
0063:     { 4*0.34032900f,    4*0.2709300f,    4*0.1450500f,   0},// CURET Leather
0064:     { 1*0.76236700f,    1*0.7623670f,    1*0.7623670f,   0},// Analytic Anisotropic Poulin-Fournier "Satin"
0065: };
0066: //-----------------------------------------------------------------------------
0067: // Name: WinMain()
0068: // Desc: メイン関数
0069: //-----------------------------------------------------------------------------
0070: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0071: {
0072:     CMyD3DApplication d3dApp;
0073: 
0074:     g_pApp  = &d3dApp;
0075:     g_hInst = hInst;
0076: 
0077:     InitCommonControls();
0078:     if( FAILED( d3dApp.Create( hInst ) ) )
0079:         return 0;
0080: 
0081:     return d3dApp.Run();
0082: }
0083: 
0084: 
0085: 
0086: 
0087: //-----------------------------------------------------------------------------
0088: // Name: CMyD3DApplication()
0089: // Desc: アプリケーションのコンストラクタ
0090: //-----------------------------------------------------------------------------
0091: CMyD3DApplication::CMyD3DApplication()
0092: {
0093:     m_nBrdf = BRDF_Garnet_Red;
0094:     for(int i=0; i< BRDF_MAX; i++){
0095:         m_pTex[i][0] = NULL;
0096:         m_pTex[i][1] = NULL;
0097:     }
0098: 
0099:     m_pEffect = NULL;
0100: 
0101:     m_dwCreationWidth           = 500;
0102:     m_dwCreationHeight          = 375;
0103:     m_strWindowTitle            = TEXT( "main" );
0104:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0105:     m_bStartFullscreen          = false;
0106:     m_bShowCursorWhenFullscreen = false;
0107: 
0108:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0109:     m_bLoadingApp               = TRUE;
0110:     m_pD3DXMesh                 = NULL;
0111: 
0112:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0113:     m_fWorldRotX                = -0.437504f;
0114:     m_fWorldRotY                = 2.35087f;
0115:     m_fViewZoom                 = 5.0f;
0116: 
0117:     m_fLightRotX                = 1.57997f;
0118:     m_fLightRotY                =-0.487442f;
0119: }
0120: 
0121: 
0122: 
0123: 
0124: //-----------------------------------------------------------------------------
0125: // Name: ~CMyD3DApplication()
0126: // Desc: デストラクタ
0127: //-----------------------------------------------------------------------------
0128: CMyD3DApplication::~CMyD3DApplication()
0129: {
0130: }
0131: 
0132: 
0133: 
0134: 
0135: //-----------------------------------------------------------------------------
0136: // Name: OneTimeSceneInit()
0137: // Desc: 一度だけ行う初期化
0138: //      ウィンドウの初期化やIDirect3D9の初期化は終わってます。
0139: //      ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。
0140: //-----------------------------------------------------------------------------
0141: HRESULT CMyD3DApplication::OneTimeSceneInit()
0142: {
0143:     // ローディングメッセージを表示する
0144:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0145: 
0146:     m_bLoadingApp = FALSE;
0147: 
0148:     return S_OK;
0149: }
0150: 
0151: 
0152: 
0153: 
0154: //-----------------------------------------------------------------------------
0155: // Name: ConfirmDevice()
0156: // Desc: 初期化の時に呼ばれます。必要な能力をチェックします。
0157: //-----------------------------------------------------------------------------
0158: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
0159:                                           D3DFORMAT Format )
0160: {
0161:     UNREFERENCED_PARAMETER( Format );
0162:     UNREFERENCED_PARAMETER( dwBehavior );
0163:     UNREFERENCED_PARAMETER( pCaps );
0164:     
0165:     // ピクセルシェーダバージョンチェック
0166:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(1,1) )
0167:         return E_FAIL;
0168: 
0169:     // 頂点シェーダバージョンが上位かソフトウェア頂点処理
0170:     if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0171:     &&  0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0172:             return E_FAIL;
0173: 
0174: }
0175: 
0176: 
0177: 
0178: 
0179: //-----------------------------------------------------------------------------
0180: // Name: InitDeviceObjects()
0181: // Desc: デバイスが生成された後の初期化をします。
0182: //      フレームバッファフォーマットやデバイスの種類が変わった
0183: //      後に通過します。
0184: //      ここで確保したメモリはDeleteDeviceObjects()で開放します
0185: //-----------------------------------------------------------------------------
0186: HRESULT CMyD3DApplication::InitDeviceObjects()
0187: {
0188:     HRESULT hr;
0189:     
0190:     // テクスチャの読み込み
0191:     for(int i=0; i< BRDF_MAX; i++){
0192:         D3DXCreateTextureFromFile(m_pd3dDevice, BRDF_file[i][0], &m_pTex[i][0]);
0193:         D3DXCreateTextureFromFile(m_pd3dDevice, BRDF_file[i][1], &m_pTex[i][1]);
0194:     }
0195:     
0196:     // シェーダの読み込み
0197:     LPD3DXBUFFER pErr;
0198:     if( FAILED( hr = D3DXCreateEffectFromFile(
0199:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0200:                 D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0201:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0202:                     , "ERROR", MB_OK);
0203:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0204:     }
0205: 
0206:     // フォント
0207:     hr = m_pFont->InitDeviceObjects( m_pd3dDevice );
0208:     if( FAILED( hr ) ) return DXTRACE_ERR( "m_pFont->InitDeviceObjects", hr );
0209:     
0210:     // teapot
0211:     if( FAILED( hr = D3DXCreateTeapot( m_pd3dDevice, &m_pD3DXMesh, NULL ) ) )
0212:         return DXTRACE_ERR( "D3DXCreateTeapot", hr );
0213: 
0214:     return S_OK;
0215: }
0216: 
0217: 
0218: 
0219: 
0220: //-----------------------------------------------------------------------------
0221: // Name: RestoreDeviceObjects()
0222: // Desc: 画面のサイズが変更された時等に呼ばれます。
0223: //      確保したメモリはInvalidateDeviceObjects()で開放します。
0224: //-----------------------------------------------------------------------------
0225: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0226: {
0227:     // シェーダ
0228:     m_pEffect->OnResetDevice();
0229: 
0230:     // レンダリング状態の設定
0231:     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   FALSE );
0232:     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
0233:     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
0234:     m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
0235: 
0236:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0237:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0238:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0239:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
0240:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
0241:     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
0242:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0243:     m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
0244: 
0245:     // ワールド行列
0246:     D3DXMATRIX matIdentity;
0247:     D3DXMatrixIdentity( &matIdentity );
0248:     m_pd3dDevice->SetTransform( D3DTS_WORLD,  &matIdentity );
0249: 
0250:     // ビュー行列
0251:     D3DXMATRIX matView;
0252:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0253:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0254:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0255:     D3DXMatrixLookAtLH( &m_mV, &vFromPt, &vLookatPt, &vUpVec );
0256:     m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_mV );
0257: 
0258:     // 射影行列
0259:     D3DXMATRIX matProj;
0260:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0261:     D3DXMatrixPerspectiveFovLH( &m_mP, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0262:     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_mP );
0263: 
0264:     // フォント
0265:     m_pFont->RestoreDeviceObjects();
0266: 
0267:     return S_OK;
0268: }
0269: 
0270: 
0271: 
0272: 
0273: //-----------------------------------------------------------------------------
0274: // Name: FrameMove()
0275: // Desc: 毎フレーム呼ばれます。アニメの処理などを行います。
0276: //-----------------------------------------------------------------------------
0277: HRESULT CMyD3DApplication::FrameMove()
0278: {
0279:     // 入力データの更新
0280:     UpdateInput( &m_UserInput );
0281: 
0282:     //---------------------------------------------------------
0283:     // 入力に応じて座標系を更新する
0284:     //---------------------------------------------------------
0285:     // BRDF のタイプ
0286:     if( m_UserInput.b2 && !m_UserInput.b1 ){
0287:         this->m_nBrdf++;
0288:         if(BRDF_MAX<=this->m_nBrdf)this->m_nBrdf = 0;
0289:     }else if( m_UserInput.b1 && !m_UserInput.b2 ){
0290:         this->m_nBrdf--;
0291:         if(this->m_nBrdf<0)this->m_nBrdf = BRDF_MAX-1;
0292:     }
0293: 
0294:     // 回転
0295:     D3DXMATRIX matRotY;
0296:     D3DXMATRIX matRotX;
0297:     
0298:     if(m_UserInput.bShift){
0299:         // ライト
0300:         if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0301:             m_fLightRotY += m_fElapsedTime;
0302:         else if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0303:             m_fLightRotY -= m_fElapsedTime;
0304: 
0305:         if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0306:             m_fLightRotX += m_fElapsedTime;
0307:         else if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0308:             m_fLightRotX -= m_fElapsedTime;
0309:     }else{
0310:         // オブジェクト
0311:         if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0312:             m_fWorldRotY += m_fElapsedTime;
0313:         else if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0314:             m_fWorldRotY -= m_fElapsedTime;
0315: 
0316:         if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0317:             m_fWorldRotX += m_fElapsedTime;
0318:         else if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0319:             m_fWorldRotX -= m_fElapsedTime;
0320:     }
0321: 
0322:     //---------------------------------------------------------
0323:     // 行列の設定
0324:     //---------------------------------------------------------
0325:     // ズーム
0326:     if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0327:         m_fViewZoom += m_fElapsedTime;
0328:     else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0329:         m_fViewZoom -= m_fElapsedTime;
0330:     // ビュー行列
0331:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0332:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0333:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0334:     D3DXMatrixLookAtLH( &m_mV, &vFromPt, &vLookatPt, &vUpVec );
0335:     
0336:     // ライトのベクトル
0337:     D3DXMatrixRotationX( &matRotX, m_fLightRotX );
0338:     D3DXMatrixRotationY( &matRotY, m_fLightRotY );
0339:     D3DXMatrixMultiply( &m_mLight, &matRotY, &matRotX );
0340:     // ワールド行列
0341:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0342:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0343:     D3DXMatrixMultiply( &m_mW, &matRotY, &matRotX );
0344: 
0345:     m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_mW );
0346: 
0347:     return S_OK;
0348: }
0349: 
0350: 
0351: 
0352: 
0353: //-----------------------------------------------------------------------------
0354: // Name: UpdateInput()
0355: // Desc: 入力データを更新する
0356: //-----------------------------------------------------------------------------
0357: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0358: {
0359:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0360:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0361:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0362:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0363:     pUserInput->bZoomIn      = ( m_bActive && (GetAsyncKeyState( 'Z'     )  & 0x8000) == 0x8000 );
0364:     pUserInput->bZoomOut     = ( m_bActive && (GetAsyncKeyState( 'X'      ) & 0x8000) == 0x8000 );
0365: 
0366:     pUserInput->bShift = ((GetAsyncKeyState( VK_SHIFT )    & 0x8000) == 0x8000);
0367: 
0368:     pUserInput->b1 = ( m_bActive && (GetAsyncKeyState( '1' ) & 0x8001) == 0x8001 );
0369:     pUserInput->b2 = ( m_bActive && (GetAsyncKeyState( '2' ) & 0x8001) == 0x8001 );
0370: }
0371: 
0372: 
0373: 
0374: 
0375: //-----------------------------------------------------------------------------
0376: // Name: Render()
0377: // Desc: 画面を描画する.
0378: //-----------------------------------------------------------------------------
0379: HRESULT CMyD3DApplication::Render()
0380: {
0381:     D3DXMATRIX m, mX, mY;
0382:     D3DXVECTOR4 v;
0383:     
0384:     // ライトの位置
0385:     D3DXVECTOR4 LightPos = D3DXVECTOR4(0.0f, 0.0f, -1.8f, 0.0f);
0386:     D3DXVec4Transform( &LightPos, &LightPos, &m_mLight );
0387:     
0388:     // 画面のクリア
0389:     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0390:                          0x000000ff, 1.0f, 0L );
0391: 
0392:     //---------------------------------------------------------
0393:     // 描画
0394:     //---------------------------------------------------------
0395:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0396:     {
0397:         if(m_pEffect){
0398:             // シェーダの設定
0399:             m_pEffect->SetTechnique( "TShader" );
0400:             m_pEffect->Begin( NULL, 0 );
0401:             m_pEffect->Pass( 0 );
0402:             
0403:             // 変換行列
0404:             D3DXMATRIX mWVP = m_mW * m_mV * m_mP;
0405:             m_pEffect->SetMatrix("mWVP", &mWVP);
0406:             
0407:             // ライトの向き
0408:             m = m_mW;
0409:             D3DXMatrixInverse( &m, NULL, &m );
0410:             D3DXVec4Transform( &v, &LightPos, &m );
0411:             m_pEffect->SetVector("LightPos", &v);
0412:             
0413:             // 視点
0414:             v = D3DXVECTOR4(0,0,-5,0);
0415:             m = m_mW;
0416:             D3DXMatrixInverse( &m, NULL, &m );
0417:             D3DXVec4Transform( &v, &v, &m );
0418:             m_pEffect->SetVector("EyePos", &v);
0419:             
0420:             // 追加する色
0421:             m_pEffect->SetVector("alpha", (D3DXVECTOR4*)&BRDF_color[m_nBrdf][0]);
0422:             
0423:             // テクスチャ
0424:             m_pEffect->SetTexture("MapP", m_pTex[m_nBrdf][0]);
0425:             m_pEffect->SetTexture("MapQ", m_pTex[m_nBrdf][1]);
0426: 
0427:             // ティーポットの表示
0428:             m_pD3DXMesh->DrawSubset(0);
0429: 
0430:             m_pEffect->End();
0431:         }
0432: 
0433:         //---------------------------------------------------------
0434:         // ライトの位置の表示
0435:         //---------------------------------------------------------
0436:         {
0437:         m = m_mV * m_mP;
0438:         v = LightPos;
0439:         v.w = 1;
0440:         D3DXVec4Transform( &v, &v, &m );
0441:         float x = (this->m_rcWindowClient.right-this->m_rcWindowClient.left)*( 0.5f*v.x/v.w+0.5f);
0442:         float y = (this->m_rcWindowClient.bottom-this->m_rcWindowClient.top)*(-0.5f*v.y/v.w+0.5f);
0443: 
0444:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0445:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_DIFFUSE);
0446:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0447:         m_pd3dDevice->SetVertexShader(NULL);
0448:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_DIFFUSE );
0449:         m_pd3dDevice->SetPixelShader(0);
0450: 
0451:         typedef struct {FLOAT p[4]; DWORD color;} LVERTEX;
0452:         for(DWORD i=0; i<2; i++){
0453:             LVERTEX Vertex[4] = {
0454:                 // x  y   z rhw tu tv
0455:                 {x-3,y-3, v.z/v.w, 1, 0xffffc0,},
0456:                 {x+3,y-3, v.z/v.w, 1, 0xffffc0,},
0457:                 {x+3,y+3, v.z/v.w, 1, 0xffffc0,},
0458:                 {x-3,y+3, v.z/v.w, 1, 0xffffc0,},
0459:             };
0460:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( LVERTEX ) );
0461:         }
0462:         }
0463: 
0464: #if 1 // デバッグ用にテクスチャを表示する
0465:         {
0466:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
0467:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
0468:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0469:         m_pd3dDevice->SetVertexShader(NULL);
0470:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0471:         m_pd3dDevice->SetPixelShader(0);
0472:         float scale = 128.0f;
0473:         typedef struct {FLOAT p[4]; FLOAT tu, tv;} TVERTEX;
0474:         for(DWORD i=0; i<2; i++){
0475:             TVERTEX Vertex[4] = {
0476:                 // x  y  z rhw tu tv
0477:                 {    0,(i+0)*scale,0, 1, 0, 0,},
0478:                 {scale,(i+0)*scale,0, 1, 1, 0,},
0479:                 {scale,(i+1)*scale,0, 1, 1, 1,},
0480:                 {    0,(i+1)*scale,0, 1, 0, 1,},
0481:             };
0482:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pTex[m_nBrdf][0] );
0483:             if(1==i) m_pd3dDevice->SetTexture( 0, m_pTex[m_nBrdf][1] );
0484:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0485:         }
0486:         }
0487: #endif      
0488:         
0489:         // ヘルプの表示
0490:         RenderText();
0491: 
0492:         // 描画の終了
0493:         m_pd3dDevice->EndScene();
0494:     }
0495: 
0496:     return S_OK;
0497: }
0498: 
0499: 
0500: 
0501: 
0502: //-----------------------------------------------------------------------------
0503: // Name: RenderText()
0504: // Desc: 状態やヘルプを画面に表示する
0505: //-----------------------------------------------------------------------------
0506: HRESULT CMyD3DApplication::RenderText()
0507: {
0508:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
0509:     TCHAR szMsg[MAX_PATH] = TEXT("");
0510: 
0511:     FLOAT fNextLine = 40.0f; // 表示する高さ
0512: 
0513:     // 操作法やパラメータを表示する
0514:     lstrcpy( szMsg, m_strDeviceStats );
0515:     fNextLine -= 20.0f;
0516:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0517: 
0518:     lstrcpy( szMsg, m_strFrameStats );
0519:     fNextLine -= 20.0f;
0520:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0521: 
0522:     fNextLine = (FLOAT) m_d3dsdBackBuffer.Height; 
0523: 
0524:     wsprintf( szMsg, TEXT("Arrow keys: Up=%d Down=%d Left=%d Right=%d"), 
0525:               m_UserInput.bRotateUp, m_UserInput.bRotateDown, m_UserInput.bRotateLeft, m_UserInput.bRotateRight );
0526:     fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0527: 
0528:     lstrcpy( szMsg, TEXT("Press '1' or '2' to change the factorization") );
0529:     fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0530: 
0531:     lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
0532:     fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0533: 
0534:     wsprintf( szMsg, TEXT("%d: %s"), this->m_nBrdf, BRDF_name[this->m_nBrdf] );
0535:     fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
0536: 
0537:     return S_OK;
0538: }
0539: 
0540: 
0541: 
0542: 
0543: //-----------------------------------------------------------------------------
0544: // Name: MsgProc()
0545: // Desc: WndProc をオーバーライドしたもの
0546: //-----------------------------------------------------------------------------
0547: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg, WPARAM wParam,
0548:                                     LPARAM lParam )
0549: {
0550:     switch( msg )
0551:     {
0552:         case WM_PAINT:
0553:         {
0554:             if( m_bLoadingApp )
0555:             {
0556:                 // ロード中
0557:                 HDC hDC = GetDC( hWnd );
0558:                 TCHAR strMsg[MAX_PATH];
0559:                 wsprintf( strMsg, TEXT("Loading... Please wait") );
0560:                 RECT rct;
0561:                 GetClientRect( hWnd, &rct );
0562:                 DrawText( hDC, strMsg, -1, &rct, DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0563:                 ReleaseDC( hWnd, hDC );
0564:             }
0565:             break;
0566:         }
0567: 
0568:     }
0569: 
0570:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0571: }
0572: 
0573: 
0574: 
0575: 
0576: //-----------------------------------------------------------------------------
0577: // Name: InvalidateDeviceObjects()
0578: // Desc: RestoreDeviceObjects() で作成したオブジェクトの開放
0579: //-----------------------------------------------------------------------------
0580: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0581: {
0582:     // シェーダ
0583:     if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
0584: 
0585:     m_pFont->InvalidateDeviceObjects(); // フォント
0586: 
0587:     return S_OK;
0588: }
0589: 
0590: 
0591: 
0592: 
0593: //-----------------------------------------------------------------------------
0594: // Name: DeleteDeviceObjects()
0595: // Desc: InitDeviceObjects() で作成したオブジェクトを開放する
0596: //-----------------------------------------------------------------------------
0597: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0598: {
0599:     // テクスチャ
0600:     for(int i=0; i< BRDF_MAX; i++){
0601:         SAFE_RELEASE( m_pTex[i][1] );
0602:         SAFE_RELEASE( m_pTex[i][0] );
0603:     }
0604: 
0605:     // シェーダ
0606:     SAFE_RELEASE( m_pEffect );
0607: 
0608:     m_pFont->DeleteDeviceObjects(); // フォント
0609:     SAFE_RELEASE( m_pD3DXMesh );    // メッシュ
0610: 
0611:     return S_OK;
0612: }
0613: 
0614: 
0615: 
0616: 
0617: //-----------------------------------------------------------------------------
0618: // Name: FinalCleanup()
0619: // Desc: 終了する直線に呼ばれる
0620: //-----------------------------------------------------------------------------
0621: HRESULT CMyD3DApplication::FinalCleanup()
0622: {
0623:     SAFE_DELETE( m_pFont ); // フォント
0624: 
0625:     return S_OK;
0626: }
0627: 
0628: 
0629: 
0630: 
0631: