//-------------------------------------------------------------------------------------- // 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() ); } }
//-------------------------------------------------------------------------------------- // 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 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); }
//-------------------------------------------------------------------------------------- // Draw the edges of the bounding box [-0.5;0.5]³ rotated with the cameras model tranformation. // (Drawn as line primitives using a DirectXTK primitive batch) //-------------------------------------------------------------------------------------- void DrawBoundingBox(ID3D11DeviceContext* pd3dImmediateContext) { // Setup position/color effect XMMATRIX worldMatrix = g_camera.GetWorldMatrix(); XMVECTOR red = Colors::Red, green = Colors::Green, blue = Colors::Blue; #if (defined RIGIDBODYCOLLISIONS) if (!SCM.showAdditionalProperties || !SCM.showUnitBox) { return; } worldMatrix = XMMatrixScaling(SCM.sceneScale, SCM.sceneScale, SCM.sceneScale) * worldMatrix; #endif g_pEffectPositionColor->SetWorld(worldMatrix); g_pEffectPositionColor->Apply(pd3dImmediateContext); pd3dImmediateContext->IASetInputLayout(g_pInputLayoutPositionColor); // Draw g_pPrimitiveBatchPositionColor->Begin(); // Lines in x direction (red color) for (int i=0; i<4; i++) { g_pPrimitiveBatchPositionColor->DrawLine( VertexPositionColor(XMVectorSet(-0.5f, (float)(i%2)-0.5f, (float)(i/2)-0.5f, 1), red), VertexPositionColor(XMVectorSet( 0.5f, (float)(i%2)-0.5f, (float)(i/2)-0.5f, 1), red) ); } // Lines in y direction for (int i=0; i<4; i++) { g_pPrimitiveBatchPositionColor->DrawLine( VertexPositionColor(XMVectorSet((float)(i%2)-0.5f, -0.5f, (float)(i/2)-0.5f, 1), green), VertexPositionColor(XMVectorSet((float)(i%2)-0.5f, 0.5f, (float)(i/2)-0.5f, 1), green) ); } // Lines in z direction for (int i=0; i<4; i++) { g_pPrimitiveBatchPositionColor->DrawLine( VertexPositionColor(XMVectorSet((float)(i%2)-0.5f, (float)(i/2)-0.5f, -0.5f, 1), blue), VertexPositionColor(XMVectorSet((float)(i%2)-0.5f, (float)(i/2)-0.5f, 0.5f, 1), blue) ); } g_pPrimitiveBatchPositionColor->End(); }
//-------------------------------------------------------------------------------------- // 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(); } }
//-------------------------------------------------------------------------------------- // 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_D3DSettingsDlg.IsActive()) { g_D3DSettingsDlg.OnRender(fElapsedTime); return; } // Get the projection & view matrix from the camera class XMMATRIX mWorld; XMMATRIX mView; XMMATRIX mProj; XMMATRIX mWorldViewProjection; mWorld = g_Camera.GetWorldMatrix(); mView = g_Camera.GetViewMatrix(); mProj = g_Camera.GetProjMatrix(); mWorldViewProjection = mWorld * mView * mProj; // Store off original render target and depth/stencil ID3D11RenderTargetView* pOrigRTV = NULL; ID3D11DepthStencilView* pOrigDSV = NULL; pd3dImmediateContext->OMGetRenderTargets(1, &pOrigRTV, &pOrigDSV); g_OIT.Render(pd3dImmediateContext, pd3dDevice, &g_Scene, mWorldViewProjection, pOrigRTV, pOrigDSV); pd3dImmediateContext->OMSetRenderTargets(1, &pOrigRTV, pOrigDSV); // Restore original render targets, and then draw UI SAFE_RELEASE(pOrigRTV); SAFE_RELEASE(pOrigDSV); DXUT_BeginPerfEvent(DXUT_PERFEVENTCOLOR, L"HUD / Stats"); g_HUD.OnRender(fElapsedTime); g_SampleUI.OnRender(fElapsedTime); RenderText(); DXUT_EndPerfEvent(); }
//-------------------------------------------------------------------------------------- // Handle updates to the scene. This is called regardless of which D3D API is used //-------------------------------------------------------------------------------------- void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext ) { D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mWorldViewProjection; // Update the camera's position based on user input g_Camera.FrameMove( fElapsedTime ); // Get the projection & view matrix from the camera class mWorld = g_mCenterWorld * *g_Camera.GetWorldMatrix(); mProj = *g_Camera.GetProjMatrix(); mView = *g_Camera.GetViewMatrix(); mWorldViewProjection = mWorld * mView * mProj; // Update the effect's variables g_pEffect->SetMatrix( g_hWorldViewProjection, &mWorldViewProjection ); g_pEffect->SetMatrix( g_hWorld, &mWorld ); g_pEffect->SetFloat( g_hTime, ( float )fTime ); }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D9 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { HRESULT hr = S_OK; // 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; } D3DXMATRIX mWorld = *g_Camera.GetWorldMatrix(); D3DXMATRIX mView = *g_Camera.GetViewMatrix(); D3DXMATRIX mProj = *g_Camera.GetProjMatrix(); D3DXMATRIX mWorldViewProjection = mWorld * mView * mProj; // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 160, 160, 250 ), 1.0f, 0 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { g_pEffect9->SetMatrix( g_hmWorld, &mWorld ); g_pEffect9->SetMatrix( g_hmWorldViewProjection, &mWorldViewProjection ); g_pEffect9->SetTexture( g_htxDiffuse, g_pTexture9 ); pd3dDevice->SetVertexDeclaration( g_pDecl9 ); g_Mesh.Render( pd3dDevice, g_pEffect9, g_hRenderScene ); 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() ); } }
//-------------------------------------------------------------------------------------- // Draw all boxes in the scene //-------------------------------------------------------------------------------------- void DrawAllRigidBoxes(ID3D11DeviceContext* pd3dImmediateContext) { XMMATRIX scaling = XMMatrixScaling(SCM.sceneScale, SCM.sceneScale, SCM.sceneScale); XMMATRIX objToWorld; XMMATRIX screenToObj; for (int i = SCM.sceneObjects.size() -1; i >= 0; i--) { Box* box = &SCM.sceneObjects[i]; objToWorld = box->getObjToWorldMatrix(SCM.getStepCount()); screenToObj = XMMatrixInverse(nullptr, objToWorld * g_camera.GetViewMatrix()); objToWorld *= scaling * g_camera.GetWorldMatrix(); // Setup position/normal/color effect g_pEffectPositionNormalColor->SetWorld(objToWorld); g_pEffectPositionNormalColor->SetEmissiveColor(Colors::Black); g_pEffectPositionNormalColor->SetDiffuseColor(0.8f * Colors::White); g_pEffectPositionNormalColor->SetSpecularColor(0.4f * Colors::White); g_pEffectPositionNormalColor->SetSpecularPower(1000); g_pEffectPositionNormalColor->Apply(pd3dImmediateContext); pd3dImmediateContext->IASetInputLayout(g_pInputLayoutPositionNormalColor); g_pPrimitiveBatchPositionNormalColor->Begin(); { DrawRigidBoxSolid(pd3dImmediateContext, box, objToWorld, screenToObj); if (SCM.enableInteraction && i == SCM.selectedBoxIndex) { DrawRigidBoxWireFrame(pd3dImmediateContext, box, objToWorld, screenToObj); } if (SCM.showMasses) { } } g_pPrimitiveBatchPositionNormalColor->End(); } }
// Draw several objects randomly positioned in [-0.5f;0.5]³ using DirectXTK geometric primitives. void DrawSomeRandomObjects(ID3D11DeviceContext* pd3dImmediateContext) { // Setup position/normal effect (constant variables) g_pEffectPositionNormal->SetEmissiveColor(Colors::Black); g_pEffectPositionNormal->SetSpecularColor(0.4f * Colors::White); g_pEffectPositionNormal->SetSpecularPower(100); std::mt19937 eng; std::uniform_real_distribution<float> randCol( 0.0f, 1.0f); std::uniform_real_distribution<float> randPos(-0.5f, 0.5f); for (int i=0; i<g_iNumSpheres; i++) { // Setup position/normal effect (per object variables) g_pEffectPositionNormal->SetDiffuseColor(0.6f * XMColorHSVToRGB(XMVectorSet(randCol(eng), 1, 1, 0))); XMMATRIX scale = XMMatrixScaling(g_fSphereSize, g_fSphereSize, g_fSphereSize); XMMATRIX trans = XMMatrixTranslation(randPos(eng),randPos(eng),randPos(eng)); g_pEffectPositionNormal->SetWorld(scale * trans * g_camera.GetWorldMatrix()); // Draw // NOTE: The following generates one draw call per object, so performance will be bad for n>>1000 or so g_pSphere->Draw(g_pEffectPositionNormal, g_pInputLayoutPositionNormal); } }
//-------------------------------------------------------------------------------------- // 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 ); 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_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 ); // Get VB and IB UINT offset = 0; UINT stride = g_Mesh.GetVertexStride( 0, 0 ); ID3D10Buffer* pVB = g_Mesh.GetVB10( 0, 0 ); ID3D10Buffer* pIB = g_Mesh.GetAdjIB10( 0 ); // Set Input Assembler params pd3dDevice->IASetInputLayout( g_pVertexLayout ); pd3dDevice->IASetIndexBuffer( pIB, g_Mesh.GetIBFormat10( 0 ), 0 ); pd3dDevice->IASetVertexBuffers( 0, 1, &pVB, &stride, &offset ); pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST_ADJ ); // Render using the technique g_pRenderTextured SDKMESH_SUBSET* pSubset = NULL; D3D10_TECHNIQUE_DESC techDesc; g_pRenderTextured->GetDesc( &techDesc ); for( UINT p = 0; p < techDesc.Passes; p++ ) { g_pRenderTextured->GetPassByIndex( p )->Apply( 0 ); for( UINT subset = 0; subset < g_Mesh.GetNumSubsets( 0 ); subset++ ) { pSubset = g_Mesh.GetSubset( 0, subset ); pd3dDevice->DrawIndexed( ( UINT )pSubset->IndexCount * 2, ( UINT )pSubset->IndexStart, ( UINT )pSubset->VertexStart ); } } // Render the chess piece just for show // Render using the technique g_pRenderPiece g_pRenderPiece->GetDesc( &techDesc ); for( UINT p = 0; p < techDesc.Passes; p++ ) { g_pRenderPiece->GetPassByIndex( p )->Apply( 0 ); for( UINT subset = 0; subset < g_Mesh.GetNumSubsets( 0 ); subset++ ) { pSubset = g_Mesh.GetSubset( 0, subset ); pd3dDevice->DrawIndexed( ( UINT )pSubset->IndexCount * 2, ( UINT )pSubset->IndexStart, ( UINT )pSubset->VertexStart ); } } }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D10 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10FrameRender( ID3D10Device* 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; } // // Clear the back buffer // float ClearColor[4] = { 0.3f, 0.3f, 0.3f, 1.0f } ; // red, green, blue, alpha ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView(); pd3dDevice->ClearRenderTargetView( pRTV, ClearColor ); // // Clear the depth stencil // ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView(); pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 ); HRESULT hr; D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mWorldViewProjection; // 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_pWorldViewProjection->SetMatrix( (float*)&mWorldViewProjection ) ); V( g_pWorld->SetMatrix( (float*)&mWorld ) ); V( g_pTime->SetFloat( (float)fTime ) ); V( g_pCameraPosition->SetFloatVector( (float*)g_Camera.GetEyePt() ) ); // // Set the Vertex Layout // pd3dDevice->IASetInputLayout( g_pVertexLayout ); UINT iCurSubset = ( UINT )( INT_PTR )g_SampleUI.GetComboBox( IDC_SUBSET )->GetSelectedData(); // // Render the mesh // if ( iCurSubset == -1 ) { for ( UINT iSubset = 0; iSubset < g_MeshLoader.GetNumSubsets(); ++iSubset ) { RenderSubset( iSubset ); } } else { RenderSubset( iCurSubset ); } DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); RenderText(); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); DXUT_EndPerfEvent(); }
//-------------------------------------------------------------------------------------- // 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() ); } }
//-------------------------------------------------------------------------------------- // 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 ); // Make sure we only grow the tree the first time we go to render. // If we recreated the same tree every frame, we would definitely waste time. static bool bFirst = true; if( bFirst ) { //----------------------------------------------------------------------------------------- // o/__ <-- BreakdancinBob NOTE: Give the grow algorithm access to the random buffer // | (\ //----------------------------------------------------------------------------------------- g_pRandomBuf->SetResource( g_pRandomBufferRV ); // Grow the branches one step at a time for( UINT i = 0; i < g_NumGrowthSpurts; i++ ) { GrowBranches( pd3dDevice ); } bFirst = false; } // Render the result D3DXMATRIX mWorld; D3DXMATRIX mView; D3DXMATRIX mProj; D3DXMATRIX mWorldViewProj; D3DXMATRIX mWorldView; 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_pmWorld->SetMatrix( ( float* )&mWorld ); g_pmWorldView->SetMatrix( ( float* )&mWorldView ); 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 ID3D10Buffer* pBuffers[1]; pBuffers[0] = g_pDrawFrom; UINT stride = g_VertStride; UINT offset = 0; pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); pd3dDevice->IASetVertexBuffers( 0, 1, pBuffers, &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->DrawAuto(); } }
//-------------------------------------------------------------------------------------- // Renders the depth only shadow map //-------------------------------------------------------------------------------------- void RenderShadowMap( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, D3DXMATRIXA16& mViewProjLight, D3DXVECTOR3& vLightDir ) { D3D11_RECT oldrects[1]; D3D11_VIEWPORT oldvp[2]; UINT num = 1; pd3dImmediateContext->RSGetScissorRects( &num, oldrects ); num = 1; pd3dImmediateContext->RSGetViewports( &num, oldvp ); oldvp[ 1 ] = oldvp[ 0 ]; D3D11_RECT rects[1] = { { 0, UINT(g_fShadowMapWidth), 0, UINT(g_fShadowMapHeight) } }; pd3dImmediateContext->RSSetScissorRects( 1, rects ); D3D11_VIEWPORT vp[1] = { { 0, 0, g_fShadowMapWidth, g_fShadowMapHeight, 0.0f, 1.0f } }; pd3dImmediateContext->RSSetViewports( 1, vp ); // Set our scene render target & keep original depth buffer ID3D11RenderTargetView* pRTVs[2] = {0,0}; pd3dImmediateContext->OMSetRenderTargets( 2, pRTVs, g_pDepthStencilTextureDSV ); // Clear the render target pd3dImmediateContext->ClearDepthStencilView( g_pDepthStencilTextureDSV, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0, 0 ); // Get the projection & view matrix from the camera class D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXVECTOR3 up(0,1,0); D3DXVECTOR4 vLight( 0.0f,0.0f,0.0f, 1.0f ); D3DXVECTOR4 vLightLookAt4d( 0.0f,-0.5f,1.0f, 0.0f ); vLightLookAt4d = vLight + vLightLookAt4d; D3DXVec4Transform( &vLight, &vLight, g_LCamera.GetWorldMatrix() ); D3DXVec4Transform( &vLightLookAt4d, &vLightLookAt4d, g_LCamera.GetWorldMatrix() ); D3DXMatrixOrthoOffCenterLH( &mProj, -8.5, 9, -15, 11, -20, 20 ); D3DXVECTOR3 vLight3d( vLight.x, vLight.y, vLight.z ); D3DXVECTOR3 vLightLookAt3d( vLightLookAt4d.x, vLightLookAt4d.y, vLightLookAt4d.z ); vLightDir = vLightLookAt3d - vLight3d; D3DXMatrixLookAtLH( &mView, &vLight3d, &vLightLookAt3d, &up); mViewProjLight = mView * mProj; // Setup the constant buffer for the scene vertex shader D3D11_MAPPED_SUBRESOURCE MappedResource; pd3dImmediateContext->Map( g_pcbConstants, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ); CB_CONSTANTS* pConstants = ( CB_CONSTANTS* )MappedResource.pData; D3DXMatrixTranspose( &pConstants->f4x4WorldViewProjection, &mViewProjLight ); D3DXMatrixTranspose( &pConstants->f4x4WorldViewProjLight, &mViewProjLight ); pConstants->vShadowMapDimensions = D3DXVECTOR4(g_fShadowMapWidth, g_fShadowMapHeight, 1.0f/g_fShadowMapWidth, 1.0f/g_fShadowMapHeight); pd3dImmediateContext->Unmap( g_pcbConstants, 0 ); pd3dImmediateContext->VSSetConstantBuffers( g_iCONSTANTSCBBind, 1, &g_pcbConstants ); pd3dImmediateContext->PSSetConstantBuffers( g_iCONSTANTSCBBind, 1, &g_pcbConstants ); // Set the shaders pd3dImmediateContext->VSSetShader( g_pSM_VS, NULL, 0 ); pd3dImmediateContext->PSSetShader( NULL, NULL, 0 ); // Set the vertex buffer format pd3dImmediateContext->IASetInputLayout( g_pSceneVertexLayout ); // Render the scene g_Poles.Render( pd3dImmediateContext, 0 ); // reset the old viewport etc. pd3dImmediateContext->RSSetScissorRects( 1, oldrects ); pd3dImmediateContext->RSSetViewports( 1, oldvp ); }
//-------------------------------------------------------------------------------------- //실제 render가 발생하는 함수 //fTime, fElapsedTime은 윈도우 콜백이 알아서 만들어서 인자 전달 //-------------------------------------------------------------------------------------- void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { //렌더 필요 변수 HRESULT hr; D3DXMATRIXA16 mWorldViewProjection; D3DXVECTOR3 vLightDir[MAX_LIGHTS]; D3DXCOLOR vLightDiffuse[MAX_LIGHTS]; UINT iPass, cPasses; D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; //디바이스 초기화 V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR( 0.0f, 0.25f, 0.25f, 0.55f ), 1.0f, 0 ) ); //씬 그리기 시작 if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { //카메라의 현재 매트릭스 값을 받아서 월드 매트릭스에 곱한 결과 값 mWorld = g_mCenterWorld * (*g_Camera.GetWorldMatrix()); //프로젝션 매트릭스 설정 mProj = *g_Camera.GetProjMatrix(); //뷰 매트릭스 설정 mView = *g_Camera.GetViewMatrix(); //월드 매트릭스에 *뷰 * 프로젝션 매트릭스 적용 mWorldViewProjection = mWorld * mView * mProj; //라이트 적용 for( int i = 0; i < g_nNumActiveLights; i++ ) { vLightDir[i] = g_LightControl[i].GetLightDirection(); vLightDiffuse[i] = g_fLightScale * D3DXCOLOR( 1, 1, 1, 1 ); } //shader에 조명 관련 정보 binding V( g_pEffect->SetValue( "g_LightDir", vLightDir, sizeof( D3DXVECTOR3 ) * MAX_LIGHTS ) ); V( g_pEffect->SetValue( "g_LightDiffuse", vLightDiffuse, sizeof( D3DXVECTOR4 ) * MAX_LIGHTS ) ); //shader에 매트릭스 관련 정보 binding V( g_pEffect->SetMatrix( "g_mWorldViewProjection", &mWorldViewProjection ) ); V( g_pEffect->SetMatrix( "g_mWorld", &mWorld ) ); //shader에 재질 속성 값 관련 정보 binding D3DXCOLOR vWhite = D3DXCOLOR( 1, 1, 1, 1 ); V( g_pEffect->SetValue( "g_MaterialDiffuseColor", &vWhite, sizeof( D3DXCOLOR ) ) ); //shader에 time 값 전달 V( g_pEffect->SetFloat( "g_fTime", ( float )fTime ) ); //shader에 활성 조명 값 V( g_pEffect->SetInt( "g_nNumLights", g_nNumActiveLights ) ); switch( g_nNumActiveLights ) { case 1: V( g_pEffect->SetTechnique( "RenderSceneWithTexture1Light" ) ); break; case 2: V( g_pEffect->SetTechnique( "RenderSceneWithTexture2Light" ) ); break; case 3: V( g_pEffect->SetTechnique( "RenderSceneWithTexture3Light" ) ); break; } //shader 시작 V( g_pEffect->Begin( &cPasses, 0 ) ); //pass는 그리는 개수를 의미 //그리는 개수 만큼 pass 순환 for( iPass = 0; iPass < cPasses; iPass++ ) { V( g_pEffect->BeginPass( iPass ) ); // Render the mesh with the applied technique V( g_pMesh->DrawSubset( 0 ) ); V( g_pEffect->EndPass() ); } V( g_pEffect->End() ); //씬 그리기 종료 V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- void RenderAOFromMesh(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, ID3D11RenderTargetView* pBackBufferRTV, SceneMesh *pMesh) { UINT SampleCount = g_MSAADesc[g_MSAACurrentSettings].SampleCount; SceneViewInfo ViewInfo; pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); pd3dImmediateContext->RSSetViewports(1, &g_FullViewport); //-------------------------------------------------------------------------------------- // Clear render target and depth buffer //-------------------------------------------------------------------------------------- float BgColor[4] = { 1.0f, 1.0f, 1.0f }; pd3dImmediateContext->ClearRenderTargetView(g_ColorRTV, BgColor); pd3dImmediateContext->ClearDepthStencilView(g_DepthStencilDSV, D3D11_CLEAR_DEPTH, 1.0, 0); //-------------------------------------------------------------------------------------- // Render color and depth with the Scene3D class //-------------------------------------------------------------------------------------- pd3dImmediateContext->OMSetRenderTargets(1, &g_ColorRTV, g_DepthStencilDSV); if (g_UseOrbitalCamera) { ViewInfo.WorldViewMatrix = (*g_OrbitalCamera.GetWorldMatrix()) * (*g_OrbitalCamera.GetViewMatrix()); ViewInfo.ProjectionMatrix = *g_OrbitalCamera.GetProjMatrix(); } else { D3DXMATRIX WorldMatrix; D3DXMatrixRotationX(&WorldMatrix, -D3DX_PI * 0.5f); ViewInfo.WorldViewMatrix = WorldMatrix * (*g_FirstPersonCamera.GetViewMatrix()); ViewInfo.ProjectionMatrix = *g_FirstPersonCamera.GetProjMatrix(); } g_pSceneRenderer.OnFrameRender(&ViewInfo, pMesh); //-------------------------------------------------------------------------------------- // Render the SSAO //-------------------------------------------------------------------------------------- GFSDK_SSAO_InputDepthData InputDepthData; InputDepthData.pFullResDepthTextureSRV = g_DepthStencilSRV; InputDepthData.pProjectionMatrix = (CONST FLOAT*)ViewInfo.ProjectionMatrix; InputDepthData.ProjectionMatrixLayout = GFSDK_SSAO_ROW_MAJOR_ORDER; InputDepthData.MetersToViewSpaceUnits = pMesh->GetSceneScale(); GFSDK_SSAO_Status status; status = g_AORenderer.RenderAO(pd3dImmediateContext, &InputDepthData, &g_AOParams, g_ColorRTV); assert(status == GFSDK_SSAO_OK); //-------------------------------------------------------------------------------------- // Copy/resolve colors to the 1xAA backbuffer //-------------------------------------------------------------------------------------- pd3dImmediateContext->OMSetRenderTargets(1, &pBackBufferRTV, NULL); ID3D11Texture2D* pBackBufferTexture; pBackBufferRTV->GetResource((ID3D11Resource**)&pBackBufferTexture); if (SampleCount > 1) { pd3dImmediateContext->ResolveSubresource(pBackBufferTexture, 0, g_ColorTexture, 0, DXGI_FORMAT_R8G8B8A8_UNORM); } else { pd3dImmediateContext->CopyResource(pBackBufferTexture, g_ColorTexture); } SAFE_RELEASE(pBackBufferTexture); }
//-------------------------------------------------------------------------------------- // 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 mWorldViewProjection; D3DXVECTOR3 vLightDir[MAX_LIGHTS]; D3DXCOLOR vLightDiffuse[MAX_LIGHTS]; 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 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { // Get the projection & view matrix from the camera class mWorld = g_mCenterWorld * *g_Camera.GetWorldMatrix(); mProj = *g_Camera.GetProjMatrix(); mView = *g_Camera.GetViewMatrix(); mWorldViewProjection = mWorld * mView * mProj; // Render the light arrow so the user can visually see the light dir for( int i = 0; i < g_nNumActiveLights; i++ ) { D3DXCOLOR arrowColor = ( i == g_nActiveLight ) ? D3DXCOLOR( 1, 1, 0, 1 ) : D3DXCOLOR( 1, 1, 1, 1 ); V( g_LightControl[i].OnRender9( arrowColor, &mView, &mProj, g_Camera.GetEyePt() ) ); vLightDir[i] = g_LightControl[i].GetLightDirection(); vLightDiffuse[i] = g_fLightScale * D3DXCOLOR( 1, 1, 1, 1 ); } V( g_pEffect->SetValue( "g_LightDir", vLightDir, sizeof( D3DXVECTOR3 ) * MAX_LIGHTS ) ); V( g_pEffect->SetValue( "g_LightDiffuse", vLightDiffuse, sizeof( D3DXVECTOR4 ) * MAX_LIGHTS ) ); // 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 ) ); D3DXCOLOR vWhite = D3DXCOLOR( 1, 1, 1, 1 ); V( g_pEffect->SetValue( "g_MaterialDiffuseColor", &vWhite, sizeof( D3DXCOLOR ) ) ); V( g_pEffect->SetFloat( "g_fTime", ( float )fTime ) ); V( g_pEffect->SetInt( "g_nNumLights", g_nNumActiveLights ) ); // Render the scene with this technique // as defined in the .fx file switch( g_nNumActiveLights ) { case 1: V( g_pEffect->SetTechnique( "RenderSceneWithTexture1Light" ) ); break; case 2: V( g_pEffect->SetTechnique( "RenderSceneWithTexture2Light" ) ); break; case 3: V( g_pEffect->SetTechnique( "RenderSceneWithTexture3Light" ) ); break; } // Apply the technique contained in the effect V( g_pEffect->Begin( &cPasses, 0 ) ); for( iPass = 0; iPass < cPasses; iPass++ ) { V( g_pEffect->BeginPass( iPass ) ); // The effect interface queues up the changes and performs them // with the CommitChanges call. You do not need to call CommitChanges if // you are not setting any parameters between the BeginPass and EndPass. // V( g_pEffect->CommitChanges() ); // Render the mesh with the applied technique V( g_pMesh->DrawSubset( 0 ) ); V( g_pEffect->EndPass() ); } V( g_pEffect->End() ); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText( fTime ); V( pd3dDevice->EndScene() ); } }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D10 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10FrameRender( ID3D10Device* 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; } // // Clear the back buffer // float ClearColor[4] = { .0f, 0.0f, 0.0f, 1.0f } ; // red, green, blue, alpha ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView(); pd3dDevice->ClearRenderTargetView( pRTV, ClearColor ); // // Clear the depth stencil // ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView(); pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 ); // // Update the Cull Mode (non-FX method) setup the render state // pd3dDevice->RSSetState( g_pRasterStates[ 0 ] ); HRESULT hr; D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mWorldViewProjection; // 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_pWorldViewProjection->SetMatrix( (float*)&mWorldViewProjection ) ); V( g_pWorld->SetMatrix( (float*)&mWorld ) ); V( g_pTime->SetFloat( (float)fTime ) ); V( g_pCameraPosition->SetFloatVector( (float*)g_Camera.GetEyePt() ) ); // Blend pd3dDevice->OMSetDepthStencilState(0, 0); float blendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f}; pd3dDevice->OMSetBlendState(0, blendFactor, 0xffffffff); // // Set the Vertex Layout // pd3dDevice->IASetInputLayout( g_pVertexLayout ); UINT iCurSubset = ( UINT )( INT_PTR )g_SampleUI.GetComboBox( IDC_SUBSET )->GetSelectedData(); // // Render the mesh // // Stop render the loader mesh bool drawLoaderMesh = true; if (drawLoaderMesh) { if ( iCurSubset == -1 ) { for ( UINT iSubset = 0; iSubset < g_MeshLoader.GetNumSubsets(); ++iSubset ) { RenderSubset( iSubset ); } } else { RenderSubset( iCurSubset ); } } // // Render the Cube // //UINT cubeAttributCount; // This is not efficiency this procedure will always call to the GPU // To get the number of suset. //g_TrialCube->GetAttributeTable(NULL, &cubeAttributCount); //for (UINT iSubset = 0; iSubset < cubeAttributCount; iSubset++) //{ // g_TrialCube->DrawSubset(iSubset); //} //g_TrialCube->DrawSubset(0); pd3dDevice->RSSetState( g_pRasterStates[ g_eSceneRasterizerMode ] ); pd3dDevice->OMSetBlendState(g_TransparentBS, blendFactor, 0xffffffff); //g_pBox->draw(); bool drawAABBLevel=false; if (drawAABBLevel){ g_AABBConstructor->DrawLevel(g_CurrentAABBLevel); } // g_FunctionDraw->DrawFunction(); g_ParabolaDis->Draw(); //g_UVFileDisp->Draw(); //g_AABBConstructor->DrawAllAABBDetial(); // // Reset our Cull Mode (non-FX method) // pd3dDevice->RSSetState( g_pRasterStates[ 0 ] ); DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); RenderText(); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); DXUT_EndPerfEvent(); }
//-------------------------------------------------------------------------------------- // 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(); XMMATRIX mWorldViewProjection = mWorld * mView * mProj; // Set the constant buffers HRESULT hr; D3D11_MAPPED_SUBRESOURCE MappedResource; V( pd3dImmediateContext->Map( g_pcbVSPerFrame11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) ); auto pVSPerFrame = reinterpret_cast<CB_VS_PER_FRAME*>( MappedResource.pData ); pVSPerFrame->m_vLightDir = XMFLOAT3( 0,0.707f,-0.707f ); pVSPerFrame->m_fTime = (float)fTime; pVSPerFrame->m_LightDiffuse = XMFLOAT4( 1.f, 1.f, 1.f, 1.f ); pd3dImmediateContext->Unmap( g_pcbVSPerFrame11, 0 ); pd3dImmediateContext->VSSetConstantBuffers( 1, 1, &g_pcbVSPerFrame11 ); V( pd3dImmediateContext->Map( g_pcbVSPerObject11, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ) ); auto pVSPerObject = reinterpret_cast<CB_VS_PER_OBJECT*>( MappedResource.pData ); XMStoreFloat4x4( &pVSPerObject->m_mWorldViewProjection, XMMatrixTranspose( mWorldViewProjection ) ); XMStoreFloat4x4( &pVSPerObject->m_mWorld, XMMatrixTranspose( mWorld ) ); pVSPerObject->m_MaterialAmbientColor = XMFLOAT4( 0.3f, 0.3f, 0.3f, 1.0f ); pVSPerObject->m_MaterialDiffuseColor = XMFLOAT4( 0.7f, 0.7f, 0.7f, 1.0f ); pd3dImmediateContext->Unmap( g_pcbVSPerObject11, 0 ); pd3dImmediateContext->VSSetConstantBuffers( 0, 1, &g_pcbVSPerObject11 ); // Set render resources pd3dImmediateContext->IASetInputLayout( g_pLayout11 ); pd3dImmediateContext->VSSetShader( g_pVertexShader11, nullptr, 0 ); pd3dImmediateContext->PSSetShader( g_pPixelShader11, nullptr, 0 ); pd3dImmediateContext->PSSetSamplers( 0, 1, &g_pSamLinear ); // Render objects here... 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(); } }
//-------------------------------------------------------------------------------------- // 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(); }
void CALLBACK OnD3D11FrameRender(ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext) { pd3dImmediateContext->ClearRenderTargetView(DXUTGetD3D11RenderTargetView(), Colors::Black); pd3dImmediateContext->ClearDepthStencilView(DXUTGetD3D11DepthStencilView(), D3D11_CLEAR_DEPTH, 1.0, 0); XMMATRIX mworld = g_camera.GetWorldMatrix(); XMMATRIX mview = g_camera.GetViewMatrix(); XMMATRIX mproj = g_camera.GetProjMatrix(); g_BatchEffect->SetView(mview); g_BatchEffect->SetProjection(mproj); // Draw Grids const XMVECTORF32 xaxis = { 50.f, 0.f, 0.f }; const XMVECTORF32 yaxis = { 0.f, 0.f, 50.f }; DrawGrid(xaxis, yaxis, g_XMZero, 50, 50, Colors::Azure); // Draw Font g_dwtext->Render(L"га╥╧юс = ", DXUTGetFPS(), 10, 10); XMVECTOR worldPos; XMVECTOR worldDir; //g_input->getMousePosWorld(worldPos, worldDir, mview, mproj, TRUE); g_input->Pick(worldPos, worldDir, mworld, mview, mproj); /* POSITION */ WCHAR strformat[256] = {}; XMFLOAT3 pos; XMStoreFloat3(&pos, worldPos); swprintf(strformat, L"pos = %f , %f , %f", pos.x, pos.y, pos.z); g_dwtext->Render(strformat, 0, 10, 30); /* DIRECTION */ XMFLOAT3 dir; XMStoreFloat3(&dir, worldDir); swprintf(strformat, L"dir = %f , %f , %f", dir.x, dir.y, dir.z); g_dwtext->Render(strformat, 0, 10, 50); /* CURSOR POSITION */ POINT pt = g_input->getMousePos(); swprintf(strformat, L"cursor = %d , %d", pt.x, pt.y ); g_dwtext->Render(strformat, 0, 10, 70); XMVECTOR eye = g_camera.GetEyePt(); XMFLOAT3 feye; XMStoreFloat3(&feye, eye); swprintf(strformat, L"eye = %f , %f , %f", feye.x, feye.y, feye.z); g_dwtext->Render(strformat, 0, 10, 90); XMVECTOR lookat = g_camera.GetLookAtPt(); XMFLOAT3 flookat; XMStoreFloat3(&flookat, lookat); swprintf(strformat, L"lookat = %f , %f , %f", flookat.x, flookat.y, flookat.z); g_dwtext->Render(strformat, 0, 10, 110); XMVECTOR axis1 = XMVectorSet( pos.x, pos.y, pos.z, 0.0f); XMVECTOR axis2 = XMVectorSet(dir.x, dir.y, dir.z, 0.0f); DrawCenterGrid(axis1, axis2); XMMATRIX wpos = XMMatrixTranslation(pos.x, pos.y, pos.z); g_ShapePos->Draw(wpos, mview, mproj, Colors::LawnGreen); XMMATRIX wdir = XMMatrixTranslation(dir.x, dir.y, dir.z); g_ShapeDir->Draw(wdir, mview, mproj, Colors::OrangeRed); }