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