[1485] | 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 "Explosion.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 | unsigned int currentMethod=8;
|
---|
| 27 | int methodCount=14;
|
---|
| 28 | bool showUI=true;
|
---|
| 29 |
|
---|
| 30 | Explosion explosion;
|
---|
| 31 |
|
---|
| 32 | IDirect3DDevice9* g_pd3dDevice = NULL;
|
---|
| 33 | ID3DXEffect* g_pEffect = NULL;
|
---|
| 34 |
|
---|
| 35 | CDXUTDialogResourceManager g_DialogResourceManager; // manager for shared resources of dialogs
|
---|
| 36 | CD3DSettingsDlg g_SettingsDlg; // Device settings dialog
|
---|
| 37 | CDXUTDialog g_HUD; // Dialog for sample specific controls
|
---|
| 38 |
|
---|
| 39 | ID3DXFont* g_pFont = NULL; // Font for drawing text
|
---|
| 40 | ID3DXSprite* g_pTextSprite = NULL; // Sprite for batching draw text calls
|
---|
| 41 |
|
---|
| 42 | IDirect3DSurface9* g_pSaveSurface; // screenshot capturing support
|
---|
| 43 | bool bSavingScreenshot = false;
|
---|
| 44 | int counter = 0;
|
---|
| 45 |
|
---|
| 46 | D3DXVECTOR3 LightPosition;
|
---|
| 47 | HRESULT hr;
|
---|
| 48 |
|
---|
| 49 | Parameters pp; // managing parameters of the algorithm
|
---|
| 50 |
|
---|
| 51 | CModelViewerCamera camera; // camera
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | //--------------------------------------------------------------------------------------
|
---|
| 55 | // Forward declarations (CALLBACK)
|
---|
| 56 | //--------------------------------------------------------------------------------------
|
---|
| 57 | bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext );
|
---|
| 58 | bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext );
|
---|
| 59 | HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* g_pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
|
---|
| 60 | HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* g_pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
|
---|
| 61 | void CALLBACK OnFrameMove( IDirect3DDevice9* g_pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
|
---|
| 62 | void CALLBACK OnFrameRender( IDirect3DDevice9* g_pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
|
---|
| 63 | LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext );
|
---|
| 64 | void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext );
|
---|
| 65 | void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext );
|
---|
| 66 | void CALLBACK OnLostDevice( void* pUserContext );
|
---|
| 67 | void CALLBACK OnDestroyDevice( void* pUserContext );
|
---|
| 68 | void InitApp();
|
---|
| 69 | void RenderText();
|
---|
| 70 |
|
---|
| 71 | //--------------------------------------------------------------------------------------
|
---|
| 72 | // Forward declarations
|
---|
| 73 | //--------------------------------------------------------------------------------------
|
---|
| 74 |
|
---|
| 75 | void SaveCameraPosition( char* fileName );
|
---|
| 76 | void LoadCameraPosition( char* fileName );
|
---|
| 77 | int GenerateNewFileName( int& counter );
|
---|
| 78 |
|
---|
| 79 | //--------------------------------------------------------------------------------------
|
---|
| 80 | // Entry point to the program. Initializes everything and goes into a message processing
|
---|
| 81 | // loop. Idle time is used to render the scene.
|
---|
| 82 | //--------------------------------------------------------------------------------------
|
---|
| 83 | INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
|
---|
| 84 | {
|
---|
| 85 | // Enable run-time memory check for debug builds.
|
---|
| 86 | #if defined(DEBUG) | defined(_DEBUG)
|
---|
| 87 | _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
|
---|
| 88 | #endif
|
---|
| 89 |
|
---|
| 90 | DXUTSetCallbackDeviceCreated( OnCreateDevice );
|
---|
| 91 | DXUTSetCallbackDeviceReset( OnResetDevice );
|
---|
| 92 | DXUTSetCallbackDeviceLost( OnLostDevice );
|
---|
| 93 | DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
|
---|
| 94 | DXUTSetCallbackMsgProc( MsgProc );
|
---|
| 95 | DXUTSetCallbackKeyboard( KeyboardProc );
|
---|
| 96 | DXUTSetCallbackFrameRender( OnFrameRender );
|
---|
| 97 | DXUTSetCallbackFrameMove( OnFrameMove );
|
---|
| 98 |
|
---|
| 99 | InitApp();
|
---|
| 100 |
|
---|
| 101 | // Show the cursor and clip it when in full screen
|
---|
| 102 | DXUTSetCursorSettings( true, true );
|
---|
| 103 |
|
---|
| 104 | DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
|
---|
| 105 | DXUTCreateWindow( L"Displacement Demo" );
|
---|
| 106 | DXUTCreateDevice( D3DADAPTER_DEFAULT, true, WIDTH, HEIGHT, IsDeviceAcceptable, ModifyDeviceSettings );
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | camera.SetButtonMasks( 0, MOUSE_WHEEL, MOUSE_LEFT_BUTTON );
|
---|
| 110 |
|
---|
| 111 | DXUTMainLoop();
|
---|
| 112 |
|
---|
| 113 | return DXUTGetExitCode();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
|
---|
| 117 | {
|
---|
| 118 | pp.UpdateFromHUD( nControlID );
|
---|
| 119 | explosion.slidersChanged();
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void OnLoad() {
|
---|
| 123 | LoadCameraPosition( ".matrix" );
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void OnSave() {
|
---|
| 127 | SaveCameraPosition( ".matrix" );
|
---|
| 128 | bSavingScreenshot = true;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void OnReset()
|
---|
| 132 | {
|
---|
| 133 | LightPosition.x=0;
|
---|
| 134 | LightPosition.y=2;
|
---|
| 135 | LightPosition.z=0;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | void InitApp()
|
---|
| 139 | {
|
---|
| 140 | g_SettingsDlg.Init( &g_DialogResourceManager );
|
---|
| 141 | g_HUD.Init( &g_DialogResourceManager );
|
---|
| 142 | g_HUD.SetCallback( OnGUIEvent ); // Event handling
|
---|
| 143 |
|
---|
| 144 | pp.Setup( &g_HUD, OnReset, OnSave, OnLoad); // you can add callbacks to these actions
|
---|
| 145 |
|
---|
| 146 | pp.Add( bShowHelp, "Show Help", VK_F1 );
|
---|
| 147 | pp.Add( bObjectOn, "Show Scene", 'O');
|
---|
| 148 | pp.Add( bPaused, "Pause", 'P');
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | explosion.addUiParams(pp);
|
---|
| 152 |
|
---|
| 153 | pp.UpdateFromHUD( IDC_RESET_BUTTON ); // do a reset
|
---|
| 154 | pp.SetBool(bObjectOn, true);
|
---|
| 155 | pp.SetFloat(fTexRatio, 0.5);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | //--------------------------------------------------------------------------------------
|
---|
| 159 | // Rejects any devices that aren't acceptable by returning false
|
---|
| 160 | //--------------------------------------------------------------------------------------
|
---|
| 161 | bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
|
---|
| 162 | D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
|
---|
| 163 | {
|
---|
| 164 | // Skip backbuffer formats that don't support alpha blending
|
---|
| 165 | IDirect3D9* pD3D = DXUTGetD3DObject();
|
---|
| 166 | if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
|
---|
| 167 | AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
|
---|
| 168 | D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
|
---|
| 169 | return false;
|
---|
| 170 |
|
---|
| 171 | return true;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | //--------------------------------------------------------------------------------------
|
---|
| 175 | // Before a device is created, modify the device settings as needed
|
---|
| 176 | //--------------------------------------------------------------------------------------
|
---|
| 177 | bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
|
---|
| 178 | {
|
---|
| 179 | // VSync off
|
---|
| 180 | pDeviceSettings->pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
|
---|
| 181 |
|
---|
| 182 | // If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW
|
---|
| 183 | // then switch to SWVP.
|
---|
| 184 | if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
|
---|
| 185 | pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) )
|
---|
| 186 | pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
|
---|
| 187 | else
|
---|
| 188 | pDeviceSettings->BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
|
---|
| 189 |
|
---|
| 190 | // This application is designed to work on a pure device by not using
|
---|
| 191 | // IDirect3D9::Get*() methods, so create a pure device if supported and using HWVP.
|
---|
| 192 | if ((pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE) != 0 &&
|
---|
| 193 | (pDeviceSettings->BehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING) != 0 )
|
---|
| 194 | pDeviceSettings->BehaviorFlags |= D3DCREATE_PUREDEVICE;
|
---|
| 195 |
|
---|
| 196 | // Debugging vertex shaders requires either REF or software vertex processing
|
---|
| 197 | // and debugging pixel shaders requires REF.
|
---|
| 198 | #ifdef DEBUG_VS
|
---|
| 199 | if( pDeviceSettings->DeviceType != D3DDEVTYPE_REF )
|
---|
| 200 | {
|
---|
| 201 | pDeviceSettings->BehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
|
---|
| 202 | pDeviceSettings->BehaviorFlags &= ~D3DCREATE_PUREDEVICE;
|
---|
| 203 | pDeviceSettings->BehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
|
---|
| 204 | }
|
---|
| 205 | #endif
|
---|
| 206 | #ifdef DEBUG_PS
|
---|
| 207 | pDeviceSettings->DeviceType = D3DDEVTYPE_REF;
|
---|
| 208 | #endif
|
---|
| 209 |
|
---|
| 210 | // For the first device created if its a REF device, optionally display a warning dialog box
|
---|
| 211 | static bool s_bFirstTime = true;
|
---|
| 212 | if( s_bFirstTime )
|
---|
| 213 | {
|
---|
| 214 | s_bFirstTime = false;
|
---|
| 215 | if( pDeviceSettings->DeviceType == D3DDEVTYPE_REF )
|
---|
| 216 | DXUTDisplaySwitchingToREFWarning();
|
---|
| 217 | }
|
---|
| 218 | return true;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | //--------------------------------------------------------------------------------------
|
---|
| 222 | // Create any D3DPOOL_MANAGED resources here
|
---|
| 223 | //--------------------------------------------------------------------------------------
|
---|
| 224 | HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
| 225 | {
|
---|
| 226 | ::g_pd3dDevice = pd3dDevice;
|
---|
| 227 |
|
---|
| 228 | HRESULT hr;
|
---|
| 229 |
|
---|
| 230 | V_RETURN( g_DialogResourceManager.OnCreateDevice( g_pd3dDevice ) );
|
---|
| 231 | V_RETURN( g_SettingsDlg.OnCreateDevice( g_pd3dDevice ) );
|
---|
| 232 |
|
---|
| 233 | // Initialize the font
|
---|
| 234 | V_RETURN( D3DXCreateFont( g_pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
|
---|
| 235 | OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
|
---|
| 236 | L"Arial", &g_pFont ) );
|
---|
| 237 |
|
---|
| 238 |
|
---|
| 239 |
|
---|
| 240 | float initialEyeDist = 35.0f;
|
---|
| 241 | D3DXVECTOR3 vecEye(0, 0, 40.0f - initialEyeDist);
|
---|
| 242 | D3DXVECTOR3 vecAt (0.0f, 0.0f, 40.0f);
|
---|
| 243 | camera.SetViewParams( &vecEye, &vecAt );
|
---|
| 244 | camera.SetScalers(0, 0.0005f); //NO EFFECT?????
|
---|
| 245 |
|
---|
| 246 |
|
---|
| 247 | explosion.OnCreateDevice(g_pd3dDevice);
|
---|
| 248 | explosion.camera = &camera;
|
---|
| 249 |
|
---|
| 250 | return S_OK;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | //--------------------------------------------------------------------------------------
|
---|
| 254 | // Release resources created in the OnCreateDevice callback here
|
---|
| 255 | //--------------------------------------------------------------------------------------
|
---|
| 256 | void CALLBACK OnDestroyDevice( void* pUserContext )
|
---|
| 257 | {
|
---|
| 258 | g_DialogResourceManager.OnDestroyDevice();
|
---|
| 259 | g_SettingsDlg.OnDestroyDevice();
|
---|
| 260 | SAFE_RELEASE(g_pFont);
|
---|
| 261 |
|
---|
| 262 | explosion.OnDestroyDevice();
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | //--------------------------------------------------------------------------------------
|
---|
| 267 | // Create any D3DPOOL_DEFAULT resources here
|
---|
| 268 | //--------------------------------------------------------------------------------------
|
---|
| 269 | HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* g_pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
| 270 | {
|
---|
| 271 | HRESULT hr;
|
---|
| 272 |
|
---|
| 273 | V_RETURN( g_DialogResourceManager.OnResetDevice() );
|
---|
| 274 | V_RETURN( g_SettingsDlg.OnResetDevice() );
|
---|
| 275 |
|
---|
| 276 | if( g_pFont ) V_RETURN( g_pFont->OnResetDevice() );
|
---|
| 277 |
|
---|
| 278 | // Create a sprite to help batch calls when drawing many lines of text
|
---|
| 279 | V_RETURN( D3DXCreateSprite( g_pd3dDevice, &g_pTextSprite ) );
|
---|
| 280 |
|
---|
| 281 | // Create a surface to save screenshots
|
---|
| 282 | IDirect3DTexture9* pSaveTexture = NULL;
|
---|
| 283 | V( D3DXCreateTexture( g_pd3dDevice, pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height, 1,
|
---|
| 284 | D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pSaveTexture) );
|
---|
| 285 | pSaveTexture->GetSurfaceLevel( 0, &g_pSaveSurface );
|
---|
| 286 | SAFE_RELEASE( pSaveTexture );
|
---|
| 287 |
|
---|
| 288 | // Setup the camera's projection parameters
|
---|
| 289 | float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
|
---|
| 290 |
|
---|
| 291 | camera.SetProjParams( D3DX_PI/4, fAspectRatio, 0.1f, 1000.0f );
|
---|
| 292 | camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
|
---|
| 293 |
|
---|
| 294 | // Position the on-screen dialog
|
---|
| 295 | g_HUD.SetLocation( 0, 0 );
|
---|
| 296 | g_HUD.SetSize( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
|
---|
| 297 |
|
---|
| 298 | explosion.OnResetDevice(pBackBufferSurfaceDesc);
|
---|
| 299 |
|
---|
| 300 | return S_OK;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | //--------------------------------------------------------------------------------------
|
---|
| 304 | // Release resources created in the OnResetDevice callback here
|
---|
| 305 | //--------------------------------------------------------------------------------------
|
---|
| 306 | void CALLBACK OnLostDevice( void* pUserContext )
|
---|
| 307 | {
|
---|
| 308 | g_DialogResourceManager.OnLostDevice();
|
---|
| 309 | g_SettingsDlg.OnLostDevice();
|
---|
| 310 |
|
---|
| 311 | if( g_pFont ) g_pFont->OnLostDevice();
|
---|
| 312 |
|
---|
| 313 | SAFE_RELEASE( g_pTextSprite );
|
---|
| 314 | SAFE_RELEASE( g_pSaveSurface );
|
---|
| 315 |
|
---|
| 316 | explosion.OnLostDevice();
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext )
|
---|
| 320 | {
|
---|
| 321 | // Always allow dialog resource manager calls to handle global messages
|
---|
| 322 | // so GUI state is updated correctly
|
---|
| 323 | *pbNoFurtherProcessing = g_DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
|
---|
| 324 | if( *pbNoFurtherProcessing )
|
---|
| 325 | return 0;
|
---|
| 326 |
|
---|
| 327 | if( g_SettingsDlg.IsActive() )
|
---|
| 328 | {
|
---|
| 329 | g_SettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam );
|
---|
| 330 | return 0;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | // Give the dialogs a chance to handle the message first
|
---|
| 334 | *pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam );
|
---|
| 335 | if( *pbNoFurtherProcessing )
|
---|
| 336 | return 0;
|
---|
| 337 |
|
---|
| 338 | // Pass all remaining windows messages to camera so it can respond to user input
|
---|
| 339 | camera.HandleMessages( hWnd, uMsg, wParam, lParam );
|
---|
| 340 |
|
---|
| 341 | return 0;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | //--------------------------------------------------------------------------------------
|
---|
| 345 | // Handle updates to the scene
|
---|
| 346 | //--------------------------------------------------------------------------------------
|
---|
| 347 | void CALLBACK OnFrameMove( IDirect3DDevice9* g_pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
|
---|
| 348 | {
|
---|
| 349 | camera.FrameMove( fElapsedTime );
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | //--------------------------------------------------------------------------------------
|
---|
| 353 | // Render the scene
|
---|
| 354 | //--------------------------------------------------------------------------------------
|
---|
| 355 | void CALLBACK OnFrameRender( IDirect3DDevice9* g_pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
|
---|
| 356 | {
|
---|
| 357 |
|
---|
| 358 |
|
---|
| 359 | IDirect3DSurface9* oldRenderTarget = NULL;
|
---|
| 360 | if (bSavingScreenshot) {
|
---|
| 361 | V( g_pd3dDevice->GetRenderTarget(0, &oldRenderTarget) );
|
---|
| 362 | V( g_pd3dDevice->SetRenderTarget(0, g_pSaveSurface) );
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | V( g_pd3dDevice->BeginScene() );
|
---|
| 366 |
|
---|
| 367 |
|
---|
| 368 | D3DXMATRIXA16 mView = *camera.GetViewMatrix();
|
---|
| 369 | D3DXMATRIXA16 mProj = *camera.GetProjMatrix();
|
---|
| 370 |
|
---|
| 371 | // V( g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(75, 75, 75), 1.0f, 0L) );
|
---|
| 372 |
|
---|
| 373 |
|
---|
| 374 | explosion.camera = &camera;
|
---|
| 375 | explosion.LightPos=&LightPosition;
|
---|
| 376 | explosion.OnFrameRender(mView,mProj);
|
---|
| 377 |
|
---|
| 378 | if (!bSavingScreenshot)
|
---|
| 379 | {
|
---|
| 380 | RenderText();
|
---|
| 381 | if(showUI)
|
---|
| 382 | {
|
---|
| 383 | V( g_HUD.OnRender( fElapsedTime ) );
|
---|
| 384 | }
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | V( g_pd3dDevice->EndScene() );
|
---|
| 388 |
|
---|
| 389 | if (bSavingScreenshot)
|
---|
| 390 | {
|
---|
| 391 | // saving surface
|
---|
| 392 | char buf[CHARBUFFER_SIZE];
|
---|
| 393 | wchar_t wbuf[CHARBUFFER_SIZE];
|
---|
| 394 |
|
---|
| 395 | GenerateNewFileName( counter );
|
---|
| 396 | sprintf(buf, "shots\\%03i.png", counter);
|
---|
| 397 | mbstowcs( wbuf, buf, CHARBUFFER_SIZE );
|
---|
| 398 |
|
---|
| 399 | D3DXSaveSurfaceToFileW(wbuf, D3DXIFF_PNG, g_pSaveSurface, NULL, NULL);
|
---|
| 400 |
|
---|
| 401 | sprintf(buf, "shots\\%03i", counter);
|
---|
| 402 | pp.SaveToFile( buf );
|
---|
| 403 |
|
---|
| 404 | sprintf(buf, "shots\\%03i.matrix", counter);
|
---|
| 405 | SaveCameraPosition( buf );
|
---|
| 406 |
|
---|
| 407 | g_pd3dDevice->SetRenderTarget(0, oldRenderTarget);
|
---|
| 408 | bSavingScreenshot = false;
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | int GenerateNewFileName( int& counter )
|
---|
| 413 | {
|
---|
| 414 | char buf[CHARBUFFER_SIZE];
|
---|
| 415 | FILE* f;
|
---|
| 416 | do {
|
---|
| 417 | sprintf(buf, "shots\\%03i.png", counter);
|
---|
| 418 | if ( (f = fopen(buf, "rt")) != NULL )
|
---|
| 419 | {
|
---|
| 420 | fclose( f );
|
---|
| 421 | counter++;
|
---|
| 422 | }
|
---|
| 423 | } while (f != NULL);
|
---|
| 424 | return counter;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | //--------------------------------------------------------------------------------------
|
---|
| 428 | // The framework does not remove the underlying keystroke messages,
|
---|
| 429 | // which are still passed to the application's MsgProc callback.
|
---|
| 430 | //--------------------------------------------------------------------------------------
|
---|
| 431 | void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
|
---|
| 432 | {
|
---|
| 433 | if( bKeyDown )
|
---|
| 434 | {
|
---|
| 435 |
|
---|
| 436 | switch(nChar)
|
---|
| 437 | {
|
---|
| 438 | case VK_SPACE:
|
---|
| 439 | showUI=!showUI;
|
---|
| 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, 0.0f, 0.0f, 1.0f ) );
|
---|
| 597 | }
|
---|
| 598 | txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
|
---|
| 599 |
|
---|
| 600 | if ( pp.Get( bShowHelp ) )
|
---|
| 601 | {
|
---|
| 602 | txtHelper.SetInsertionPos( backBufferDesc->Width - 260, backBufferDesc->Height-24*15 );
|
---|
| 603 |
|
---|
| 604 | txtHelper.DrawTextLine(
|
---|
| 605 | L"Controls (F1 to hide):\n\n"
|
---|
| 606 | L"___________________________________\n"
|
---|
| 607 | L" GENERAL CONTROLS\n"
|
---|
| 608 | L"Left click drag: Rotate mesh\n"
|
---|
| 609 | L"Mouse wheel: Zoom\n"
|
---|
| 610 | L"F8: Switch to Wireframe mode\n"
|
---|
| 611 | L"Space: Show/Hide UI Elements\n"
|
---|
| 612 | L"___________________________________\n"
|
---|
| 613 | L" ALGORITHM\n"
|
---|
| 614 | L"Use sliders to adjust parameters\n"
|
---|
| 615 | L"___________________________________\n"
|
---|
| 616 | L" UTILITIES\n"
|
---|
| 617 | L"L: [L]oad view & eye params\n"
|
---|
| 618 | L"S: [S]ave view & eye params\n"
|
---|
| 619 | L"Alt+S: [S]ave Screenshot\n"
|
---|
| 620 | L"___________________________________\n"
|
---|
| 621 | L" Quit: ESC");
|
---|
| 622 | }
|
---|
| 623 | else
|
---|
| 624 | {
|
---|
| 625 | // txtHelper.DrawTextLine( L"Press F1 for help" );
|
---|
| 626 | }
|
---|
| 627 | txtHelper.End();
|
---|
| 628 | } |
---|