//-------------------------------------------------------------------------------------- // Initialize the app //-------------------------------------------------------------------------------------- void InitApp() { //g_LightControl.SetLightDirection( D3DXVECTOR3(-0.29f, 0.557f, 0.778f) ); g_LightControl.SetLightDirection( D3DXVECTOR3( -0.789f, 0.527f, 0.316f ) ); g_LightControl.SetButtonMask( MOUSE_MIDDLE_BUTTON ); // Initialize dialogs g_SettingsDlg.Init( &g_DialogResourceManager ); g_HUD.Init( &g_DialogResourceManager ); g_SampleUI.Init( &g_DialogResourceManager ); g_HUD.SetCallback( OnGUIEvent ); int iX = 15; int iY = 10; g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", iX, iY, 125, 22 ); g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", iX, iY += 24, 125, 22 ); g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", iX, iY += 24, 125, 22, VK_F2 ); g_SampleUI.SetCallback( OnGUIEvent ); iX = 15; iY = 10; // Title font for static g_SampleUI.SetFont( 1, L"Arial", 14, FW_NORMAL ); CDXUTElement* pElement = g_SampleUI.GetDefaultElement( DXUT_CONTROL_STATIC, 0 ); if( pElement ) { pElement->iFont = 1; pElement->dwTextFormat = DT_RIGHT | DT_VCENTER; } // Technique g_SampleUI.AddStatic( -1, L"Technique", iX, iY += 24, 115, 22 ); g_SampleUI.AddComboBox( IDC_TECHNIQUE, iX + 125, iY, 150, 22 ); g_SampleUI.GetComboBox( IDC_TECHNIQUE )->SetScrollBarWidth( 0 ); g_SampleUI.GetComboBox( IDC_TECHNIQUE )->AddItem( L"Local-deformable PRT", ( void* )"LDPRT" ); g_SampleUI.GetComboBox( IDC_TECHNIQUE )->AddItem( L"N dot L lighting", ( void* )"NdotL" ); // Animation speed iY += 10; g_SampleUI.AddStatic( -1, L"Animation Speed", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_ANIMATION_SPEED, iX + 125, iY, 125, 22, 0, 3000, 700 ); // Light intensity iY += 10; g_SampleUI.AddStatic( -1, L"Light Intensity", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_LIGHT_SLIDER, iX + 125, iY, 125, 22, 0, 1000, 500 ); g_SampleUI.AddStatic( IDC_ENV_LABEL, L"Env Intensity", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_ENV_SLIDER, iX + 125, iY, 125, 22, 0, 3000, 600 ); // Color transmission iY += 10; g_SampleUI.AddStatic( IDC_RED_TRANSMIT_LABEL, L"Transmit Red", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_RED_TRANSMIT_SLIDER, iX + 125, iY, 125, 22, 0, 3000, 1200 ); g_SampleUI.AddStatic( IDC_GREEN_TRANSMIT_LABEL, L"Transmit Green", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_GREEN_TRANSMIT_SLIDER, iX + 125, iY, 125, 22, 0, 3000, 800 ); g_SampleUI.AddStatic( IDC_BLUE_TRANSMIT_LABEL, L"Transmit Blue", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_BLUE_TRANSMIT_SLIDER, iX + 125, iY, 125, 22, 0, 3000, 350 ); }
//-------------------------------------------------------------------------------------- // Handle messages to the application //-------------------------------------------------------------------------------------- LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext ) { // Pass messages to dialog resource manager calls so GUI state is updated correctly *pbNoFurtherProcessing = g_DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam ); if( *pbNoFurtherProcessing ) return 0; // Pass messages to settings dialog if its active if( g_D3DSettingsDlg.IsActive() ) { g_D3DSettingsDlg.MsgProc( hWnd, uMsg, wParam, lParam ); return 0; } // Give the dialogs a chance to handle the message first *pbNoFurtherProcessing = g_HUD.MsgProc( hWnd, uMsg, wParam, lParam ); if( *pbNoFurtherProcessing ) return 0; *pbNoFurtherProcessing = g_SampleUI.MsgProc( hWnd, uMsg, wParam, lParam ); if( *pbNoFurtherProcessing ) return 0; g_LightControl.HandleMessages( hWnd, uMsg, wParam, lParam ); // Pass all remaining windows messages to camera so it can respond to user input g_Camera.HandleMessages( hWnd, uMsg, wParam, lParam ); return 0; }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT) // or that are tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9ResetDevice() ); V_RETURN( g_D3DSettingsDlg.OnD3D9ResetDevice() ); if( g_pFont9 ) V_RETURN( g_pFont9->OnResetDevice() ); if( g_pEffect9 ) V_RETURN( g_pEffect9->OnResetDevice() ); // Create a sprite to help batch calls when drawing many lines of text V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pSprite9 ) ); g_pTxtHelper = new CDXUTTextHelper( g_pFont9, g_pSprite9, 15 ); g_LightControl.OnD3D9ResetDevice( pBackBufferSurfaceDesc ); // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 2.0f, 4000.0f ); g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); g_Camera.SetButtonMasks( MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_MIDDLE_BUTTON ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 300 ); g_SampleUI.SetSize( 170, 300 ); return S_OK; }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // reset, which will happen after a lost device scenario. This is the best location to // create D3DPOOL_DEFAULT resources since these resources need to be reloaded whenever // the device is lost. Resources created here should be released in the OnLostDevice // callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9ResetDevice() ); V_RETURN( g_SettingsDlg.OnD3D9ResetDevice() ); if( g_pFont ) V_RETURN( g_pFont->OnResetDevice() ); if( g_pEffect ) V_RETURN( g_pEffect->OnResetDevice() ); g_LightControl.OnD3D9ResetDevice( pBackBufferSurfaceDesc ); g_Skybox.OnResetDevice( pBackBufferSurfaceDesc ); // Create a sprite to help batch calls when drawing many lines of text V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) ); // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.001f, 1000.0f ); g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); g_Camera.SetButtonMasks( MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_RIGHT_BUTTON ); g_Camera.SetAttachCameraToModel( true ); g_Camera.SetRadius( 5.0f, 0.1f, 20.0f ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 300, pBackBufferSurfaceDesc->Height - 245 ); g_SampleUI.SetSize( 300, 300 ); return S_OK; }
//-------------------------------------------------------------------------------------- // This callback function will be called at the end of every frame to perform all the // rendering calls for the scene, and it will also be called if the window needs to be // repainted. After this function has returned, DXUT will call // IDirect3DDevice9::Present to display the contents of the next buffer in the swap chain //-------------------------------------------------------------------------------------- void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { // If the settings dialog is being shown, then // render it instead of rendering the app's scene if( g_SettingsDlg.IsActive() ) { g_SettingsDlg.OnRender( fElapsedTime ); return; } HRESULT hr; // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 50, 50, 50 ), 1.0f, 0 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { // Get the projection & view matrix from the camera class D3DXMATRIXA16 mViewProjection = ( *g_Camera.GetViewMatrix() ) * ( *g_Camera.GetProjMatrix() ); g_Skybox.SetDrawSH( false ); g_Skybox.Render( &mViewProjection, 1.0f, 1.0f ); V( g_pEffect->SetMatrix( "g_mViewProjection", &mViewProjection ) ); V( g_pEffect->SetFloat( "g_fTime", ( float )fTime ) ); // Set the amount of transmitted light per color channel D3DXVECTOR3 vColorTransmit; vColorTransmit.x = g_SampleUI.GetSlider( IDC_RED_TRANSMIT_SLIDER )->GetValue() / 1000.0f; vColorTransmit.y = g_SampleUI.GetSlider( IDC_GREEN_TRANSMIT_SLIDER )->GetValue() / 1000.0f; vColorTransmit.z = g_SampleUI.GetSlider( IDC_BLUE_TRANSMIT_SLIDER )->GetValue() / 1000.0f; V( g_pEffect->SetFloatArray( "g_vColorTransmit", vColorTransmit, 3 ) ); // for Cubic degree rendering V( g_pEffect->SetFloat( "g_fLightIntensity", g_fLightIntensity ) ); V( g_pEffect->SetFloatArray( "g_vLightDirection", g_vLightDirection, 3 * sizeof( float ) ) ); V( g_pEffect->SetFloatArray( "g_vLightCoeffsR", m_fRLC, 4 * sizeof( float ) ) ); V( g_pEffect->SetFloatArray( "g_vLightCoeffsG", m_fGLC, 4 * sizeof( float ) ) ); V( g_pEffect->SetFloatArray( "g_vLightCoeffsB", m_fBLC, 4 * sizeof( float ) ) ); pd3dDevice->SetRenderState( D3DRS_FILLMODE, g_bWireFrame ? D3DFILL_WIREFRAME : D3DFILL_SOLID ); DrawFrame( pd3dDevice, g_pFrameRoot ); DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); // These events are to help PIX identify what the code is doing RenderText(); V( g_HUD.OnRender( fElapsedTime ) ); V( g_SampleUI.OnRender( fElapsedTime ) ); V( g_LightControl.OnRender9( D3DXCOLOR( 1, 1, 1, 1 ), ( D3DXMATRIX* )g_Camera.GetViewMatrix(), ( D3DXMATRIX* )g_Camera.GetProjMatrix(), g_Camera.GetEyePt() ) ); DXUT_EndPerfEvent(); V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- // Initialize the app //-------------------------------------------------------------------------------------- void InitApp() { displacement_level = 1.5; tess_lvl = 80; drawwire = false; D3DXVECTOR3 vLightDir( -1, 1, -1 ); D3DXVec3Normalize( &vLightDir, &vLightDir ); g_LightControl.SetLightDirection( vLightDir ); // Initialize dialogs g_D3DSettingsDlg.Init( &g_DialogResourceManager ); g_HUD.Init( &g_DialogResourceManager ); g_SampleUI.Init( &g_DialogResourceManager ); g_HUD.SetCallback( OnGUIEvent ); int iY = 10; g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 0, iY, 170, 23 ); g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 0, iY += 26, 170, 23, VK_F3 ); g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 0, iY += 26, 170, 23, VK_F2 ); g_SampleUI.SetCallback( OnGUIEvent ); iY = -30; WCHAR sz[100]; //iY += 24; swprintf_s( sz, L"Tessellation Level: %2.1f", tess_lvl ); g_SampleUI.AddStatic( IDC_TESSLVL_STATIC, sz, -10, iY += 26, 150, 22 ); g_SampleUI.AddSlider( IDC_TESSLVL, 10, iY += 24, 150, 22, 1, 80, tess_lvl ); iY += 14; swprintf_s( sz, L"Displacement Level: %2.1f", displacement_level ); g_SampleUI.AddStatic( IDC_DISPLACEMENTLVL_STATIC, sz, -10, iY += 26, 150, 22 ); g_SampleUI.AddSlider( IDC_DISPLACEMENTLVL, 10, iY += 24, 150, 22, 0, 150, (float)(displacement_level)/ 100.0f ); iY += 10; g_SampleUI.AddCheckBox( IDC_TOGGLEWIRE, L"Toggle Wires", 20, iY += 26, 150, 22, drawwire ); iY += 10; g_SampleUI.AddCheckBox( IDC_TOGGLEDT, L"Toggle Distanced Based tesselation", -120, iY += 26, 150, 22, true ); iY += 10; g_SampleUI.AddRadioButton( IDC_PARTITION_INTEGER, IDC_PARTITION_MODE, L"Integer", 20, iY += 26, 170, 22 ); g_SampleUI.AddRadioButton( IDC_PARTITION_FRAC_EVEN, IDC_PARTITION_MODE, L"Fractional Even", 20, iY += 26, 170, 22 ); g_SampleUI.AddRadioButton( IDC_PARTITION_FRAC_ODD, IDC_PARTITION_MODE, L"Fractional Odd", 20, iY += 26, 170, 22 ); g_SampleUI.GetRadioButton( IDC_PARTITION_INTEGER )->SetChecked( true ); iY += 10; g_SampleUI.AddCheckBox( IDC_TOGGLEBUILDING, L"Toggle buildings", 20, iY += 26, 150, 22, true ); }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has // entered a lost state and before IDirect3DDevice9::Reset is called. Resources created // in the OnResetDevice callback should be released here, which generally includes all // D3DPOOL_DEFAULT resources. See the "Lost Devices" section of the documentation for // information about lost devices. //-------------------------------------------------------------------------------------- void CALLBACK OnLostDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D9LostDevice(); g_SettingsDlg.OnD3D9LostDevice(); if( g_pFont ) g_pFont->OnLostDevice(); if( g_pEffect ) g_pEffect->OnLostDevice(); g_LightControl.StaticOnD3D9LostDevice(); g_Skybox.OnLostDevice(); SAFE_RELEASE( g_pTextSprite ); }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has // been destroyed, which generally happens as a result of application termination or // windowed/full screen toggles. Resources created in the OnCreateDevice callback // should be released here, which generally includes all D3DPOOL_MANAGED resources. //-------------------------------------------------------------------------------------- void CALLBACK OnDestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D9DestroyDevice(); g_SettingsDlg.OnD3D9DestroyDevice(); SAFE_RELEASE( g_pEffect ); SAFE_RELEASE( g_pFont ); g_LightControl.StaticOnD3D9DestroyDevice(); g_Skybox.OnDestroyDevice(); if( g_pFrameRoot ) { CAllocateHierarchy Alloc; D3DXFrameDestroy( g_pFrameRoot, &Alloc ); g_pFrameRoot = NULL; } SAFE_RELEASE( g_pAnimController ); }
//-------------------------------------------------------------------------------------- // Initialize the app //-------------------------------------------------------------------------------------- void InitApp() { D3DXVECTOR3 vLightDir( 1, 0, 0 ); D3DXVec3Normalize( &vLightDir, &vLightDir ); g_LightControl.SetLightDirection( vLightDir ); // Initialize dialogs g_D3DSettingsDlg.Init( &g_DialogResourceManager ); g_HUD.Init( &g_DialogResourceManager ); g_SampleUI.Init( &g_DialogResourceManager ); g_HUD.SetCallback( OnGUIEvent ); int iY = 10; g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 0, iY, 170, 23 ); g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 0, iY += 26, 170, 23, VK_F3 ); g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 0, iY += 26, 170, 23, VK_F2 ); g_HUD.AddButton( IDC_PAUSE, L"Pause", 0, iY += 26, 170, 23 ); g_HUD.AddButton( IDC_WIREFRAME, L"Wire frame", 0, iY += 26, 170, 23 ); g_SampleUI.SetCallback( OnGUIEvent ); iY = 10; }
//-------------------------------------------------------------------------------------- // Initialize the app //-------------------------------------------------------------------------------------- void InitApp() { static const XMVECTORF32 s_vLightDir = { -1.f, 1.f, -1.f, 0.f }; XMVECTOR vLightDir = XMVector3Normalize( s_vLightDir ); g_LightControl.SetLightDirection( vLightDir ); // Initialize dialogs g_D3DSettingsDlg.Init( &g_DialogResourceManager ); g_HUD.Init( &g_DialogResourceManager ); g_SampleUI.Init( &g_DialogResourceManager ); g_HUD.SetCallback( OnGUIEvent ); int iY = 25; g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 0, iY, 170, 22 ); g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 0, iY += 26, 170, 22, VK_F3 ); g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 0, iY += 26, 170, 22, VK_F2 ); g_HUD.AddButton( IDC_TOGGLEWIRE, L"Toggle Wires (F4)", 0, iY += 26, 170, 22, VK_F4 ); // Material Controls iY = 10; g_SampleUI.AddRadioButton( IDC_MATERIAL_PLASTIC, IDC_MATERIAL_GROUP, L"Plastic", 0, iY += 26, 170, 22 ); g_SampleUI.AddRadioButton( IDC_MATERIAL_PLASTIC_TEXTURED, IDC_MATERIAL_GROUP, L"Plastic Textured", 0, iY += 26, 170, 22 ); g_SampleUI.AddRadioButton( IDC_MATERIAL_ROUGH, IDC_MATERIAL_GROUP, L"Rough", 0, iY += 26, 170, 22 ); g_SampleUI.AddRadioButton( IDC_MATERIAL_ROUGH_TEXTURED, IDC_MATERIAL_GROUP, L"Rough Textured", 0, iY += 26, 170, 22 ); auto pRadioButton = g_SampleUI.GetRadioButton( IDC_MATERIAL_PLASTIC_TEXTURED ); pRadioButton->SetChecked( true ); iY += 24; // Lighting Controls g_SampleUI.AddRadioButton( IDC_LIGHT_CONST_AMBIENT, IDC_AMBIENT_LIGHTING_GROUP, L"Constant Ambient", 0, iY += 26, 170, 22 ); g_SampleUI.AddRadioButton( IDC_LIGHT_HEMI_AMBIENT, IDC_AMBIENT_LIGHTING_GROUP, L"Hemi Ambient", 0, iY += 26, 170, 22 ); pRadioButton = g_SampleUI.GetRadioButton( IDC_LIGHT_CONST_AMBIENT ); pRadioButton->SetChecked( true ); g_SampleUI.AddCheckBox( IDC_LIGHT_DIRECT, L"Direct Lighting", 0, iY += 26, 170, 22, g_bDirectLighting ); g_SampleUI.AddCheckBox( IDC_LIGHTING_ONLY, L"Lighting Only", 0, iY += 26, 170, 22, g_bLightingOnly ); g_SampleUI.SetCallback( OnGUIEvent ); }
//-------------------------------------------------------------------------------------- void UpdateLightingEnvironment() { // Gather lighting options from the HUD g_vLightDirection = g_LightControl.GetLightDirection(); g_fLightIntensity = g_SampleUI.GetSlider( IDC_LIGHT_SLIDER )->GetValue() / 100.0f; g_fEnvIntensity = g_SampleUI.GetSlider( IDC_ENV_SLIDER )->GetValue() / 1000.0f; // Create the spotlight D3DXSHEvalConeLight( D3DXSH_MAXORDER, &g_vLightDirection, D3DX_PI / 8.0f, g_fLightIntensity, g_fLightIntensity, g_fLightIntensity, m_fRLC, m_fGLC, m_fBLC ); float fSkybox[3][D3DXSH_MAXORDER*D3DXSH_MAXORDER]; // Scale the light probe environment contribution based on input options D3DXSHScale( fSkybox[0], D3DXSH_MAXORDER, g_fSkyBoxLightSH[0], g_fEnvIntensity ); D3DXSHScale( fSkybox[1], D3DXSH_MAXORDER, g_fSkyBoxLightSH[1], g_fEnvIntensity ); D3DXSHScale( fSkybox[2], D3DXSH_MAXORDER, g_fSkyBoxLightSH[2], g_fEnvIntensity ); // Combine the environment and the spotlight D3DXSHAdd( m_fRLC, D3DXSH_MAXORDER, m_fRLC, fSkybox[0] ); D3DXSHAdd( m_fGLC, D3DXSH_MAXORDER, m_fGLC, fSkybox[1] ); D3DXSHAdd( m_fBLC, D3DXSH_MAXORDER, m_fBLC, fSkybox[2] ); }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_D3DSettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); // D3DXVECTOR3 vCenter( 0.25767413f, -28.503521f, 111.00689f ); FLOAT fObjectRadius = 378.15607f; lightpos = D3DXVECTOR3(300,300,-200); //test = new testing(); hr = test.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = tessplane.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = tesscube.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,80,D3DXVECTOR3(300,50,-200));//D3DXVECTOR3(300,50,-200 hr = lightsphere.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,10,lightpos); hr = fuse.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = board1.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,D3DXVECTOR3(300,-300,-1000)); hr = deboard.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,D3DXVECTOR3(-300,-300,-1000)); hr = geo_alien.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = FirePart.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,80,0); //0 = fire hr = sky.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,D3DXVECTOR3(0,0,0)); hr = buildings.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext); hr = MissilePart.setup(pd3dDevice,pBackBufferSurfaceDesc,pUserContext,20,1); g_LightControl.SetRadius( 2000 ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 50.0f, -1000.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f ); g_Camera.SetRotateButtons(true,false,false); g_Camera.SetViewParams( &vecEye, &vecAt ); g_Camera.SetEnablePositionMovement( true ); g_Camera.SetScalers( 0.005f, 500.0f ); D3D11_DEPTH_STENCIL_DESC descDS; ZeroMemory(&descDS, sizeof(descDS)); descDS.DepthEnable = false; descDS.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; descDS.DepthFunc = D3D11_COMPARISON_LESS; descDS.StencilEnable = FALSE; hr = pd3dDevice->CreateDepthStencilState( &descDS, &g_DepthState); //setup stuff for post process ID3DBlob* pVertexShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"PP1.hlsl", "VS", "vs_4_0", &pVertexShaderBuffer ) ); ID3DBlob* pPixelShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"PP1.hlsl", "PS", "ps_4_0", &pPixelShaderBuffer ) ); V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), NULL, &VSPPFirstPass ) ); DXUT_SetDebugName( VSPPFirstPass, "VSPost1" ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), NULL, &PSPPFirstPass ) ); DXUT_SetDebugName( PSPPFirstPass, "PSPost1" ); pVertexShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"Gaussblur.hlsl", "VS", "vs_4_0", &pVertexShaderBuffer ) ); pPixelShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"Gaussblur.hlsl", "PS", "ps_4_0", &pPixelShaderBuffer ) ); V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), NULL, &VSPPBlur ) ); DXUT_SetDebugName( VSPPBlur, "VSBlur" ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), NULL, &PSPPBlur ) ); DXUT_SetDebugName( PSPPBlur, "PSBlur" ); pVertexShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"bloom_combine.hlsl", "VS", "vs_4_0", &pVertexShaderBuffer ) ); pPixelShaderBuffer = NULL; V_RETURN( CompileShaderFromFile( L"bloom_combine.hlsl", "PS", "ps_4_0", &pPixelShaderBuffer ) ); V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), NULL, &VSPPComb ) ); DXUT_SetDebugName( VSPPComb, "VSComb" ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), NULL, &PSPPComb ) ); DXUT_SetDebugName( PSPPComb, "PSComb" ); D3D11_BUFFER_DESC Desc; Desc.Usage = D3D11_USAGE_DYNAMIC; Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; Desc.MiscFlags = 0; Desc.ByteWidth = sizeof( blur_cbuffer ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &blur_cb_buffer ) ); DXUT_SetDebugName( blur_cb_buffer, "blur_cb" ); //g_Camera.SetRadius( fObjectRadius * 3.0f, fObjectRadius * 0.5f, fObjectRadius * 10.0f ); return S_OK; }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // created, which will happen during application initialization and windowed/full screen // toggles. This is the best location to create D3DPOOL_MANAGED resources since these // resources need to be reloaded whenever the device is destroyed. Resources created // here should be released in the OnDestroyDevice callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont ) ); // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the // shader debugger. Debugging vertex shaders requires either REF or software vertex // processing, and debugging pixel shaders requires REF. The // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the // shader debugger. It enables source level debugging, prevents instruction // reordering, prevents dead code elimination, and forces the compiler to compile // against the next higher available software target, which ensures that the // unoptimized shaders do not exceed the shader model limitations. Setting these // flags will cause slower rendering since the shaders will be unoptimized and // forced into software. See the DirectX documentation for more information about // using the shader debugger. DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DXSHADER_DEBUG; #endif #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif // Determine which LDPRT texture and SH coefficient cubemap formats are supported IDirect3D9* pD3D = DXUTGetD3D9Object(); D3DCAPS9 Caps; pd3dDevice->GetDeviceCaps( &Caps ); D3DDISPLAYMODE DisplayMode; pd3dDevice->GetDisplayMode( 0, &DisplayMode ); GetSupportedTextureFormat( pD3D, &Caps, DisplayMode.Format, &g_fmtTexture, &g_fmtCubeMap ); if( D3DFMT_UNKNOWN == g_fmtTexture || D3DFMT_UNKNOWN == g_fmtCubeMap ) return E_FAIL; // Create the skybox g_Skybox.OnCreateDevice( pd3dDevice, 50, L"Light Probes\\rnl_cross.dds", L"SkyBox.fx" ); V( D3DXSHProjectCubeMap( 6, g_Skybox.GetEnvironmentMap(), g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ) ); // Now compute the SH projection of the skybox... LPDIRECT3DCUBETEXTURE9 pSHCubeTex = NULL; V( D3DXCreateCubeTexture( pd3dDevice, 256, 1, 0, D3DFMT_A16B16G16R16F, D3DPOOL_MANAGED, &pSHCubeTex ) ); SHCubeProj projData; projData.Init( g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ); V( D3DXFillCubeTexture( pSHCubeTex, SHCubeFill, &projData ) ); g_Skybox.InitSH( pSHCubeTex ); // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, TEXT( "LocalDeformablePRT.fx" ) ) ); // If this fails, there should be debug output as to they the .fx file failed to compile V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, NULL ) ); V_RETURN( LoadTechniqueObjects( "bat" ) ); V_RETURN( g_LightControl.StaticOnD3D9CreateDevice( pd3dDevice ) ); g_LightControl.SetRadius( 2.0f ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, 0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); // Set the model's initial orientation D3DXQUATERNION quatRotation; D3DXQuaternionRotationYawPitchRoll( &quatRotation, -0.5f, 0.7f, 0.0f ); g_Camera.SetWorldQuat( quatRotation ); return hr; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D9 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { // If the settings dialog is being shown, then render it instead of rendering the app's scene if( g_D3DSettingsDlg.IsActive() ) { g_D3DSettingsDlg.OnRender( fElapsedTime ); return; } HRESULT hr; D3DXMATRIXA16 mWorldViewProjection; D3DXVECTOR3 vLightDir; D3DXCOLOR vLightDiffuse; UINT iPass, cPasses; D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR( 0.0f, 0.25f, 0.25f, 0.55f ), 1.0f, 0 ) ); //pd3dDevice->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, TRUE); //pd3dDevice->SetRenderState( D3DRS_SRGBWRITEENABLE, TRUE ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { // Get the projection & view matrix from the camera class mWorld = g_mCenterMesh * *g_Camera.GetWorldMatrix(); mProj = *g_Camera.GetProjMatrix(); mView = *g_Camera.GetViewMatrix(); mWorldViewProjection = mWorld * mView * mProj; g_fLightScale = 1.0; vLightDir = g_LightControl.GetLightDirection(); vLightDiffuse = g_fLightScale * D3DXCOLOR( 1, 1, 1, 1 ); V( g_pEffect9->SetValue( g_hLightDir, &vLightDir, sizeof( D3DXVECTOR3 ) * 1 ) ); V( g_pEffect9->SetValue( g_hLightDiffuse, &vLightDiffuse, sizeof( D3DXVECTOR4 ) * 1 ) ); // Update the effect's variables. Instead of using strings, it would // be more efficient to cache a handle to the parameter by calling // ID3DXEffect::GetParameterByName V( g_pEffect9->SetMatrix( g_hmWorldViewProjection, &mWorldViewProjection ) ); V( g_pEffect9->SetMatrix( g_hmWorld, &mWorld ) ); D3DXCOLOR vWhite = D3DXCOLOR( 1, 1, 1, 1 ); V( g_pEffect9->SetValue( g_hMaterialDiffuseColor, &vWhite, sizeof( D3DXCOLOR ) ) ); V( g_pEffect9->SetFloat( g_hfTime, ( float )fTime ) ); V( g_pEffect9->SetInt( g_hnNumLights, g_nNumActiveLights ) ); // Render the scene with this technique as defined in the .fx file switch( g_nNumActiveLights ) { case 1: V( g_pEffect9->SetTechnique( g_hRenderSceneWithTexture1Light ) ); break; case 2: V( g_pEffect9->SetTechnique( g_hRenderSceneWithTexture2Light ) ); break; case 3: V( g_pEffect9->SetTechnique( g_hRenderSceneWithTexture3Light ) ); break; } // Apply the technique contained in the effect and render the mesh V( g_pEffect9->Begin( &cPasses, 0 ) ); for( iPass = 0; iPass < cPasses; iPass++ ) { V( g_pEffect9->BeginPass( iPass ) ); V( g_pMesh9->DrawSubset( 0 ) ); V( g_pEffect9->EndPass() ); } V( g_pEffect9->End() ); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText(); V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- // Create any D3D resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr = S_OK; auto pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_D3DSettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); XMFLOAT3 vCenter( 0.25767413f, -28.503521f, 111.00689f ); FLOAT fObjectRadius = 378.15607f; g_mCenterMesh = XMMatrixTranslation( -vCenter.x, -vCenter.y, -vCenter.z ); XMMATRIX m; m = XMMatrixRotationY( XM_PI ); g_mCenterMesh *= m; m = XMMatrixRotationX( XM_PI / 2.0f ); g_mCenterMesh *= m; // Init the UI widget for directional lighting V_RETURN( CDXUTDirectionWidget::StaticOnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); g_LightControl.SetRadius( fObjectRadius ); // Compile and create the effect. DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _DEBUG // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DCOMPILE_DEBUG; // Disable optimizations to further improve shader debugging dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif #if D3D_COMPILER_VERSION >= 46 WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"DynamicShaderLinkageFX11.fx" ) ); ID3DBlob* pErrorBlob = nullptr; hr = D3DX11CompileEffectFromFile( str, nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, dwShaderFlags, D3DCOMPILE_EFFECT_ALLOW_SLOW_OPS, pd3dDevice, &g_pEffect, &pErrorBlob ); if ( pErrorBlob ) { OutputDebugStringA( reinterpret_cast<const char*>( pErrorBlob->GetBufferPointer() ) ); pErrorBlob->Release(); } if( FAILED(hr) ) { return hr; } #else ID3DBlob* pEffectBuffer = nullptr; V_RETURN( DXUTCompileFromFile( L"DynamicShaderLinkageFX11.fx", nullptr, "none", "fx_5_0", dwShaderFlags, D3DCOMPILE_EFFECT_ALLOW_SLOW_OPS, &pEffectBuffer ) ); hr = D3DX11CreateEffectFromMemory( pEffectBuffer->GetBufferPointer(), pEffectBuffer->GetBufferSize(), 0, pd3dDevice, &g_pEffect ); SAFE_RELEASE( pEffectBuffer ); if ( FAILED(hr) ) return hr; #endif // Get the light Class Interfaces for setting values // and as potential binding sources. g_pAmbientLightClass = g_pEffect->GetVariableByName( "g_ambientLight" )->AsClassInstance(); g_pAmbientLightColor = g_pAmbientLightClass->GetMemberByName( "m_vLightColor" )->AsVector(); g_pAmbientLightEnable = g_pAmbientLightClass->GetMemberByName( "m_bEnable" )->AsScalar(); g_pHemiAmbientLightClass = g_pEffect->GetVariableByName( "g_hemiAmbientLight" )->AsClassInstance(); g_pHemiAmbientLightColor = g_pHemiAmbientLightClass->GetMemberByName( "m_vLightColor" )->AsVector(); g_pHemiAmbientLightEnable = g_pHemiAmbientLightClass->GetMemberByName( "m_bEnable" )->AsScalar(); g_pHemiAmbientLightGroundColor = g_pHemiAmbientLightClass->GetMemberByName( "m_vGroundColor" )->AsVector(); g_pHemiAmbientLightDirUp = g_pHemiAmbientLightClass->GetMemberByName( "m_vDirUp" )->AsVector(); g_pDirectionalLightClass = g_pEffect->GetVariableByName( "g_directionalLight")->AsClassInstance(); g_pDirectionalLightColor = g_pDirectionalLightClass->GetMemberByName( "m_vLightColor" )->AsVector(); g_pDirectionalLightEnable = g_pDirectionalLightClass->GetMemberByName( "m_bEnable" )->AsScalar(); g_pDirectionalLightDir = g_pDirectionalLightClass->GetMemberByName( "m_vLightDir" )->AsVector(); g_pEnvironmentLightClass = g_pEffect->GetVariableByName( "g_environmentLight")->AsClassInstance(); g_pEnvironmentLightColor = g_pEnvironmentLightClass->GetMemberByName( "m_vLightColor" )->AsVector(); g_pEnvironmentLightEnable = g_pEnvironmentLightClass->GetMemberByName( "m_bEnable" )->AsScalar(); g_pEyeDir = g_pEffect->GetVariableByName( "g_vEyeDir" )->AsVector(); // Acquire the material Class Instances for all possible material settings for( UINT i=0; i < MATERIAL_TYPE_COUNT; i++) { char pTechName[50]; sprintf_s( pTechName, sizeof(pTechName), "FeatureLevel11_%s", g_pMaterialClassNames[ i ] ); g_MaterialClasses[i].pTechnique = g_pEffect->GetTechniqueByName( pTechName ); g_MaterialClasses[i].pClass = g_pEffect->GetVariableByName( g_pMaterialClassNames[i] )->AsClassInstance(); g_MaterialClasses[i].pColor = g_MaterialClasses[i].pClass->GetMemberByName( "m_vColor" )->AsVector(); g_MaterialClasses[i].pSpecPower = g_MaterialClasses[i].pClass->GetMemberByName( "m_iSpecPower" )->AsScalar(); } // Select which technique to use based on the feature level we acquired D3D_FEATURE_LEVEL supportedFeatureLevel = DXUTGetD3D11DeviceFeatureLevel(); if (supportedFeatureLevel >= D3D_FEATURE_LEVEL_11_0) { // We are going to use Dynamic shader linkage with SM5 so we need to look up interface and class instance variables // Get the abstract class interfaces so we can dynamically permute and assign linkages g_pAmbientLightIface = g_pEffect->GetVariableByName( "g_abstractAmbientLighting" )->AsInterface(); g_pDirectionalLightIface = g_pEffect->GetVariableByName( "g_abstractDirectLighting" )->AsInterface(); g_pEnvironmentLightIface = g_pEffect->GetVariableByName( "g_abstractEnvironmentLighting" )->AsInterface(); g_pMaterialIface = g_pEffect->GetVariableByName( "g_abstractMaterial" )->AsInterface(); g_pTechnique = g_pEffect->GetTechniqueByName( "FeatureLevel11" ); } else // Lower feature levels than 11 have no support for Dynamic Shader Linkage - need to use a statically specialized shaders { LPCSTR pTechniqueName; g_pAmbientLightIface = nullptr; g_pDirectionalLightIface = nullptr; g_pEnvironmentLightIface = nullptr; g_pMaterialIface = nullptr; switch( supportedFeatureLevel ) { case D3D_FEATURE_LEVEL_10_1: pTechniqueName = "FeatureLevel10_1"; break; case D3D_FEATURE_LEVEL_10_0: pTechniqueName = "FeatureLevel10"; break; default: return E_FAIL; } g_pTechnique = g_pEffect->GetTechniqueByName( pTechniqueName ); } // Create our vertex input layout const D3D11_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R10G10B10A2_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R16G16_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TANGENT", 0, DXGI_FORMAT_R10G10B10A2_UNORM, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "BINORMAL", 0, DXGI_FORMAT_R10G10B10A2_UNORM, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; D3DX11_PASS_SHADER_DESC VsPassDesc; D3DX11_EFFECT_SHADER_DESC VsDesc; V_RETURN( g_pTechnique->GetPassByIndex(0)->GetVertexShaderDesc(&VsPassDesc) ); V_RETURN( VsPassDesc.pShaderVariable->GetShaderDesc(VsPassDesc.ShaderIndex, &VsDesc) ); V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), VsDesc.pBytecode, VsDesc.BytecodeLength, &g_pVertexLayout11 ) ); DXUT_SetDebugName( g_pVertexLayout11, "Primary" ); // Load the mesh V_RETURN( g_Mesh11.Create( pd3dDevice, L"Squid\\squid.sdkmesh", false ) ); g_pWorldViewProjection = g_pEffect->GetVariableByName( "g_mWorldViewProjection" )->AsMatrix(); g_pWorld = g_pEffect->GetVariableByName( "g_mWorld" )->AsMatrix(); // Load a HDR Environment for reflections V_RETURN( DXUTCreateShaderResourceViewFromFile( pd3dDevice, L"Light Probes\\uffizi_cross.dds", &g_pEnvironmentMapSRV )); g_pEnvironmentMapVar = g_pEffect->GetVariableByName( "g_txEnvironmentMap" )->AsShaderResource(); g_pEnvironmentMapVar->SetResource( g_pEnvironmentMapSRV ); // Setup the camera's view parameters static const XMVECTORF32 s_vecEye = { 0.0f, 0.0f, -50.0f, 0.f }; g_Camera.SetViewParams( s_vecEye, g_XMZero ); g_Camera.SetRadius( fObjectRadius , fObjectRadius , fObjectRadius ); // Find Rasterizer State Object index for WireFrame / Solid rendering g_pFillMode = g_pEffect->GetVariableByName( "g_fillMode" )->AsScalar(); return hr; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D11 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext ) { HRESULT hr; // If the settings dialog is being shown, then render it instead of rendering the app's scene if( g_D3DSettingsDlg.IsActive() ) { g_D3DSettingsDlg.OnRender( fElapsedTime ); return; } // Clear the render target and depth stencil auto pRTV = DXUTGetD3D11RenderTargetView(); pd3dImmediateContext->ClearRenderTargetView( pRTV, Colors::MidnightBlue ); auto pDSV = DXUTGetD3D11DepthStencilView(); pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 ); // Get the projection & view matrix from the camera class XMMATRIX mWorld = g_Camera.GetWorldMatrix(); XMMATRIX mProj = g_Camera.GetProjMatrix(); XMMATRIX mView = g_Camera.GetViewMatrix(); // Get the light direction XMVECTOR vLightDir = g_LightControl.GetLightDirection(); // Render the light arrow so the user can visually see the light dir V( g_LightControl.OnRender( Colors::Yellow, mView, mProj, g_Camera.GetEyePt() ) ); // Ambient Light static const XMVECTORF32 s_vLightColorA = { 0.1f, 0.1f, 0.1f, 1.0f }; g_pAmbientLightColor->SetFloatVector( s_vLightColorA ); g_pAmbientLightEnable->SetBool(true); // Hemi Ambient Light static const XMVECTORF32 s_vLightColorH1 = { 0.3f, 0.3f, 0.4f, 1.0f }; g_pHemiAmbientLightColor->SetFloatVector( s_vLightColorH1 ); g_pHemiAmbientLightEnable->SetBool(true); XMFLOAT4 vLightGrndClr( 0.05f, 0.05f, 0.05f, 1.f ); g_pHemiAmbientLightGroundColor->SetFloatVector( reinterpret_cast<float*>( &vLightGrndClr ) ); XMFLOAT4 vVec(0.0f, 1.0f, 0.0f, 1.0f); g_pHemiAmbientLightDirUp->SetFloatVector( reinterpret_cast<float*>( &vVec ) ); // Directional Light g_pDirectionalLightColor->SetFloatVector( Colors::White ); g_pDirectionalLightEnable->SetBool(true); XMFLOAT4 tmp; XMStoreFloat4( &tmp, vLightDir ); tmp.w = 1.f; g_pDirectionalLightDir->SetFloatVector( reinterpret_cast<float*>( &tmp ) ); // Environment Light - color comes from the texture g_pEnvironmentLightColor->SetFloatVector( Colors::Black ); g_pEnvironmentLightEnable->SetBool(true); // Setup the Eye based on the DXUT camera XMVECTOR vEyePt = g_Camera.GetEyePt(); XMVECTOR vDir = g_Camera.GetLookAtPt() - vEyePt; XMStoreFloat4( &tmp, vDir ); tmp.w = 1.f; g_pEyeDir->SetFloatVector( reinterpret_cast<float*>( &tmp ) ); //Get the mesh //IA setup pd3dImmediateContext->IASetInputLayout( g_pVertexLayout11 ); UINT Strides[1]; UINT Offsets[1]; ID3D11Buffer* pVB[1]; pVB[0] = g_Mesh11.GetVB11( 0, 0 ); Strides[0] = ( UINT )g_Mesh11.GetVertexStride( 0, 0 ); Offsets[0] = 0; pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets ); pd3dImmediateContext->IASetIndexBuffer( g_Mesh11.GetIB11( 0 ), g_Mesh11.GetIBFormat11( 0 ), 0 ); // Set the per object constant data XMMATRIX mWorldViewProjection = mWorld * mView * mProj; // VS Per object XMFLOAT4X4 tmp4x4; XMStoreFloat4x4( &tmp4x4, mWorldViewProjection ); g_pWorldViewProjection->SetMatrix( reinterpret_cast<float*>( &tmp4x4 ) ); XMStoreFloat4x4( &tmp4x4, mWorld ); g_pWorld->SetMatrix( reinterpret_cast<float*>( &tmp4x4 ) ); // Setup the Shader Linkage based on the user settings for Lighting ID3DX11EffectClassInstanceVariable* pLightClassVar; // Ambient Lighting First - Constant or Hemi? if ( g_bHemiAmbientLighting ) { pLightClassVar = g_pHemiAmbientLightClass; } else { pLightClassVar = g_pAmbientLightClass; } if (g_pAmbientLightIface) { g_pAmbientLightIface->SetClassInstance(pLightClassVar); } // Direct Light - None or Directional if (g_bDirectLighting) { pLightClassVar = g_pDirectionalLightClass; } else { // Disable ALL Direct Lighting pLightClassVar = g_pAmbientLightClass; } if (g_pDirectionalLightIface) { g_pDirectionalLightIface->SetClassInstance(pLightClassVar); } // Setup the selected material class instance E_MATERIAL_TYPES iMaterialTech = g_iMaterial; switch( g_iMaterial ) { case MATERIAL_PLASTIC: case MATERIAL_PLASTIC_TEXTURED: // Bind the Environment light for reflections pLightClassVar = g_pEnvironmentLightClass; if (g_bLightingOnly) { iMaterialTech = MATERIAL_PLASTIC_LIGHTING_ONLY; } break; case MATERIAL_ROUGH: case MATERIAL_ROUGH_TEXTURED: // UnBind the Environment light pLightClassVar = g_pAmbientLightClass; if (g_bLightingOnly) { iMaterialTech = MATERIAL_ROUGH_LIGHTING_ONLY; } break; } if (g_pEnvironmentLightIface) { g_pEnvironmentLightIface->SetClassInstance(pLightClassVar); } ID3DX11EffectTechnique* pTechnique = g_pTechnique; if (g_pMaterialIface) { #if USE_BIND_INTERFACES // We're using the techniques with pre-bound materials, // so select the appropriate technique. pTechnique = g_MaterialClasses[ iMaterialTech ].pTechnique; #else // We're using a single technique and need to explicitly // bind a concrete material instance. g_pMaterialIface->SetClassInstance( g_MaterialClasses[ iMaterialTech ].pClass ); #endif } // PS Per Prim // Shiny Plastic XMFLOAT3 clr1(1, 0, 0.5f); g_MaterialClasses[MATERIAL_PLASTIC].pColor->SetFloatVector( reinterpret_cast<float*>( &clr1 ) ); g_MaterialClasses[MATERIAL_PLASTIC].pSpecPower->SetInt(255); // Shiny Plastic with Textures XMFLOAT3 clr2(1, 0, 0.5f); g_MaterialClasses[MATERIAL_PLASTIC_TEXTURED].pColor->SetFloatVector( reinterpret_cast<float*>( &clr2 ) ); g_MaterialClasses[MATERIAL_PLASTIC_TEXTURED].pSpecPower->SetInt(128); // Lighting Only Plastic XMFLOAT3 clr3(1, 1, 1); g_MaterialClasses[MATERIAL_PLASTIC_LIGHTING_ONLY].pColor->SetFloatVector( reinterpret_cast<float*>( &clr3 ) ); g_MaterialClasses[MATERIAL_PLASTIC_LIGHTING_ONLY].pSpecPower->SetInt(128); // Rough Material XMFLOAT3 clr4(0, 0.5f, 1); g_MaterialClasses[MATERIAL_ROUGH].pColor->SetFloatVector( reinterpret_cast<float*>( &clr4 ) ); g_MaterialClasses[MATERIAL_ROUGH].pSpecPower->SetInt(6); // Rough Material with Textures XMFLOAT3 clr5(0, 0.5f, 1); g_MaterialClasses[MATERIAL_ROUGH_TEXTURED].pColor->SetFloatVector( reinterpret_cast<float*>( &clr5 ) ); g_MaterialClasses[MATERIAL_ROUGH_TEXTURED].pSpecPower->SetInt(6); // Lighting Only Rough XMFLOAT3 clr6(1, 1, 1); g_MaterialClasses[MATERIAL_ROUGH_LIGHTING_ONLY].pColor->SetFloatVector( reinterpret_cast<float*>( &clr6 ) ); g_MaterialClasses[MATERIAL_ROUGH_LIGHTING_ONLY].pSpecPower->SetInt(6); if (g_bWireFrame) g_pFillMode->SetInt(1); else g_pFillMode->SetInt(0); // Apply the technique to update state. pTechnique->GetPassByIndex(0)->Apply(0, pd3dImmediateContext); //Render g_Mesh11.Render( pd3dImmediateContext, 0, 1, INVALID_SAMPLER_SLOT); // Tell the UI items to render DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText(); DXUT_EndPerfEvent(); }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED) // and aren't tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { MessageBox(0, L"We aren't using DirectX9", L"We aren't using DirectX9", 0); exit(1); HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_D3DSettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont9 ) ); // Load the mesh V_RETURN( LoadMesh( pd3dDevice, L"tiny\\tiny.x", &g_pMesh9 ) ); D3DXVECTOR3* pData; D3DXVECTOR3 vCenter; FLOAT fObjectRadius; V( g_pMesh9->LockVertexBuffer( 0, ( LPVOID* )&pData ) ); V( D3DXComputeBoundingSphere( pData, g_pMesh9->GetNumVertices(), D3DXGetFVFVertexSize( g_pMesh9->GetFVF() ), &vCenter, &fObjectRadius ) ); V( g_pMesh9->UnlockVertexBuffer() ); D3DXMatrixTranslation( &g_mCenterMesh, -vCenter.x, -vCenter.y, -vCenter.z ); D3DXMATRIXA16 m; D3DXMatrixRotationY( &m, D3DX_PI ); g_mCenterMesh *= m; D3DXMatrixRotationX( &m, D3DX_PI / 2.0f ); g_mCenterMesh *= m; V_RETURN( CDXUTDirectionWidget::StaticOnD3D9CreateDevice( pd3dDevice ) ); g_LightControl.SetRadius( fObjectRadius ); // Read the D3DX effect file WCHAR str[MAX_PATH]; DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE | D3DXSHADER_NO_PRESHADER | D3DXFX_LARGEADDRESSAWARE; #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"BasicHLSL.fx" ) ); V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect9, NULL ) ); // Create the mesh texture from a file V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"tiny\\tiny_skin.dds" ) ); V_RETURN( D3DXCreateTextureFromFileEx( pd3dDevice, str, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &g_pMeshTexture9 ) ); // Set effect variables as needed D3DXCOLOR colorMtrlDiffuse( 1.0f, 1.0f, 1.0f, 1.0f ); D3DXCOLOR colorMtrlAmbient( 0.35f, 0.35f, 0.35f, 0 ); D3DXHANDLE hMaterialAmbientColor = g_pEffect9->GetParameterByName( NULL, "g_MaterialAmbientColor" ); D3DXHANDLE hMaterialDiffuseColor = g_pEffect9->GetParameterByName( NULL, "g_MaterialDiffuseColor" ); D3DXHANDLE hMeshTexture = g_pEffect9->GetParameterByName( NULL, "g_MeshTexture" ); V_RETURN( g_pEffect9->SetValue( hMaterialAmbientColor, &colorMtrlAmbient, sizeof( D3DXCOLOR ) ) ); V_RETURN( g_pEffect9->SetValue( hMaterialDiffuseColor, &colorMtrlDiffuse, sizeof( D3DXCOLOR ) ) ); V_RETURN( g_pEffect9->SetTexture( hMeshTexture, g_pMeshTexture9 ) ); g_hLightDir = g_pEffect9->GetParameterByName( NULL, "g_LightDir" ); g_hLightDiffuse = g_pEffect9->GetParameterByName( NULL, "g_LightDiffuse" ); g_hmWorldViewProjection = g_pEffect9->GetParameterByName( NULL, "g_mWorldViewProjection" ); g_hmWorld = g_pEffect9->GetParameterByName( NULL, "g_mWorld" ); g_hMaterialDiffuseColor = g_pEffect9->GetParameterByName( NULL, "g_MaterialDiffuseColor" ); g_hfTime = g_pEffect9->GetParameterByName( NULL, "g_fTime" ); g_hnNumLights = g_pEffect9->GetParameterByName( NULL, "g_nNumLights" ); g_hRenderSceneWithTexture1Light = g_pEffect9->GetTechniqueByName( "RenderSceneWithTexture1Light" ); g_hRenderSceneWithTexture2Light = g_pEffect9->GetTechniqueByName( "RenderSceneWithTexture2Light" ); g_hRenderSceneWithTexture3Light = g_pEffect9->GetTechniqueByName( "RenderSceneWithTexture3Light" ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -15.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); g_Camera.SetRadius( fObjectRadius * 3.0f, fObjectRadius * 0.5f, fObjectRadius * 10.0f ); return S_OK; }