1 | //#define DEBUG_VS // Uncomment this line to debug vertex shaders
|
---|
2 | //#define DEBUG_PS // Uncomment this line to debug pixel shaders
|
---|
3 |
|
---|
4 | #define SHADOWMAPMAX_SIZE 1024 // shadow map size
|
---|
5 | #define FOCUSINGMAPMAX_SIZE 512 // focusing map size
|
---|
6 | #define LIGHTSOURCEMAX_SAMPLES 625 // light source max samples
|
---|
7 | #define WINDOW_HEIGHT 512 // main window height
|
---|
8 | #define WINDOW_WIDTH 512 // main window width
|
---|
9 | #define NUM_OBJ (sizeof(g_aszMeshFile)/sizeof(g_aszMeshFile[0])) // number of objects
|
---|
10 |
|
---|
11 | #define PI 3.14159265f // PI
|
---|
12 |
|
---|
13 | // Sources of objects
|
---|
14 | LPCWSTR g_aszMeshFile[] =
|
---|
15 | {
|
---|
16 | L"media\\objects\\room.x",
|
---|
17 | L"media\\objects\\airplane.x",
|
---|
18 | L"media\\objects\\car.x",
|
---|
19 | L"media\\objects\\sphere.x",
|
---|
20 | L"media\\objects\\arrow.x",
|
---|
21 | L"media\\objects\\arrow.x",
|
---|
22 | L"media\\objects\\arrow.x",
|
---|
23 | L"media\\objects\\arrow.x",
|
---|
24 | L"media\\objects\\arrow.x",
|
---|
25 | L"media\\objects\\arrow.x",
|
---|
26 | L"media\\objects\\arrow.x",
|
---|
27 | L"media\\objects\\arrow.x",
|
---|
28 | L"media\\objects\\ring.x",
|
---|
29 | L"media\\objects\\ring.x",
|
---|
30 | };
|
---|
31 |
|
---|
32 |
|
---|
33 | // identifiers of sliders
|
---|
34 | #define HUNDRED 100
|
---|
35 |
|
---|
36 | #define IDC_FOV_SLIDER HUNDRED + 0
|
---|
37 | #define IDC_REAL_FOV_SLIDER HUNDRED + 1
|
---|
38 | #define IDC_LIGHTSIZE_SLIDER HUNDRED + 2
|
---|
39 | #define IDC_INTENSITY_SLIDER HUNDRED + 3
|
---|
40 | #define IDC_SHADOWBIAS_SLIDER HUNDRED + 4
|
---|
41 | #define IDC_BIASSLOPE_SLIDER HUNDRED + 5
|
---|
42 | #define IDC_ITERATION_SLIDER HUNDRED + 6
|
---|
43 | #define IDC_SHADOWMAP_SLIDER HUNDRED + 7
|
---|
44 | #define IDC_FOCUSINGMAP_SLIDER HUNDRED + 8
|
---|
45 | #define IDC_LIGHSOURCE_SAMPLES_SLIDER HUNDRED + 9
|
---|
46 |
|
---|
47 | #define LAST_SLIDER HUNDRED + 10
|
---|
48 |
|
---|
49 | // value ranges of sliders
|
---|
50 | int g_iSliderMinValues[] = { 0, 0, 1, 0, 0, 0, 0, 0, 0, 50 };
|
---|
51 | int g_iSliderMaxValues[] = { 180, 180, 1000, 1000, 50, 200, 32, (int)( log( (float)SHADOWMAPMAX_SIZE ) / log( 2.0f ) ), (int)( log( (float)FOCUSINGMAPMAX_SIZE ) / log( 2.0f ) ), 1000 };
|
---|
52 |
|
---|
53 | // Default position of objects
|
---|
54 | D3DXMATRIXA16 g_amInitObjWorld[NUM_OBJ] =
|
---|
55 | {
|
---|
56 | D3DXMATRIXA16( 3.5f, 0.0f, 0.0f, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 3.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ),
|
---|
57 | D3DXMATRIXA16( 0.43301f, 0.25f, 0.0f, 0.0f, -0.25f, 0.43301f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 5.0f, 1.33975f, 0.0f, 1.0f ),
|
---|
58 | D3DXMATRIXA16( 0.8f, 0.0f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, 0.0f, 0.0f, 0.0f, 0.8f, 0.0f, -14.5f, -7.1f, 0.0f, 1.0f ),
|
---|
59 | D3DXMATRIXA16( 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 2.0f, 0.0f, 0.0f, -7.0f, 0.0f, 1.0f ),
|
---|
60 | D3DXMATRIXA16( 5.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0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RIXA16( 0.9f, 0.0f, 0.0f, 0.0f, 0.0f, 0.9f, 0.0f, 0.0f, 0.0f, 0.0f, 0.9f, 0.0f, 14.5f, -9.0f, 0.0f, 1.0f ),
|
---|
70 | };
|
---|
71 |
|
---|
72 | // Vertex definition
|
---|
73 | D3DVERTEXELEMENT9 g_aVertDecl[] =
|
---|
74 | {
|
---|
75 | { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
|
---|
76 | { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
|
---|
77 | { 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
|
---|
78 | D3DDECL_END()
|
---|
79 | };
|
---|
80 |
|
---|
81 |
|
---|
82 | //-----------------------------------------------------------------------------
|
---|
83 | // Name: class CObj
|
---|
84 | // Desc: Encapsulates a mesh object in the scene by grouping its world matrix
|
---|
85 | // with the mesh.
|
---|
86 | //-----------------------------------------------------------------------------
|
---|
87 | struct CObj
|
---|
88 | {
|
---|
89 | CDXUTMesh m_Mesh;
|
---|
90 | D3DXMATRIXA16 m_mWorld;
|
---|
91 | };
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 |
|
---|
96 | //-----------------------------------------------------------------------------
|
---|
97 | // Name: class CViewCamera
|
---|
98 | // Desc: A camera class derived from CFirstPersonCamera. The arrow keys and
|
---|
99 | // numpad keys are disabled for this type of camera.
|
---|
100 | //-----------------------------------------------------------------------------
|
---|
101 | class CViewCamera : public CFirstPersonCamera
|
---|
102 | {
|
---|
103 | protected:
|
---|
104 | virtual D3DUtil_CameraKeys MapKey( UINT nKey )
|
---|
105 | {
|
---|
106 | // Provide custom mapping here.
|
---|
107 | // Same as default mapping but disable arrow keys.
|
---|
108 | switch( nKey )
|
---|
109 | {
|
---|
110 | case 'A': return CAM_STRAFE_LEFT;
|
---|
111 | case 'D': return CAM_STRAFE_RIGHT;
|
---|
112 | case 'W': return CAM_MOVE_FORWARD;
|
---|
113 | case 'S': return CAM_MOVE_BACKWARD;
|
---|
114 | case 'Q': return CAM_MOVE_DOWN;
|
---|
115 | case 'E': return CAM_MOVE_UP;
|
---|
116 |
|
---|
117 | case VK_HOME: return CAM_RESET;
|
---|
118 | }
|
---|
119 |
|
---|
120 | return CAM_UNKNOWN;
|
---|
121 | }
|
---|
122 | };
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 |
|
---|
127 | //-----------------------------------------------------------------------------
|
---|
128 | // Name: class CLightCamera
|
---|
129 | // Desc: A camera class derived from CFirstPersonCamera. The letter keys
|
---|
130 | // are disabled for this type of camera. This class is intended for use
|
---|
131 | // by the spot light.
|
---|
132 | //-----------------------------------------------------------------------------
|
---|
133 | class CLightCamera : public CFirstPersonCamera
|
---|
134 | {
|
---|
135 | protected:
|
---|
136 | virtual D3DUtil_CameraKeys MapKey( UINT nKey )
|
---|
137 | {
|
---|
138 | // Provide custom mapping here.
|
---|
139 | // Same as default mapping but disable arrow keys.
|
---|
140 | switch( nKey )
|
---|
141 | {
|
---|
142 | case VK_LEFT: return CAM_STRAFE_LEFT;
|
---|
143 | case VK_RIGHT: return CAM_STRAFE_RIGHT;
|
---|
144 | case VK_UP: return CAM_MOVE_FORWARD;
|
---|
145 | case VK_DOWN: return CAM_MOVE_BACKWARD;
|
---|
146 | case VK_PRIOR: return CAM_MOVE_UP; // pgup
|
---|
147 | case VK_NEXT: return CAM_MOVE_DOWN; // pgdn
|
---|
148 |
|
---|
149 | case VK_NUMPAD4: return CAM_STRAFE_LEFT;
|
---|
150 | case VK_NUMPAD6: return CAM_STRAFE_RIGHT;
|
---|
151 | case VK_NUMPAD8: return CAM_MOVE_FORWARD;
|
---|
152 | case VK_NUMPAD2: return CAM_MOVE_BACKWARD;
|
---|
153 | case VK_NUMPAD9: return CAM_MOVE_UP;
|
---|
154 | case VK_NUMPAD3: return CAM_MOVE_DOWN;
|
---|
155 |
|
---|
156 | case VK_HOME: return CAM_RESET;
|
---|
157 | }
|
---|
158 |
|
---|
159 | return CAM_UNKNOWN;
|
---|
160 | }
|
---|
161 | };
|
---|
162 |
|
---|
163 |
|
---|
164 | //--------------------------------------------------------------------------------------
|
---|
165 | // Global variables
|
---|
166 | //--------------------------------------------------------------------------------------
|
---|
167 | IDirect3DDevice9* g_pd3dDevice = NULL; // Device
|
---|
168 | ID3DXFont* g_pFont = NULL; // Font for drawing text
|
---|
169 | ID3DXFont* g_pFontSmall = NULL; // Font for drawing text
|
---|
170 | ID3DXSprite* g_pTextSprite = NULL; // Sprite for batching draw text calls
|
---|
171 | ID3DXEffect* g_pEffect = NULL; // D3DX effect interface
|
---|
172 | CDXUTDialogResourceManager g_DialogResourceManager; // manager for shared resources of dialogs
|
---|
173 | CD3DSettingsDlg g_SettingsDlg; // Device settings dialog
|
---|
174 | CDXUTDialog g_HUD; // dialog for standard controls
|
---|
175 | CDXUTComboBox* g_pTechniquesListComboBox; // List for shadow techniques
|
---|
176 | CFirstPersonCamera g_CamCamera; // View camera
|
---|
177 | CFirstPersonCamera g_LightCamera; // Camera obj to help adjust light
|
---|
178 | CObj g_Obj[NUM_OBJ]; // Scene object meshes
|
---|
179 | LPDIRECT3DVERTEXDECLARATION9 g_pVertDecl = NULL; // Vertex decl for the sample
|
---|
180 | CDXUTMesh g_LightMesh; // Mesh of light
|
---|
181 |
|
---|
182 | LPDIRECT3DTEXTURE9 g_pDefaultTexture = NULL; // Default texture for objects
|
---|
183 | LPDIRECT3DTEXTURE9 g_pShadowMapColorTexture = NULL; // Texture to which the shadow map is rendered
|
---|
184 | LPDIRECT3DTEXTURE9 g_pShadowWeightTexture = NULL;
|
---|
185 | LPDIRECT3DTEXTURE9 g_pShadowMapZTexture = NULL; // Texture to which the shadow map (Z) is rendered
|
---|
186 | LPDIRECT3DTEXTURE9 g_pShadowWeightZTexture = NULL;
|
---|
187 | LPDIRECT3DTEXTURE9 g_pFocusingMapColorTexture = NULL; // Texture to which the focusing map is rendered
|
---|
188 | LPDIRECT3DTEXTURE9 g_pMemoryMappedTexture; // Memory mapped texture for focusing
|
---|
189 |
|
---|
190 | LPDIRECT3DSURFACE9 g_pShadowMapColorSurface = NULL; // Surface for shadow map
|
---|
191 | LPDIRECT3DSURFACE9 g_pShadowWeightSurface = NULL;
|
---|
192 | LPDIRECT3DSURFACE9 g_pShadowMapZSurface = NULL; // Depth-stencil buffer for rendering to shadow map
|
---|
193 | LPDIRECT3DSURFACE9 g_pShadowWeightZSurface = NULL;
|
---|
194 | LPDIRECT3DSURFACE9 g_pFocusingMapColorSurface = NULL; // Surface for focusing map
|
---|
195 | LPDIRECT3DSURFACE9 g_pMemoryMappedTextureSurface; // Surface for memory mapped map
|
---|
196 |
|
---|
197 | D3DXMATRIXA16 g_mFocusingMatrix; // Matrix to contain the focusing parameters
|
---|
198 | D3DXMATRIXA16 g_mLightProj; // Lights projection matrix
|
---|
199 | D3DXMATRIXA16 g_mLightView; // Lights projection matrix
|
---|
200 |
|
---|
201 | float g_fRoomRadius; // Radius of room
|
---|
202 | float g_fLightSize = 2.13f; // Represents the size of the light source
|
---|
203 | float g_fIntensity = 1.0f; // It is a helper property
|
---|
204 | float g_fShadowBias = 0.15f; // Represents the shadow bias
|
---|
205 | float g_fBiasSlope = 0.0f; // Represents the shadow slope
|
---|
206 |
|
---|
207 | float g_fLightFrontPlane = 0.1f; // Light sources front plane
|
---|
208 | float g_fLightBackPlane = 100.0f; // Light sources back plane
|
---|
209 | float g_fLightSourceSamples = 8;
|
---|
210 | float g_fRealSamples = 0;
|
---|
211 |
|
---|
212 | int g_iSelectedMenuItem = 0; // ID of highlighted menu item
|
---|
213 | int g_iKernelSize = 4; // This property stores the value of kernel size
|
---|
214 | int g_iShadowMethod = 0; // This property represents the different shadow calculation methods
|
---|
215 | int g_iNumberOfMethods = 0; // This is the number of shadow c. method
|
---|
216 | int g_iLightFov = 90; // Real FOV of the light
|
---|
217 | int g_iFov = 90; // FOV of the light in the shader
|
---|
218 | int g_iShadowMapSize = 512; // ShadowMapSize
|
---|
219 | int g_iFocusingMapSize = 16; // FocusingMapSize
|
---|
220 | int g_iPictureNumber = 0;
|
---|
221 |
|
---|
222 | bool g_bRightMouseDown = false; // Indicates whether right mouse button is held
|
---|
223 | bool g_bCameraPerspective = true; // Indicates whether we should render view from the camera's or the light's perspective
|
---|
224 | bool g_bShowText = true; // If true, it renders the UI control text
|
---|
225 | bool g_bUseTexture = true; // Use texture
|
---|
226 | bool g_bPause = false; // Pause the movement
|
---|
227 | bool g_bUseFocusing = false; // Use focusing technique
|
---|
228 | bool g_bRenderShadowMap = false; // Render shadow map
|
---|
229 | bool g_bRenderFocusingMap = false; // Render focusing map
|
---|
230 | bool g_bSaveFrame = false; // Save image into PNG file
|
---|
231 |
|
---|
232 | bool g_bRenderBaseTexture = false;
|
---|
233 | bool g_bRenderGUI = false;
|
---|
234 |
|
---|
235 | IDirect3DVertexDeclaration9* g_pFullScreenQuadVertexDeclaration = NULL; // Vertex declaration for the full screen quad
|
---|
236 | LPDIRECT3DVERTEXBUFFER9 g_pFullScreenQuadVertexBuffer = NULL; // Contain vertex data for the full screen quad
|
---|
237 |
|
---|
238 | // Vertex structure for the full screen quad
|
---|
239 | D3DVERTEXELEMENT9 g_aFullScreneQuadVertex[] =
|
---|
240 | {
|
---|
241 | { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
|
---|
242 | D3DDECL_END()
|
---|
243 | };
|
---|
244 |
|
---|
245 | struct FullScreenQuadVertexStruct
|
---|
246 | {
|
---|
247 | FLOAT x, y, z, rhw; // The transformed position for the vertex.
|
---|
248 | };
|
---|
249 |
|
---|
250 | #define D3DFVF_FULLSCREENQUADVERTEX (D3DFVF_XYZRHW)
|
---|
251 |
|
---|
252 | //Structure for the full screen vertex quad
|
---|
253 | FullScreenQuadVertexStruct fullscreen[] =
|
---|
254 | { // x, y, z, rhw
|
---|
255 | { -1.0f, -1.0f, 0.5f, 1.0f, },
|
---|
256 | { 1.0f, -1.0f, 0.5f, 1.0f, },
|
---|
257 | { 1.0f, 1.0f, 0.5f, 1.0f, },
|
---|
258 | { -1.0f, -1.0f, 0.5f, 1.0f, },
|
---|
259 | { 1.0f, 1.0f, 0.5f, 1.0f, },
|
---|
260 | { -1.0f, 1.0f, 0.5f, 1.0f, },
|
---|
261 | };
|
---|
262 |
|
---|
263 |
|
---|
264 | //--------------------------------------------------------------------------------------
|
---|
265 | // UI control IDs
|
---|
266 | //--------------------------------------------------------------------------------------
|
---|
267 | #define IDC_TOGGLEFULLSCREEN 1
|
---|
268 | #define IDC_TOGGLEREF 2
|
---|
269 | #define IDC_CHANGEDEVICE 3
|
---|
270 | #define IDC_CHECKBOX 4
|
---|
271 | #define IDC_LIGHTPERSPECTIVE 5
|
---|
272 | #define IDC_PAUSE_CHECKBOX 6
|
---|
273 | #define IDC_USE_TEXTURE_CHECKBOX 7
|
---|
274 | #define IDC_USE_FOCUSING_CHECKBOX 8
|
---|
275 |
|
---|
276 | #define IDC_METHOD_COMBO_BOX 50
|
---|
277 |
|
---|
278 |
|
---|
279 | //--------------------------------------------------------------------------------------
|
---|
280 | // Forward declarations
|
---|
281 | //--------------------------------------------------------------------------------------
|
---|
282 | void InitializeDialogs();
|
---|
283 | bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext );
|
---|
284 | bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext );
|
---|
285 | HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
|
---|
286 | HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
|
---|
287 | void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
|
---|
288 | void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext );
|
---|
289 | void RenderText();
|
---|
290 | LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext );
|
---|
291 | void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext );
|
---|
292 | void CALLBACK MouseProc( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos, void* pUserContext );
|
---|
293 | void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext );
|
---|
294 | void CALLBACK OnLostDevice( void* pUserContext );
|
---|
295 | void CALLBACK OnDestroyDevice( void* pUserContext );
|
---|
296 | void RenderScene( IDirect3DDevice9* pd3dDevice, bool g_bRenderShadowMap, float fElapsedTime, const D3DXMATRIX *pmView, const D3DXMATRIX *pmProj );
|
---|
297 | void CreateTexture();
|
---|
298 | void ComputeFocusingMatrix( IDirect3DDevice9* g_pd3dDevice ); |
---|