0001: //--------------------------------------------------------------------------------------
0002: // File: main.cpp
0003: //--------------------------------------------------------------------------------------
0004: #include "dxstdafx.h"
0005: #include "resource.h"
0006: #include "rendertarget.h"
0007: 
0008: #undef FAST_RENDERING
0009: //#define FAST_RENDERING
0010: 
0011: //#define DEBUG_VS   // Uncomment this line to debug vertex shaders 
0012: //#define DEBUG_PS   // Uncomment this line to debug pixel shaders 
0013: 
0014: #define ORIGINAL_WIDTH   1024.0f
0015: #define ORIGINAL_HEIGHT  1024.0f
0016: 
0017: #define SCREEN_WIDTH   512.0f
0018: #define SCREEN_HEIGHT  512.0f
0019: //#define SCREEN_WIDTH   1024.0f
0020: //#define SCREEN_HEIGHT  1024.0f
0021: //#define SCREEN_WIDTH  1280.0f
0022: //#define SCREEN_HEIGHT  720.0f
0023: 
0024: #define SCREEN_FORMAT   RTF_RGBA
0025: //#define SCREEN_FORMAT     RTF_RGBA16F
0026: 
0027: #define DEPTH_FORMAT    RTF_RGBA
0028: //#define DEPTH_FORMAT  RTF_R16F
0029: 
0030: #define Z_NEAR 0.5f
0031: #define Z_FAR  10.0f
0032: //--------------------------------------------------------------------------------------
0033: // Vertex format
0034: //--------------------------------------------------------------------------------------
0035: struct VERTEX 
0036: {
0037:     D3DXVECTOR4 pos;
0038:     D3DXVECTOR2 tex1;
0039: 
0040:     static const DWORD FVF;
0041: };
0042: const DWORD VERTEX::FVF = D3DFVF_XYZRHW | D3DFVF_TEX1;
0043: 
0044: 
0045: typedef struct {
0046:     FLOAT       p[4];
0047:     FLOAT       tu, tv;
0048: } TVERTEX;
0049: 
0050: 
0051: 
0052: //--------------------------------------------------------------------------------------
0053: // UI control IDs
0054: //--------------------------------------------------------------------------------------
0055: #define IDC_TOGGLEFULLSCREEN     1
0056: #define IDC_TOGGLEREF            2
0057: #define IDC_CHANGEDEVICE         3
0058: #define IDC_CHANGE_SCENE         4
0059: #define IDC_PAUSE                10
0060: #define IDC_INTENSITY_STATIC     11
0061: #define IDC_INTENSITY            12
0062: #define IDC_RADIUS_STATIC        13
0063: #define IDC_RADIUS               14
0064: 
0065: //--------------------------------------------------------------------------------------
0066: // Global variables
0067: //--------------------------------------------------------------------------------------
0068: HWND g_hWnd;
0069: ID3DXFont*              g_pFont = NULL;         // Font for drawing text
0070: ID3DXSprite*            g_pTextSprite = NULL;   // Sprite for batching draw text calls
0071: CFirstPersonCamera      g_Camera;               // A model viewing camera
0072: bool                    g_bShowHelp = true;     // If true, it renders the UI control text
0073: CDXUTDialogResourceManager g_DialogResourceManager; // manager for shared resources of dialogs
0074: CD3DSettingsDlg         g_SettingsDlg;          // Device settings dialog
0075: CDXUTDialog             g_HUD;                  // dialog for standard controls
0076: CDXUTDialog             g_SampleUI;             // dialog for sample specific controls
0077: bool                    g_bPause = false;
0078: //bool                    g_bPause = true;
0079: float                   g_fPhase = D3DX_PI * 0.8f;
0080: 
0081: VERTEX                  g_Vertex[4];
0082: 
0083: // Render target
0084: CRenderTarget          *g_pRT_FullScreen=0;
0085: D3DVIEWPORT9            g_ViewportFB;
0086: CRenderTarget          *g_pRT_Depth=0;
0087: CRenderTarget          *g_pRT_GaussX=0;
0088: CRenderTarget          *g_pRT_Blur=0;
0089: 
0090: // BG object
0091: bool                    g_bObj = true;
0092: LPD3DXMESH              g_pObjMesh=0;
0093: LPDIRECT3DTEXTURE9      g_pObjMeshTexture=0;
0094: D3DXMATRIXA16           g_matObjWorld;
0095: 
0096: // Map object
0097: bool                    g_bMap = true;
0098: LPD3DXMESH              g_pMapMesh=0;
0099: LPDIRECT3DTEXTURE9      g_pMapMeshTexture=0;
0100: D3DXMATRIXA16           g_matMapWorld;
0101: 
0102: // Scene management
0103: DWORD                   g_dwBackgroundColor;
0104: 
0105: 
0106: // Effect system
0107: LPD3DXEFFECT            g_pEffect;
0108: D3DXHANDLE              g_hWorld = NULL;
0109: D3DXHANDLE              g_hWorldView = NULL;
0110: D3DXHANDLE              g_hWorldViewProjection = NULL;
0111: 
0112: D3DXHANDLE              g_hMeshTexture = NULL;
0113: 
0114: D3DXHANDLE              g_hTechScene           = NULL;
0115: D3DXHANDLE              g_hTechFinal           = NULL;
0116: float                   g_fGaussRadius = 4.0f;
0117: float                   g_fIntensity = 20.0f;
0118: 
0119: //--------------------------------------------------------------------------------------
0120: // Forward declarations 
0121: //--------------------------------------------------------------------------------------
0122: bool    CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext );
0123: bool    CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext );
0124: HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
0125: HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
0126: void    CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
0127: void    CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
0128: LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext );
0129: void    CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext );
0130: void    CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext );
0131: void    CALLBACK OnLostDevice( void* pUserContext );
0132: void    CALLBACK OnDestroyDevice( void* pUserContext );
0133: 
0134: void    InitApp();
0135: HRESULT LoadMesh( IDirect3DDevice9* pd3dDevice, WCHAR* strFileName, ID3DXMesh** ppMesh );
0136: void    RenderText(bool bFast);
0137: void    SetupQuad( const D3DSURFACE_DESC* pBackBufferSurfaceDesc );
0138: 
0139: 
0140: void SetGaussWeight()
0141: {
0142: //   const float dispersion = 0.3f;
0143:      const float dispersion = 1.f;
0144:      const unsigned int WEIGHT_MUN = 4;
0145:      float tbl[WEIGHT_MUN];
0146:      DWORD i;
0147:  
0148:      FLOAT total=0;
0149:      for( i=0; i<WEIGHT_MUN; i++ ){
0150:          tbl[i] = expf(-0.5f*(FLOAT)(i*i)/dispersion);
0151:          if(0==i){
0152:              total += tbl[i];
0153:          }else{
0154:              // 中心以外は、2回同じ係数を使うので2倍
0155:              total += 2.0f*tbl[i];
0156:          }
0157:      }
0158:      // 規格化
0159:      for( i=0; i<WEIGHT_MUN; i++ ) tbl[i] /= total;
0160:  
0161:      if(g_pEffect) g_pEffect->SetFloatArray("weight", tbl, WEIGHT_MUN);
0162:  
0163: }
0164: 
0165: void SetGaussRadius()
0166: {
0167:     float range = g_fGaussRadius;
0168:     
0169:     D3DXVECTOR4  width(2.0f*range/ORIGINAL_WIDTH, 4.0f*range/ORIGINAL_WIDTH, 6.0f*range/ORIGINAL_WIDTH, 8.0f*range/ORIGINAL_WIDTH);
0170:     D3DXVECTOR4 height(2.0f*range/ORIGINAL_WIDTH, 4.0f*range/ORIGINAL_WIDTH, 6.0f*range/ORIGINAL_WIDTH, 8.0f*range/ORIGINAL_WIDTH);
0171:     
0172:     if(g_pEffect) g_pEffect->SetVector("vBias",  &width);
0173:     if(g_pEffect) g_pEffect->SetVector("hBias", &height);
0174: }
0175: 
0176: //--------------------------------------------------------------------------------------
0177: // Entry point to the program. Initializes everything and goes into a message processing 
0178: // loop. Idle time is used to render the scene.
0179: //--------------------------------------------------------------------------------------
0180: INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
0181: {
0182:     // Enable run-time memory check for debug builds.
0183: #if defined(DEBUG) | defined(_DEBUG)
0184:     _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
0185: #endif
0186: 
0187:     // Set the callback functions. These functions allow DXUT to notify
0188:     // the application about device changes, user input, and windows messages.  The 
0189:     // callbacks are optional so you need only set callbacks for events you're interested 
0190:     // in. However, if you don't handle the device reset/lost callbacks then the sample 
0191:     // framework won't be able to reset your device since the application must first 
0192:     // release all device resources before resetting.  Likewise, if you don't handle the 
0193:     // device created/destroyed callbacks then DXUT won't be able to 
0194:     // recreate your device resources.
0195:     DXUTSetCallbackDeviceCreated( OnCreateDevice );
0196:     DXUTSetCallbackDeviceReset( OnResetDevice );
0197:     DXUTSetCallbackDeviceLost( OnLostDevice );
0198:     DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
0199:     DXUTSetCallbackMsgProc( MsgProc );
0200:     DXUTSetCallbackKeyboard( KeyboardProc );
0201:     DXUTSetCallbackFrameRender( OnFrameRender );
0202:     DXUTSetCallbackFrameMove( OnFrameMove );
0203: 
0204:     // Show the cursor and clip it when in full screen
0205:     DXUTSetCursorSettings( true, true );
0206: 
0207:     InitApp();
0208: 
0209:     // Initialize DXUT and create the desired Win32 window and Direct3D 
0210:     // device for the application. Calling each of these functions is optional, but they
0211:     // allow you to set several options which control the behavior of the framework.
0212:     DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
0213:     DXUTCreateWindow( L"Unsharp Masking" );
0214:     DXUTCreateDevice( D3DADAPTER_DEFAULT, true, (DWORD)SCREEN_WIDTH, (DWORD)SCREEN_HEIGHT, IsDeviceAcceptable, ModifyDeviceSettings );
0215: //    DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );
0216: 
0217:     // Pass control to DXUT for handling the message pump and 
0218:     // dispatching render calls. DXUT will call your FrameMove 
0219:     // and FrameRender callback when there is idle time between handling window messages.
0220:     DXUTMainLoop();
0221: 
0222:     // Perform any application-level cleanup here. Direct3D device resources are released within the
0223:     // appropriate callback functions and therefore don't require any cleanup code here.
0224: 
0225:     return DXUTGetExitCode();
0226: }
0227: 
0228: 
0229: //--------------------------------------------------------------------------------------
0230: // Initialize the app 
0231: //--------------------------------------------------------------------------------------
0232: void InitApp()
0233: {
0234:     g_pFont = NULL;
0235: 
0236:     g_pEffect = NULL;
0237: 
0238:     g_bShowHelp = TRUE;
0239:     g_dwBackgroundColor = 0x00003F3F;
0240: 
0241:     // Initialize dialogs
0242:     g_SettingsDlg.Init( &g_DialogResourceManager );
0243:     g_HUD.Init( &g_DialogResourceManager );
0244:     g_SampleUI.Init( &g_DialogResourceManager );
0245: 
0246:     g_HUD.SetCallback( OnGUIEvent ); int iY = 10; 
0247:     g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 125, 22 );
0248:     g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 35, iY += 24, 125, 22 );
0249:     g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY += 24, 125, 22, VK_F2 );
0250: 
0251:     g_SampleUI.SetCallback( OnGUIEvent ); iY = 100; 
0252: 
0253:     WCHAR sz[100];
0254: 
0255:     g_SampleUI.AddCheckBox( IDC_PAUSE, L"PAUSE", 50, iY += 24, 145, 22, g_bPause );
0256: //    iY += 24;
0257:     StringCchPrintf( sz, 100, L"Intensity: %0.2f", g_fIntensity ); 
0258:     g_SampleUI.AddStatic( IDC_INTENSITY_STATIC, sz, 35, iY += 24, 125, 22 );
0259:     g_SampleUI.AddSlider( IDC_INTENSITY, 50, iY += 24, 100, 22, 0, 10000, (int) (100.0f*g_fIntensity) );
0260: 
0261:     StringCchPrintf( sz, 100, L"Radius: %0.2f", g_fGaussRadius ); 
0262:     g_SampleUI.AddStatic( IDC_RADIUS_STATIC, sz, 35, iY += 24, 125, 22 );
0263:     g_SampleUI.AddSlider( IDC_RADIUS, 50, iY += 24, 100, 22, 0, 1000, (int) (100.0f*g_fGaussRadius) );
0264: 
0265: }
0266: 
0267: 
0268: //--------------------------------------------------------------------------------------
0269: // Called during device initialization, this code checks the device for some 
0270: // minimum set of capabilities, and rejects those that don't pass by returning false.
0271: //--------------------------------------------------------------------------------------
0272: bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
0273:                                   D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
0274: {
0275:     // Skip backbuffer formats that don't support alpha blending
0276:     IDirect3D9* pD3D = DXUTGetD3DObject(); 
0277:     if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
0278:                     AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, 
0279:                     D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
0280:         return false;
0281: 
0282:     // Must support pixel shader 1.1
0283:     if( pCaps->PixelShaderVersion < D3DPS_VERSION( 2, 0 ) )
0284:         return false;
0285: 
0286:     return true;
0287: }
0288: 
0289: 
0290: //--------------------------------------------------------------------------------------
0291: // This callback function is called immediately before a device is created to allow the 
0292: // application to modify the device settings. The supplied pDeviceSettings parameter 
0293: // contains the settings that the framework has selected for the new device, and the 
0294: // application can make any desired changes directly to this structure.  Note however that 
0295: // DXUT will not correct invalid device settings so care must be taken 
0296: // to return valid device settings, otherwise IDirect3D9::CreateDevice() will fail.  
0297: //--------------------------------------------------------------------------------------
0298: bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
0299: {
0300:     // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW 
0301:     // then switch to SWVP.
0302:     if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
0303:          pCaps->VertexShaderVersion < D3DVS_VERSION(2,0) )
0304:     {
0305:         pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
0306:     }
0307: 
0308:     // Debugging vertex shaders requires either REF or software vertex processing 
0309:     // and debugging pixel shaders requires REF.  
0310: #ifdef DEBUG_VS
0311:     if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF )
0312:     {
0313:         pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
0314:         pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;                            
0315:         pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
0316:     }
0317: #endif
0318: #ifdef DEBUG_PS
0319:     pDeviceSettings->DeviceType = D3DDEVTYPE_REF;
0320: #endif
0321: 
0322:     // For the first device created if its a REF device, optionally display a warning dialog box
0323:     static bool s_bFirstTime = true;
0324:     if( s_bFirstTime )
0325:     {
0326:         s_bFirstTime = false;
0327:         if( pDeviceSettings->DeviceType == D3DDEVTYPE_REF )
0328:             DXUTDisplaySwitchingToREFWarning();
0329:     }
0330: 
0331:     return true;
0332: }
0333: 
0334: 
0335: //--------------------------------------------------------------------------------------
0336: // This function loads the mesh and ensures the mesh has normals; it also optimizes the 
0337: // mesh for the graphics card's vertex cache, which improves performance by organizing 
0338: // the internal triangle list for less cache misses.
0339: //--------------------------------------------------------------------------------------
0340: HRESULT LoadMesh( IDirect3DDevice9* pd3dDevice, WCHAR* strFileName, ID3DXMesh** ppMesh )
0341: {
0342:     ID3DXMesh* pMesh = NULL;
0343:     WCHAR str[MAX_PATH];
0344:     HRESULT hr;
0345: 
0346:     // Load the mesh with D3DX and get back a ID3DXMesh*.  For this
0347:     // sample we'll ignore the X file's embedded materials since we know 
0348:     // exactly the model we're loading.  See the mesh samples such as
0349:     // "OptimizedMesh" for a more generic mesh loading example.
0350:     V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, strFileName ) );
0351: 
0352:     V_RETURN( D3DXLoadMeshFromX(str, D3DXMESH_MANAGED, pd3dDevice, NULL, NULL, NULL, NULL, &pMesh) );
0353: 
0354:     // Make sure there are normals which are required for lighting
0355:     if( !(pMesh->GetFVF() & D3DFVF_NORMAL) )
0356:     {
0357:         ID3DXMesh* pTempMesh;
0358:         V( pMesh->CloneMeshFVF( pMesh->GetOptions(), 
0359:                                   pMesh->GetFVF() | D3DFVF_NORMAL, 
0360:                                   pd3dDevice, &pTempMesh ) );
0361:         V( D3DXComputeNormals( pTempMesh, NULL ) );
0362: 
0363:         SAFE_RELEASE( pMesh );
0364:         pMesh = pTempMesh;
0365:     }
0366: 
0367:     *ppMesh = pMesh;
0368: 
0369:     return S_OK;
0370: }
0371: 
0372: HRESULT OptimizeMesh( IDirect3DDevice9* pd3dDevice, ID3DXMesh** ppMesh )
0373: {
0374:     ID3DXMesh* pMesh = *ppMesh;
0375:     DWORD *rgdwAdjacency = NULL;
0376:     HRESULT hr;
0377: 
0378:     // Optimize the mesh for this graphics card's vertex cache 
0379:     // so when rendering the mesh's triangle list the vertices will 
0380:     // cache hit more often so it won't have to re-execute the vertex shader 
0381:     // on those vertices so it will improve perf.     
0382:     rgdwAdjacency = new DWORD[pMesh->GetNumFaces() * 3];
0383:     if( rgdwAdjacency == NULL )
0384:         return E_OUTOFMEMORY;
0385:     V( pMesh->GenerateAdjacency(1e-6f,rgdwAdjacency) );
0386:     V( pMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE, rgdwAdjacency, NULL, NULL, NULL) );
0387:     delete []rgdwAdjacency;
0388: 
0389:     *ppMesh = pMesh;
0390: 
0391:     return S_OK;
0392: }
0393: 
0394: 
0395: //--------------------------------------------------------------------------------------
0396: // This callback function will be called immediately after the Direct3D device has been 
0397: // created, which will happen during application initialization and windowed/full screen 
0398: // toggles. This is the best location to create D3DPOOL_MANAGED resources since these 
0399: // resources need to be reloaded whenever the device is destroyed. Resources created  
0400: // here should be released in the OnDestroyDevice callback. 
0401: //--------------------------------------------------------------------------------------
0402: HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
0403: {
0404:     WCHAR str[MAX_PATH];
0405:     HRESULT hr;
0406: 
0407:     V_RETURN( g_DialogResourceManager.OnCreateDevice( pd3dDevice ) );
0408:     V_RETURN( g_SettingsDlg.OnCreateDevice( pd3dDevice ) );
0409: 
0410:     // Initialize the font
0411:     V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, 
0412:                          OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, 
0413:                          L"Arial", &g_pFont ) );
0414: 
0415:     // Load meshs
0416:     V_RETURN( LoadMesh( pd3dDevice, TEXT("t-pot.x"), &g_pObjMesh ) );
0417:     V_RETURN( OptimizeMesh( pd3dDevice, &g_pObjMesh ) );
0418:     V_RETURN( LoadMesh( pd3dDevice, TEXT("room.x"), &g_pMapMesh ) );
0419:     V_RETURN( OptimizeMesh( pd3dDevice, &g_pMapMesh ) );
0420: 
0421:     // Load mesh textures
0422:     V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"t-pot.bmp" ) );
0423:     V_RETURN( D3DXCreateTextureFromFile( pd3dDevice, str, &g_pObjMeshTexture) );
0424:     V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"room.bmp" ) );
0425:     V_RETURN( D3DXCreateTextureFromFile( pd3dDevice, str, &g_pMapMeshTexture) );
0426: 
0427: 
0428:     // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the 
0429:     // shader debugger. Debugging vertex shaders requires either REF or software vertex 
0430:     // processing, and debugging pixel shaders requires REF.  The 
0431:     // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the 
0432:     // shader debugger.  It enables source level debugging, prevents instruction 
0433:     // reordering, prevents dead code elimination, and forces the compiler to compile 
0434:     // against the next higher available software target, which ensures that the 
0435:     // unoptimized shaders do not exceed the shader model limitations.  Setting these 
0436:     // flags will cause slower rendering since the shaders will be unoptimized and 
0437:     // forced into software.  See the DirectX documentation for more information about 
0438:     // using the shader debugger.
0439:     DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE;
0440:     #ifdef DEBUG_VS
0441:         dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT;
0442:     #endif
0443:     #ifdef DEBUG_PS
0444:         dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT;
0445:     #endif
0446: 
0447:     // Read the D3DX effect file
0448:     // If this fails, there should be debug output as to 
0449:     // they the .fx file failed to compile
0450:     V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"main.fx" ) );
0451:     V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, 
0452:                                         NULL, &g_pEffect, NULL ) );
0453: 
0454:     return S_OK;
0455: }
0456: 
0457: 
0458: 
0459: //--------------------------------------------------------------------------------------
0460: // This callback function will be called immediately after the Direct3D device has 
0461: // been destroyed, which generally happens as a result of application termination or 
0462: // windowed/full screen toggles. Resources created in the OnCreateDevice callback 
0463: // should be released here, which generally includes all D3DPOOL_MANAGED resources. 
0464: //--------------------------------------------------------------------------------------
0465: void CALLBACK OnDestroyDevice( void* pUserContext )
0466: {
0467:     g_DialogResourceManager.OnDestroyDevice();
0468:     g_SettingsDlg.OnDestroyDevice();
0469:     SAFE_RELEASE(g_pEffect);
0470:     SAFE_RELEASE(g_pFont);
0471: 
0472:     SAFE_RELEASE(g_pMapMeshTexture);
0473:     SAFE_RELEASE(g_pObjMeshTexture);
0474:     SAFE_RELEASE(g_pMapMesh);
0475:     SAFE_RELEASE(g_pObjMesh);
0476: }
0477: 
0478: 
0479: 
0480: //--------------------------------------------------------------------------------------
0481: // This callback function will be called immediately after the Direct3D device has been 
0482: // reset, which will happen after a lost device scenario. This is the best location to 
0483: // create D3DPOOL_DEFAULT resources since these resources need to be reloaded whenever 
0484: // the device is lost. Resources created here should be released in the OnLostDevice 
0485: // callback. 
0486: //--------------------------------------------------------------------------------------
0487: HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, 
0488:                                 const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
0489: {
0490:     HRESULT hr;
0491: 
0492:     V_RETURN( g_DialogResourceManager.OnResetDevice() );
0493:     V_RETURN( g_SettingsDlg.OnResetDevice() );
0494: 
0495:     if( g_pFont )
0496:         V_RETURN( g_pFont->OnResetDevice() );
0497:     if( g_pEffect )
0498:         V_RETURN( g_pEffect->OnResetDevice() );
0499: 
0500:     // Setup the camera with view & projection matrix
0501:     D3DXVECTOR3 vecEye(0.0f, 0.0f, 4.0f);
0502:     D3DXVECTOR3 vecAt (0.0f, 0.0f, 0.0f);
0503:     g_Camera.SetViewParams( &vecEye, &vecAt );
0504:     g_Camera.SetProjParams( D3DXToRadian(50.0f), 1.0f, Z_NEAR, Z_FAR );
0505:     g_Camera.SetRotateButtons( TRUE, FALSE, FALSE, FALSE );// L M R 押しながら動かす
0506: 
0507: 
0508:     pd3dDevice->GetViewport(&g_ViewportFB);
0509: 
0510:     // Create fullscreen renders target texture
0511:     g_pRT_FullScreen = CRenderTarget::Create(pd3dDevice
0512:                                     , (DWORD)ORIGINAL_WIDTH, (DWORD)ORIGINAL_HEIGHT
0513:                                     , SCREEN_FORMAT);
0514:     g_pRT_Depth      = CRenderTarget::Create(pd3dDevice
0515:                                     , (DWORD)ORIGINAL_WIDTH, (DWORD)ORIGINAL_HEIGHT
0516:                                     , DEPTH_FORMAT, RTF_NONE);
0517:     g_pRT_GaussX = CRenderTarget::Create(pd3dDevice
0518:                                     , (DWORD)ORIGINAL_WIDTH, (DWORD)ORIGINAL_HEIGHT
0519:                                     , DEPTH_FORMAT, RTF_NONE);
0520:     g_pRT_Blur = CRenderTarget::Create(pd3dDevice
0521:                                     , (DWORD)ORIGINAL_WIDTH, (DWORD)ORIGINAL_HEIGHT
0522:                                     , DEPTH_FORMAT, RTF_NONE);
0523: 
0524: 
0525:     SetupQuad( pBackBufferSurfaceDesc );
0526: 
0527:     
0528:     // Get D3DXHANDLEs to the parameters/techniques that are set every frame so 
0529:     // D3DX doesn't spend time doing string compares.  Doing this likely won't affect
0530:     // the perf of this simple sample but it should be done in complex engine.
0531:     g_hWorld                    = g_pEffect->GetParameterByName( NULL, "mWorld" );
0532:     g_hWorldView                = g_pEffect->GetParameterByName( NULL, "mWorldView" );
0533:     g_hWorldViewProjection      = g_pEffect->GetParameterByName( NULL, "mWorldViewProjection" );
0534:     g_hMeshTexture              = g_pEffect->GetParameterByName( NULL, "MeshTexture" );
0535: 
0536:     g_hTechScene                = g_pEffect->GetTechniqueByName("TechScene");
0537:     g_hTechFinal                = g_pEffect->GetTechniqueByName("TechFinal");
0538: 
0539:     // Set the vars in the effect that doesn't change each frame
0540:     V_RETURN( g_pEffect->SetTexture("RenderTargetTexture", g_pRT_FullScreen->GetTexture()) );
0541:     V_RETURN( g_pEffect->SetTexture("DepthRenderTarget", g_pRT_Depth->GetTexture()) );
0542: 
0543:     V_RETURN( g_pEffect->SetFloat("z_min", Z_NEAR) );
0544:     V_RETURN( g_pEffect->SetFloat("z_max", Z_FAR) );
0545: 
0546:     SetGaussWeight();
0547:     SetGaussRadius();
0548: 
0549: 
0550:     pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
0551:     pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
0552: 
0553:     g_HUD.SetLocation( pBackBufferSurfaceDesc->Width-170, 0 );
0554:     g_HUD.SetSize( 170, 170 );
0555:     g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width-170, pBackBufferSurfaceDesc->Height-300 );
0556:     g_SampleUI.SetSize( 170, 250 );
0557: 
0558:     return S_OK;
0559: }
0560: 
0561: 
0562: //--------------------------------------------------------------------------------------
0563: // This callback function will be called immediately after the Direct3D device has 
0564: // entered a lost state and before IDirect3DDevice9::Reset is called. Resources created
0565: // in the OnResetDevice callback should be released here, which generally includes all 
0566: // D3DPOOL_DEFAULT resources. See the "Lost Devices" section of the documentation for 
0567: // information about lost devices.
0568: //--------------------------------------------------------------------------------------
0569: void CALLBACK OnLostDevice( void* pUserContext )
0570: {
0571:     g_DialogResourceManager.OnLostDevice();
0572:     g_SettingsDlg.OnLostDevice();
0573:     if( g_pFont )
0574:         g_pFont->OnLostDevice();
0575:     if( g_pEffect )
0576:         g_pEffect->OnLostDevice();
0577:     SAFE_RELEASE(g_pTextSprite);
0578: 
0579:     SAFE_RELEASE(g_pRT_Blur);
0580:     SAFE_RELEASE(g_pRT_GaussX);
0581:     SAFE_RELEASE(g_pRT_Depth);
0582:     SAFE_RELEASE(g_pRT_FullScreen);
0583: }
0584: 
0585: 
0586: //--------------------------------------------------------------------------------------
0587: // Sets up a quad to render the fullscreen render target to the backbuffer
0588: // so it can run a fullscreen pixel shader pass that blurs based
0589: // on the depth of the objects.  It set the texcoords based on the blur factor
0590: //--------------------------------------------------------------------------------------
0591: void SetupQuad( const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
0592: {
0593:     FLOAT fWidth5  = pBackBufferSurfaceDesc->Width -0.5f;
0594:     FLOAT fHeight5 = pBackBufferSurfaceDesc->Height-0.5f;
0595: 
0596:     FLOAT fTexWidth1  = 1.0f;
0597:     FLOAT fTexHeight1 = 1.0f;
0598: 
0599:     g_Vertex[0].pos = D3DXVECTOR4(fWidth5, -0.5f, 0.0f, 1.0f);
0600:     g_Vertex[0].tex1 = D3DXVECTOR2(fTexWidth1, 0.0f);
0601: 
0602:     g_Vertex[1].pos = D3DXVECTOR4(fWidth5, fHeight5, 0.0f, 1.0f);
0603:     g_Vertex[1].tex1 = D3DXVECTOR2(fTexWidth1, fTexHeight1);
0604: 
0605:     g_Vertex[2].pos = D3DXVECTOR4(-0.5f, -0.5f, 0.0f, 1.0f);
0606:     g_Vertex[2].tex1 = D3DXVECTOR2(0.0f, 0.0f);
0607: 
0608:     g_Vertex[3].pos = D3DXVECTOR4(-0.5f, fHeight5, 0.0f, 1.0f);
0609:     g_Vertex[3].tex1 = D3DXVECTOR2(0.0f, fTexHeight1);
0610: }
0611: 
0612: void RenderQuad(IDirect3DDevice9* pd3dDevice, D3DXHANDLE hTech)
0613: {
0614:     HRESULT hr;
0615:     UINT cPasses, iPass;
0616: 
0617:     V( g_pEffect->SetTechnique(hTech) );
0618: 
0619:     V( pd3dDevice->SetFVF(VERTEX::FVF) );
0620: 
0621:     // Render the fullscreen quad on to the backbuffer
0622:     V( g_pEffect->Begin(&cPasses, 0) );
0623:     for (iPass = 0; iPass < cPasses; iPass++)
0624:     {
0625:         V( g_pEffect->BeginPass(iPass) );
0626:         V( pd3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, g_Vertex, sizeof(VERTEX)) );
0627:         V( g_pEffect->EndPass() );
0628:     }
0629:     V( g_pEffect->End() );
0630: }
0631: 
0632: //--------------------------------------------------------------------------------------
0633: // This callback function will be called once at the beginning of every frame. This is the
0634: // best location for your application to handle updates to the scene, but is not 
0635: // intended to contain actual rendering calls, which should instead be placed in the 
0636: // OnFrameRender callback.  
0637: //--------------------------------------------------------------------------------------
0638: void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
0639: {
0640:     // Update the camera's position based on user input 
0641:     g_Camera.FrameMove( fElapsedTime );
0642: 
0643:     if(!DXUTIsTimePaused())
0644:     {
0645:         if(!g_bPause)
0646:         {
0647:             g_fPhase += 2.0f * fElapsedTime;
0648:             if(2.0f * D3DX_PI < g_fPhase) g_fPhase -= 2.0f * D3DX_PI;
0649:         }
0650:     }
0651: 
0652: 
0653:     D3DXMATRIXA16 matTrans, matRot, matScale;
0654:     D3DXMatrixScaling(&matScale, 1.0f, 1.0f, 1.0f);
0655:     D3DXMatrixTranslation( &matTrans, 0.0f,+0.0f, -0.5f );
0656:     D3DXMatrixRotationY(&matRot, g_fPhase);
0657:     g_matObjWorld = matScale * matRot * matTrans;
0658: 
0659:     D3DXMatrixRotationY(&matRot, -D3DX_PI * 0.5f);
0660:     D3DXMatrixScaling(&matScale, 3.5f, 3.5f, 3.5f);
0661:     g_matMapWorld = matScale * matRot;
0662: }
0663: 
0664: // render map
0665: void RenderBG(IDirect3DDevice9* pd3dDevice)
0666: {
0667:     HRESULT hr;
0668:     UINT iPass, cPasses;
0669:     D3DXMATRIXA16 matView = *g_Camera.GetViewMatrix();
0670:     D3DXMATRIXA16 matProj = *g_Camera.GetProjMatrix();
0671:     D3DXMATRIXA16 matViewProj = matView * matProj;
0672: 
0673:     // Set world render technique
0674:     V( g_pEffect->SetTechnique( g_hTechScene ) );
0675:     
0676:     // Update effect vars
0677:     D3DXMATRIXA16 matWorldView;
0678:     D3DXMATRIXA16 matWorldViewProj;
0679: 
0680:     if(g_bObj)
0681:     {
0682:         matWorldView     = g_matObjWorld * matView;
0683:         matWorldViewProj = g_matObjWorld * matViewProj;
0684: 
0685:         V( g_pEffect->SetMatrix( g_hWorld, &g_matObjWorld) );
0686:         V( g_pEffect->SetMatrix( g_hWorldView, &matWorldView) );
0687:         V( g_pEffect->SetMatrix( g_hWorldViewProjection, &matWorldViewProj) );
0688: 
0689:         // Set the mesh texture 
0690:         V( g_pEffect->SetTexture( g_hMeshTexture, g_pObjMeshTexture) );
0691: 
0692:         // Draw the mesh on the rendertarget
0693:         V( g_pEffect->Begin(&cPasses, 0) );
0694:         for (iPass = 0; iPass < cPasses; iPass++)
0695:         {
0696:             V( g_pEffect->BeginPass(iPass) );
0697:             V( g_pObjMesh->DrawSubset(0) );
0698:             V( g_pEffect->EndPass() );
0699:         }
0700:         V( g_pEffect->End() );
0701:     }
0702: 
0703: 
0704:     if(g_bMap)
0705:     {
0706:         matWorldView     = g_matMapWorld * matView;
0707:         matWorldViewProj = g_matMapWorld * matViewProj;
0708:         V( g_pEffect->SetMatrix( g_hWorld, &g_matMapWorld) );
0709:         V( g_pEffect->SetMatrix( g_hWorldView, &matWorldView) );
0710:         V( g_pEffect->SetMatrix( g_hWorldViewProjection, &matWorldViewProj) );
0711: 
0712:         // Set the mesh texture 
0713:         V( g_pEffect->SetTexture( g_hMeshTexture, g_pMapMeshTexture) );
0714: 
0715:         // Draw the mesh on the rendertarget
0716:         V( g_pEffect->Begin(&cPasses, 0) );
0717:         for (iPass = 0; iPass < cPasses; iPass++)
0718:         {
0719:             V( g_pEffect->BeginPass(iPass) );
0720:             V( g_pMapMesh->DrawSubset(0) );
0721:             V( g_pEffect->EndPass() );
0722:         }
0723:         V( g_pEffect->End() );
0724:     }
0725: }
0726: 
0727: 
0728: 
0729: void RenderScene(IDirect3DDevice9* pd3dDevice)
0730: {
0731:     if( SUCCEEDED( g_pRT_Depth->Begin(1,RT_FLAG_NOT_SET_ZBUFFER) ) )
0732:     if( SUCCEEDED( g_pRT_FullScreen->Begin(0) ) )
0733:     {
0734:         HRESULT hr;
0735: 
0736:         V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, g_dwBackgroundColor, 1.0f, 0) );
0737: 
0738:         RenderBG(pd3dDevice);
0739: 
0740:         g_pRT_FullScreen->End();
0741:         g_pRT_Depth->End();
0742:     }
0743: }
0744: 
0745: void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
0746: {
0747:     UINT iPass, cPasses;
0748:     HRESULT hr;
0749: 
0750:     // If the settings dialog is being shown, then
0751:     // render it instead of rendering the app's scene
0752:     if( g_SettingsDlg.IsActive() )
0753:     {
0754:         g_SettingsDlg.OnRender( fElapsedTime );
0755:         return;
0756:     }
0757: 
0758: 
0759:     LPDIRECT3DSURFACE9 pOriginalZbuffer = 0;
0760:     V( pd3dDevice->GetDepthStencilSurface( &pOriginalZbuffer ) );
0761: 
0762:     // First render the world on the rendertarget
0763:     RenderScene(pd3dDevice);
0764: 
0765:     if( SUCCEEDED( g_pRT_GaussX->Begin() ) )
0766:     {
0767:         static TVERTEX Vertex[4] = {
0768:             //    x                             y         z rhw tu tv
0769:             {-0.5,                             -0.5, 0, 1, 0, 0,},
0770:             {ORIGINAL_WIDTH-0.5,               -0.5, 0, 1, 1, 0,},
0771:             {ORIGINAL_WIDTH-0.5, ORIGINAL_HEIGHT-0.5, 0, 1, 1, 1,},
0772:             {-0.5,               ORIGINAL_HEIGHT-0.5, 0, 1, 0, 1,},
0773:         };
0774:         pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0775:         V( g_pEffect->SetTexture(g_hMeshTexture, g_pRT_Depth->GetTexture()) );
0776:         V( g_pEffect->SetTechnique("TechGaussX") );
0777: 
0778:         V( g_pEffect->Begin(&cPasses, 0) );
0779:         for (iPass = 0; iPass < cPasses; iPass++)
0780:         {
0781:             V( g_pEffect->BeginPass(iPass) );
0782:             pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0783:             V( g_pEffect->EndPass() );
0784:         }
0785:         V( g_pEffect->End() );
0786: 
0787:         g_pRT_GaussX->End();
0788:     }
0789: 
0790:     if( SUCCEEDED( g_pRT_Blur->Begin() ) )
0791:     {
0792:         static TVERTEX Vertex[4] = {
0793:             //    x                             y         z rhw tu tv
0794:             {-0.5,                             -0.5, 0, 1, 0, 0,},
0795:             {ORIGINAL_WIDTH-0.5,               -0.5, 0, 1, 1, 0,},
0796:             {ORIGINAL_WIDTH-0.5, ORIGINAL_HEIGHT-0.5, 0, 1, 1, 1,},
0797:             {-0.5,               ORIGINAL_HEIGHT-0.5, 0, 1, 0, 1,},
0798:         };
0799:         pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0800:         V( g_pEffect->SetTexture(g_hMeshTexture, g_pRT_GaussX->GetTexture()) );
0801:         V( g_pEffect->SetTechnique("TechGaussY") );
0802: 
0803:         V( g_pEffect->Begin(&cPasses, 0) );
0804:         for (iPass = 0; iPass < cPasses; iPass++)
0805:         {
0806:             V( g_pEffect->BeginPass(iPass) );
0807:             pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0808:             V( g_pEffect->EndPass() );
0809:         }
0810:         V( g_pEffect->End() );
0811: 
0812:         g_pRT_Blur->End();
0813:     }
0814: 
0815:     V( pd3dDevice->SetDepthStencilSurface( pOriginalZbuffer ) );
0816:     SAFE_RELEASE( pOriginalZbuffer );
0817: 
0818:     // Clear the backbuffer 
0819: //    V( pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, 0x00000000, 1.0f, 0L ) );
0820: 
0821:     // Begin the scene, rendering to the backbuffer
0822:     if( SUCCEEDED( pd3dDevice->BeginScene() ) )
0823:     {
0824:         pd3dDevice->SetViewport(&g_ViewportFB);
0825: 
0826:         g_pEffect->SetFloat("fIntensity", g_fIntensity);
0827:         V( g_pEffect->SetTexture( g_hMeshTexture,  g_pRT_Blur->GetTexture()) );
0828:         RenderQuad(pd3dDevice, g_hTechFinal);
0829: 
0830: #ifdef FAST_RENDERING
0831:         BOOL bFastRendering = true;
0832: #else // FAST_RENDERING
0833:         BOOL bFastRendering = false;
0834: #endif // FAST_RENDERING
0835: 
0836:         if(bFastRendering)
0837:         {
0838:             RenderText(true);
0839:         }else{
0840: #if 1
0841: #ifdef _DEBUG // Display textures when debugging, (デバッグ用にテクスチャを表示する)
0842:             {
0843:             pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,  D3DTOP_SELECTARG1);
0844:             pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,    D3DTA_TEXTURE);
0845:             pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
0846:             pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
0847:             pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT );
0848:             pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
0849:             float scale = 64.0f;
0850:             LPDIRECT3DTEXTURE9 texture_tbl[] = 
0851:             {
0852:                 g_pRT_FullScreen->GetTexture(),
0853:                 g_pRT_Depth->GetTexture(),
0854:                 g_pRT_GaussX->GetTexture(),
0855:                 g_pRT_Blur->GetTexture(),
0856:                 NULL,
0857:             };
0858:             for(DWORD i=0; texture_tbl[i]; i++){
0859:                 TVERTEX Vertex[4] = {
0860:                     //    x                             y         z rhw tu tv
0861:                     {(i+0)*scale, (FLOAT)g_ViewportFB.Height-scale, 0, 1, 0, 0,},
0862:                     {(i+1)*scale, (FLOAT)g_ViewportFB.Height-scale, 0, 1, 1, 0,},
0863:                     {(i+1)*scale, (FLOAT)g_ViewportFB.Height-    0, 0, 1, 1, 1,},
0864:                     {(i+0)*scale, (FLOAT)g_ViewportFB.Height-    0, 0, 1, 0, 1,},
0865:                 };
0866:                 pd3dDevice->SetTexture( 0, texture_tbl[i] );
0867:                 pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
0868:             }
0869:             }
0870: #endif      
0871: #endif
0872:             V( g_HUD.OnRender( fElapsedTime ) );
0873:             V( g_SampleUI.OnRender( fElapsedTime ) );
0874: 
0875:             // Render the text
0876:             RenderText(false);
0877: 
0878:         }
0879: 
0880:         // End the scene.
0881:         pd3dDevice->EndScene();
0882:     }
0883: }
0884: 
0885: //--------------------------------------------------------------------------------------
0886: // Render the help and statistics text. This function uses the ID3DXFont interface for 
0887: // efficient text rendering.
0888: //--------------------------------------------------------------------------------------
0889: void RenderText(bool bFast)
0890: {
0891:     if(bFast)
0892:     {
0893:         // なるべく負荷をかけたくないときには、
0894:         // FPS が更新されたときだけウィンドウのタイトルを変更するシステムにする
0895:         wchar_t buf[256];
0896:         static float s_fFPS_Last = -1.0f;
0897:         float fFps = DXUTGetFPS();
0898:         if(fFps != s_fFPS_Last)
0899:         {
0900:             swprintf_s(  buf, 256, TEXT("%0.4f"), fFps );
0901:             SetWindowText(g_hWnd, buf );
0902:             s_fFPS_Last = fFps;
0903:         }
0904: 
0905:         return;
0906:     }
0907: 
0908:     // The helper object simply helps keep track of text position, and color
0909:     // and then it calls pFont->DrawText( g_pSprite, strMsg, -1, &rc, DT_NOCLIP, g_clr );
0910:     // If NULL is passed in as the sprite object, then it will work however the 
0911:     // pFont->DrawText() will not be batched together.  Batching calls will improves performance.
0912:     CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
0913: 
0914:     // Output statistics
0915:     txtHelper.Begin();
0916:     txtHelper.SetInsertionPos( 5, 5 );
0917:     txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
0918:     txtHelper.DrawTextLine( DXUTGetFrameStats() );
0919:     txtHelper.DrawTextLine( DXUTGetDeviceStats() );
0920: 
0921:     txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
0922:     txtHelper.DrawFormattedTextLine( L"FPS: %0.4f", DXUTGetFPS() );
0923: 
0924: #if 0
0925:     // Draw help
0926:     if( g_bShowHelp )
0927:     {
0928:         const D3DSURFACE_DESC* pd3dsdBackBuffer = DXUTGetBackBufferSurfaceDesc();
0929:         txtHelper.SetInsertionPos( 2, pd3dsdBackBuffer->Height-15*6 );
0930:         txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 0.75f, 0.0f, 1.0f ) );
0931:         txtHelper.DrawTextLine( L"Controls (F1 to hide):" );
0932: 
0933:         txtHelper.SetInsertionPos( 20, pd3dsdBackBuffer->Height-15*5 );
0934:         txtHelper.DrawTextLine( L"Look: Left drag mouse\n"
0935:                                 L"Move: A,W,S,D or Arrow Keys\n"
0936:                                 L"Move up/down: Q,E or PgUp,PgDn\n"
0937:                                 L"Reset camera: Home\n" );
0938:     }
0939:     else
0940:     {
0941:         txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
0942:         txtHelper.DrawTextLine( L"Press F1 for help" );
0943:     }
0944: #endif
0945:     txtHelper.End();
0946: }
0947: 
0948: 
0949: //--------------------------------------------------------------------------------------
0950: // Before handling window messages, DXUT passes incoming windows 
0951: // messages to the application through this callback function. If the application sets 
0952: // *pbNoFurtherProcessing to TRUE, then DXUT will not process this message.
0953: //--------------------------------------------------------------------------------------
0954: LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext )
0955: {
0956:     g_hWnd = hWnd;
0957: 
0958:     // Always allow dialog resource manager calls to handle global messages
0959:     // so GUI state is updated correctly
0960:     *pbNoFurtherProcessing = g_DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
0961:     if( *pbNoFurtherProcessing )
0962:         return 0;
0963: 
0964:     if( g_SettingsDlg.IsActive() )
0965:     {
0966:         g_SettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam );
0967:         return 0;
0968:     }
0969: 
0970:     // Give the dialogs a chance to handle the message first
0971:     *pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
0972:     if( *pbNoFurtherProcessing )
0973:         return 0;
0974:     *pbNoFurtherProcessing = g_SampleUI.MsgProc( hWnd, uMsg, wParam, lParam );
0975:     if( *pbNoFurtherProcessing )
0976:         return 0;
0977: 
0978:     // Pass all remaining windows messages to camera so it can respond to user input
0979:     g_Camera.HandleMessages( hWnd, uMsg, wParam, lParam );
0980: 
0981:     return 0;
0982: }
0983: 
0984: 
0985: //--------------------------------------------------------------------------------------
0986: // As a convenience, DXUT inspects the incoming windows messages for
0987: // keystroke messages and decodes the message parameters to pass relevant keyboard
0988: // messages to the application.  The framework does not remove the underlying keystroke 
0989: // messages, which are still passed to the application's MsgProc callback.
0990: //--------------------------------------------------------------------------------------
0991: void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
0992: {
0993:     if( bKeyDown )
0994:     {
0995:         switch( nChar )
0996:         {
0997:             case VK_F1: g_bShowHelp = !g_bShowHelp; break;
0998:         }
0999:     }
1000: }
1001: 
1002: 
1003: //--------------------------------------------------------------------------------------
1004: // Handles the GUI events
1005: //--------------------------------------------------------------------------------------
1006: void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
1007: {
1008:     switch( nControlID )
1009:     {
1010:         case IDC_TOGGLEFULLSCREEN: DXUTToggleFullScreen(); break;
1011:         case IDC_TOGGLEREF:        DXUTToggleREF(); break;
1012:         case IDC_CHANGEDEVICE:     g_SettingsDlg.SetActive( !g_SettingsDlg.IsActive() ); break;
1013: 
1014:         case IDC_PAUSE:
1015:             g_bPause = g_SampleUI.GetCheckBox( IDC_PAUSE )->GetChecked(); 
1016:             break;
1017: 
1018:         case IDC_INTENSITY: {
1019:             g_fIntensity = 0.01f*(float) (g_SampleUI.GetSlider( IDC_INTENSITY )->GetValue());
1020: 
1021:             WCHAR sz[100];
1022:             StringCchPrintf( sz, 100, L"Intensity: %0.2f", g_fIntensity ); 
1023:             g_SampleUI.GetStatic( IDC_INTENSITY_STATIC )->SetText( sz );
1024:             }break;
1025: 
1026:         case IDC_RADIUS: {
1027:             g_fGaussRadius = 0.01f*(float) (g_SampleUI.GetSlider( IDC_RADIUS )->GetValue());
1028: 
1029:             WCHAR sz[100];
1030:             StringCchPrintf( sz, 100, L"Radius: %0.2f", g_fGaussRadius ); 
1031:             g_SampleUI.GetStatic( IDC_RADIUS_STATIC )->SetText( sz );
1032:             SetGaussRadius();
1033:             }break;
1034: 
1035:         default:
1036:             break;
1037:     }
1038: }
1039: 
1040: 
1041: