0001: //-------------------------------------------------------------
0002: // File: main.cpp
0003: //
0004: // Desc: ascii art
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_WIDTH   512
0027: #define MAP_HEIGHT  512
0028: const int COLOR_MAP[] = {MAP_WIDTH/8, MAP_WIDTH/32};
0029: 
0030: 
0031: // 長いから短縮形を作ってみた
0032: #define RS   m_pd3dDevice->SetRenderState
0033: #define TSS  m_pd3dDevice->SetTextureStageState
0034: #define SAMP m_pd3dDevice->SetSamplerState
0035: 
0036: 
0037: //-------------------------------------------------------------
0038: // 頂点の構造体
0039: //-------------------------------------------------------------
0040: typedef struct {
0041:     FLOAT       p[4];
0042:     FLOAT       tu, tv;
0043: } TVERTEX;
0044: 
0045: //-------------------------------------------------------------
0046: // グローバル変数
0047: //-------------------------------------------------------------
0048: CMyD3DApplication* g_pApp  = NULL;
0049: HINSTANCE          g_hInst = NULL;
0050: 
0051: 
0052: //-------------------------------------------------------------
0053: // Name: WinMain()
0054: // Desc: メイン関数
0055: //-------------------------------------------------------------
0056: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
0057: {
0058:     CMyD3DApplication d3dApp;
0059: 
0060:     g_pApp  = &d3dApp;
0061:     g_hInst = hInst;
0062: 
0063:     InitCommonControls();
0064:     if( FAILED( d3dApp.Create( hInst ) ) )
0065:         return 0;
0066: 
0067:     return d3dApp.Run();
0068: }
0069: 
0070: 
0071: 
0072: 
0073: //-------------------------------------------------------------
0074: // Name: CMyD3DApplication()
0075: // Desc: アプリケーションのコンストラクタ
0076: //-------------------------------------------------------------
0077: CMyD3DApplication::CMyD3DApplication()
0078: {
0079:     DWORD i, j;
0080: 
0081:     m_pMesh                     = new CD3DMesh();
0082:     m_pMeshBg                   = new CD3DMesh();
0083: 
0084:     m_pMapZ                     = NULL;
0085:     m_pOriginalMap              = NULL;
0086:     m_pOriginalMapSurf          = NULL;
0087:     m_pFinalMap                 = NULL;
0088:     m_pFinalMapSurf             = NULL;
0089:     m_pPostMap                  = NULL;
0090:     m_pPostMapSurf              = NULL;
0091:     m_pAsciiMap                 = NULL;
0092:     m_pAsciiCoeffMap            = NULL;
0093: 
0094:     for( i=0;i<2;i++ )
0095:     {
0096:         m_pColorMap[i]          = NULL;
0097:         m_pColorMapSurf[i]      = NULL;
0098:     }
0099:     for( i=0;i<2;i++ )
0100:     for( j=0;j<4;j++ )
0101:     {
0102:         m_pCoeffMap[i][j]       = NULL;
0103:         m_pCoeffMapSurf[i][j]       = NULL;
0104:     }
0105:     for( j=0;j<4;j++ )
0106:     {
0107:         m_pWaveletMap[j]        = NULL;
0108:     }
0109:     for( i=0;i<13;i++ )
0110:     {
0111:         m_pDiffMap[i]           = NULL;
0112:         m_pDiffMapSurf[i]       = NULL;
0113:     }
0114:     m_pIndex0Map            = NULL;
0115:     m_pValue0Map            = NULL;
0116:     m_pIndex0MapSurf        = NULL;
0117:     m_pValue0MapSurf        = NULL;
0118:     m_pIndex1Map            = NULL;
0119:     m_pIndex1MapSurf        = NULL;
0120: 
0121: 
0122:     m_pEffect                   = NULL;
0123:     m_hTechnique                = NULL;
0124:     m_htSrcMap                  = NULL;
0125: 
0126:     m_fWorldRotX                = -0.195;
0127:     m_fWorldRotY                = 3.14159265f;
0128:     m_fViewZoom                 = 7.0f;
0129: 
0130:     m_dwCreationWidth           = 512;
0131:     m_dwCreationHeight          = 512;
0132:     m_strWindowTitle            = TEXT( "main" );
0133:     m_d3dEnumeration.AppUsesDepthBuffer   = TRUE;
0134:     m_bStartFullscreen          = false;
0135:     m_bShowCursorWhenFullscreen = false;
0136: 
0137:     m_pFont                     = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
0138:     m_bLoadingApp               = TRUE;
0139: 
0140:     ZeroMemory( &m_UserInput, sizeof(m_UserInput) );
0141: }
0142: 
0143: 
0144: 
0145: 
0146: //-------------------------------------------------------------
0147: // Name: ~CMyD3DApplication()
0148: // Desc: デストラクタ
0149: //-------------------------------------------------------------
0150: CMyD3DApplication::~CMyD3DApplication()
0151: {
0152: }
0153: 
0154: 
0155: 
0156: 
0157: //-------------------------------------------------------------
0158: // Name: OneTimeSceneInit()
0159: // Desc: 一度だけ行う初期化
0160: //      ウィンドウの初期化やIDirect3D9の初期化は終わってます。
0161: //      ただ、LPDIRECT3DDEVICE9 の初期化は終わっていません。
0162: //-------------------------------------------------------------
0163: HRESULT CMyD3DApplication::OneTimeSceneInit()
0164: {
0165:     // ローディングメッセージを表示する
0166:     SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0167: 
0168:     m_bLoadingApp = FALSE;
0169: 
0170:     return S_OK;
0171: }
0172: 
0173: 
0174: 
0175: 
0176: //-------------------------------------------------------------
0177: // Name: ConfirmDevice()
0178: // Desc: 初期化の時に呼ばれます。必要な能力をチェックします。
0179: //-------------------------------------------------------------
0180: HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps,
0181:                      DWORD dwBehavior,    D3DFORMAT Format )
0182: {
0183:     UNREFERENCED_PARAMETER( Format );
0184:     UNREFERENCED_PARAMETER( dwBehavior );
0185:     UNREFERENCED_PARAMETER( pCaps );
0186:     
0187: 
0188:     // ピクセルシェーダバージョンチェック
0189:     if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
0190:         return E_FAIL;
0191: 
0192:     // 頂点シェーダバージョンが上位かソフトウェア頂点処理
0193:     if( pCaps->VertexShaderVersion < D3DVS_VERSION(1,1)
0194:     &&  0==(dwBehavior & D3DCREATE_SOFTWARE_VERTEXPROCESSING) )
0195:             return E_FAIL;
0196: 
0197:     return S_OK;
0198: }
0199: 
0200: // ----------------------------------------------------------------------------
0201: // テクスチャーを作成
0202: // ----------------------------------------------------------------------------
0203: float GetHaarWavelet( int n, int m, float x, float y )
0204: {
0205:     switch(n)
0206:     {
0207:     case 0:
0208:         return 1;
0209:         break;
0210:     case 1:
0211:         switch(m)
0212:         {
0213:         case 0: return (y<0.5) ? -1 : 1;
0214:         case 1: return (x<0.5) ? -1 : 1;
0215:         case 2: return ((x<0.5)^(y<0.5)) ? -1 : 1;
0216:         }
0217:         break;
0218:     case 2:
0219:         if(1==((m/3)&1)) x -= 0.5f;
0220:         if(1==((m/3)/2)) y -= 0.5f;
0221: 
0222:         if( 0.0<=x && x<0.5 && 0.0<=y && y<0.5 )
0223:         {
0224:             switch(m%3)
0225:             {
0226:             case 0: return (x<0.25) ? -1 : 1;
0227:             case 1: return (y<0.25) ? -1 : 1;
0228:             case 2: return ((x<0.25)^(y<0.25)) ? -1 : 1;
0229:             }
0230:             break;
0231:         }
0232:     }
0233: 
0234:     return 0;
0235: }
0236: 
0237: 
0238: // ----------------------------------------------------------------------------
0239: // テクスチャーを作成
0240: // ----------------------------------------------------------------------------
0241: VOID WINAPI FillTextureHaarWavelet(D3DXVECTOR4* pOut, CONST D3DXVECTOR2 *pTexCoord, CONST D3DXVECTOR2 *pTexelSize, LPVOID pData)
0242: {
0243:     float o[4] = {0.0, 0.0, 0.0, 0.0};
0244:     int idx = (int)pData;
0245: 
0246:     switch(idx)
0247:     {
0248:     case 0:
0249:         o[0] = GetHaarWavelet( 1, 0, pTexCoord->x, pTexCoord->y );
0250:         o[1] = GetHaarWavelet( 1, 1, pTexCoord->x, pTexCoord->y );
0251:         o[2] = GetHaarWavelet( 1, 2, pTexCoord->x, pTexCoord->y );
0252:         o[3] = GetHaarWavelet( 0, 0, pTexCoord->x, pTexCoord->y );
0253:         break;
0254:     case 1:
0255:         o[0] = GetHaarWavelet( 2, 0, pTexCoord->x, pTexCoord->y );
0256:         o[1] = GetHaarWavelet( 2, 1, pTexCoord->x, pTexCoord->y );
0257:         o[2] = GetHaarWavelet( 2, 2, pTexCoord->x, pTexCoord->y );
0258:         o[3] = GetHaarWavelet( 2, 9, pTexCoord->x, pTexCoord->y );
0259:         break;
0260:     case 2:
0261:         o[0] = GetHaarWavelet( 2, 3, pTexCoord->x, pTexCoord->y );
0262:         o[1] = GetHaarWavelet( 2, 4, pTexCoord->x, pTexCoord->y );
0263:         o[2] = GetHaarWavelet( 2, 5, pTexCoord->x, pTexCoord->y );
0264:         o[3] = GetHaarWavelet( 2,10, pTexCoord->x, pTexCoord->y );
0265:         break;
0266:     case 3:
0267:         o[0] = GetHaarWavelet( 2, 6, pTexCoord->x, pTexCoord->y );
0268:         o[1] = GetHaarWavelet( 2, 7, pTexCoord->x, pTexCoord->y );
0269:         o[2] = GetHaarWavelet( 2, 8, pTexCoord->x, pTexCoord->y );
0270:         o[3] = GetHaarWavelet( 2,11, pTexCoord->x, pTexCoord->y );
0271:         break;
0272:     }
0273:     pOut->x = 0.5f * o[0] + 0.5f;
0274:     pOut->y = 0.5f * o[1] + 0.5f;
0275:     pOut->z = 0.5f * o[2] + 0.5f;
0276:     pOut->w = 0.5f * o[3] + 0.5f;
0277: }
0278: 
0279: // ----------------------------------------------------------------------------
0280: // テクスチャーを作成
0281: // ----------------------------------------------------------------------------
0282: VOID WINAPI FillTextureAsciiCoeff(D3DXVECTOR4* pOut, CONST D3DXVECTOR2 *pTexCoord, CONST D3DXVECTOR2 *pTexelSize, LPVOID pData)
0283: {
0284:     CMyD3DApplication *pApp = (CMyD3DApplication*)pData;
0285:     DWORD i, j;
0286:     int ascii = 52.0 * pTexCoord->x;
0287:     int idx   =  4.0 * pTexCoord->y;
0288:     int n[4];
0289:     int m[4];
0290:     float o[4] = {0,0,0,0};
0291: 
0292:     switch(idx)
0293:     {
0294:     case 0:
0295:         n[0] = 1; m[0] = 0;
0296:         n[1] = 1; m[1] = 1;
0297:         n[2] = 1; m[2] = 2;
0298:         n[3] = 0; m[3] = 0;
0299:         break;
0300:     case 1:
0301:         n[0] = 2; m[0] = 0;
0302:         n[1] = 2; m[1] = 1;
0303:         n[2] = 2; m[2] = 2;
0304:         n[3] = 2; m[3] = 9;
0305:         break;
0306:     case 2:
0307:         n[0] = 2; m[0] = 3;
0308:         n[1] = 2; m[1] = 4;
0309:         n[2] = 2; m[2] = 5;
0310:         n[3] = 2; m[3] =10;
0311:         break;
0312:     case 3:
0313:         n[0] = 2; m[0] = 6;
0314:         n[1] = 2; m[1] = 7;
0315:         n[2] = 2; m[2] = 8;
0316:         n[3] = 2; m[3] =11;
0317:         break;
0318:     }
0319:     for( i=0;i<24;i++ )
0320:     for( j=0;j<24;j++ )
0321:     {
0322:         float x = (1.0f/24.0f) * (float)i;
0323:         float y = (1.0f/24.0f) * (float)j;
0324:         float tex = (2.0f/255.0f) * (float) pApp->m_iTex[ascii][j][i] - 1.0f;
0325: 
0326:         o[0] += GetHaarWavelet( n[0], m[0], x, y ) * tex;
0327:         o[1] += GetHaarWavelet( n[1], m[1], x, y ) * tex;
0328:         o[2] += GetHaarWavelet( n[2], m[2], x, y ) * tex;
0329:         o[3] += GetHaarWavelet( n[3], m[3], x, y ) * tex;
0330:     }
0331:     o[0] /= (24.0*24.0);
0332:     o[1] /= (24.0*24.0);
0333:     o[2] /= (24.0*24.0);
0334:     o[3] /= (24.0*24.0);
0335: 
0336:     pOut->x = 0.5f * o[0] + 0.5f;
0337:     pOut->y = 0.5f * o[1] + 0.5f;
0338:     pOut->z = 0.5f * o[2] + 0.5f;
0339:     pOut->w = 0.5f * o[3] + 0.5f;
0340: }
0341: 
0342: 
0343: 
0344: 
0345: void CMyD3DApplication::CreateTextureArray( LPDIRECT3DTEXTURE9 pTex )
0346: {
0347:     D3DLOCKED_RECT d3dlr;
0348: 
0349:     pTex->LockRect(0,&d3dlr,NULL,D3DLOCK_READONLY); //サーフェイス上の矩形をロック
0350:     
0351:     unsigned char *p = (unsigned char *)d3dlr.pBits;
0352:     for( int y = 0; y < ASCII_HEIGHT; y++ )
0353:     {
0354:         for( int x = 0; x < ASCII_MAX * ASCII_WIDTH; x++ )
0355:         {
0356:             int idx = x/ASCII_WIDTH;
0357:             m_iTex[idx][y][x%ASCII_WIDTH] = p[ 32 * d3dlr.Pitch * y / ASCII_HEIGHT
0358:                                             + d3dlr.Pitch * x / (ASCII_MAX * ASCII_WIDTH) ];
0359:         }
0360:     }
0361: 
0362:     pTex->UnlockRect(0);
0363: }
0364: 
0365: 
0366: 
0367: 
0368: //-------------------------------------------------------------
0369: // Name: InitDeviceObjects()
0370: // Desc: デバイスが生成された後の初期化をします。
0371: //      フレームバッファフォーマットやデバイスの種類が変わった
0372: //      後に通過します。
0373: //      ここで確保したメモリはDeleteDeviceObjects()で開放します
0374: //-------------------------------------------------------------
0375: HRESULT CMyD3DApplication::InitDeviceObjects()
0376: {
0377:     HRESULT hr;
0378: 
0379: #ifdef DX9C
0380:     // UFOの読み込み
0381:     if(FAILED(hr=m_pMesh  ->Create( m_pd3dDevice, (LPCWSTR)("t-pot.x"))))
0382:         return DXTRACE_ERR( "LoadCar", hr );
0383:     // 地面の読み込み
0384:     if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, (LPCWSTR)("map.x"))))
0385:         return DXTRACE_ERR( "Load BG", hr );
0386: #else // DX9C
0387:     // UFOの読み込み
0388:     if(FAILED(hr=m_pMesh  ->Create( m_pd3dDevice, _T("t-pot.x"))))
0389:         return DXTRACE_ERR( "LoadCar", hr );
0390:     // 地面の読み込み
0391:     if(FAILED(hr=m_pMeshBg->Create( m_pd3dDevice, _T("map.x"))))
0392:         return DXTRACE_ERR( "Load BG", hr );
0393: #endif // DX9C
0394:     m_pMesh->UseMeshMaterials(false);
0395:     m_pMeshBg->UseMeshMaterials(false);
0396:         
0397:     // シェーダの読み込み
0398:     LPD3DXBUFFER pErr;
0399:     if( FAILED( hr = D3DXCreateEffectFromFile(
0400:                 m_pd3dDevice, "hlsl.fx", NULL, NULL, 
0401:                 D3DXSHADER_DEBUG , NULL, &m_pEffect, &pErr ))){
0402:         MessageBox( NULL, (LPCTSTR)pErr->GetBufferPointer()
0403:                     , "ERROR", MB_OK);
0404:         return DXTRACE_ERR( "CreateEffectFromFile", hr );
0405:     }
0406:     m_hTechnique = m_pEffect->GetTechniqueByName( "TShader" );
0407:     m_htSrcMap  = m_pEffect->GetParameterByName( NULL, "SrcMap" );
0408: 
0409:     // フォント
0410:     m_pFont->InitDeviceObjects( m_pd3dDevice );
0411:     return S_OK;
0412: }
0413: 
0414: //-------------------------------------------------------------
0415: // Name: RestoreDeviceObjects()
0416: // Desc: 画面のサイズが変更された時等に呼ばれます。
0417: //      確保したメモリはInvalidateDeviceObjects()で開放します。
0418: //-------------------------------------------------------------
0419: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0420: {
0421:     DWORD i, j;
0422: 
0423:     // メッシュ
0424:     m_pMesh  ->RestoreDeviceObjects( m_pd3dDevice );
0425:     m_pMeshBg->RestoreDeviceObjects( m_pd3dDevice );
0426: 
0427:     // 質感の設定
0428:     D3DMATERIAL9 mtrl;
0429:     D3DUtil_InitMaterial( mtrl, 1.0f, 0.0f, 0.0f );
0430:     m_pd3dDevice->SetMaterial( &mtrl );
0431: 
0432: 
0433:     // レンダリング状態の設定
0434:     RS( D3DRS_DITHERENABLE,   FALSE );
0435:     RS( D3DRS_SPECULARENABLE, FALSE );
0436:     RS( D3DRS_ALPHATESTENABLE, FALSE );
0437:     RS( D3DRS_ZENABLE,        TRUE );
0438:     RS( D3DRS_AMBIENT,        0x000F0F0F );
0439:     
0440:     TSS( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
0441:     TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
0442:     TSS( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
0443:     TSS( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
0444:     TSS( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
0445:     SAMP( 0, D3DSAMP_MINFILTER, D3DTEXF_POINT );
0446:     SAMP( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
0447:     SAMP( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP );
0448:     SAMP( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP );
0449: 
0450:     // ワールド行列
0451:     D3DXMATRIX matIdentity;
0452:     D3DXMatrixIdentity( &m_mWorld );
0453: 
0454:     // ビュー行列
0455:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -5.0f );
0456:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0457:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0458:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0459: 
0460:     // 射影行列
0461:     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
0462:     D3DXMatrixPerspectiveFovLH( &m_mProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
0463: 
0464:     // フォント
0465:     m_pFont->RestoreDeviceObjects();
0466: 
0467:     // テクスチャ
0468:     D3DXCreateTextureFromFileEx(m_pd3dDevice, "abc.bmp", 0,0,1,
0469:         0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_POINT, D3DX_DEFAULT,0, NULL,NULL,
0470:         &m_pAsciiMap);
0471:     CreateTextureArray( m_pAsciiMap );
0472: 
0473:     for( i=0;i<4;i++ )
0474:     {
0475:         if (FAILED(m_pd3dDevice->CreateTexture( 4, 4, 1, 
0476:             0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, m_pWaveletMap+i, NULL)))
0477:             return E_FAIL;
0478:     }
0479: 
0480:     D3DXCreateTexture( m_pd3dDevice, 52, 4, 1, 
0481:             0, D3DFMT_A32B32G32R32F, D3DPOOL_MANAGED, &m_pAsciiCoeffMap);
0482: 
0483:     // シャドウマップの生成
0484:     if (FAILED(m_pd3dDevice->CreateDepthStencilSurface(MAP_WIDTH, MAP_HEIGHT, 
0485:         D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, TRUE, &m_pMapZ, NULL)))
0486:         return E_FAIL;
0487:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_WIDTH, MAP_HEIGHT, 1, 
0488:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pOriginalMap, NULL)))
0489:         return E_FAIL;
0490:     if (FAILED(m_pOriginalMap->GetSurfaceLevel(0, &m_pOriginalMapSurf)))
0491:         return E_FAIL;
0492:     // モノトーン
0493:     if (FAILED(m_pd3dDevice->CreateTexture(
0494:                         COLOR_MAP[0], COLOR_MAP[0], 1,
0495:                         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,
0496:                         D3DPOOL_DEFAULT, &m_pPostMap, NULL)))
0497:         return E_FAIL;
0498:     if (FAILED(m_pPostMap->GetSurfaceLevel(0, &m_pPostMapSurf)))
0499:         return E_FAIL;
0500:     // 最終画像
0501:     if (FAILED(m_pd3dDevice->CreateTexture(MAP_WIDTH, MAP_HEIGHT, 1, 
0502:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pFinalMap, NULL)))
0503:         return E_FAIL;
0504:     if (FAILED(m_pFinalMap->GetSurfaceLevel(0, &m_pFinalMapSurf)))
0505:         return E_FAIL;
0506: 
0507:     // テクスチャ
0508:     for( i=0;i<2;i++ )
0509:     {
0510:         if (FAILED(m_pd3dDevice->CreateTexture( COLOR_MAP[i], COLOR_MAP[i], 1, 
0511:             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pColorMap[i], NULL)))
0512:             return E_FAIL;
0513:         if (FAILED(m_pColorMap[i]->GetSurfaceLevel(0, &m_pColorMapSurf[i])))
0514:             return E_FAIL;
0515:     }
0516: 
0517:     for( i=0;i<2;i++ )
0518:     for( j=0;j<4;j++ )
0519:     {
0520:         if (FAILED(m_pd3dDevice->CreateTexture( COLOR_MAP[i], COLOR_MAP[i], 1, 
0521:             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pCoeffMap[i][j], NULL)))
0522:             return E_FAIL;
0523:         if (FAILED(m_pCoeffMap[i][j]->GetSurfaceLevel(0, &m_pCoeffMapSurf[i][j])))
0524:             return E_FAIL;
0525:     }
0526: 
0527:     for( i=0;i<13;i++ )
0528:     {
0529:         if (FAILED(m_pd3dDevice->CreateTexture( COLOR_MAP[1], COLOR_MAP[1], 1, 
0530:             D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pDiffMap[i], NULL)))
0531:             return E_FAIL;
0532:         if (FAILED(m_pDiffMap[i]->GetSurfaceLevel(0, &m_pDiffMapSurf[i])))
0533:             return E_FAIL;
0534:     }
0535: 
0536:     if (FAILED(m_pd3dDevice->CreateTexture( COLOR_MAP[1], COLOR_MAP[1], 1, 
0537:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pValue0Map, NULL)))
0538:         return E_FAIL;
0539:     if (FAILED(m_pValue0Map->GetSurfaceLevel(0, &m_pValue0MapSurf)))
0540:         return E_FAIL;
0541:     if (FAILED(m_pd3dDevice->CreateTexture( COLOR_MAP[1], COLOR_MAP[1], 1, 
0542:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pIndex0Map, NULL)))
0543:         return E_FAIL;
0544:     if (FAILED(m_pIndex0Map->GetSurfaceLevel(0, &m_pIndex0MapSurf)))
0545:         return E_FAIL;
0546: 
0547:     if (FAILED(m_pd3dDevice->CreateTexture( COLOR_MAP[1], COLOR_MAP[1], 1, 
0548:         D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pIndex1Map, NULL)))
0549:         return E_FAIL;
0550:     if (FAILED(m_pIndex1Map->GetSurfaceLevel(0, &m_pIndex1MapSurf)))
0551:         return E_FAIL;
0552: 
0553:     // Fill textures
0554:     D3DXFillTexture( m_pAsciiCoeffMap, (LPD3DXFILL2D)FillTextureAsciiCoeff, (LPVOID)this );
0555:     for( i=0;i<4;i++ )
0556:     {
0557:         D3DXFillTexture( m_pWaveletMap[i], (LPD3DXFILL2D)FillTextureHaarWavelet, (LPVOID)i );
0558:     }
0559: 
0560:     m_pEffect->OnResetDevice();
0561: 
0562:     return S_OK;
0563: }
0564: 
0565: 
0566: 
0567: 
0568: //-------------------------------------------------------------
0569: // Name: FrameMove()
0570: // Desc: 毎フレーム呼ばれます。アニメの処理などを行います。
0571: //-------------------------------------------------------------
0572: HRESULT CMyD3DApplication::FrameMove()
0573: {
0574:     // 入力データの更新
0575:     UpdateInput( &m_UserInput );
0576: 
0577:     //---------------------------------------------------------
0578:     // 入力に応じて座標系を更新する
0579:     //---------------------------------------------------------
0580:     // 回転
0581:     D3DXMATRIX matRotY;
0582:     D3DXMATRIX matRotX;
0583: 
0584:     if( m_UserInput.bRotateLeft && !m_UserInput.bRotateRight )
0585:         m_fWorldRotY += m_fElapsedTime;
0586:     else
0587:     if( m_UserInput.bRotateRight && !m_UserInput.bRotateLeft )
0588:         m_fWorldRotY -= m_fElapsedTime;
0589: 
0590:     if( m_UserInput.bRotateUp && !m_UserInput.bRotateDown )
0591:         m_fWorldRotX += m_fElapsedTime;
0592:     else
0593:     if( m_UserInput.bRotateDown && !m_UserInput.bRotateUp )
0594:         m_fWorldRotX -= m_fElapsedTime;
0595: 
0596:     D3DXMatrixRotationX( &matRotX, m_fWorldRotX );
0597:     D3DXMatrixRotationY( &matRotY, m_fWorldRotY );
0598: 
0599:     D3DXMatrixMultiply( &m_mWorld, &matRotY, &matRotX );
0600:     
0601:     //---------------------------------------------------------
0602:     // ビュー行列の設定
0603:     //---------------------------------------------------------
0604:     // ズーム
0605:     if( m_UserInput.bZoomIn && !m_UserInput.bZoomOut )
0606:         m_fViewZoom += m_fElapsedTime;
0607:     else if( m_UserInput.bZoomOut && !m_UserInput.bZoomIn )
0608:         m_fViewZoom -= m_fElapsedTime;
0609: 
0610:     D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
0611:     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
0612:     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
0613:     D3DXMatrixLookAtLH( &m_mView, &vFromPt, &vLookatPt, &vUpVec );
0614: 
0615:     return S_OK;
0616: }
0617: //-------------------------------------------------------------
0618: // Name: UpdateInput()
0619: // Desc: 入力データを更新する
0620: //-------------------------------------------------------------
0621: void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
0622: {
0623:     pUserInput->bRotateUp    = ( m_bActive && (GetAsyncKeyState( VK_UP )    & 0x8000) == 0x8000 );
0624:     pUserInput->bRotateDown  = ( m_bActive && (GetAsyncKeyState( VK_DOWN )  & 0x8000) == 0x8000 );
0625:     pUserInput->bRotateLeft  = ( m_bActive && (GetAsyncKeyState( VK_LEFT )  & 0x8000) == 0x8000 );
0626:     pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
0627:     
0628:     pUserInput->bZoomIn      = ( m_bActive && (GetAsyncKeyState( 'Z'     )  & 0x8000) == 0x8000 );
0629:     pUserInput->bZoomOut     = ( m_bActive && (GetAsyncKeyState( 'X'      ) & 0x8000) == 0x8000 );
0630: }
0631: 
0632: 
0633: //-------------------------------------------------------------
0634: // Name: Render()
0635: // Desc: 画面を描画する.
0636: //-------------------------------------------------------------
0637: HRESULT CMyD3DApplication::Render()
0638: {
0639:     D3DXMATRIX m, mT, mR, mW, mView, mProj;
0640:     D3DXVECTOR4 v;
0641:     LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
0642:     D3DVIEWPORT9 oldViewport;
0643:     DWORD i, j;
0644: 
0645:     //---------------------------------------------------------
0646:     // 描画
0647:     //---------------------------------------------------------
0648:     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
0649:     {
0650:         //-------------------------------------------------
0651:         // レンダリングターゲットの保存
0652:         //-------------------------------------------------
0653:         m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
0654:         m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
0655:         m_pd3dDevice->GetViewport(&oldViewport);
0656: 
0657:         if( m_pEffect != NULL ) 
0658:         {
0659:             //-------------------------------------------------
0660:             // レンダリングターゲットの変更
0661:             //-------------------------------------------------
0662:             m_pd3dDevice->SetRenderTarget(0, m_pOriginalMapSurf);
0663:             m_pd3dDevice->SetDepthStencilSurface(m_pMapZ);
0664: 
0665:             // ビューポートの変更
0666:             D3DVIEWPORT9 viewport = {0,0      // 左上の座標
0667:                             , MAP_WIDTH  // 幅
0668:                             , MAP_HEIGHT // 高さ
0669:                             , 0.0f,1.0f};     // 前面、後面
0670:             m_pd3dDevice->SetViewport(&viewport);
0671: 
0672:             // マップのクリア
0673:             m_pd3dDevice->Clear(0L, NULL
0674:                             , D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
0675:                             , 0x003040, 1.0f, 0L);
0676: 
0677:             //-------------------------------------------------
0678:             // 1パス:テクスチャにレンダリングの作成
0679:             //-------------------------------------------------
0680:             m_pEffect->SetTechnique( "SimpleTech" );
0681:             m_pEffect->Begin( NULL, 0 );
0682: 
0683:             mW = m_mWorld;
0684:             m_pEffect->SetMatrix( "mWorldIT", &mW );
0685:             m = mW * m_mView * m_mProj;
0686:             m_pEffect->SetMatrix( "mWorldViewProjection", &m);
0687:             v = D3DXVECTOR4(1,1,1,0);
0688:             m_pEffect->SetVector( "MaterialAmbientColor", &v);
0689:             v = D3DXVECTOR4(0,0,0,0);
0690:             m_pEffect->SetVector( "MaterialDiffuseColor", &v);
0691: 
0692:             m_pEffect->SetTexture( "SrcMap", m_pMeshBg->m_pTextures[0] );
0693: 
0694:             BEGIN_PASS(0);
0695:             m_pMeshBg->Render( m_pd3dDevice );
0696:             END_PASS();
0697: 
0698:             // 飛行モデルの描画
0699:             D3DXMatrixTranslation( &m, 1.0f, 0.0f ,0.0f );
0700:             D3DXMatrixRotationY( &mR,  fmod( 2.0f * m_fTime, 2.0f*3.14159265f) );
0701:             D3DXMatrixTranslation( &mT, 0.0f, 1.0f ,1.0f );
0702:             mW = m * mR * mT * m_mWorld;
0703:             m_pEffect->SetMatrix( "mWorldIT", &mW);
0704:             m = mW * m_mView * m_mProj;
0705:             m_pEffect->SetMatrix( "mWorldViewProjection", &m);
0706:             v = D3DXVECTOR4(0.3,0.3,0.3,0);
0707:             m_pEffect->SetVector( "MaterialAmbientColor", &v);
0708:             v = D3DXVECTOR4(0.7,0.7,0.7,0);
0709:             m_pEffect->SetVector( "MaterialDiffuseColor", &v);
0710:             m_pEffect->SetTexture( "SrcMap", m_pMesh->m_pTextures[0] );
0711: 
0712:             BEGIN_PASS(0);
0713:             m_pMesh  ->Render( m_pd3dDevice );
0714:             END_PASS();
0715: 
0716:             m_pEffect->End();
0717: 
0718: 
0719:             //-------------------------------------------------
0720:             // 縮小バッファにコピー
0721:             //-------------------------------------------------
0722:             RS( D3DRS_ZENABLE, FALSE );
0723:             m_pEffect->SetTechnique( "SmallTech" );
0724:             m_pEffect->Begin( NULL, 0 );
0725:             
0726:             for( i=0;i<2;i++ )
0727:             {
0728:                 m_pd3dDevice->SetRenderTarget(0, m_pColorMapSurf[i]);
0729: 
0730:                 static const float Vertex1[4][5] = {
0731:                     // x      y     z      tu    tv
0732:                     {-1.0f, -1.0f, 0.5f,   0, 0,},
0733:                     {+1.0f, -1.0f, 0.5f,   1, 0,},
0734:                     {+1.0f, +1.0f, 0.5f,   1, 1,},
0735:                     {-1.0f, +1.0f, 0.5f,   0, 1,},
0736:                 };
0737:                 m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0738:                 if(0==i)
0739:                 {
0740:                     m_pEffect->SetTexture( "SrcMap", m_pOriginalMap );
0741:                     m_pEffect->SetFloat( "INV_TEXSIZE", 1.0 / 512.0 );
0742:                 }else{
0743:                     m_pEffect->SetTexture( "SrcMap", m_pColorMap[i-1]);
0744:                     m_pEffect->SetFloat( "INV_TEXSIZE", 1.0f/(FLOAT)COLOR_MAP[i-1]);
0745:                 }
0746:                 
0747:                 BEGIN_PASS(0);
0748:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0749:                                 , 2, Vertex1, 5*sizeof( float ) );
0750:                 END_PASS();
0751:             }
0752:             m_pEffect->End();
0753: 
0754:             //-------------------------------------------------
0755:             // モノトーン化
0756:             //-------------------------------------------------
0757:             m_pEffect->SetTechnique( "TShader" );
0758:             m_pEffect->Begin( NULL, 0 );
0759: 
0760:             //-------------------------------------------------
0761:             // フィルタリング
0762:             //-------------------------------------------------
0763:             m_pd3dDevice->SetRenderTarget(0, m_pPostMapSurf);
0764: 
0765:             TVERTEX Vertex1[4] = {
0766:                 //   x         y         z     w       tu    tv
0767:                 {        0.0f-0.5,         0.0f-0.5, 0.1f, 1.0f,   0, 0,},
0768:                 {COLOR_MAP[0]-0.5,         0.0f-0.5, 0.1f, 1.0f,   1, 0,},
0769:                 {COLOR_MAP[0]-0.5, COLOR_MAP[0]-0.5, 0.1f, 1.0f,   1, 1,},
0770:                 {        0.0f-0.5, COLOR_MAP[0]-0.5, 0.1f, 1.0f,   0, 1,},
0771:             };
0772:             m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0773:             m_pEffect->SetTexture( m_htSrcMap, m_pColorMap[0] );
0774: 
0775:             BEGIN_PASS(0);
0776:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0777:                             , 2, Vertex1, sizeof( TVERTEX ) );
0778:             END_PASS();
0779: 
0780:             m_pEffect->End();
0781: 
0782:             //-------------------------------------------------
0783:             // Wavelet係数の計算
0784:             //-------------------------------------------------
0785:             m_pEffect->SetTechnique( "WaveletTech" );
0786:             m_pEffect->Begin( NULL, 0 );
0787: 
0788:             m_pEffect->SetFloat( "INV_TEXSIZE", 1.0f/(FLOAT)COLOR_MAP[0]);
0789:             m_pEffect->SetTexture( "PointMap", m_pPostMap );
0790:             m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0791: 
0792:             for( i=0;i<4;i++ )
0793:             {
0794:                 m_pd3dDevice->SetRenderTarget(0, m_pCoeffMapSurf[0][i]);
0795: 
0796:                 static float Vertex[4][5] = {
0797:                     // x      y     z     tu tv
0798:                     {-1.0f, -1.0f, 0.5f,   0, 0,},
0799:                     {+1.0f, -1.0f, 0.5f,   1, 0,},
0800:                     {+1.0f, +1.0f, 0.5f,   1, 1,},
0801:                     {-1.0f, +1.0f, 0.5f,   0, 1,},
0802:                 };
0803:                 m_pEffect->SetTexture( "WaveletTexture", m_pWaveletMap[i] );
0804:                 BEGIN_PASS(0);
0805:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0806:                                 , 2, Vertex, 5*sizeof( float ) );
0807:                 END_PASS();
0808:             }
0809: 
0810:             m_pEffect->End();
0811: 
0812: 
0813: 
0814:             //-------------------------------------------------
0815:             // Wavelet係数の縮小化
0816:             //-------------------------------------------------
0817:             m_pEffect->SetTechnique( "SmallTech4" );
0818:             m_pEffect->Begin( NULL, 0 );
0819:             
0820:             for( i=0;i<4;i++ )
0821:             {
0822:                 m_pd3dDevice->SetRenderTarget(0, m_pCoeffMapSurf[1][i]);
0823: 
0824:                 float Vertex1[4][5] = {
0825:                     // x      y     z      tu    tv
0826:                     {-1.0f, -1.0f, 0.5f,   0, 0,},
0827:                     {+1.0f, -1.0f, 0.5f,   1, 0,},
0828:                     {+1.0f, +1.0f, 0.5f,   1, 1,},
0829:                     {-1.0f, +1.0f, 0.5f,   0, 1,},
0830:                 };
0831:                 m_pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_TEX1 );
0832:                 m_pEffect->SetTexture( "SrcMap", m_pCoeffMap[0][i]);
0833:                 m_pEffect->SetFloat( "INV_TEXSIZE", 1.0f/(FLOAT)COLOR_MAP[0]);
0834:                 BEGIN_PASS(0);
0835:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0836:                                 , 2, Vertex1, 5*sizeof( float ) );
0837:                 END_PASS();
0838: 
0839:             }
0840:             m_pEffect->End();
0841: 
0842:             //-------------------------------------------------
0843:             // 各文字との係数の比較をする
0844:             //-------------------------------------------------
0845:             {
0846:             m_pEffect->SetTechnique( "DiffTech" );
0847:             m_pEffect->Begin( NULL, 0 );
0848:             
0849:             m_pEffect->SetTexture( "PointMap", m_pAsciiCoeffMap );
0850:             m_pEffect->SetTexture( "SrcMap0", m_pCoeffMap[1][0]);
0851:             m_pEffect->SetTexture( "SrcMap1", m_pCoeffMap[1][1]);
0852:             m_pEffect->SetTexture( "SrcMap2", m_pCoeffMap[1][2]);
0853:             m_pEffect->SetTexture( "SrcMap3", m_pCoeffMap[1][3]);
0854: 
0855:             static const TVERTEX Vertex[4] = {
0856:                 //        x                 y         z     w     tu tv
0857:                 {        0.0f-0.5,         0.0f-0.5, 0.1f, 1.0f,   0, 0,},
0858:                 {COLOR_MAP[1]-0.5,         0.0f-0.5, 0.1f, 1.0f,   1, 0,},
0859:                 {COLOR_MAP[1]-0.5, COLOR_MAP[1]-0.5, 0.1f, 1.0f,   1, 1,},
0860:                 {        0.0f-0.5, COLOR_MAP[1]-0.5, 0.1f, 1.0f,   0, 1,},
0861:             };
0862: 
0863:             m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0864: 
0865:             for( i=0;i<13;i++ )
0866:             {
0867:                 m_pd3dDevice->SetRenderTarget( 0, m_pDiffMapSurf[i] );
0868: 
0869:                 for(j=0;j<4;j++)
0870:                 {
0871:                     m_pEffect->SetFloat( "INV_TEXSIZE", (1.0/13.0)*(float)i + (1.0/52.0)*(float)j );
0872:                     if(0==j) m_pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED ); else
0873:                     if(1==j) m_pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN ); else
0874:                     if(2==j) m_pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE,  D3DCOLORWRITEENABLE_BLUE); else
0875:                     if(3==j) m_pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA );
0876: 
0877:                     BEGIN_PASS(0);
0878:                     m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0879:                                         , 2, Vertex, sizeof( TVERTEX ) );
0880:                     END_PASS();
0881:                 }
0882:             }
0883:             m_pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE, 0xf );
0884:             m_pEffect->End();
0885:             }
0886: 
0887:             //-------------------------------------------------
0888:             // 係数を比較(52→13)
0889:             //-------------------------------------------------
0890:             {
0891:             m_pEffect->SetTechnique( "CompareTech" );
0892:             m_pEffect->Begin( NULL, 0 );
0893:             
0894:             static const TVERTEX Vertex[4] = {
0895:                 //   x         y         z     w       tu    tv
0896:                 {        0.0f-0.5,         0.0f-0.5, 0.1f, 1.0f,   0, 0,},
0897:                 {COLOR_MAP[1]-0.5,         0.0f-0.5, 0.1f, 1.0f,   1, 0,},
0898:                 {COLOR_MAP[1]-0.5, COLOR_MAP[1]-0.5, 0.1f, 1.0f,   1, 1,},
0899:                 {        0.0f-0.5, COLOR_MAP[1]-0.5, 0.1f, 1.0f,   0, 1,},
0900:             };
0901: 
0902:             m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0903:             m_pd3dDevice->SetRenderTarget( 0, m_pValue0MapSurf );
0904:             m_pd3dDevice->SetRenderTarget( 1, m_pIndex0MapSurf );
0905: 
0906:             for( i=0;i<4;i++ )
0907:             {
0908:                 switch(i)
0909:                 {
0910:                 case 0:
0911:                     m_pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE,D3DCOLORWRITEENABLE_RED );
0912:                     m_pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE1,D3DCOLORWRITEENABLE_RED );
0913:                     break;
0914:                 case 1:
0915:                     m_pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE,D3DCOLORWRITEENABLE_GREEN);
0916:                     m_pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE1,D3DCOLORWRITEENABLE_GREEN);
0917:                     break;
0918:                 case 2:
0919:                     m_pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE,D3DCOLORWRITEENABLE_BLUE);
0920:                     m_pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE1,D3DCOLORWRITEENABLE_BLUE);
0921:                     break;
0922:                 case 3:
0923:                     m_pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE,D3DCOLORWRITEENABLE_ALPHA);
0924:                     m_pd3dDevice->SetRenderState(D3DRS_COLORWRITEENABLE1,D3DCOLORWRITEENABLE_ALPHA);
0925:                     break;
0926:                 }
0927: 
0928:                 if( 3!=i )
0929:                 {
0930:                     m_pEffect->SetTexture( "SrcMap0", m_pDiffMap[i*4+0]);
0931:                     m_pEffect->SetTexture( "SrcMap1", m_pDiffMap[i*4+1]);
0932:                     m_pEffect->SetTexture( "SrcMap2", m_pDiffMap[i*4+2]);
0933:                     m_pEffect->SetTexture( "SrcMap3", m_pDiffMap[i*4+3]);
0934:                 }else{
0935:                     m_pEffect->SetTexture( "SrcMap0", m_pDiffMap[i*4+0]);
0936:                     m_pEffect->SetTexture( "SrcMap1", m_pDiffMap[i*4+0]);
0937:                     m_pEffect->SetTexture( "SrcMap2", m_pDiffMap[i*4+0]);
0938:                     m_pEffect->SetTexture( "SrcMap3", m_pDiffMap[i*4+0]);
0939:                 }
0940: 
0941:                 m_pEffect->SetFloat( "INV_TEXSIZE", (16.0/52.0)*(float)i );
0942: 
0943:                 BEGIN_PASS(0);
0944:                 m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0945:                                 , 2, Vertex, sizeof( TVERTEX ) );
0946:                 END_PASS();
0947:             }
0948:             m_pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE, 0xf );
0949:             m_pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE1, 0xf );
0950:             m_pd3dDevice->SetRenderTarget( 1, NULL );
0951:             m_pEffect->End();
0952:             }
0953:             //-------------------------------------------------
0954:             // 係数を比較(13→1)
0955:             //-------------------------------------------------
0956:             {
0957:             m_pEffect->SetTechnique( "Compare2Tech" );
0958:             m_pEffect->Begin( NULL, 0 );
0959:             
0960:             static const TVERTEX Vertex[4] = {
0961:                 //        x                 y         z     w     tu tv
0962:                 {        0.0f-0.5,         0.0f-0.5, 0.1f, 1.0f,   0, 0,},
0963:                 {COLOR_MAP[1]-0.5,         0.0f-0.5, 0.1f, 1.0f,   1, 0,},
0964:                 {COLOR_MAP[1]-0.5, COLOR_MAP[1]-0.5, 0.1f, 1.0f,   1, 1,},
0965:                 {        0.0f-0.5, COLOR_MAP[1]-0.5, 0.1f, 1.0f,   0, 1,},
0966:             };
0967: 
0968:             m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0969: 
0970:             m_pd3dDevice->SetRenderTarget( 0, m_pIndex1MapSurf );
0971:             
0972:             m_pEffect->SetTexture( "SrcMap0",  m_pValue0Map );
0973:             m_pEffect->SetTexture( "IndexMap", m_pIndex0Map);
0974: 
0975:             BEGIN_PASS(0);
0976:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
0977:                                 , 2, Vertex, sizeof( TVERTEX ) );
0978:             END_PASS();
0979: 
0980:             m_pEffect->End();
0981:             }
0982: 
0983:             //-------------------------------------------------
0984:             // 最終合成
0985:             //-------------------------------------------------
0986:             {
0987:             m_pEffect->SetTechnique( "FinalTech" );
0988:             m_pEffect->Begin( NULL, 0 );
0989: 
0990:             //-------------------------------------------------
0991:             // フィルタリング
0992:             //-------------------------------------------------
0993:             m_pd3dDevice->SetRenderTarget(0, m_pFinalMapSurf);
0994: 
0995:             static const TVERTEX Vertex[4] = {
0996:                 //      x               y         z     w     tu tv
0997:                 {      0.0f-0.5,       0.0f-0.5, 0.1f, 1.0f,   0, 0,},
0998:                 { MAP_WIDTH-0.5,       0.0f-0.5, 0.1f, 1.0f,   1, 0,},
0999:                 { MAP_WIDTH-0.5, MAP_HEIGHT-0.5, 0.1f, 1.0f,   1, 1,},
1000:                 {      0.0f-0.5, MAP_HEIGHT-0.5, 0.1f, 1.0f,   0, 1,},
1001:             };
1002:             m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1003:             m_pEffect->SetTexture( m_htSrcMap, m_pAsciiMap );
1004:             m_pEffect->SetTexture( "PointMap", m_pColorMap[1] );
1005:             m_pEffect->SetTexture( "SrcMap0",  m_pIndex1Map );
1006:             
1007:             BEGIN_PASS(0);
1008:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1009:                             , 2, Vertex, sizeof( TVERTEX ) );
1010:             END_PASS();
1011: 
1012:             m_pEffect->End();
1013:             }
1014: 
1015: 
1016:         }
1017: 
1018:         //-----------------------------------------------------
1019:         // レンダリングターゲットを元に戻す
1020:         //-----------------------------------------------------
1021:         m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
1022:         m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
1023:         m_pd3dDevice->SetViewport(&oldViewport);
1024:         pOldBackBuffer->Release();
1025:         pOldZBuffer->Release();
1026: 
1027:         //-----------------------------------------------------
1028:         // そのまま張る
1029:         //-----------------------------------------------------
1030:         FLOAT w = (FLOAT)oldViewport.Width;
1031:         FLOAT h = (FLOAT)oldViewport.Height;
1032:         TSS(0,D3DTSS_COLOROP,   D3DTOP_SELECTARG1);
1033:         TSS(0,D3DTSS_COLORARG1, D3DTA_TEXTURE);
1034:         TSS(1,D3DTSS_COLOROP,   D3DTOP_DISABLE);
1035:             
1036:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1037: #if 0
1038:         TVERTEX Vertex1[4] = {
1039:             //x  y   z    rhw  tu tv
1040:             { 0 - 0.5f, 0 - 0.5f, 0.1f, 1.0f, 0, 0,},
1041:             { w - 0.5f, 0 - 0.5f, 0.1f, 1.0f, 1, 0,},
1042:             { w - 0.5f, h - 0.5f, 0.1f, 1.0f, 1, 1,},
1043:             { 0 - 0.5f, h - 0.5f, 0.1f, 1.0f, 0, 1,},
1044:         };
1045:         m_pd3dDevice->SetTexture(0, m_pOriginalMap);
1046:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1047:                             , 2, Vertex1, sizeof( TVERTEX ) );
1048: #endif
1049:         //-----------------------------------------------------
1050:         // 変換したものを張る
1051:         //-----------------------------------------------------
1052: 
1053:         TVERTEX Vertex2[4] = {
1054:             //   x    y   z    rhw    tu    tv
1055: #if 0
1056:             { 0.0f - 0.5f,   0 - 0.5f, 0.1f, 1.0f, 0.0f, 0.0f,},
1057:             { 0.5f*w - 0.5f, 0 - 0.5f, 0.1f, 1.0f, 0.5f, 0.0f,},
1058:             { 0.5f*w - 0.5f, h - 0.5f, 0.1f, 1.0f, 0.5f, 1.0f,},
1059:             { 0.0f - 0.5f,   h - 0.5f, 0.1f, 1.0f, 0.0f, 1.0f,},
1060: #else
1061:             { 0.0f - 0.5f, 0 - 0.5f, 0.1f, 1.0f, 0.0f, 0.0f,},
1062:             {    w - 0.5f, 0 - 0.5f, 0.1f, 1.0f, 1.0f, 0.0f,},
1063:             {    w - 0.5f, h - 0.5f, 0.1f, 1.0f, 1.0f, 1.0f,},
1064:             { 0.0f - 0.5f, h - 0.5f, 0.1f, 1.0f, 0.0f, 1.0f,},
1065: #endif
1066:         };
1067:         m_pd3dDevice->SetTexture(0, m_pFinalMap );
1068: //      m_pd3dDevice->SetTexture(0, m_pColorMap[1] );
1069: //      m_pd3dDevice->SetTexture(0, m_pPostMap);
1070:         m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
1071:                             , 2, Vertex2, sizeof( TVERTEX ) );
1072: 
1073:         {
1074:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,    D3DTOP_SELECTARG1);
1075:         m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,  D3DTA_TEXTURE);
1076:         m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
1077:         m_pd3dDevice->SetVertexShader(NULL);
1078:         m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
1079:         m_pd3dDevice->SetPixelShader(0);
1080:         float scale = 64.0f;
1081: #ifdef _DEBUG// デバッグ用にテクスチャを表示する
1082:         for(DWORD i=0; i<8; i++){
1083: #else
1084:         for(DWORD i=0; i<1; i++){
1085: #endif
1086:             TVERTEX Vertex[4] = {
1087:                 // x  y  z rhw tu tv
1088:                 {    0,(i+0)*scale,0, 1, 0, 0,},
1089:                 {scale,(i+0)*scale,0, 1, 1, 0,},
1090:                 {scale,(i+1)*scale,0, 1, 1, 1,},
1091:                 {    0,(i+1)*scale,0, 1, 0, 1,},
1092:             };
1093:             if(0==i) m_pd3dDevice->SetTexture( 0, m_pOriginalMap );
1094:             if(1==i) m_pd3dDevice->SetTexture( 0, m_pPostMap );
1095:             if(2==i) m_pd3dDevice->SetTexture( 0, m_pAsciiCoeffMap );
1096:             if(3==i) m_pd3dDevice->SetTexture( 0, m_pColorMap[1] );
1097: 
1098:             if(4==i) m_pd3dDevice->SetTexture( 0, m_pValue0Map );
1099:             if(5==i) m_pd3dDevice->SetTexture( 0, m_pIndex0Map );
1100:             if(6==i) m_pd3dDevice->SetTexture( 0, m_pIndex1Map );
1101:             if(4==i) m_pd3dDevice->SetTexture( 0, m_pDiffMap[0] );
1102:             if(5==i) m_pd3dDevice->SetTexture( 0, m_pDiffMap[1] );
1103:             if(6==i) m_pd3dDevice->SetTexture( 0, m_pDiffMap[2] );
1104:             if(7==i) m_pd3dDevice->SetTexture( 0, m_pDiffMap[3] );
1105: //          if(4==i) m_pd3dDevice->SetTexture( 0, m_pCoeffMap[1][0] );
1106: //          if(5==i) m_pd3dDevice->SetTexture( 0, m_pCoeffMap[1][1] );
1107: //          if(6==i) m_pd3dDevice->SetTexture( 0, m_pCoeffMap[1][2] );
1108: //          if(7==i) m_pd3dDevice->SetTexture( 0, m_pCoeffMap[1][3] );
1109: //          if(4==i) m_pd3dDevice->SetTexture( 0, m_pWaveletMap[0] );
1110: //          if(5==i) m_pd3dDevice->SetTexture( 0, m_pWaveletMap[1] );
1111: //          if(6==i) m_pd3dDevice->SetTexture( 0, m_pWaveletMap[2] );
1112: //          if(7==i) m_pd3dDevice->SetTexture( 0, m_pWaveletMap[3] );
1113:             m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
1114:         }
1115:         }
1116: 
1117:         // ヘルプの表示
1118:         RenderText();
1119: 
1120:         RS( D3DRS_ZENABLE, TRUE );
1121: 
1122:         // 描画の終了
1123:         m_pd3dDevice->EndScene();
1124:     }
1125: 
1126:     return S_OK;
1127: }
1128: 
1129: 
1130: 
1131: 
1132: //-------------------------------------------------------------
1133: // Name: RenderText()
1134: // Desc: 状態やヘルプを画面に表示する
1135: //-------------------------------------------------------------
1136: HRESULT CMyD3DApplication::RenderText()
1137: {
1138:     D3DCOLOR fontColor        = D3DCOLOR_ARGB(255,255,255,0);
1139:     TCHAR szMsg[MAX_PATH] = TEXT("");
1140: 
1141:     FLOAT fNextLine = 40.0f; // 表示する高さ
1142: 
1143:     // 操作法やパラメータを表示する
1144:     fNextLine = (FLOAT) m_d3dsdBackBuffer.Height; 
1145: //    lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
1146: //    fNextLine -= 20.0f;
1147: //    m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1148: 
1149:     lstrcpy( szMsg, m_strDeviceStats );
1150:     fNextLine -= 20.0f;
1151:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1152:     lstrcpy( szMsg, m_strFrameStats );
1153:     fNextLine -= 20.0f;
1154:     m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
1155:     
1156: #ifndef _DEBUG
1157:     fNextLine = (FLOAT) 2; 
1158:     m_pFont->DrawText( m_d3dsdBackBuffer.Width-80+2, fNextLine+2, 0xff000000, TEXT("t-pot.com") );
1159:     m_pFont->DrawText( m_d3dsdBackBuffer.Width-80+0, fNextLine,   0xffff8040, TEXT("t-pot.com") );
1160: #endif //! _DEBUG
1161: 
1162:     return S_OK;
1163: }
1164: 
1165: 
1166: 
1167: 
1168: //-------------------------------------------------------------
1169: // Name: MsgProc()
1170: // Desc: WndProc をオーバーライドしたもの
1171: //-------------------------------------------------------------
1172: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
1173:                                  WPARAM wParam, LPARAM lParam )
1174: {
1175:     switch( msg )
1176:     {
1177:         case WM_PAINT:
1178:         {
1179:             if( m_bLoadingApp )
1180:             {
1181:                 // ロード中
1182:                 HDC hDC = GetDC( hWnd );
1183:                 TCHAR strMsg[MAX_PATH];
1184:                 wsprintf(strMsg, TEXT("Loading... Please wait"));
1185:                 RECT rct;
1186:                 GetClientRect( hWnd, &rct );
1187:                 DrawText( hDC, strMsg, -1, &rct
1188:                         , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
1189:                 ReleaseDC( hWnd, hDC );
1190:             }
1191:             break;
1192:         }
1193: 
1194:     }
1195: 
1196:     return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
1197: }
1198: 
1199: 
1200: 
1201: 
1202: //-------------------------------------------------------------
1203: // Name: InvalidateDeviceObjects()
1204: // Desc: RestoreDeviceObjects() で作成したオブジェクトの開放
1205: //-------------------------------------------------------------
1206: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
1207: {
1208:     DWORD i, j;
1209: 
1210:     // テクスチャ
1211:     SAFE_RELEASE(m_pFinalMapSurf);
1212:     SAFE_RELEASE(m_pFinalMap);
1213:     SAFE_RELEASE(m_pPostMapSurf);
1214:     SAFE_RELEASE(m_pPostMap);
1215:     SAFE_RELEASE(m_pOriginalMapSurf);
1216:     SAFE_RELEASE(m_pOriginalMap);
1217:     SAFE_RELEASE(m_pMapZ);
1218: 
1219:     SAFE_RELEASE(m_pIndex1MapSurf);
1220:     SAFE_RELEASE(m_pIndex1Map);
1221:     SAFE_RELEASE(m_pValue0MapSurf);
1222:     SAFE_RELEASE(m_pValue0Map);
1223:     SAFE_RELEASE(m_pIndex0MapSurf);
1224:     SAFE_RELEASE(m_pIndex0Map);
1225: 
1226:     for( i=0;i<13;i++ )
1227:     {
1228:         SAFE_RELEASE(m_pDiffMapSurf[i]);
1229:         SAFE_RELEASE(m_pDiffMap[i]);
1230:     }
1231: 
1232:     for( i=0;i<2;i++ )
1233:     {
1234:         SAFE_RELEASE(m_pColorMapSurf[i]);
1235:         SAFE_RELEASE(m_pColorMap[i]);
1236:     }
1237: 
1238:     // テクスチャ
1239:     for( i=0;i<2;i++ )
1240:     for( j=0;j<4;j++ )
1241:     {
1242:         SAFE_RELEASE(m_pCoeffMapSurf[i][j]);
1243:         SAFE_RELEASE(m_pCoeffMap[i][j]);
1244:     }
1245: 
1246:     // テクスチャ
1247:     SAFE_RELEASE(m_pAsciiCoeffMap);
1248:     SAFE_RELEASE(m_pAsciiMap);
1249:     for( i=0;i<4;i++ )
1250:     {
1251:         SAFE_RELEASE(m_pWaveletMap[i]);
1252:     }
1253: 
1254:     m_pMesh  ->InvalidateDeviceObjects(); // メッシュ
1255:     m_pMeshBg->InvalidateDeviceObjects();
1256: 
1257:     m_pFont->InvalidateDeviceObjects(); // フォント
1258: 
1259:     // シェーダ
1260:     if( m_pEffect != NULL ) m_pEffect->OnLostDevice();
1261: 
1262:     return S_OK;
1263: }
1264: 
1265: 
1266: 
1267: 
1268: //-------------------------------------------------------------
1269: // Name: DeleteDeviceObjects()
1270: // Desc: InitDeviceObjects() で作成したオブジェクトを開放する
1271: //-------------------------------------------------------------
1272: HRESULT CMyD3DApplication::DeleteDeviceObjects()
1273: {
1274:     // シェーダ
1275:     SAFE_RELEASE( m_pEffect );
1276: 
1277:     // メッシュ
1278:     m_pMesh  ->Destroy();
1279:     m_pMeshBg->Destroy();
1280: 
1281:     // フォント
1282:     m_pFont->DeleteDeviceObjects();
1283: 
1284:     return S_OK;
1285: }
1286: 
1287: 
1288: 
1289: 
1290: //-------------------------------------------------------------
1291: // Name: FinalCleanup()
1292: // Desc: 終了する直線に呼ばれる
1293: //-------------------------------------------------------------
1294: HRESULT CMyD3DApplication::FinalCleanup()
1295: {
1296:     SAFE_DELETE( m_pMeshBg ); // メッシュ
1297:     SAFE_DELETE( m_pMesh );
1298: 
1299:     SAFE_DELETE( m_pFont ); // フォント
1300: 
1301:     return S_OK;
1302: }
1303: 
1304: 
1305: 
1306: 
1307: