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 |
|
---|
13 | PathMapEffect* pathMapEffect = NULL;
|
---|
14 |
|
---|
15 | bool gShowUI = true;
|
---|
16 | CDXUTDialogResourceManager gDialogResourceManager; // manager for shared resources of dialogs
|
---|
17 | CD3DSettingsDlg gSettingsDlg; // Device settings dialog
|
---|
18 | CDXUTDialog gHUD; // Dialog for sample specific controls
|
---|
19 |
|
---|
20 | ID3DXFont* gFont = NULL; // Font for drawing text
|
---|
21 | ID3DXSprite* gTextSprite = NULL; // Sprite for batching draw text calls
|
---|
22 |
|
---|
23 | Parameters gParameters;
|
---|
24 |
|
---|
25 | char prmDirectory[256];
|
---|
26 | char meshDirectory[256];
|
---|
27 | char mediaDirectory[256];
|
---|
28 | char levelFileName[256];
|
---|
29 | char materialFileName[256];
|
---|
30 |
|
---|
31 | bool segmentMeshes = false;
|
---|
32 | bool computePRM = false;
|
---|
33 |
|
---|
34 | unsigned int nEntryPoints = 4096;
|
---|
35 | unsigned int nClusters = 32;
|
---|
36 | unsigned int depthMapResolution = 512;
|
---|
37 |
|
---|
38 | void 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 | //--------------------------------------------------------------------------------------
|
---|
46 | bool 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 | //--------------------------------------------------------------------------------------
|
---|
63 | bool 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 | //--------------------------------------------------------------------------------------
|
---|
75 | HRESULT 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 | //--------------------------------------------------------------------------------------
|
---|
92 | HRESULT 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 | //--------------------------------------------------------------------------------------
|
---|
126 | void 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 | //--------------------------------------------------------------------------------------
|
---|
136 | void 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 | //--------------------------------------------------------------------------------------
|
---|
190 | void 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 | //--------------------------------------------------------------------------------------
|
---|
205 | LRESULT 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 |
|
---|
231 | void 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 |
|
---|
266 | void 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 |
|
---|
287 | void OnLoad()
|
---|
288 | {
|
---|
289 | }
|
---|
290 |
|
---|
291 | void OnSave()
|
---|
292 | {
|
---|
293 | }
|
---|
294 |
|
---|
295 | void OnReset()
|
---|
296 | {
|
---|
297 | }
|
---|
298 |
|
---|
299 | //--------------------------------------------------------------------------------------
|
---|
300 | // Release resources created in the OnResetDevice callback here
|
---|
301 | //--------------------------------------------------------------------------------------
|
---|
302 | void 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 | //--------------------------------------------------------------------------------------
|
---|
320 | void 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 | //--------------------------------------------------------------------------------------
|
---|
334 | INT 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, "space.level");
|
---|
383 | strcpy(materialFileName, "space.materials");
|
---|
384 | nClusters = 64;
|
---|
385 | nEntryPoints = 4096;
|
---|
386 | depthMapResolution = 512;
|
---|
387 |
|
---|
388 | char* toki;
|
---|
389 | toki = strtok(commandLine, " ");
|
---|
390 | while(toki)
|
---|
391 | {
|
---|
392 | if(toki[0] == '-')
|
---|
393 | {
|
---|
394 | switch(toki[1])
|
---|
395 | {
|
---|
396 | case 'D' :
|
---|
397 | toki = strtok(NULL, " ");
|
---|
398 | if(toki == NULL)
|
---|
399 | printUsage();
|
---|
400 | strcpy(mediaDirectory, toki);
|
---|
401 | break;
|
---|
402 | case 'P' :
|
---|
403 | toki = strtok(NULL, " ");
|
---|
404 | if(toki == NULL)
|
---|
405 | printUsage();
|
---|
406 | strcpy(prmDirectory, toki);
|
---|
407 | break;
|
---|
408 | case 'O' :
|
---|
409 | toki = strtok(NULL, " ");
|
---|
410 | if(toki == NULL)
|
---|
411 | printUsage();
|
---|
412 | strcpy(meshDirectory, toki);
|
---|
413 | break;
|
---|
414 | case 'L' :
|
---|
415 | toki = strtok(NULL, " ");
|
---|
416 | if(toki == NULL)
|
---|
417 | printUsage();
|
---|
418 | strcpy(levelFileName, toki);
|
---|
419 | break;
|
---|
420 | case 'M' :
|
---|
421 | toki = strtok(NULL, " ");
|
---|
422 | if(toki == NULL)
|
---|
423 | printUsage();
|
---|
424 | strcpy(materialFileName, toki);
|
---|
425 | break;
|
---|
426 | case 'E' :
|
---|
427 | toki = strtok(NULL, " ");
|
---|
428 | if(toki == NULL)
|
---|
429 | printUsage();
|
---|
430 | nEntryPoints = atoi(toki);
|
---|
431 | break;
|
---|
432 | case 'C' :
|
---|
433 | toki = strtok(NULL, " ");
|
---|
434 | if(toki == NULL)
|
---|
435 | printUsage();
|
---|
436 | nClusters = atoi(toki);
|
---|
437 | break;
|
---|
438 | case 'S' :
|
---|
439 | toki = strtok(NULL, " ");
|
---|
440 | if(toki == NULL)
|
---|
441 | printUsage();
|
---|
442 | depthMapResolution = atoi(toki);
|
---|
443 | break;
|
---|
444 | default:
|
---|
445 | printUsage();
|
---|
446 | }
|
---|
447 | }
|
---|
448 | toki = strtok(NULL, " ");
|
---|
449 | }
|
---|
450 |
|
---|
451 | // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
|
---|
452 | DXUTInit( true, true, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
|
---|
453 | DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
|
---|
454 | DXUTCreateWindow( L"PathMap" );
|
---|
455 | DXUTCreateDevice( D3DADAPTER_DEFAULT, true, 640, 480, IsDeviceAcceptable, ModifyDeviceSettings );
|
---|
456 |
|
---|
457 | // Start the render loop
|
---|
458 | DXUTMainLoop();
|
---|
459 |
|
---|
460 | // TODO: Perform any application-level cleanup here
|
---|
461 |
|
---|
462 | return DXUTGetExitCode();
|
---|
463 | }
|
---|
464 |
|
---|
465 |
|
---|