//#define DEBUG_VS // Uncomment this line to debug vertex shaders //#define DEBUG_PS // Uncomment this line to debug pixel shaders #define SHADOWMAPMAX_SIZE 1024 // shadow map size #define FOCUSINGMAPMAX_SIZE 512 // focusing map size #define LIGHTSOURCEMAX_SAMPLES 625 // light source max samples #define WINDOW_HEIGHT 512 // main window height #define WINDOW_WIDTH 512 // main window width #define NUM_OBJ (sizeof(g_aszMeshFile)/sizeof(g_aszMeshFile[0])) // number of objects #define PI 3.14159265f // PI // Sources of objects LPCWSTR g_aszMeshFile[] = { L"media\\objects\\room.x", L"media\\objects\\airplane.x", L"media\\objects\\car.x", L"media\\objects\\sphere.x", L"media\\objects\\arrow.x", L"media\\objects\\arrow.x", L"media\\objects\\arrow.x", L"media\\objects\\arrow.x", L"media\\objects\\arrow.x", L"media\\objects\\arrow.x", L"media\\objects\\arrow.x", L"media\\objects\\arrow.x", L"media\\objects\\ring.x", L"media\\objects\\ring.x", }; // identifiers of sliders #define HUNDRED 100 #define IDC_FOV_SLIDER HUNDRED + 0 #define IDC_REAL_FOV_SLIDER HUNDRED + 1 #define IDC_LIGHTSIZE_SLIDER HUNDRED + 2 #define IDC_INTENSITY_SLIDER HUNDRED + 3 #define IDC_SHADOWBIAS_SLIDER HUNDRED + 4 #define IDC_BIASSLOPE_SLIDER HUNDRED + 5 #define IDC_ITERATION_SLIDER HUNDRED + 6 #define IDC_SHADOWMAP_SLIDER HUNDRED + 7 #define IDC_FOCUSINGMAP_SLIDER HUNDRED + 8 #define IDC_LIGHSOURCE_SAMPLES_SLIDER HUNDRED + 9 #define LAST_SLIDER HUNDRED + 10 // value ranges of sliders int g_iSliderMinValues[] = { 0, 0, 1, 0, 0, 0, 0, 0, 0, 50 }; 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 }; // Default position of objects D3DXMATRIXA16 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 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 5.5f, 0.0f, 0.0f, -9.0f, 0.0f, 0.0f, 14.0f, 0.2f, -14.0f, 1.0f ), D3DXMATRIXA16( 5.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 5.5f, 0.0f, 0.0f, -9.0f, 0.0f, 0.0f, -14.0f, 0.2f, 14.0f, 1.0f ), D3DXMATRIXA16( 5.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 5.5f, 0.0f, 0.0f, -9.0f, 0.0f, 0.0f, -14.0f, 0.2f, -14.0f, 1.0f ), D3DXMATRIXA16( 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 ), D3DXMATRIXA16( 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 ), }; // Vertex definition D3DVERTEXELEMENT9 g_aVertDecl[] = { { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 }, { 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() }; //----------------------------------------------------------------------------- // Name: class CObj // Desc: Encapsulates a mesh object in the scene by grouping its world matrix // with the mesh. //----------------------------------------------------------------------------- struct CObj { CDXUTMesh m_Mesh; D3DXMATRIXA16 m_mWorld; }; //----------------------------------------------------------------------------- // Name: class CViewCamera // Desc: A camera class derived from CFirstPersonCamera. The arrow keys and // numpad keys are disabled for this type of camera. //----------------------------------------------------------------------------- class CViewCamera : public CFirstPersonCamera { protected: virtual D3DUtil_CameraKeys MapKey( UINT nKey ) { // Provide custom mapping here. // Same as default mapping but disable arrow keys. switch( nKey ) { case 'A': return CAM_STRAFE_LEFT; case 'D': return CAM_STRAFE_RIGHT; case 'W': return CAM_MOVE_FORWARD; case 'S': return CAM_MOVE_BACKWARD; case 'Q': return CAM_MOVE_DOWN; case 'E': return CAM_MOVE_UP; case VK_HOME: return CAM_RESET; } return CAM_UNKNOWN; } }; //----------------------------------------------------------------------------- // Name: class CLightCamera // Desc: A camera class derived from CFirstPersonCamera. The letter keys // are disabled for this type of camera. This class is intended for use // by the spot light. //----------------------------------------------------------------------------- class CLightCamera : public CFirstPersonCamera { protected: virtual D3DUtil_CameraKeys MapKey( UINT nKey ) { // Provide custom mapping here. // Same as default mapping but disable arrow keys. switch( nKey ) { case VK_LEFT: return CAM_STRAFE_LEFT; case VK_RIGHT: return CAM_STRAFE_RIGHT; case VK_UP: return CAM_MOVE_FORWARD; case VK_DOWN: return CAM_MOVE_BACKWARD; case VK_PRIOR: return CAM_MOVE_UP; // pgup case VK_NEXT: return CAM_MOVE_DOWN; // pgdn case VK_NUMPAD4: return CAM_STRAFE_LEFT; case VK_NUMPAD6: return CAM_STRAFE_RIGHT; case VK_NUMPAD8: return CAM_MOVE_FORWARD; case VK_NUMPAD2: return CAM_MOVE_BACKWARD; case VK_NUMPAD9: return CAM_MOVE_UP; case VK_NUMPAD3: return CAM_MOVE_DOWN; case VK_HOME: return CAM_RESET; } return CAM_UNKNOWN; } }; //-------------------------------------------------------------------------------------- // Global variables //-------------------------------------------------------------------------------------- IDirect3DDevice9* g_pd3dDevice = NULL; // Device ID3DXFont* g_pFont = NULL; // Font for drawing text ID3DXFont* g_pFontSmall = NULL; // Font for drawing text ID3DXSprite* g_pTextSprite = NULL; // Sprite for batching draw text calls ID3DXEffect* g_pEffect = NULL; // D3DX effect interface CDXUTDialogResourceManager g_DialogResourceManager; // manager for shared resources of dialogs CD3DSettingsDlg g_SettingsDlg; // Device settings dialog CDXUTDialog g_HUD; // dialog for standard controls CDXUTComboBox* g_pTechniquesListComboBox; // List for shadow techniques CFirstPersonCamera g_CamCamera; // View camera CFirstPersonCamera g_LightCamera; // Camera obj to help adjust light CObj g_Obj[NUM_OBJ]; // Scene object meshes LPDIRECT3DVERTEXDECLARATION9 g_pVertDecl = NULL; // Vertex decl for the sample CDXUTMesh g_LightMesh; // Mesh of light LPDIRECT3DTEXTURE9 g_pDefaultTexture = NULL; // Default texture for objects LPDIRECT3DTEXTURE9 g_pShadowMapColorTexture = NULL; // Texture to which the shadow map is rendered LPDIRECT3DTEXTURE9 g_pShadowWeightTexture = NULL; LPDIRECT3DTEXTURE9 g_pShadowMapZTexture = NULL; // Texture to which the shadow map (Z) is rendered LPDIRECT3DTEXTURE9 g_pShadowWeightZTexture = NULL; LPDIRECT3DTEXTURE9 g_pFocusingMapColorTexture = NULL; // Texture to which the focusing map is rendered LPDIRECT3DTEXTURE9 g_pMemoryMappedTexture; // Memory mapped texture for focusing LPDIRECT3DSURFACE9 g_pShadowMapColorSurface = NULL; // Surface for shadow map LPDIRECT3DSURFACE9 g_pShadowWeightSurface = NULL; LPDIRECT3DSURFACE9 g_pShadowMapZSurface = NULL; // Depth-stencil buffer for rendering to shadow map LPDIRECT3DSURFACE9 g_pShadowWeightZSurface = NULL; LPDIRECT3DSURFACE9 g_pFocusingMapColorSurface = NULL; // Surface for focusing map LPDIRECT3DSURFACE9 g_pMemoryMappedTextureSurface; // Surface for memory mapped map D3DXMATRIXA16 g_mFocusingMatrix; // Matrix to contain the focusing parameters D3DXMATRIXA16 g_mLightProj; // Lights projection matrix D3DXMATRIXA16 g_mLightView; // Lights projection matrix float g_fRoomRadius; // Radius of room float g_fLightSize = 2.13f; // Represents the size of the light source float g_fIntensity = 1.0f; // It is a helper property float g_fShadowBias = 0.15f; // Represents the shadow bias float g_fBiasSlope = 0.0f; // Represents the shadow slope float g_fLightFrontPlane = 0.1f; // Light sources front plane float g_fLightBackPlane = 100.0f; // Light sources back plane float g_fLightSourceSamples = 8; float g_fRealSamples = 0; int g_iSelectedMenuItem = 0; // ID of highlighted menu item int g_iKernelSize = 4; // This property stores the value of kernel size int g_iShadowMethod = 0; // This property represents the different shadow calculation methods int g_iNumberOfMethods = 0; // This is the number of shadow c. method int g_iLightFov = 90; // Real FOV of the light int g_iFov = 90; // FOV of the light in the shader int g_iShadowMapSize = 512; // ShadowMapSize int g_iFocusingMapSize = 16; // FocusingMapSize int g_iPictureNumber = 0; bool g_bRightMouseDown = false; // Indicates whether right mouse button is held bool g_bCameraPerspective = true; // Indicates whether we should render view from the camera's or the light's perspective bool g_bShowText = true; // If true, it renders the UI control text bool g_bUseTexture = true; // Use texture bool g_bPause = false; // Pause the movement bool g_bUseFocusing = false; // Use focusing technique bool g_bRenderShadowMap = false; // Render shadow map bool g_bRenderFocusingMap = false; // Render focusing map bool g_bSaveFrame = false; // Save image into PNG file bool g_bRenderBaseTexture = false; bool g_bRenderGUI = false; IDirect3DVertexDeclaration9* g_pFullScreenQuadVertexDeclaration = NULL; // Vertex declaration for the full screen quad LPDIRECT3DVERTEXBUFFER9 g_pFullScreenQuadVertexBuffer = NULL; // Contain vertex data for the full screen quad // Vertex structure for the full screen quad D3DVERTEXELEMENT9 g_aFullScreneQuadVertex[] = { { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() }; struct FullScreenQuadVertexStruct { FLOAT x, y, z, rhw; // The transformed position for the vertex. }; #define D3DFVF_FULLSCREENQUADVERTEX (D3DFVF_XYZRHW) //Structure for the full screen vertex quad FullScreenQuadVertexStruct fullscreen[] = { // x, y, z, rhw { -1.0f, -1.0f, 0.5f, 1.0f, }, { 1.0f, -1.0f, 0.5f, 1.0f, }, { 1.0f, 1.0f, 0.5f, 1.0f, }, { -1.0f, -1.0f, 0.5f, 1.0f, }, { 1.0f, 1.0f, 0.5f, 1.0f, }, { -1.0f, 1.0f, 0.5f, 1.0f, }, }; //-------------------------------------------------------------------------------------- // UI control IDs //-------------------------------------------------------------------------------------- #define IDC_TOGGLEFULLSCREEN 1 #define IDC_TOGGLEREF 2 #define IDC_CHANGEDEVICE 3 #define IDC_CHECKBOX 4 #define IDC_LIGHTPERSPECTIVE 5 #define IDC_PAUSE_CHECKBOX 6 #define IDC_USE_TEXTURE_CHECKBOX 7 #define IDC_USE_FOCUSING_CHECKBOX 8 #define IDC_METHOD_COMBO_BOX 50 //-------------------------------------------------------------------------------------- // Forward declarations //-------------------------------------------------------------------------------------- void InitializeDialogs(); bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext ); bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext ); HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ); HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ); void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ); void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ); void RenderText(); LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext ); void CALLBACK KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext ); void CALLBACK MouseProc( bool bLeftButtonDown, bool bRightButtonDown, bool bMiddleButtonDown, bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta, int xPos, int yPos, void* pUserContext ); void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext ); void CALLBACK OnLostDevice( void* pUserContext ); void CALLBACK OnDestroyDevice( void* pUserContext ); void RenderScene( IDirect3DDevice9* pd3dDevice, bool g_bRenderShadowMap, float fElapsedTime, const D3DXMATRIX *pmView, const D3DXMATRIX *pmProj ); void CreateTexture(); void ComputeFocusingMatrix( IDirect3DDevice9* g_pd3dDevice );