[760] | 1 | //--------------------------------------------------------------------------------------
|
---|
| 2 | // Main.cpp:
|
---|
| 3 | // - DirectX initialization
|
---|
| 4 | // - create resources (textures)
|
---|
| 5 | //--------------------------------------------------------------------------------------
|
---|
| 6 |
|
---|
| 7 | #include "dxstdafx.h"
|
---|
| 8 | #include "resource.h"
|
---|
| 9 | #include <math.h>
|
---|
| 10 | #include <stdio.h>
|
---|
| 11 |
|
---|
| 12 | //#define DEBUG_VS // Uncomment this line to debug vertex shaders
|
---|
| 13 | //#define DEBUG_PS // Uncomment this line to debug pixel shaders
|
---|
| 14 |
|
---|
| 15 | #include "Mesh.h"
|
---|
| 16 | #include "Parameters.h"
|
---|
| 17 | #include "CameraEffects.h"
|
---|
| 18 |
|
---|
| 19 | const int WIDTH = 512;
|
---|
| 20 | const int HEIGHT = WIDTH;
|
---|
| 21 | const int CHARBUFFER_SIZE = 200;
|
---|
| 22 |
|
---|
| 23 | //--------------------------------------------------------------------------------------
|
---|
| 24 | // Global variables
|
---|
| 25 | //--------------------------------------------------------------------------------------
|
---|
| 26 | int currentMethod=0;
|
---|
| 27 | int methodCount=1;
|
---|
| 28 | bool showUI=true;
|
---|
| 29 | CameraEffects PD;
|
---|
| 30 | IDirect3DDevice9* g_pd3dDevice = NULL;
|
---|
| 31 | ID3DXEffect* g_pEffect = NULL;
|
---|
| 32 |
|
---|
| 33 | CDXUTDialogResourceManager g_DialogResourceManager; // manager for shared resources of dialogs
|
---|
| 34 | CD3DSettingsDlg g_SettingsDlg; // Device settings dialog
|
---|
| 35 | CDXUTDialog g_HUD; // Dialog for sample specific controls
|
---|
| 36 |
|
---|
| 37 | ID3DXFont* g_pFont = NULL; // Font for drawing text
|
---|
| 38 | ID3DXSprite* g_pTextSprite = NULL; // Sprite for batching draw text calls
|
---|
| 39 |
|
---|
| 40 | IDirect3DSurface9* g_pSaveSurface; // screenshot capturing support
|
---|
| 41 | bool bSavingScreenshot = false;
|
---|
| 42 | int counter = 0;
|
---|
| 43 |
|
---|
| 44 | D3DXVECTOR3 LightPosition;
|
---|
| 45 | HRESULT hr;
|
---|
| 46 |
|
---|
| 47 | Parameters pp; // managing parameters of the algorithm
|
---|
| 48 |
|
---|
| 49 | CModelViewerCamera camera; // camera
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | //--------------------------------------------------------------------------------------
|
---|
| 53 | // Forward declarations (CALLBACK)
|
---|
| 54 | //--------------------------------------------------------------------------------------
|
---|
| 55 | bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext );
|
---|
| 56 | bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext );
|
---|
| 57 | HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* g_pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
|
---|
| 58 | HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* g_pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
|
---|
| 59 | void CALLBACK OnFrameMove( IDirect3DDevice9* g_pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
|
---|
| 60 | void CALLBACK OnFrameRender( IDirect3DDevice9* g_pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
|
---|
| 61 | LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext );
|
---|
| 62 | void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext );
|
---|
| 63 | void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext );
|
---|
| 64 | void CALLBACK OnLostDevice( void* pUserContext );
|
---|
| 65 | void CALLBACK OnDestroyDevice( void* pUserContext );
|
---|
| 66 | void InitApp();
|
---|
| 67 | void RenderText();
|
---|
| 68 |
|
---|
| 69 | //--------------------------------------------------------------------------------------
|
---|
| 70 | // Forward declarations
|
---|
| 71 | //--------------------------------------------------------------------------------------
|
---|
| 72 |
|
---|
| 73 | void SaveCameraPosition( char* fileName );
|
---|
| 74 | void LoadCameraPosition( char* fileName );
|
---|
| 75 | int GenerateNewFileName( int& counter );
|
---|
| 76 |
|
---|
| 77 | //--------------------------------------------------------------------------------------
|
---|
| 78 | // Entry point to the program. Initializes everything and goes into a message processing
|
---|
| 79 | // loop. Idle time is used to render the scene.
|
---|
| 80 | //--------------------------------------------------------------------------------------
|
---|
| 81 | INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
|
---|
| 82 | {
|
---|
| 83 | // Enable run-time memory check for debug builds.
|
---|
| 84 | #if defined(DEBUG) | defined(_DEBUG)
|
---|
| 85 | _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
|
---|
| 86 | #endif
|
---|
| 87 |
|
---|
| 88 | DXUTSetCallbackDeviceCreated( OnCreateDevice );
|
---|
| 89 | DXUTSetCallbackDeviceReset( OnResetDevice );
|
---|
| 90 | DXUTSetCallbackDeviceLost( OnLostDevice );
|
---|
| 91 | DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
|
---|
| 92 | DXUTSetCallbackMsgProc( MsgProc );
|
---|
| 93 | DXUTSetCallbackKeyboard( KeyboardProc );
|
---|
| 94 | DXUTSetCallbackFrameRender( OnFrameRender );
|
---|
| 95 | DXUTSetCallbackFrameMove( OnFrameMove );
|
---|
| 96 |
|
---|
| 97 | InitApp();
|
---|
| 98 |
|
---|
| 99 | // Show the cursor and clip it when in full screen
|
---|
| 100 | DXUTSetCursorSettings( true, true );
|
---|
| 101 |
|
---|
| 102 | DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
|
---|
| 103 | DXUTCreateWindow( L"Depth of Field Demo" );
|
---|
| 104 | DXUTCreateDevice( D3DADAPTER_DEFAULT, true, WIDTH, HEIGHT, IsDeviceAcceptable, ModifyDeviceSettings );
|
---|
| 105 |
|
---|
| 106 | camera.SetButtonMasks( 0, MOUSE_WHEEL, MOUSE_LEFT_BUTTON );
|
---|
| 107 |
|
---|
| 108 | DXUTMainLoop();
|
---|
| 109 |
|
---|
| 110 | return DXUTGetExitCode();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
|
---|
| 114 | {
|
---|
| 115 | pp.UpdateFromHUD( nControlID );
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void OnLoad() {
|
---|
| 119 | LoadCameraPosition( ".matrix" );
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void OnSave() {
|
---|
| 123 | SaveCameraPosition( ".matrix" );
|
---|
| 124 | bSavingScreenshot = true;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | void OnReset()
|
---|
| 128 | {
|
---|
| 129 | LightPosition.x=-0.4f;
|
---|
| 130 | LightPosition.y=1.3f;
|
---|
| 131 | LightPosition.z=9;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void InitApp()
|
---|
| 135 | {
|
---|
| 136 | g_SettingsDlg.Init( &g_DialogResourceManager );
|
---|
| 137 | g_HUD.Init( &g_DialogResourceManager );
|
---|
| 138 | g_HUD.SetCallback( OnGUIEvent ); // Event handling
|
---|
| 139 |
|
---|
| 140 | pp.Setup( &g_HUD, OnReset, OnSave, OnLoad); // you can add callbacks to these actions
|
---|
| 141 |
|
---|
| 142 | pp.Add( bShowHelp, "Show Help", VK_F1 );
|
---|
| 143 |
|
---|
| 144 | PD.addUiParams(pp);
|
---|
| 145 |
|
---|
| 146 | pp.UpdateFromHUD( IDC_RESET_BUTTON ); // do a reset
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | //--------------------------------------------------------------------------------------
|
---|
| 150 | // Rejects any devices that aren't acceptable by returning false
|
---|
| 151 | //--------------------------------------------------------------------------------------
|
---|
| 152 | bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
|
---|
| 153 | D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
|
---|
| 154 | {
|
---|
| 155 | // Skip backbuffer formats that don't support alpha blending
|
---|
| 156 | IDirect3D9* pD3D = DXUTGetD3DObject();
|
---|
| 157 | if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
|
---|
| 158 | AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
|
---|
| 159 | D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
|
---|
| 160 | return false;
|
---|
| 161 |
|
---|
| 162 | return true;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | //--------------------------------------------------------------------------------------
|
---|
| 166 | // Before a device is created, modify the device settings as needed
|
---|
| 167 | //--------------------------------------------------------------------------------------
|
---|
| 168 | bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
|
---|
| 169 | {
|
---|
| 170 | // VSync off
|
---|
| 171 | pDeviceSettings->pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
|
---|
| 172 |
|
---|
| 173 | // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW
|
---|
| 174 | // then switch to SWVP.
|
---|
| 175 | if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
|
---|
| 176 | pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) )
|
---|
| 177 | pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
|
---|
| 178 | else
|
---|
| 179 | pDeviceSettings->BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
|
---|
| 180 |
|
---|
| 181 | // This application is designed to work on a pure device by not using
|
---|
| 182 | // IDirect3D9::Get*() methods, so create a pure device if supported and using HWVP.
|
---|
| 183 | if ((pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE) != 0 &&
|
---|
| 184 | (pDeviceSettings->BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING) != 0 )
|
---|
| 185 | pDeviceSettings->BehaviorFlags |= D3DCREATE_PUREDEVICE;
|
---|
| 186 |
|
---|
| 187 | // Debugging vertex shaders requires either REF or software vertex processing
|
---|
| 188 | // and debugging pixel shaders requires REF.
|
---|
| 189 | #ifdef DEBUG_VS
|
---|
| 190 | if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF )
|
---|
| 191 | {
|
---|
| 192 | pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
|
---|
| 193 | pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;
|
---|
| 194 | pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
|
---|
| 195 | }
|
---|
| 196 | #endif
|
---|
| 197 | #ifdef DEBUG_PS
|
---|
| 198 | pDeviceSettings->DeviceType = D3DDEVTYPE_REF;
|
---|
| 199 | #endif
|
---|
| 200 |
|
---|
| 201 | // For the first device created if its a REF device, optionally display a warning dialog box
|
---|
| 202 | static bool s_bFirstTime = true;
|
---|
| 203 | if( s_bFirstTime )
|
---|
| 204 | {
|
---|
| 205 | s_bFirstTime = false;
|
---|
| 206 | if( pDeviceSettings->DeviceType == D3DDEVTYPE_REF )
|
---|
| 207 | DXUTDisplaySwitchingToREFWarning();
|
---|
| 208 | }
|
---|
| 209 | return true;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | //--------------------------------------------------------------------------------------
|
---|
| 213 | // Create any D3DPOOL_MANAGED resources here
|
---|
| 214 | //--------------------------------------------------------------------------------------
|
---|
| 215 | HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
| 216 | {
|
---|
| 217 | ::g_pd3dDevice = pd3dDevice;
|
---|
| 218 |
|
---|
| 219 | HRESULT hr;
|
---|
| 220 |
|
---|
| 221 | V_RETURN( g_DialogResourceManager.OnCreateDevice( g_pd3dDevice ) );
|
---|
| 222 | V_RETURN( g_SettingsDlg.OnCreateDevice( g_pd3dDevice ) );
|
---|
| 223 |
|
---|
| 224 | // Initialize the font
|
---|
| 225 | V_RETURN( D3DXCreateFont( g_pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
|
---|
| 226 | OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
|
---|
| 227 | L"Arial", &g_pFont ) );
|
---|
| 228 |
|
---|
| 229 |
|
---|
| 230 |
|
---|
| 231 | float initialEyeDist = 6.82f;
|
---|
| 232 | D3DXVECTOR3 vecEye(0, 0, -initialEyeDist);
|
---|
| 233 | D3DXVECTOR3 vecAt (0.0f, 0.0f, -0.0f);
|
---|
| 234 | camera.SetViewParams( &vecEye, &vecAt );
|
---|
| 235 |
|
---|
| 236 | PD.OnCreateDevice(g_pd3dDevice);
|
---|
| 237 | PD.camera = &camera;
|
---|
| 238 |
|
---|
| 239 | return S_OK;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | //--------------------------------------------------------------------------------------
|
---|
| 243 | // Release resources created in the OnCreateDevice callback here
|
---|
| 244 | //--------------------------------------------------------------------------------------
|
---|
| 245 | void CALLBACK OnDestroyDevice( void* pUserContext )
|
---|
| 246 | {
|
---|
| 247 | g_DialogResourceManager.OnDestroyDevice();
|
---|
| 248 | g_SettingsDlg.OnDestroyDevice();
|
---|
| 249 | SAFE_RELEASE(g_pFont);
|
---|
| 250 | PD.OnDestroyDevice();
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 |
|
---|
| 254 | //--------------------------------------------------------------------------------------
|
---|
| 255 | // Create any D3DPOOL_DEFAULT resources here
|
---|
| 256 | //--------------------------------------------------------------------------------------
|
---|
| 257 | HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* g_pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
| 258 | {
|
---|
| 259 | HRESULT hr;
|
---|
| 260 |
|
---|
| 261 | V_RETURN( g_DialogResourceManager.OnResetDevice() );
|
---|
| 262 | V_RETURN( g_SettingsDlg.OnResetDevice() );
|
---|
| 263 |
|
---|
| 264 | if( g_pFont ) V_RETURN( g_pFont->OnResetDevice() );
|
---|
| 265 |
|
---|
| 266 | // Create a sprite to help batch calls when drawing many lines of text
|
---|
| 267 | V_RETURN( D3DXCreateSprite( g_pd3dDevice, &g_pTextSprite ) );
|
---|
| 268 |
|
---|
| 269 | // Create a surface to save screenshots
|
---|
| 270 | IDirect3DTexture9* pSaveTexture = NULL;
|
---|
| 271 | V( D3DXCreateTexture( g_pd3dDevice, pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height, 1,
|
---|
| 272 | D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSaveTexture) );
|
---|
| 273 | pSaveTexture->GetSurfaceLevel( 0, &g_pSaveSurface );
|
---|
| 274 | SAFE_RELEASE( pSaveTexture );
|
---|
| 275 |
|
---|
| 276 | // Setup the camera's projection parameters
|
---|
| 277 | float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
|
---|
| 278 |
|
---|
| 279 | camera.SetProjParams( D3DX_PI/4, fAspectRatio, 0.1f, 100 );
|
---|
| 280 | camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
|
---|
| 281 |
|
---|
| 282 | // Position the on-screen dialog
|
---|
| 283 | g_HUD.SetLocation( 0, 0 );
|
---|
| 284 | g_HUD.SetSize( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
|
---|
| 285 |
|
---|
| 286 | PD.OnResetDevice((D3DSURFACE_DESC*)pBackBufferSurfaceDesc);
|
---|
| 287 |
|
---|
| 288 | return S_OK;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | //--------------------------------------------------------------------------------------
|
---|
| 292 | // Release resources created in the OnResetDevice callback here
|
---|
| 293 | //--------------------------------------------------------------------------------------
|
---|
| 294 | void CALLBACK OnLostDevice( void* pUserContext )
|
---|
| 295 | {
|
---|
| 296 | g_DialogResourceManager.OnLostDevice();
|
---|
| 297 | g_SettingsDlg.OnLostDevice();
|
---|
| 298 |
|
---|
| 299 | if( g_pFont ) g_pFont->OnLostDevice();
|
---|
| 300 |
|
---|
| 301 | SAFE_RELEASE( g_pTextSprite );
|
---|
| 302 | SAFE_RELEASE( g_pSaveSurface );
|
---|
| 303 |
|
---|
| 304 | PD.OnLostDevice();
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext )
|
---|
| 308 | {
|
---|
| 309 | // Always allow dialog resource manager calls to handle global messages
|
---|
| 310 | // so GUI state is updated correctly
|
---|
| 311 | *pbNoFurtherProcessing = g_DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
|
---|
| 312 | if( *pbNoFurtherProcessing )
|
---|
| 313 | return 0;
|
---|
| 314 |
|
---|
| 315 | if( g_SettingsDlg.IsActive() )
|
---|
| 316 | {
|
---|
| 317 | g_SettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam );
|
---|
| 318 | return 0;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | // Give the dialogs a chance to handle the message first
|
---|
| 322 | *pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
|
---|
| 323 | if( *pbNoFurtherProcessing )
|
---|
| 324 | return 0;
|
---|
| 325 |
|
---|
| 326 | // Pass all remaining windows messages to camera so it can respond to user input
|
---|
| 327 | camera.HandleMessages( hWnd, uMsg, wParam, lParam );
|
---|
| 328 |
|
---|
| 329 | return 0;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | //--------------------------------------------------------------------------------------
|
---|
| 333 | // Handle updates to the scene
|
---|
| 334 | //--------------------------------------------------------------------------------------
|
---|
| 335 | void CALLBACK OnFrameMove( IDirect3DDevice9* g_pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
|
---|
| 336 | {
|
---|
| 337 | camera.FrameMove( fElapsedTime );
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | //--------------------------------------------------------------------------------------
|
---|
| 341 | // Render the scene
|
---|
| 342 | //--------------------------------------------------------------------------------------
|
---|
| 343 | void CALLBACK OnFrameRender( IDirect3DDevice9* g_pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
|
---|
| 344 | {
|
---|
| 345 | /* // If the settings dialog is being shown, then
|
---|
| 346 | // render it instead of rendering the app's scene
|
---|
| 347 | if( g_SettingsDlg.IsActive() )
|
---|
| 348 | {
|
---|
| 349 | g_SettingsDlg.OnRender( fElapsedTime );
|
---|
| 350 | return;
|
---|
| 351 | }*/
|
---|
| 352 |
|
---|
| 353 | IDirect3DSurface9* oldRenderTarget = NULL;
|
---|
| 354 | if (bSavingScreenshot) {
|
---|
| 355 | V( g_pd3dDevice->GetRenderTarget(0, &oldRenderTarget) );
|
---|
| 356 | V( g_pd3dDevice->SetRenderTarget(0, g_pSaveSurface) );
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | V( g_pd3dDevice->BeginScene() );
|
---|
| 360 |
|
---|
| 361 |
|
---|
| 362 | D3DXMATRIXA16 mView = *camera.GetViewMatrix();
|
---|
| 363 | D3DXMATRIXA16 mProj = *camera.GetProjMatrix();
|
---|
| 364 |
|
---|
| 365 | V( g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(75, 75, 75), 1.0f, 0L) );
|
---|
| 366 |
|
---|
| 367 | PD.currentmethod=currentMethod;
|
---|
| 368 | PD.camera = &camera;
|
---|
| 369 | PD.LightPos=&LightPosition;
|
---|
| 370 | PD.OnFrameRender(mView,mProj);
|
---|
| 371 |
|
---|
| 372 | if (!bSavingScreenshot)
|
---|
| 373 | {
|
---|
| 374 | RenderText();
|
---|
| 375 | if(showUI)
|
---|
| 376 | {
|
---|
| 377 | V( g_HUD.OnRender( fElapsedTime ) );
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | V( g_pd3dDevice->EndScene() );
|
---|
| 382 |
|
---|
| 383 | if (bSavingScreenshot)
|
---|
| 384 | {
|
---|
| 385 | // saving surface
|
---|
| 386 | char buf[CHARBUFFER_SIZE];
|
---|
| 387 | wchar_t wbuf[CHARBUFFER_SIZE];
|
---|
| 388 |
|
---|
| 389 | GenerateNewFileName( counter );
|
---|
| 390 | sprintf(buf, "shots\\%03i.png", counter);
|
---|
| 391 | mbstowcs( wbuf, buf, CHARBUFFER_SIZE );
|
---|
| 392 |
|
---|
| 393 | D3DXSaveSurfaceToFileW(wbuf, D3DXIFF_PNG, g_pSaveSurface, NULL, NULL);
|
---|
| 394 |
|
---|
| 395 | sprintf(buf, "shots\\%03i", counter);
|
---|
| 396 | pp.SaveToFile( buf );
|
---|
| 397 |
|
---|
| 398 | sprintf(buf, "shots\\%03i.matrix", counter);
|
---|
| 399 | SaveCameraPosition( buf );
|
---|
| 400 |
|
---|
| 401 | g_pd3dDevice->SetRenderTarget(0, oldRenderTarget);
|
---|
| 402 | bSavingScreenshot = false;
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | int GenerateNewFileName( int& counter )
|
---|
| 407 | {
|
---|
| 408 | char buf[CHARBUFFER_SIZE];
|
---|
| 409 | FILE* f;
|
---|
| 410 | do {
|
---|
| 411 | sprintf(buf, "shots\\%03i.png", counter);
|
---|
| 412 | if ( (f = fopen(buf, "rt")) != NULL )
|
---|
| 413 | {
|
---|
| 414 | fclose( f );
|
---|
| 415 | counter++;
|
---|
| 416 | }
|
---|
| 417 | } while (f != NULL);
|
---|
| 418 | return counter;
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | //--------------------------------------------------------------------------------------
|
---|
| 422 | // The framework does not remove the underlying keystroke messages,
|
---|
| 423 | // which are still passed to the application's MsgProc callback.
|
---|
| 424 | //--------------------------------------------------------------------------------------
|
---|
| 425 | void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
|
---|
| 426 | {
|
---|
| 427 | if( bKeyDown )
|
---|
| 428 | {
|
---|
| 429 | if ( nChar >= '0' && nChar <= '9' ) {
|
---|
| 430 | UINT whichMesh = nChar-'0';
|
---|
| 431 | PD.changeMesh(whichMesh);
|
---|
| 432 | }
|
---|
| 433 | switch(nChar)
|
---|
| 434 | {
|
---|
| 435 | case VK_SPACE:
|
---|
| 436 | showUI=!showUI;
|
---|
| 437 | break;
|
---|
| 438 | case VK_TAB:
|
---|
| 439 | currentMethod=(++currentMethod)%methodCount;
|
---|
| 440 | break;
|
---|
| 441 | case VK_DOWN:
|
---|
| 442 | LightPosition.z-=0.1f;
|
---|
| 443 | break;
|
---|
| 444 | case VK_UP:
|
---|
| 445 | LightPosition.z+=0.1f;
|
---|
| 446 | break;
|
---|
| 447 | case VK_LEFT:
|
---|
| 448 | LightPosition.x-=0.1f;
|
---|
| 449 | break;
|
---|
| 450 | case VK_RIGHT:
|
---|
| 451 | LightPosition.x+=0.1f;
|
---|
| 452 | break;
|
---|
| 453 | case VK_DELETE:
|
---|
| 454 | LightPosition.y+=0.1f;
|
---|
| 455 | break;
|
---|
| 456 | case VK_END:
|
---|
| 457 | LightPosition.y-=0.1f;
|
---|
| 458 | break;
|
---|
| 459 | default:
|
---|
| 460 | break;
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | void SaveCameraPosition( char* fileName )
|
---|
| 466 | {
|
---|
| 467 | // save parameters to file:
|
---|
| 468 | // world matrix (4x4)
|
---|
| 469 | // camera position (3)
|
---|
| 470 |
|
---|
| 471 | // save world matrix
|
---|
| 472 | D3DXMATRIXA16 W = *camera.GetViewMatrix();
|
---|
| 473 |
|
---|
| 474 | FILE* f;
|
---|
| 475 |
|
---|
| 476 | if ((f = fopen(fileName, "wt")) == NULL)
|
---|
| 477 | {
|
---|
| 478 | wchar_t wbuf[CHARBUFFER_SIZE];
|
---|
| 479 | mbstowcs( wbuf, fileName, CHARBUFFER_SIZE );
|
---|
| 480 | MessageBox(NULL, wbuf, L"File creation failed!", MB_ICONEXCLAMATION);
|
---|
| 481 | return;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | fprintf(f, "\n");
|
---|
| 485 | fprintf(f, "World matrix:\n");
|
---|
| 486 | fprintf(f, "%10.4g %10.4g %10.4g %10.4g\n", W._11, W._12, W._13, W._14);
|
---|
| 487 | fprintf(f, "%10.4g %10.4g %10.4g %10.4g\n", W._21, W._22, W._23, W._24);
|
---|
| 488 | fprintf(f, "%10.4g %10.4g %10.4g %10.4g\n", W._31, W._32, W._33, W._34);
|
---|
| 489 | fprintf(f, "%10.4g %10.4g %10.4g %10.4g\n", W._41, W._42, W._43, W._44);
|
---|
| 490 |
|
---|
| 491 | // save camera position
|
---|
| 492 | fprintf(f, "\n");
|
---|
| 493 | fprintf(f, "Camera position:\n");
|
---|
| 494 | const D3DXVECTOR3* eye = camera.GetEyePt();
|
---|
| 495 | fprintf(f, "%10g %10g %10g", eye->x, eye->y, eye->z);
|
---|
| 496 | fprintf(f, "\n");
|
---|
| 497 |
|
---|
| 498 | fclose(f);
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | void LoadCameraPosition( char* fileName )
|
---|
| 502 | {
|
---|
| 503 | FILE* f;
|
---|
| 504 |
|
---|
| 505 | if ((f = fopen(fileName, "rt")) == NULL) {
|
---|
| 506 | wchar_t wbuf[CHARBUFFER_SIZE];
|
---|
| 507 | mbstowcs( wbuf, fileName, CHARBUFFER_SIZE );
|
---|
| 508 | MessageBox(NULL, wbuf, L"File not found!", MB_ICONEXCLAMATION);
|
---|
| 509 | return;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | const int BufSize = 500; // size of char buffers
|
---|
| 513 | char buf[BufSize];
|
---|
| 514 | D3DXMATRIXA16 W;
|
---|
| 515 |
|
---|
| 516 | fgets(buf, BufSize, f); // skip comment
|
---|
| 517 | fgets(buf, BufSize, f); // skip comment
|
---|
| 518 | fgets(buf, BufSize, f); sscanf(buf, "%f %f %f %f", &W._11, &W._12, &W._13, &W._14);
|
---|
| 519 | fgets(buf, BufSize, f); sscanf(buf, "%f %f %f %f", &W._21, &W._22, &W._23, &W._24);
|
---|
| 520 | fgets(buf, BufSize, f); sscanf(buf, "%f %f %f %f", &W._31, &W._32, &W._33, &W._34);
|
---|
| 521 | fgets(buf, BufSize, f); sscanf(buf, "%f %f %f %f", &W._41, &W._42, &W._43, &W._44);
|
---|
| 522 |
|
---|
| 523 | // load camera position
|
---|
| 524 | D3DXVECTOR3 vecAt(0.0f, 0.0f, 0.0f);
|
---|
| 525 | D3DXVECTOR3 vecEye;
|
---|
| 526 | fgets(buf, BufSize, f); // skip comment
|
---|
| 527 | fgets(buf, BufSize, f); // skip comment
|
---|
| 528 | fgets(buf, BufSize, f); sscanf(buf, "%f %f %f", &vecEye.x, &vecEye.y, &vecEye.z);
|
---|
| 529 |
|
---|
| 530 | fclose(f);
|
---|
| 531 |
|
---|
| 532 | camera.SetViewParams( &vecEye, &vecAt );
|
---|
| 533 |
|
---|
| 534 | D3DXQUATERNION q;
|
---|
| 535 | D3DXQuaternionRotationMatrix(&q, &W);
|
---|
| 536 | camera.SetViewQuat(q);
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | //--------------------------------------------------------------------------------------
|
---|
| 540 | // Util function.
|
---|
| 541 | // Creates an empty texture. These textures will be used as render targets, therefore
|
---|
| 542 | // the Usage flag is set to D3DUSAGE_RENDERTARGET and consequently, the assigned
|
---|
| 543 | // memory pool is D3DPOOL_DEFAULT.
|
---|
| 544 | // Params: size and format (eg. D3DFMT_A32B32G32R32F) of the new texture.
|
---|
| 545 | //--------------------------------------------------------------------------------------
|
---|
| 546 | IDirect3DTexture9* CreateTexture( int size, D3DFORMAT Format )
|
---|
| 547 | {
|
---|
| 548 | HRESULT hr;
|
---|
| 549 | IDirect3DTexture9* pTexture;
|
---|
| 550 | V( g_pd3dDevice->CreateTexture( size, size, // dimensions
|
---|
| 551 | 1, // mipmap levels
|
---|
| 552 | D3DUSAGE_RENDERTARGET, // usage
|
---|
| 553 | Format,
|
---|
| 554 | D3DPOOL_DEFAULT,// memory pool
|
---|
| 555 | &pTexture, NULL ) );
|
---|
| 556 | return pTexture;
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | //--------------------------------------------------------------------------------------
|
---|
| 560 | // Util function.
|
---|
| 561 | // Creates an empty cubemap texture of the given resolution and format.
|
---|
| 562 | //--------------------------------------------------------------------------------------
|
---|
| 563 |
|
---|
| 564 | IDirect3DCubeTexture9* CreateCubeTexture( int size, D3DFORMAT Format )
|
---|
| 565 | {
|
---|
| 566 | HRESULT hr;
|
---|
| 567 | IDirect3DCubeTexture9* pCubeTexture;
|
---|
| 568 | V( g_pd3dDevice->CreateCubeTexture( size, 1, D3DUSAGE_RENDERTARGET,
|
---|
| 569 | Format, D3DPOOL_DEFAULT, &pCubeTexture, NULL ) );
|
---|
| 570 | return pCubeTexture;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 |
|
---|
| 574 | //--------------------------------------------------------------------------------------
|
---|
| 575 | // Render the help and statistics text.
|
---|
| 576 | //--------------------------------------------------------------------------------------
|
---|
| 577 | void RenderText()
|
---|
| 578 | {
|
---|
| 579 | const D3DSURFACE_DESC* backBufferDesc = DXUTGetBackBufferSurfaceDesc();
|
---|
| 580 |
|
---|
| 581 | CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
|
---|
| 582 | txtHelper.Begin();
|
---|
| 583 |
|
---|
| 584 | txtHelper.SetInsertionPos( 5, 5 );
|
---|
| 585 | txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
|
---|
| 586 |
|
---|
| 587 | if(showUI)
|
---|
| 588 | {
|
---|
| 589 | txtHelper.DrawFormattedTextLine( L"%.2f fps @ %i x %i",
|
---|
| 590 | DXUTGetFPS(), backBufferDesc->Width, backBufferDesc->Height );
|
---|
| 591 | //txtHelper.DrawTextLine( DXUTGetFrameStats() );
|
---|
| 592 | txtHelper.DrawTextLine( DXUTGetDeviceStats() );
|
---|
| 593 | //txtHelper.DrawTextLine( L"" );
|
---|
| 594 |
|
---|
| 595 | txtHelper.SetInsertionPos( 5, 120 );
|
---|
| 596 | txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
|
---|
| 597 |
|
---|
| 598 | switch(currentMethod)
|
---|
| 599 | {
|
---|
| 600 | case 0:txtHelper.DrawFormattedTextLine( L"Method : Depth of Field with Circle of Confusion");break;
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
| 603 | txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
|
---|
| 604 |
|
---|
| 605 | if ( pp.Get( bShowHelp ) )
|
---|
| 606 | {
|
---|
| 607 | txtHelper.SetInsertionPos( backBufferDesc->Width - 260, backBufferDesc->Height-24*15 );
|
---|
| 608 |
|
---|
| 609 | txtHelper.DrawTextLine(
|
---|
| 610 | L"Controls (F1 to hide):\n\n"
|
---|
| 611 | L"___________________________________\n"
|
---|
| 612 | L" GENERAL CONTROLS\n"
|
---|
| 613 | L"Left click drag: Rotate mesh\n"
|
---|
| 614 | L"Mouse wheel: Zoom\n"
|
---|
| 615 | L"F3: Switch to REF device\n"
|
---|
| 616 | L"F8: Switch to Wireframe mode\n"
|
---|
| 617 | L"Space: Show/Hide UI Elements\n"
|
---|
| 618 | L"___________________________________\n"
|
---|
| 619 | L" ALGORITHM\n"
|
---|
[1463] | 620 | L"Use the slider to change the focal distance\n"
|
---|
[760] | 621 | L"___________________________________\n"
|
---|
| 622 | L" UTILITIES\n"
|
---|
| 623 | L"L: [L]oad view & eye params\n"
|
---|
| 624 | L"S: [S]ave view & eye params\n"
|
---|
| 625 | L"Alt+S: [S]ave Screenshot\n"
|
---|
| 626 | L"___________________________________\n"
|
---|
| 627 | L" Quit: ESC");
|
---|
| 628 | }
|
---|
| 629 | else
|
---|
| 630 | {
|
---|
| 631 | // txtHelper.DrawTextLine( L"Press F1 for help" );
|
---|
| 632 | }
|
---|
| 633 | txtHelper.End();
|
---|
| 634 | } |
---|