source: GTP/trunk/App/Demos/Illum/pathmap/PathMap.cpp @ 2197

Revision 2197, 13.8 KB checked in by szirmay, 17 years ago (diff)
Line 
1//--------------------------------------------------------------------------------------
2// File: PathMap.cpp
3//
4// Empty starting point for new Direct3D applications
5//
6// Copyright (c) Microsoft Corporation. All rights reserved.
7//--------------------------------------------------------------------------------------
8#include "dxstdafx.h"
9#include "resource.h"
10#include "PathMapEffect.h"
11#include "Parameters.h"
12
13PathMapEffect*  pathMapEffect = NULL;
14
15bool                                            gShowUI = true;
16CDXUTDialogResourceManager      gDialogResourceManager;         // manager for shared resources of dialogs
17CD3DSettingsDlg                         gSettingsDlg;                           // Device settings dialog
18CDXUTDialog                                     gHUD;                                           // Dialog for sample specific controls
19
20ID3DXFont*                                      gFont = NULL;                           // Font for drawing text
21ID3DXSprite*                            gTextSprite = NULL;                     // Sprite for batching draw text calls
22
23Parameters                                      gParameters;
24
25char    prmDirectory[256];
26char    meshDirectory[256];
27char    mediaDirectory[256];
28char    levelFileName[256];
29char    materialFileName[256];
30
31bool    segmentMeshes = false;
32bool    computePRM = false;
33
34unsigned int nEntryPoints = 4096;
35unsigned int nClusters = 32;
36unsigned int depthMapResolution = 512;
37
38void printUsage()
39{
40        MessageBoxA( NULL, "pathmap.exe [s|p/v] -D <media input directory> -L <level file> -M <materials file> -O <mesh output dir> -P <prm out put dir> -E <number of entry points> -C <number of clusters> -S <shadow map resolution>", "Missing argument!", MB_OK);
41}
42
43//--------------------------------------------------------------------------------------
44// Rejects any devices that aren't acceptable by returning false
45//--------------------------------------------------------------------------------------
46bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,
47                                  D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
48{
49    // Typically want to skip backbuffer formats that don't support alpha blending
50    IDirect3D9* pD3D = DXUTGetD3DObject();
51    if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
52                    AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
53                    D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
54        return false;
55
56    return true;
57}
58
59
60//--------------------------------------------------------------------------------------
61// Before a device is created, modify the device settings as needed
62//--------------------------------------------------------------------------------------
63bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
64{
65        pDeviceSettings->pp.Flags &= ~D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
66        // VSync off
67        pDeviceSettings->pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
68    return true;
69}
70
71
72//--------------------------------------------------------------------------------------
73// Create any D3DPOOL_MANAGED resources here
74//--------------------------------------------------------------------------------------
75HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
76{
77        gDialogResourceManager.OnCreateDevice( pd3dDevice );
78        gSettingsDlg.OnCreateDevice( pd3dDevice );
79
80    // Initialize the font
81        D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
82                         OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
83                         L"Arial", &gFont );
84
85    return S_OK;
86}
87
88
89//--------------------------------------------------------------------------------------
90// Create any D3DPOOL_DEFAULT resources here
91//--------------------------------------------------------------------------------------
92HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice,
93                                const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
94{
95        gDialogResourceManager.OnResetDevice();
96        gSettingsDlg.OnResetDevice();
97
98    if( gFont )
99                gFont->OnResetDevice();
100 
101    // Create a sprite to help batch calls when drawing many lines of text
102        D3DXCreateSprite( pd3dDevice, &gTextSprite );
103
104    // Position the on-screen dialog
105    gHUD.SetLocation( 0, 0 );
106    gHUD.SetSize( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
107
108        pathMapEffect = new PathMapEffect(pd3dDevice,
109                prmDirectory,
110                meshDirectory,
111                mediaDirectory,
112                levelFileName,
113                materialFileName,
114                segmentMeshes,
115                computePRM,
116                nEntryPoints,
117                nClusters,
118                depthMapResolution);
119    return S_OK;
120}
121
122
123//--------------------------------------------------------------------------------------
124// Handle updates to the scene
125//--------------------------------------------------------------------------------------
126void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
127{
128        if(pathMapEffect)
129                pathMapEffect->move(fElapsedTime);
130}
131
132
133//--------------------------------------------------------------------------------------
134// Render the help and statistics text.
135//--------------------------------------------------------------------------------------
136void RenderText()
137{
138        const D3DSURFACE_DESC* backBufferDesc = DXUTGetBackBufferSurfaceDesc();
139
140    CDXUTTextHelper txtHelper( gFont, gTextSprite, 15 );
141    txtHelper.Begin();
142
143        txtHelper.SetInsertionPos( 5, 5 );
144        txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
145
146        if(gShowUI)
147        {
148                txtHelper.DrawFormattedTextLine( L"%.2f fps @ %i x %i",
149                DXUTGetFPS(), backBufferDesc->Width, backBufferDesc->Height );
150                txtHelper.DrawTextLine( DXUTGetFrameStats() );
151                txtHelper.DrawTextLine( DXUTGetDeviceStats() );
152               
153                txtHelper.SetInsertionPos( 5, 180 );
154                txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
155       
156                if(pathMapEffect)
157                {
158                        txtHelper.DrawFormattedTextLine( pathMapEffect->getCurrentMethodName());
159//                      txtHelper.DrawFormattedTextLine( pathMapEffect->getWeightsString());
160                }
161
162        }
163        txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
164
165        if ( gParameters.Get( bShowHelp ) )
166        {
167                txtHelper.SetInsertionPos( backBufferDesc->Width - 260, backBufferDesc->Height-24*8 );
168
169                txtHelper.DrawTextLine(
170                                L"Controls (F1 to hide):\n\n"
171                                L"___________________________________\n"
172                                L"                     GENERAL CONTROLS\n"
173                                L"Left click drag: Turn camera\n"
174                                L"Space: Show/Hide UI Elements\n"
175                                L"___________________________________\n"
176                                L"___________________________________\n"
177                                L"                             Quit: ESC");
178        }
179        else
180        {
181        //      txtHelper.DrawTextLine( L"Press F1 for help" );
182        }
183
184        txtHelper.End();
185}
186
187//--------------------------------------------------------------------------------------
188// Render the scene
189//--------------------------------------------------------------------------------------
190void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
191{
192    HRESULT hr;
193
194        pathMapEffect->render();
195        pd3dDevice->BeginScene();
196        RenderText();
197        if(gShowUI)
198                gHUD.OnRender( fElapsedTime );
199        pd3dDevice->EndScene();}
200
201
202//--------------------------------------------------------------------------------------
203// Handle messages to the application
204//--------------------------------------------------------------------------------------
205LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
206                          bool* pbNoFurtherProcessing, void* pUserContext )
207{
208    *pbNoFurtherProcessing = gDialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam );
209    if( *pbNoFurtherProcessing )
210        return 0;
211
212    if( gSettingsDlg.IsActive() )
213    {
214        gSettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam );
215        return 0;
216    }
217
218        // Give the dialogs a chance to handle the message first
219    *pbNoFurtherProcessing = gHUD.MsgProc( hWnd, uMsg, wParam, lParam );
220    if( *pbNoFurtherProcessing )
221        return 0;
222
223        if(pathMapEffect)
224                pathMapEffect->handleMessage(hWnd, uMsg, wParam, lParam);
225
226
227    return 0;
228}
229
230
231void    CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext )
232{
233        if( bKeyDown )
234    {
235                switch(nChar)
236                {
237                        case VK_SPACE:
238                                gShowUI=!gShowUI;
239                                break;
240                        case VK_TAB:
241                                if(pathMapEffect)
242                                        pathMapEffect->nextMethod();
243                                break;
244                        case '0':
245                                if(pathMapEffect)
246                                        pathMapEffect->prevMethod();
247                                break;
248                        case VK_DOWN:
249                                break;
250                        case VK_UP:
251                                break;
252                        case VK_LEFT:
253                                break;
254                        case VK_RIGHT:
255                                break;
256                        case VK_DELETE:
257                                break;
258                        case VK_END:
259                                break;                         
260                        default:
261                                break;
262                }
263        }
264}
265
266void    CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
267{
268        gParameters.UpdateFromHUD( nControlID );
269        if(nControlID == IDC_GEN_BUTTON)
270        {
271                LPDIRECT3DDEVICE9 dev = pathMapEffect->getDevice();
272                delete pathMapEffect;
273                pathMapEffect = new PathMapEffect(dev,
274                        prmDirectory,
275                        meshDirectory,
276                        mediaDirectory,
277                        levelFileName,
278                        materialFileName,
279                        segmentMeshes,
280                        computePRM,
281                        nEntryPoints,
282                        nClusters,
283                        depthMapResolution);
284        }
285}
286
287void OnLoad()
288{
289}
290
291void OnSave()
292{
293}
294
295void OnReset()
296{
297}
298
299//--------------------------------------------------------------------------------------
300// Release resources created in the OnResetDevice callback here
301//--------------------------------------------------------------------------------------
302void CALLBACK OnLostDevice( void* pUserContext )
303{
304        gDialogResourceManager.OnLostDevice();
305    gSettingsDlg.OnLostDevice();
306
307    if( gFont )
308                gFont->OnLostDevice();
309   
310        gTextSprite->Release();
311
312        delete pathMapEffect;
313        pathMapEffect = NULL;
314}
315
316
317//--------------------------------------------------------------------------------------
318// Release resources created in the OnCreateDevice callback here
319//--------------------------------------------------------------------------------------
320void CALLBACK OnDestroyDevice( void* pUserContext )
321{
322        gDialogResourceManager.OnDestroyDevice();
323    gSettingsDlg.OnDestroyDevice();
324
325        gFont->Release();
326
327}
328
329
330
331//--------------------------------------------------------------------------------------
332// Initialize everything and go into a render loop
333//--------------------------------------------------------------------------------------
334INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR commandLine, int )
335{
336    // Enable run-time memory check for debug builds.
337#if defined(DEBUG) | defined(_DEBUG)
338    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
339#endif
340
341    // Set the callback functions
342    DXUTSetCallbackDeviceCreated( OnCreateDevice );
343    DXUTSetCallbackDeviceReset( OnResetDevice );
344    DXUTSetCallbackDeviceLost( OnLostDevice );
345    DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
346    DXUTSetCallbackMsgProc( MsgProc );
347    DXUTSetCallbackFrameRender( OnFrameRender );
348    DXUTSetCallbackFrameMove( OnFrameMove );
349        DXUTSetCallbackKeyboard( KeyboardProc );
350   
351    // TODO: Perform any application-level initialization here
352
353        gSettingsDlg.Init( &gDialogResourceManager );
354        gHUD.Init( &gDialogResourceManager );
355        gHUD.SetCallback( OnGUIEvent ); // Event handling
356
357        gParameters.Setup( &gHUD, OnReset, OnSave, OnLoad);     // you can add callbacks to these actions
358       
359        gParameters.Add( bShowHelp, "Show Help", VK_F1 );
360
361        PathMapEffect::addUiParameters(&gParameters);
362       
363        gParameters.UpdateFromHUD( IDC_RESET_BUTTON );  // do a reset
364
365        PathMapEffect::setUiParameterDefaults(&gParameters);
366
367        std::cerr << commandLine;
368
369        if(commandLine[0] == 's' || commandLine[1] == 's')
370                segmentMeshes = true;
371        else
372                segmentMeshes = false;
373
374        if(commandLine[0] == 'p' || commandLine[1] == 'p')
375                computePRM = true;
376        else
377                computePRM = false;
378
379        strcpy(mediaDirectory, "media");
380        strcpy(prmDirectory, "prm");
381        strcpy(meshDirectory, "processedMeshes");
382        strcpy(levelFileName, "ss_ironnail.level");
383        strcpy(materialFileName, "ss_ironnail.materials");
384
385        char* toki;
386        toki = strtok(commandLine, " ");
387        while(toki)
388        {
389                if(toki[0] == '-')
390                {
391                        switch(toki[1])
392                        {
393                        case 'D' :
394                                toki = strtok(NULL, " ");
395                                if(toki == NULL)
396                                        printUsage();
397                                strcpy(mediaDirectory, toki);
398                                break;
399                        case 'P' :
400                                toki = strtok(NULL, " ");
401                                if(toki == NULL)
402                                        printUsage();
403                                strcpy(prmDirectory, toki);
404                                break;
405                        case 'O' :
406                                toki = strtok(NULL, " ");
407                                if(toki == NULL)
408                                        printUsage();
409                                strcpy(meshDirectory, toki);
410                                break;
411                        case 'L' :
412                                toki = strtok(NULL, " ");
413                                if(toki == NULL)
414                                        printUsage();
415                                strcpy(levelFileName, toki);
416                                break;
417                        case 'M' :
418                                toki = strtok(NULL, " ");
419                                if(toki == NULL)
420                                        printUsage();
421                                strcpy(materialFileName, toki);
422                                break;
423                        case 'E' :
424                                toki = strtok(NULL, " ");
425                                if(toki == NULL)
426                                        printUsage();
427                                nEntryPoints = atoi(toki);
428                                break;
429                        case 'C' :
430                                toki = strtok(NULL, " ");
431                                if(toki == NULL)
432                                        printUsage();
433                                nClusters = atoi(toki);
434                                break;
435                        case 'S' :
436                                toki = strtok(NULL, " ");
437                                if(toki == NULL)
438                                        printUsage();
439                                depthMapResolution = atoi(toki);
440                                break;
441                        default:
442                                        printUsage();
443                        }
444                }
445                toki = strtok(NULL, " ");
446        }
447
448    // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
449    DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
450    DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
451    DXUTCreateWindow( L"PathMap" );
452    DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );
453
454    // Start the render loop
455    DXUTMainLoop();
456
457    // TODO: Perform any application-level cleanup here
458
459    return DXUTGetExitCode();
460}
461
462
Note: See TracBrowser for help on using the repository browser.