//-------------------------------------------------------------------------------------- // 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 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() ); // 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.1f, 1000.0f ); g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_SampleUI.SetLocation( 0, 0 ); g_SampleUI.SetSize( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); g_SampleUI.GetControl( IDC_STATIC )->SetSize( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height * 6 / 10 ); g_SampleUI.GetControl( IDC_OUTPUT )->SetSize( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height / 4 ); g_SampleUI.GetControl( IDC_EDITBOX1 )->SetLocation( 20, pBackBufferSurfaceDesc->Height - 230 ); g_SampleUI.GetControl( IDC_EDITBOX1 )->SetSize( pBackBufferSurfaceDesc->Width - 40, 32 ); if( g_SampleUI.GetControl( IDC_EDITBOX2 ) ) { g_SampleUI.GetControl( IDC_EDITBOX2 )->SetLocation( 20, pBackBufferSurfaceDesc->Height - 280 ); g_SampleUI.GetControl( IDC_EDITBOX2 )->SetSize( pBackBufferSurfaceDesc->Width - 40, 45 ); } g_SampleUI.GetControl( IDC_ENABLEIME )->SetLocation( 130, pBackBufferSurfaceDesc->Height - 80 ); g_SampleUI.GetControl( IDC_DISABLEIME )->SetLocation( 220, pBackBufferSurfaceDesc->Height - 80 ); g_SampleUI.GetControl( IDC_SLIDER )->SetLocation( 10, pBackBufferSurfaceDesc->Height - 140 ); g_SampleUI.GetControl( IDC_CHECKBOX )->SetLocation( 120, pBackBufferSurfaceDesc->Height - 50 ); g_SampleUI.GetControl( IDC_CLEAREDIT )->SetLocation( 120, pBackBufferSurfaceDesc->Height - 25 ); g_SampleUI.GetControl( IDC_COMBOBOX )->SetLocation( 20, pBackBufferSurfaceDesc->Height - 180 ); g_SampleUI.GetControl( IDC_RADIO1A )->SetLocation( pBackBufferSurfaceDesc->Width - 160, 100 ); g_SampleUI.GetControl( IDC_RADIO1B )->SetLocation( pBackBufferSurfaceDesc->Width - 160, 124 ); g_SampleUI.GetControl( IDC_RADIO1C )->SetLocation( pBackBufferSurfaceDesc->Width - 160, 148 ); g_SampleUI.GetControl( IDC_RADIO2A )->SetLocation( 20, pBackBufferSurfaceDesc->Height - 100 ); g_SampleUI.GetControl( IDC_RADIO2B )->SetLocation( 20, pBackBufferSurfaceDesc->Height - 76 ); g_SampleUI.GetControl( IDC_RADIO2C )->SetLocation( 20, pBackBufferSurfaceDesc->Height - 52 ); g_SampleUI.GetControl( IDC_LISTBOX )->SetLocation( pBackBufferSurfaceDesc->Width - 400, pBackBufferSurfaceDesc->Height - 180 ); g_SampleUI.GetControl( IDC_LISTBOX )->SetSize( 190, 96 ); g_SampleUI.GetControl( IDC_LISTBOXM )->SetLocation( pBackBufferSurfaceDesc->Width - 200, pBackBufferSurfaceDesc->Height - 180 ); g_SampleUI.GetControl( IDC_LISTBOXM )->SetSize( 190, 124 ); g_Mesh.RestoreDeviceObjects( pd3dDevice ); return S_OK; }
//-------------------------------------------------------------------------------------- // 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; }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that depend on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10ResizedSwapChain(ID3D10Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext) { HRESULT hr; V_RETURN(g_DialogResourceManager.OnD3D10ResizedSwapChain(pd3dDevice, pBackBufferSurfaceDesc)); V_RETURN(g_D3DSettingsDlg.OnD3D10ResizedSwapChain(pd3dDevice, pBackBufferSurfaceDesc)); // Setup the camera's projection parameters float fAspectRatio = 1.0;//pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams(D3DX_PI / 4, fAspectRatio, 0.1f, 10.0f); g_Camera.SetWindow(pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height); g_Camera.SetButtonMasks(MOUSE_RIGHT_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); // resize the texture so that it fits to the current screen size UINT width = (DXUTIsAppRenderingWithD3D9()) ? DXUTGetD3D9BackBufferSurfaceDesc()->Width : DXUTGetDXGIBackBufferSurfaceDesc()->Width; UINT height = (DXUTIsAppRenderingWithD3D9()) ? DXUTGetD3D9BackBufferSurfaceDesc()->Height : DXUTGetDXGIBackBufferSurfaceDesc()->Height; g_vsObj->SetupTextures(pd3dDevice, g_pEffect10, width, height); 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; }
//-------------------------------------------------------------------------------------- // 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_SettingsDlg.OnD3D9ResetDevice() ); if( g_pFont9 ) V_RETURN( g_pFont9->OnResetDevice() ); V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pSprite9 ) ); g_pTxtHelper = new CDXUTTextHelper( g_pFont9, g_pSprite9, NULL, NULL, 15 ); // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f ); g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 350 ); g_SampleUI.SetSize( 170, 300 ); SCEMAN->SetActiveDevice( pd3dDevice ); SCEMAN->OnDeviceReset(); return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that depend on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10SwapChainResized( ID3D10Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr = S_OK; V_RETURN( g_DialogResourceManager.OnD3D10ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) ); V_RETURN( g_D3DSettingsDlg.OnD3D10ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) ); // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( 60.0f * ( D3DX_PI / 180.0f ), fAspectRatio, 0.1f, 100.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 ); // Update the sample count UpdateMSAASampleCounts( pd3dDevice, pBackBufferSurfaceDesc->Format ); // Create a render target g_BackBufferWidth = pBackBufferSurfaceDesc->Width; g_BackBufferHeight = pBackBufferSurfaceDesc->Height; V_RETURN( CreateRenderTarget( pd3dDevice, g_BackBufferWidth, g_BackBufferHeight, g_MSAASampleCount, 0 ) ); return hr; }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that depend on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11ResizedSwapChain(ID3D11Device* pd3dDevice, IDXGISwapChain* /*pSwapChain*/, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* /*pUserContext*/) { HRESULT hr; V_RETURN(g_DialogResourceManager.OnD3D11ResizedSwapChain(pd3dDevice, pBackBufferSurfaceDesc)); V_RETURN(g_D3DSettingsDlg.OnD3D11ResizedSwapChain(pd3dDevice, pBackBufferSurfaceDesc)); g_nFrameWidth = pBackBufferSurfaceDesc->Width; g_nFrameHeight = pBackBufferSurfaceDesc->Height; // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams(XM_PI / 4, fAspectRatio, 0.1f, 5000.0f); g_Camera.SetWindow(pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height); g_HUD.SetLocation(pBackBufferSurfaceDesc->Width - 170, 0); g_HUD.SetSize(170, 170); g_SampleUI.SetLocation(pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 350); g_SampleUI.SetSize(170, 300); g_OIT.OnD3D11ResizedSwapChain(pBackBufferSurfaceDesc, pd3dDevice); return S_OK; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D11 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext ) { pd3dImmediateContext->ClearRenderTargetView(DXUTGetD3D11RenderTargetView(), Colors::MidnightBlue); pd3dImmediateContext->ClearDepthStencilView(DXUTGetD3D11DepthStencilView(), D3D11_CLEAR_DEPTH, 1.0, 0); XMMATRIX mview = g_camera.GetViewMatrix(); XMMATRIX mproj = g_camera.GetProjMatrix(); XMFLOAT4 LitDir = XMFLOAT4(-0.577f, 0.577f, -0.577f, 1.0f); XMFLOAT4 LitCol = XMFLOAT4(0.9f, 0.2f, 0.3f, 1.0f); /* SHADER CONSTANT BUFFER */ ConstantBuffer cb; XMStoreFloat4x4(&(cb.world), XMMatrixTranspose(g_mesh.World())); XMStoreFloat4x4(&(cb.view), XMMatrixTranspose(mview)); XMStoreFloat4x4(&(cb.projection), XMMatrixTranspose(mproj)); cb.litDir = LitDir; cb.litCol = LitCol; g_shader->RenderPrepare(&cb); g_mesh.Render(); }
//-------------------------------------------------------------------------------------- // 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; D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mWorldViewProjection; // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 141, 153, 191 ), 1.0f, 0 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { // Get the projection & view matrix from the camera class mWorld = *g_Camera.GetWorldMatrix(); mView = *g_Camera.GetViewMatrix(); mProj = *g_Camera.GetProjMatrix(); mWorldViewProjection = mWorld * mView * mProj; // Update the effect's variables. V( g_pEffect->SetMatrix( g_hWorldViewProjection, &mWorldViewProjection ) ); V( g_pEffect->SetMatrix( g_hWorld, &mWorld ) ); V( g_pEffect->SetFloat( g_hTime, ( float )fTime ) ); V( g_pEffect->SetValue( g_hCameraPosition, g_Camera.GetEyePt(), sizeof( D3DXVECTOR3 ) ) ); UINT iCurSubset = ( UINT )( INT_PTR )g_SampleUI.GetComboBox( IDC_SUBSET )->GetSelectedData(); // A subset of -1 was arbitrarily chosen to represent all subsets if( iCurSubset == -1 ) { // Iterate through subsets, changing material properties for each for( UINT iSubset = 0; iSubset < g_MeshLoader.GetNumMaterials(); iSubset++ ) { RenderSubset( iSubset ); } } else { RenderSubset( iCurSubset ); } RenderText(); V( g_HUD.OnRender( fElapsedTime ) ); V( g_SampleUI.OnRender( fElapsedTime ) ); V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D9 device //-------------------------------------------------------------------------------------- void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { if( g_SettingsDlg.IsActive() ) { g_SettingsDlg.OnRender( fElapsedTime ); return; } HRESULT hr; D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mWorldViewProjection; // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { // Get the projection & view matrix from the camera class mWorld = *g_Camera.GetWorldMatrix(); mProj = *g_Camera.GetProjMatrix(); mView = g_mView; mWorldViewProjection = mWorld * mView * mProj; // 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_pEffect->SetMatrix( "g_mWorldViewProjection", &mWorldViewProjection ) ); V( g_pEffect->SetMatrix( "g_mWorld", &mWorld ) ); V( g_pEffect->SetFloat( "g_fTime", ( float )fTime ) ); g_pEffect->SetTechnique( "RenderScene" ); UINT cPasses; g_pEffect->Begin( &cPasses, 0 ); ID3DXMesh* pMesh = g_Mesh.GetMesh(); for( UINT p = 0; p < cPasses; ++p ) { g_pEffect->BeginPass( p ); for( UINT m = 0; m < g_Mesh.m_dwNumMaterials; ++m ) { g_pEffect->SetTexture( "g_txScene", g_Mesh.m_pTextures[m] ); g_pEffect->CommitChanges(); pMesh->DrawSubset( m ); } g_pEffect->EndPass(); } g_pEffect->End(); RenderText(); V( g_HUD.OnRender( fElapsedTime ) ); V( g_SampleUI.OnRender( fElapsedTime ) ); V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- // 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() ); } }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that depend on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height; g_camera.SetProjParams(XM_PI / 4, fAspectRatio, 0.1f, 1000.0f); g_camera.SetWindow(pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height); g_camera.SetButtonMasks(MOUSE_LEFT_BUTTON, MOUSE_WHEEL, MOUSE_RIGHT_BUTTON); return S_OK; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D10 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10FrameRender( ID3D10Device* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { // Clear the render target float ClearColor[4] = { 0.9569f, 0.9569f, 1.0f, 0.0f }; ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView(); pd3dDevice->ClearRenderTargetView( pRTV, ClearColor ); ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView(); pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 ); // Render the result D3DXMATRIX mWorld; D3DXMATRIX mView; D3DXMATRIX mProj; D3DXMATRIX mWorldView; D3DXMATRIX mWorldViewProj; mWorld = *g_Camera.GetWorldMatrix(); mProj = *g_Camera.GetProjMatrix(); mView = *g_Camera.GetViewMatrix(); mWorldView = mWorld * mView; mWorldViewProj = mWorldView * mProj; // Set variables g_pmWorldViewProj->SetMatrix( ( float* )&mWorldViewProj ); g_pmWorldView->SetMatrix( ( float* )&mWorldView ); g_pmWorld->SetMatrix( ( float* )&mWorld ); g_pmView->SetMatrix( ( float* )&mView ); g_pmProj->SetMatrix( ( float* )&mProj ); g_pDiffuseTex->SetResource( g_pMeshTexRV ); D3DXVECTOR3 lightDir( -1,1,-1 ); D3DXVECTOR3 viewLightDir; D3DXVec3TransformNormal( &viewLightDir, &lightDir, &mView ); D3DXVec3Normalize( &viewLightDir, &viewLightDir ); g_pViewSpaceLightDir->SetFloatVector( ( float* )&viewLightDir ); // Set Input Assembler params UINT stride = g_VertStride; UINT offset = 0; pd3dDevice->IASetInputLayout( g_pVertexLayout ); pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); pd3dDevice->IASetIndexBuffer( g_pIB, DXGI_FORMAT_R32_UINT, 0 ); pd3dDevice->IASetVertexBuffers( 0, 1, &g_pVB, &stride, &offset ); // Render using the technique g_pRenderTextured D3D10_TECHNIQUE_DESC techDesc; g_pRenderTextured->GetDesc( &techDesc ); for( UINT p = 0; p < techDesc.Passes; p++ ) { g_pRenderTextured->GetPassByIndex( p )->Apply( 0 ); pd3dDevice->DrawIndexed( g_NumIndices, 0, 0 ); } }
//-------------------------------------------------------------------------------------- // 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() ); // Store the correct technique handles for each material for( UINT i = 0; i < g_MeshLoader.GetNumMaterials(); i++ ) { Material* pMaterial = g_MeshLoader.GetMaterial( i ); const char* strTechnique = NULL; if( pMaterial->pTexture && pMaterial->bSpecular ) strTechnique = "TexturedSpecular"; else if( pMaterial->pTexture && !pMaterial->bSpecular ) strTechnique = "TexturedNoSpecular"; else if( !pMaterial->pTexture && pMaterial->bSpecular ) strTechnique = "Specular"; else if( !pMaterial->pTexture && !pMaterial->bSpecular ) strTechnique = "NoSpecular"; pMaterial->hTechnique = g_pEffect->GetTechniqueByName( strTechnique ); } // 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.1f, 1000.0f ); g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_HUD.Refresh(); g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 350 ); g_SampleUI.SetSize( 170, 300 ); g_SampleUI.Refresh(); return S_OK; }
//-------------------------------------------------------------------------------------- // This callback function will be called once at the beginning of every frame. This is the // best location for your application to handle updates to the scene, but is not // intended to contain actual rendering calls, which should instead be placed in the // OnFrameRender callback. //-------------------------------------------------------------------------------------- void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext ) { // Update the camera's position based on user input g_Camera.FrameMove( fElapsedTime ); UpdateLightingEnvironment(); if( g_pAnimController != NULL ) { g_pAnimController->SetTrackSpeed( 0, g_SampleUI.GetSlider( IDC_ANIMATION_SPEED )->GetValue() / 1000.0f ); g_pAnimController->AdvanceTime( fElapsedTime, NULL ); } UpdateFrameMatrices( g_pFrameRoot, g_Camera.GetWorldMatrix() ); }
// Draw a simple triangle using custom shaders (g_pEffect) void DrawTriangle(ID3D11DeviceContext* pd3dImmediateContext) { XMMATRIX world = g_camera.GetWorldMatrix(); XMMATRIX view = g_camera.GetViewMatrix(); XMMATRIX proj = g_camera.GetProjMatrix(); XMFLOAT4X4 mViewProj; XMStoreFloat4x4(&mViewProj, world * view * proj); g_pEffect->GetVariableByName("g_worldViewProj")->AsMatrix()->SetMatrix((float*)mViewProj.m); g_pEffect->GetTechniqueByIndex(0)->GetPassByIndex(0)->Apply(0, pd3dImmediateContext); pd3dImmediateContext->IASetVertexBuffers(0, 0, nullptr, nullptr, nullptr); pd3dImmediateContext->IASetIndexBuffer(nullptr, DXGI_FORMAT_R16_UINT, 0); pd3dImmediateContext->IASetInputLayout(nullptr); pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); pd3dImmediateContext->Draw(3, 0); }
void CALLBACK OnFrameMove(_In_ double fTime, _In_ float fElapsedTime, _In_opt_ void* pUserContext) { static float rot = 0.0f; rot += fElapsedTime; XMMATRIX mscale = XMMatrixScaling(1.0f, 1.0f, 1.0f); XMMATRIX mrot = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rot); XMMATRIX mtrans = XMMatrixTranslation(20.0f, 50.0f, 0.0f); // gmesh 의 월드 매트릭스 -> XMMATRIX XMMATRIX mParent = XMLoadFloat4x4(&g_mesh->getWorld()); g_matLight = (mscale * mtrans * mrot) * mParent; // 조명 메시 연결 XMFLOAT4X4 mlit = XMFLOAT4X4(); XMStoreFloat4x4(&mlit, g_matLight); g_meshLight->setWorld(mlit); g_camera.FrameMove(fElapsedTime); }
//-------------------------------------------------------------------------------------- // Handle messages to the application //-------------------------------------------------------------------------------------- LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext ) { // Always allow dialog resource manager calls to handle global messages // so GUI state is updated correctly *pbNoFurtherProcessing = CDXUTIMEEditBox::StaticMsgProc( hWnd, uMsg, wParam, lParam ); if( *pbNoFurtherProcessing ) return 0; *pbNoFurtherProcessing = g_DialogResourceManager.MsgProc( hWnd, uMsg, wParam, lParam ); if( *pbNoFurtherProcessing ) return 0; if( g_SettingsDlg.IsActive() ) { g_SettingsDlg.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; // Pass all remaining windows messages to camera so it can respond to user input g_Camera.HandleMessages( hWnd, uMsg, wParam, lParam ); return 0; }
HRESULT CALLBACK OnD3D11CreateDevice(ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext) { HRESULT hr; g_input = new Input; g_dwtext = new DwriteText; g_ShapePos = GeometricPrimitive::CreateSphere(DXUTGetD3D11DeviceContext(), 0.01f, 16, false); g_ShapeDir = GeometricPrimitive::CreateOctahedron(DXUTGetD3D11DeviceContext(), 0.1f, false); g_Batch.reset(new PrimitiveBatch<VertexPositionColor>(DXUTGetD3D11DeviceContext())); g_BatchEffect.reset( new BasicEffect( pd3dDevice ) ); g_BatchEffect->SetVertexColorEnabled(true); { void const* shaderByteCode; size_t byteCodeLength; g_BatchEffect->GetVertexShaderBytecode( &shaderByteCode, &byteCodeLength ); hr = pd3dDevice->CreateInputLayout( VertexPositionColor::InputElements, VertexPositionColor::InputElementCount, shaderByteCode, byteCodeLength, &g_pBatchInputLayout ); if( FAILED( hr ) ) return hr; } WCHAR str[MAX_PATH]; static const XMVECTOR eye = { 2.0f, 5.0f, -5.0f, 0.f }; static const XMVECTOR lookat = { 0.0f, 0.0f, 0.0f, 0.f }; g_camera.SetViewParams(eye, lookat); return S_OK; }
//-------------------------------------------------------------------------------------- // Sets the camera to view a particular group of objects //-------------------------------------------------------------------------------------- void SetViewForGroup( int group ) { assert( group < GROUP_COUNT ); g_Camera.Reset(); static const XMVECTORF32 s_Offset0 = { 0.f, 20.f, 20.f, 0.f }; static const XMVECTORF32 s_Offset = { 0.f, 20.f, -20.f, 0.f }; XMVECTOR vecEye = g_CameraOrigins[group] + ( ( group == 0 ) ? s_Offset0 : s_Offset ); g_Camera.SetViewParams( vecEye, g_CameraOrigins[group] ); XMFLOAT3 vecAt; XMStoreFloat3( &vecAt, g_CameraOrigins[group] ); g_Camera.SetModelCenter( vecAt ); }
//-------------------------------------------------------------------------------------- // 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; }
//-------------------------------------------------------------------------------------- // Initialize the app //-------------------------------------------------------------------------------------- void InitApp() { // Initialize dialogs g_D3DSettingsDlg.Init( &g_DialogResourceManager ); g_HUD.Init( &g_DialogResourceManager ); g_SampleUI.Init( &g_DialogResourceManager ); g_HUD.SetCallback( OnGUIEvent ); int iY = 20; 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_SampleUI.SetCallback( OnGUIEvent ); iY = 10; WCHAR sz[100]; iY += 24; swprintf_s( sz, L"Patch Divisions: %2.1f", g_fSubdivs ); g_SampleUI.AddStatic( IDC_PATCH_SUBDIVS_STATIC, sz, 10, iY += 26, 150, 22 ); g_SampleUI.AddSlider( IDC_PATCH_SUBDIVS, 10, iY += 24, 150, 22, 10 * MIN_DIVS, 10 * MAX_DIVS, (int)(g_fSubdivs * 10) ); iY += 24; g_SampleUI.AddCheckBox( IDC_TOGGLE_LINES, L"Toggle Wires", 20, iY += 26, 150, 22, g_bDrawWires ); iY += 24; 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 ); // Setup the camera's view parameters static const XMVECTORF32 s_vecEye = { 1.0f, 1.5f, -3.5f, 0.f }; static const XMVECTORF32 s_vecAt = { 0.0f, 0.0f, 0.0f, 0.f }; g_Camera.SetViewParams( s_vecEye, s_vecAt ); }
//-------------------------------------------------------------------------------------- // Handle messages to the application //-------------------------------------------------------------------------------------- LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing, void* pUserContext ) { g_camera.HandleMessages(hWnd, uMsg, wParam, lParam); return 0; }
HRESULT CALLBACK OnResetDevice(IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext) { HRESULT hr; V_RETURN(g_DialogResourceManager.OnD3D9ResetDevice()); if(g_pFont) V_RETURN(g_pFont->OnResetDevice()); V_RETURN(D3DXCreateSprite(pd3dDevice, &g_pTextSprite)); V_RETURN(g_Board.OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc)); V_RETURN(g_BackgroundQuad.OnResetDevice()); D3DXMATRIXA16 matrix; D3DXMatrixIdentity(&g_BackgroundQuad.World); D3DXMatrixScaling(&matrix, (float)pBackBufferSurfaceDesc->Width, (float)pBackBufferSurfaceDesc->Height, 1.0f); D3DXMatrixMultiply(&g_BackgroundQuad.World, &g_BackgroundQuad.World, &matrix); D3DXMatrixTranslation(&matrix, (float)pBackBufferSurfaceDesc->Width / 2.0f, pBackBufferSurfaceDesc->Height / 2.0f, 0.0f); D3DXMatrixMultiply(&g_BackgroundQuad.World, &g_BackgroundQuad.World, &matrix); g_Board.SetPosition(D3DXVECTOR3(pBackBufferSurfaceDesc->Width / 2.0f, pBackBufferSurfaceDesc->Height / 2.0f, 0)); g_Board.SetSize(min(pBackBufferSurfaceDesc->Height, pBackBufferSurfaceDesc->Width) * 0.7f); g_WhiteArrowDialog.SetLocation(100, pBackBufferSurfaceDesc->Height / 2 - g_WhiteArrowDialog.GetHeight() / 2); g_BlackArrowDialog.SetLocation(-100 + pBackBufferSurfaceDesc->Width - g_BlackArrowDialog.GetWidth(), pBackBufferSurfaceDesc->Height / 2 - g_BlackArrowDialog.GetHeight() / 2); g_TheWhiteWinnerDialog.SetLocation(50, pBackBufferSurfaceDesc->Height - 100); g_TheBlackWinnerDialog.SetLocation(50, pBackBufferSurfaceDesc->Height - 100); float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams(D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f); g_Camera.SetWindow(pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height); g_HUD.SetLocation(pBackBufferSurfaceDesc->Width - 170, 0); g_HUD.SetSize(170, 170); g_SampleUI.SetLocation(pBackBufferSurfaceDesc->Width - 170, 0); g_SampleUI.SetSize(170, 300); return S_OK; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D9 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, 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_SettingsDlg.IsActive() ) { g_SettingsDlg.OnRender( fElapsedTime ); return; } // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) ); // set active device SCEMAN->SetActiveDevice( pd3dDevice ); SCEMAN->SetProjectionMatrix( g_Camera.GetProjMatrix() ); SCEMAN->SetViewMatrix( g_Camera.GetViewMatrix() ); // call update SCEMAN->Update( (float) fElapsedTime ); // update auto-params SHAMAN->UpdateAutoShaderParameters( (float) fElapsedTime ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { 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 ) ); SCEMAN->Render(); //PrintMatrix( &mWorld, L"World Mtx" ); //PrintMatrix( &mView, L"View Mtx" ); DXUT_EndPerfEvent(); V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that depend on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10SwapChainResized( ID3D10Device* pd3dDevice, IDXGISwapChain* pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr = S_OK; // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.05f, 500.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 hr; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D9 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { HRESULT hr; D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mWorldViewProjection; // 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; } // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { // Get the projection & view matrix from the camera class mWorld = *g_Camera.GetWorldMatrix(); mProj = *g_Camera.GetProjMatrix(); mView = *g_Camera.GetViewMatrix(); mWorldViewProjection = mWorld * mView * mProj; // 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 ) ); V( g_pEffect9->SetFloat( g_hfTime, ( float )fTime ) ); 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 ) ); DXUT_EndPerfEvent(); V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D11 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, 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; } auto pRTV = DXUTGetD3D11RenderTargetView(); pd3dImmediateContext->ClearRenderTargetView( pRTV, Colors::MidnightBlue ); // Clear the depth stencil 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 mView = g_Camera.GetViewMatrix(); XMMATRIX mProj = g_Camera.GetProjMatrix(); g_BatchEffect->SetWorld( mWorld ); g_BatchEffect->SetView( mView ); g_BatchEffect->SetProjection( mProj ); // Draw objects RenderObjects(); // Render HUD DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText(); DXUT_EndPerfEvent(); static ULONGLONG timefirst = GetTickCount64(); if ( GetTickCount64() - timefirst > 5000 ) { OutputDebugString( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) ); OutputDebugString( L"\n" ); timefirst = GetTickCount64(); } }
void CALLBACK OnD3D11FrameRender(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext) { pd3dImmediateContext->ClearRenderTargetView(DXUTGetD3D11RenderTargetView(), Colors::DeepSkyBlue); pd3dImmediateContext->ClearDepthStencilView(DXUTGetD3D11DepthStencilView(), D3D11_CLEAR_DEPTH, 1.0f, 0); CONSTANTBUFFER cb; ZeroMemory(&cb, sizeof(CONSTANTBUFFER)); XMMATRIX mat = XMLoadFloat4x4(&(g_mesh->getWorld())); XMStoreFloat4x4(&cb.world, XMMatrixTranspose(mat)); //cb.world = g_mesh->getWorld(); XMStoreFloat4x4(&cb.view, XMMatrixTranspose(g_camera.GetViewMatrix())); XMStoreFloat4x4(&cb.projection, XMMatrixTranspose(g_camera.GetProjMatrix())); /* 1 0 0 0 Side (Right) 0 1 0 0 Up 0 0 1 0 Foward x y z 1 Position */ //1. XMMATRIX -> FLOAT4X4 XMVECTOR v = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f); XMVECTOR litpos = XMVector4Transform(v, g_matLight); litpos = XMVector4Normalize(litpos); XMStoreFloat4(&cb.litDir, litpos); cb.litCol = XMFLOAT4(0.7f, 0.7f, 0.6f, 1.0f); g_shader->RenderPrepare(&cb); g_mesh->Render(); // mat = XMLoadFloat4x4(&(g_meshLight->getWorld())); XMStoreFloat4x4(&cb.world, XMMatrixTranspose(mat)); g_shader->RenderPrepare(&cb); g_meshLight->Render(); }
//-------------------------------------------------------------------------------------- // 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 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_Mesh.RestoreDeviceObjects( pd3dDevice ); // Create a sprite to help batch calls when drawing many lines of text V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pTextSprite ) ); // Set effect variables as needed D3DXCOLOR colorMtrlDiffuse( 1.0f, 1.0f, 1.0f, 1.0f ); D3DXCOLOR colorMtrlAmbient( 0.35f, 0.35f, 0.35f, 0 ); g_pEffect->SetVector( "g_MaterialAmbientColor", ( D3DXVECTOR4* )&colorMtrlAmbient ); g_pEffect->SetVector( "g_MaterialDiffuseColor", ( D3DXVECTOR4* )&colorMtrlDiffuse ); // To read or write to D3DX effect variables we can use the string name // instead of using handles, however it improves perf to use handles since then // D3DX won't have to spend time doing string compares g_hTechniqueRenderScene = g_pEffect->GetTechniqueByName( "RenderScene" ); g_hTime = g_pEffect->GetParameterByName( NULL, "g_fTime" ); g_hWorld = g_pEffect->GetParameterByName( NULL, "g_mWorld" ); g_hWorldViewProjection = g_pEffect->GetParameterByName( NULL, "g_mWorldViewProjection" ); g_hMeshTexture = g_pEffect->GetParameterByName( NULL, "g_MeshTexture" ); // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f ); g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); return S_OK; }