//-------------------------------------------------------------------------------------- // File: PathMap.cpp // // Empty starting point for new Direct3D applications // // Copyright (c) Microsoft Corporation. All rights reserved. //-------------------------------------------------------------------------------------- #include "dxstdafx.h" #include "resource.h" #include "PathMapEffect.h" #include "Parameters.h" PathMapEffect* pathMapEffect = NULL; bool gShowUI = true; CDXUTDialogResourceManager gDialogResourceManager; // manager for shared resources of dialogs CD3DSettingsDlg gSettingsDlg; // Device settings dialog CDXUTDialog gHUD; // Dialog for sample specific controls ID3DXFont* gFont = NULL; // Font for drawing text ID3DXSprite* gTextSprite = NULL; // Sprite for batching draw text calls Parameters gParameters; char prmDirectory[256]; char meshDirectory[256]; char mediaDirectory[256]; char levelFileName[256]; char materialFileName[256]; bool segmentMeshes = false; bool computePRM = false; bool noAtlasGen = false; bool uniformSampling = false; unsigned int nEntryPoints = 4096; unsigned int nClusters = 32; unsigned int depthMapResolution = 512; void printUsage() { MessageBoxA( NULL, "pathmap.exe [s|p/v] -D -L -M -O -P -E -C -S ", "Missing argument!", MB_OK); } //-------------------------------------------------------------------------------------- // Rejects any devices that aren't acceptable by returning false //-------------------------------------------------------------------------------------- bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext ) { // Typically want to skip backbuffer formats that don't support alpha blending IDirect3D9* pD3D = DXUTGetD3DObject(); if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, BackBufferFormat ) ) ) return false; return true; } //-------------------------------------------------------------------------------------- // Before a device is created, modify the device settings as needed //-------------------------------------------------------------------------------------- bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext ) { pDeviceSettings->pp.Flags &= ~D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL; // VSync off pDeviceSettings->pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; return true; } //-------------------------------------------------------------------------------------- // Create any D3DPOOL_MANAGED resources here //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { gDialogResourceManager.OnCreateDevice( pd3dDevice ); gSettingsDlg.OnCreateDevice( pd3dDevice ); // Initialize the font D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &gFont ); return S_OK; } //-------------------------------------------------------------------------------------- // Create any D3DPOOL_DEFAULT resources here //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { gDialogResourceManager.OnResetDevice(); gSettingsDlg.OnResetDevice(); if( gFont ) gFont->OnResetDevice(); // Create a sprite to help batch calls when drawing many lines of text D3DXCreateSprite( pd3dDevice, &gTextSprite ); // Position the on-screen dialog gHUD.SetLocation( 0, 0 ); gHUD.SetSize( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); pathMapEffect = new PathMapEffect(pd3dDevice, prmDirectory, meshDirectory, mediaDirectory, levelFileName, materialFileName, segmentMeshes, computePRM, uniformSampling, !noAtlasGen, nEntryPoints, nClusters, depthMapResolution); return S_OK; } //-------------------------------------------------------------------------------------- // Handle updates to the scene //-------------------------------------------------------------------------------------- void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { if(pathMapEffect) pathMapEffect->move(fElapsedTime); } //-------------------------------------------------------------------------------------- // Render the help and statistics text. //-------------------------------------------------------------------------------------- void RenderText() { const D3DSURFACE_DESC* backBufferDesc = DXUTGetBackBufferSurfaceDesc(); CDXUTTextHelper txtHelper( gFont, gTextSprite, 15 ); txtHelper.Begin(); txtHelper.SetInsertionPos( 5, 5 ); txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) ); if(gShowUI) { txtHelper.DrawFormattedTextLine( L"%.2f fps @ %i x %i", DXUTGetFPS(), backBufferDesc->Width, backBufferDesc->Height ); txtHelper.DrawTextLine( DXUTGetFrameStats() ); txtHelper.DrawTextLine( DXUTGetDeviceStats() ); txtHelper.SetInsertionPos( 5, 180 ); txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) ); if(pathMapEffect) { txtHelper.DrawFormattedTextLine( pathMapEffect->getCurrentMethodName()); txtHelper.DrawFormattedTextLine( pathMapEffect->getCruiseLoop()); } } txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) ); if ( gParameters.Get( bShowHelp ) ) { txtHelper.SetInsertionPos( backBufferDesc->Width - 260, backBufferDesc->Height-24*8 ); txtHelper.DrawTextLine( L"Controls (F1 to hide):\n\n" L"___________________________________\n" L" GENERAL CONTROLS\n" L"Left click drag: Turn camera\n" L"Space: Show/Hide UI Elements\n" L"___________________________________\n" L"___________________________________\n" L" Quit: ESC"); } else { // txtHelper.DrawTextLine( L"Press F1 for help" ); } txtHelper.End(); } //-------------------------------------------------------------------------------------- // Render the scene //-------------------------------------------------------------------------------------- void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { HRESULT hr; pathMapEffect->render(); pd3dDevice->BeginScene(); RenderText(); if(gShowUI) gHUD.OnRender( fElapsedTime ); pd3dDevice->EndScene();} //-------------------------------------------------------------------------------------- // Handle messages to the application //-------------------------------------------------------------------------------------- LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext ) { *pbNoFurtherProcessing = gDialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam ); if( *pbNoFurtherProcessing ) return 0; if( gSettingsDlg.IsActive() ) { gSettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam ); return 0; } // Give the dialogs a chance to handle the message first *pbNoFurtherProcessing = gHUD.MsgProc( hWnd, uMsg, wParam, lParam ); if( *pbNoFurtherProcessing ) return 0; if(pathMapEffect) pathMapEffect->handleMessage(hWnd, uMsg, wParam, lParam); return 0; } void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext ) { if( bKeyDown ) { switch(nChar) { case VK_SPACE: gShowUI=!gShowUI; break; case VK_TAB: if(pathMapEffect) pathMapEffect->nextMethod(); break; case '0': if(pathMapEffect) pathMapEffect->prevMethod(); break; case VK_DOWN: break; case VK_UP: break; case VK_LEFT: break; case VK_RIGHT: break; case VK_DELETE: break; case VK_END: break; default: break; } } } void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext ) { gParameters.UpdateFromHUD( nControlID ); if(nControlID == IDC_GEN_BUTTON) { LPDIRECT3DDEVICE9 dev = pathMapEffect->getDevice(); delete pathMapEffect; pathMapEffect = new PathMapEffect(dev, prmDirectory, meshDirectory, mediaDirectory, levelFileName, materialFileName, segmentMeshes, computePRM, uniformSampling, !noAtlasGen, nEntryPoints, nClusters, depthMapResolution); } } void OnLoad() { } void OnSave() { } void OnReset() { } //-------------------------------------------------------------------------------------- // Release resources created in the OnResetDevice callback here //-------------------------------------------------------------------------------------- void CALLBACK OnLostDevice( void* pUserContext ) { gDialogResourceManager.OnLostDevice(); gSettingsDlg.OnLostDevice(); if( gFont ) gFont->OnLostDevice(); gTextSprite->Release(); delete pathMapEffect; pathMapEffect = NULL; } //-------------------------------------------------------------------------------------- // Release resources created in the OnCreateDevice callback here //-------------------------------------------------------------------------------------- void CALLBACK OnDestroyDevice( void* pUserContext ) { gDialogResourceManager.OnDestroyDevice(); gSettingsDlg.OnDestroyDevice(); gFont->Release(); } //-------------------------------------------------------------------------------------- // Initialize everything and go into a render loop //-------------------------------------------------------------------------------------- INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR commandLine, int ) { // Enable run-time memory check for debug builds. #if defined(DEBUG) | defined(_DEBUG) _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); #endif // Set the callback functions DXUTSetCallbackDeviceCreated( OnCreateDevice ); DXUTSetCallbackDeviceReset( OnResetDevice ); DXUTSetCallbackDeviceLost( OnLostDevice ); DXUTSetCallbackDeviceDestroyed( OnDestroyDevice ); DXUTSetCallbackMsgProc( MsgProc ); DXUTSetCallbackFrameRender( OnFrameRender ); DXUTSetCallbackFrameMove( OnFrameMove ); DXUTSetCallbackKeyboard( KeyboardProc ); // TODO: Perform any application-level initialization here gSettingsDlg.Init( &gDialogResourceManager ); gHUD.Init( &gDialogResourceManager ); gHUD.SetCallback( OnGUIEvent ); // Event handling gParameters.Setup( &gHUD, OnReset, OnSave, OnLoad); // you can add callbacks to these actions gParameters.Add( bShowHelp, "Show Help", VK_F1 ); PathMapEffect::addUiParameters(&gParameters); gParameters.UpdateFromHUD( IDC_RESET_BUTTON ); // do a reset PathMapEffect::setUiParameterDefaults(&gParameters); std::cerr << commandLine; if(commandLine[0] == 's' || commandLine[1] == 's') segmentMeshes = true; else segmentMeshes = false; if(commandLine[0] == 'p' || commandLine[1] == 'p') computePRM = true; else computePRM = false; strcpy(mediaDirectory, "media"); strcpy(prmDirectory, "prm"); strcpy(meshDirectory, "processedMeshes"); strcpy(levelFileName, "space.level"); strcpy(materialFileName, "space.materials"); nClusters = 64; nEntryPoints = 4096; depthMapResolution = 512; char* toki; toki = strtok(commandLine, " "); while(toki) { if(toki[0] == '-') { switch(toki[1]) { case 'A' : noAtlasGen = true; break; case 'U' : uniformSampling = true; break; case 'D' : toki = strtok(NULL, " "); if(toki == NULL) printUsage(); strcpy(mediaDirectory, toki); break; case 'P' : toki = strtok(NULL, " "); if(toki == NULL) printUsage(); strcpy(prmDirectory, toki); break; case 'O' : toki = strtok(NULL, " "); if(toki == NULL) printUsage(); strcpy(meshDirectory, toki); break; case 'L' : toki = strtok(NULL, " "); if(toki == NULL) printUsage(); strcpy(levelFileName, toki); break; case 'M' : toki = strtok(NULL, " "); if(toki == NULL) printUsage(); strcpy(materialFileName, toki); break; case 'E' : toki = strtok(NULL, " "); if(toki == NULL) printUsage(); nEntryPoints = atoi(toki); break; case 'C' : toki = strtok(NULL, " "); if(toki == NULL) printUsage(); nClusters = atoi(toki); break; case 'S' : toki = strtok(NULL, " "); if(toki == NULL) printUsage(); depthMapResolution = atoi(toki); break; default: printUsage(); } } toki = strtok(NULL, " "); } // Initialize DXUT and create the desired Win32 window and Direct3D device for the application DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen DXUTCreateWindow( L"PathMap" ); DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings ); // Start the render loop DXUTMainLoop(); // TODO: Perform any application-level cleanup here return DXUTGetExitCode(); }