0001:
0002:
0003:
0004: #include "dxstdafx.h"
0005: #include "resource.h"
0006: #include "rendertarget.h"
0007:
0008: #undef FAST_RENDERING
0009:
0010:
0011:
0012:
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:
0020:
0021:
0022:
0023:
0024: #define SCREEN_FORMAT RTF_RGBA
0025:
0026:
0027: #define DEPTH_FORMAT RTF_RGBA
0028:
0029:
0030: #define Z_NEAR 0.5f
0031: #define Z_FAR 10.0f
0032:
0033:
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:
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:
0067:
0068: HWND g_hWnd;
0069: ID3DXFont* g_pFont = NULL;
0070: ID3DXSprite* g_pTextSprite = NULL;
0071: CFirstPersonCamera g_Camera;
0072: bool g_bShowHelp = true;
0073: CDXUTDialogResourceManager g_DialogResourceManager;
0074: CD3DSettingsDlg g_SettingsDlg;
0075: CDXUTDialog g_HUD;
0076: CDXUTDialog g_SampleUI;
0077: bool g_bPause = false;
0078:
0079: float g_fPhase = D3DX_PI * 0.8f;
0080:
0081: VERTEX g_Vertex[4];
0082:
0083:
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:
0091: bool g_bObj = true;
0092: LPD3DXMESH g_pObjMesh=0;
0093: LPDIRECT3DTEXTURE9 g_pObjMeshTexture=0;
0094: D3DXMATRIXA16 g_matObjWorld;
0095:
0096:
0097: bool g_bMap = true;
0098: LPD3DXMESH g_pMapMesh=0;
0099: LPDIRECT3DTEXTURE9 g_pMapMeshTexture=0;
0100: D3DXMATRIXA16 g_matMapWorld;
0101:
0102:
0103: DWORD g_dwBackgroundColor;
0104:
0105:
0106:
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:
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:
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:
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:
0178:
0179:
0180: INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
0181: {
0182:
0183: #if defined(DEBUG) | defined(_DEBUG)
0184: _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
0185: #endif
0186:
0187:
0188:
0189:
0190:
0191:
0192:
0193:
0194:
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:
0205: DXUTSetCursorSettings( true, true );
0206:
0207: InitApp();
0208:
0209:
0210:
0211:
0212: DXUTInit( true, true, true );
0213: DXUTCreateWindow( L"Unsharp Masking" );
0214: DXUTCreateDevice( D3DADAPTER_DEFAULT, true, (DWORD)SCREEN_WIDTH, (DWORD)SCREEN_HEIGHT, IsDeviceAcceptable, ModifyDeviceSettings );
0215:
0216:
0217:
0218:
0219:
0220: DXUTMainLoop();
0221:
0222:
0223:
0224:
0225: return DXUTGetExitCode();
0226: }
0227:
0228:
0229:
0230:
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:
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:
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:
0270:
0271:
0272: bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
0273: D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
0274: {
0275:
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:
0283: if( pCaps->PixelShaderVersion < D3DPS_VERSION( 2, 0 ) )
0284: return false;
0285:
0286: return true;
0287: }
0288:
0289:
0290:
0291:
0292:
0293:
0294:
0295:
0296:
0297:
0298: bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
0299: {
0300:
0301:
0302: if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
0303: pCaps->VertexShaderVersion < D3DVS_VERSION(2,0) )
0304: {
0305: pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
0306: }
0307:
0308:
0309:
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:
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:
0337:
0338:
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:
0347:
0348:
0349:
0350: V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, strFileName ) );
0351:
0352: V_RETURN( D3DXLoadMeshFromX(str, D3DXMESH_MANAGED, pd3dDevice, NULL, NULL, NULL, NULL, &pMesh) );
0353:
0354:
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:
0379:
0380:
0381:
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:
0397:
0398:
0399:
0400:
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:
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:
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:
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:
0429:
0430:
0431:
0432:
0433:
0434:
0435:
0436:
0437:
0438:
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:
0448:
0449:
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:
0461:
0462:
0463:
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:
0482:
0483:
0484:
0485:
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:
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 );
0506:
0507:
0508: pd3dDevice->GetViewport(&g_ViewportFB);
0509:
0510:
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:
0529:
0530:
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:
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:
0564:
0565:
0566:
0567:
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:
0588:
0589:
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:
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:
0634:
0635:
0636:
0637:
0638: void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
0639: {
0640:
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:
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:
0674: V( g_pEffect->SetTechnique( g_hTechScene ) );
0675:
0676:
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:
0690: V( g_pEffect->SetTexture( g_hMeshTexture, g_pObjMeshTexture) );
0691:
0692:
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:
0713: V( g_pEffect->SetTexture( g_hMeshTexture, g_pMapMeshTexture) );
0714:
0715:
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:
0751:
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:
0763: RenderScene(pd3dDevice);
0764:
0765: if( SUCCEEDED( g_pRT_GaussX->Begin() ) )
0766: {
0767: static TVERTEX Vertex[4] = {
0768:
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:
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:
0819:
0820:
0821:
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
0833: BOOL bFastRendering = false;
0834: #endif
0835:
0836: if(bFastRendering)
0837: {
0838: RenderText(true);
0839: }else{
0840: #if 1
0841: #ifdef _DEBUG
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:
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:
0876: RenderText(false);
0877:
0878: }
0879:
0880:
0881: pd3dDevice->EndScene();
0882: }
0883: }
0884:
0885:
0886:
0887:
0888:
0889: void RenderText(bool bFast)
0890: {
0891: if(bFast)
0892: {
0893:
0894:
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:
0909:
0910:
0911:
0912: CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
0913:
0914:
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:
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:
0951:
0952:
0953:
0954: LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext )
0955: {
0956: g_hWnd = hWnd;
0957:
0958:
0959:
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:
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:
0979: g_Camera.HandleMessages( hWnd, uMsg, wParam, lParam );
0980:
0981: return 0;
0982: }
0983:
0984:
0985:
0986:
0987:
0988:
0989:
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:
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: