//-------------------------------------------------------------------------------------- // 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() ); } }
//-------------------------------------------------------------------------------------- // 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() ); } }
//-------------------------------------------------------------------------------------- // 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 ); } }
// 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); }
//-------------------------------------------------------------------------------------- // 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() ); } }
//-------------------------------------------------------------------------------------- // 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() ); } }
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(); }
//-------------------------------------------------------------------------------------- // 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 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(); }
//-------------------------------------------------------------------------------------- // 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() ); } }
//-------------------------------------------------------------------------------------- // 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 ) { float ClearColor[4] = { 1.0f, 1.0f, 1.0f, 0.0f }; // R, G, B, A ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView(); pd3dDevice->ClearRenderTargetView( pRTV, ClearColor ); ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView(); pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 ); // 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; } // Set our render target since we can't present multisampled ref ID3D10RenderTargetView* pOrigRT; ID3D10DepthStencilView* pOrigDS; pd3dDevice->OMGetRenderTargets( 1, &pOrigRT, &pOrigDS ); ID3D10RenderTargetView* aRTViews[ 1 ] = { g_pRTRV }; pd3dDevice->OMSetRenderTargets( 1, aRTViews, g_pDSRV ); pd3dDevice->ClearRenderTargetView( g_pRTRV, ClearColor ); pd3dDevice->ClearDepthStencilView( g_pDSRV, D3D10_CLEAR_DEPTH, 1.0, 0 ); // Set Matrices D3DXMATRIX mWorldViewProj; D3DXMATRIX mWorldView; D3DXMATRIX mViewProj; D3DXMATRIX mWorld; D3DXMATRIX mView; D3DXMATRIX mProj; // Fix the camera motion for now D3DXMATRIX mBlurViewProj[MAX_TIME_STEPS]; for( int i = 0; i < MAX_TIME_STEPS; i++ ) { mView = *g_Camera.GetViewMatrix(); mProj = *g_Camera.GetProjMatrix(); mBlurViewProj[i] = mView * mProj; } g_pmBlurViewProj->SetMatrixArray( ( float* )mBlurViewProj, 0, MAX_TIME_STEPS ); D3DXMatrixIdentity( &mWorld ); mView = *g_Camera.GetViewMatrix(); mProj = *g_Camera.GetProjMatrix(); mViewProj = mView * mProj; mWorldViewProj = mWorld * mViewProj; mWorldView = mWorld * mView; g_pmWorldViewProj->SetMatrix( ( float* )&mWorldViewProj ); g_pmViewProj->SetMatrix( ( float* )&mViewProj ); g_pmWorldView->SetMatrix( ( float* )&mWorldView ); RenderSceneMesh( pd3dDevice, &g_SceneMesh, false ); RenderFanMesh( pd3dDevice, &g_FanMesh, fTime, false ); if( g_bRenderOgre ) RenderSkinnedMesh( pd3dDevice, &g_AnimMesh, fTime, false ); for( UINT iMesh = 0; iMesh < g_NumLinkedMeshes; iMesh++ ) RenderLinkedMesh( pd3dDevice, &g_pLinkedMeshes[iMesh], &g_AnimMesh, g_MeshLinkages[iMesh].iBone, fTime, false ); if( g_bUseMotionBlur ) { RenderFanMesh( pd3dDevice, &g_FanMesh, fTime, true ); if( g_bRenderOgre ) RenderSkinnedMesh( pd3dDevice, &g_AnimMesh, fTime, true ); for( UINT iMesh = 0; iMesh < g_NumLinkedMeshes; iMesh++ ) RenderLinkedMesh( pd3dDevice, &g_pLinkedMeshes[iMesh], &g_AnimMesh, g_MeshLinkages[iMesh].iBone, fTime, true ); } //MSAA resolve ID3D10Resource* pRT; pOrigRT->GetResource( &pRT ); D3D10_RENDER_TARGET_VIEW_DESC rtDesc; pOrigRT->GetDesc( &rtDesc ); pd3dDevice->ResolveSubresource( pRT, D3D10CalcSubresource( 0, 0, 1 ), g_pRenderTarget, D3D10CalcSubresource( 0, 0, 1 ), rtDesc.Format ); SAFE_RELEASE( pRT ); // Use our Old RT again aRTViews[0] = pOrigRT; pd3dDevice->OMSetRenderTargets( 1, aRTViews, pOrigDS ); SAFE_RELEASE( pOrigRT ); SAFE_RELEASE( pOrigDS ); 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 ) { // 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 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(); } }
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); }
//-------------------------------------------------------------------------------------- // 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_D3DSettingsDlg.IsActive() ) { g_D3DSettingsDlg.OnRender( fElapsedTime ); return; } // // Clear the back buffer // float ClearColor[4] = { 0.0f, 0.125f, 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 ); // // Update variables that change once per frame // g_pProjectionVariable->SetMatrix( ( float* )g_Camera.GetProjMatrix() ); g_pViewVariable->SetMatrix( ( float* )g_Camera.GetViewMatrix() ); g_pWorldVariable->SetMatrix( ( float* )&g_World ); // // Set the Vertex Layout // pd3dDevice->IASetInputLayout( g_pVertexLayout ); // // Render the mesh // UINT Strides[1]; UINT Offsets[1]; ID3D10Buffer* pVB[1]; pVB[0] = g_Mesh.GetVB10( 0, 0 ); Strides[0] = ( UINT )g_Mesh.GetVertexStride( 0, 0 ); Offsets[0] = 0; pd3dDevice->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets ); pd3dDevice->IASetIndexBuffer( g_Mesh.GetIB10( 0 ), g_Mesh.GetIBFormat10( 0 ), 0 ); D3D10_TECHNIQUE_DESC techDesc; g_pTechnique->GetDesc( &techDesc ); SDKMESH_SUBSET* pSubset = NULL; ID3D10ShaderResourceView* pDiffuseRV = NULL; D3D10_PRIMITIVE_TOPOLOGY PrimType; for( UINT p = 0; p < techDesc.Passes; ++p ) { for( UINT subset = 0; subset < g_Mesh.GetNumSubsets( 0 ); ++subset ) { pSubset = g_Mesh.GetSubset( 0, subset ); PrimType = g_Mesh.GetPrimitiveType10( ( SDKMESH_PRIMITIVE_TYPE )pSubset->PrimitiveType ); pd3dDevice->IASetPrimitiveTopology( PrimType ); pDiffuseRV = g_Mesh.GetMaterial( pSubset->MaterialID )->pDiffuseRV10; g_ptxDiffuseVariable->SetResource( pDiffuseRV ); g_pTechnique->GetPassByIndex( p )->Apply( 0 ); pd3dDevice->DrawIndexed( ( UINT )pSubset->IndexCount, 0, ( UINT )pSubset->VertexStart ); } } //the mesh class also had a render method that allows rendering the mesh with the most common options //g_Mesh.Render( pd3dDevice, g_pTechnique, g_ptxDiffuseVariable ); // // Render the UI // g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText(); }
//-------------------------------------------------------------------------------------- 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); }
//-------------------------------------------------------------------------------------- // 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; } // // Clear the back buffer // auto pRTV = DXUTGetD3D11RenderTargetView(); pd3dImmediateContext->ClearRenderTargetView( pRTV, Colors::MidnightBlue ); // // Clear the depth stencil // auto pDSV = DXUTGetD3D11DepthStencilView(); pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 ); XMMATRIX mView = g_Camera.GetViewMatrix(); XMMATRIX mProj = g_Camera.GetProjMatrix(); XMMATRIX mWorldViewProjection = g_World * mView * mProj; // // Update variables that change once per frame // g_pProjectionVariable->SetMatrix( ( float* )&mProj ); g_pViewVariable->SetMatrix( ( float* )&mView ); g_pWorldVariable->SetMatrix( ( float* )&g_World ); g_pTimeVariable->SetFloat( ( float )fTime ); // // Set the Vertex Layout // pd3dImmediateContext->IASetInputLayout( g_pVertexLayout ); // // Render the mesh // UINT Strides[1]; UINT Offsets[1]; ID3D11Buffer* pVB[1]; pVB[0] = g_Mesh.GetVB11( 0, 0 ); Strides[0] = ( UINT )g_Mesh.GetVertexStride( 0, 0 ); Offsets[0] = 0; pd3dImmediateContext->IASetVertexBuffers( 0, 1, pVB, Strides, Offsets ); pd3dImmediateContext->IASetIndexBuffer( g_Mesh.GetIB11( 0 ), g_Mesh.GetIBFormat11( 0 ), 0 ); D3DX11_TECHNIQUE_DESC techDesc; HRESULT hr; V( g_pTechnique->GetDesc( &techDesc ) ); for( UINT p = 0; p < techDesc.Passes; ++p ) { for( UINT subset = 0; subset < g_Mesh.GetNumSubsets( 0 ); ++subset ) { auto pSubset = g_Mesh.GetSubset( 0, subset ); auto PrimType = g_Mesh.GetPrimitiveType11( ( SDKMESH_PRIMITIVE_TYPE )pSubset->PrimitiveType ); pd3dImmediateContext->IASetPrimitiveTopology( PrimType ); auto pDiffuseRV = g_Mesh.GetMaterial( pSubset->MaterialID )->pDiffuseRV11; g_ptxDiffuseVariable->SetResource( pDiffuseRV ); g_pTechnique->GetPassByIndex( p )->Apply( 0, pd3dImmediateContext ); pd3dImmediateContext->DrawIndexed( ( UINT )pSubset->IndexCount, 0, ( UINT )pSubset->VertexStart ); } } // // Render the UI // g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText(); }
//-------------------------------------------------------------------------------------- // 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; } // WVP XMMATRIX mProj = g_Camera.GetProjMatrix(); XMMATRIX mView = g_Camera.GetViewMatrix(); XMMATRIX mViewProjection = mView * mProj; // Update per-frame variables D3D11_MAPPED_SUBRESOURCE MappedResource; pd3dImmediateContext->Map( g_pcbPerFrame, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ); auto pData = reinterpret_cast<CB_PER_FRAME_CONSTANTS*>( MappedResource.pData ); XMStoreFloat4x4( &pData->mViewProjection, XMMatrixTranspose( mViewProjection ) ); XMStoreFloat3( &pData->vCameraPosWorld, g_Camera.GetEyePt() ); pData->fTessellationFactor = (float)g_fSubdivs; pd3dImmediateContext->Unmap( g_pcbPerFrame, 0 ); // Clear the render target and depth stencil auto pRTV = DXUTGetD3D11RenderTargetView(); pd3dImmediateContext->ClearRenderTargetView( pRTV, Colors::Black ); auto pDSV = DXUTGetD3D11DepthStencilView(); pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 ); // Set state for solid rendering pd3dImmediateContext->RSSetState( g_pRasterizerStateSolid ); // Render the meshes // Bind all of the CBs pd3dImmediateContext->VSSetConstantBuffers( g_iBindPerFrame, 1, &g_pcbPerFrame ); pd3dImmediateContext->HSSetConstantBuffers( g_iBindPerFrame, 1, &g_pcbPerFrame ); pd3dImmediateContext->DSSetConstantBuffers( g_iBindPerFrame, 1, &g_pcbPerFrame ); pd3dImmediateContext->PSSetConstantBuffers( g_iBindPerFrame, 1, &g_pcbPerFrame ); // Set the shaders pd3dImmediateContext->VSSetShader( g_pVertexShader, nullptr, 0 ); // For this sample, choose either the "integer", "fractional_even", // or "fractional_odd" hull shader if (g_iPartitionMode == PARTITION_INTEGER) pd3dImmediateContext->HSSetShader( g_pHullShaderInteger, nullptr, 0 ); else if (g_iPartitionMode == PARTITION_FRACTIONAL_EVEN) pd3dImmediateContext->HSSetShader( g_pHullShaderFracEven, nullptr, 0 ); else if (g_iPartitionMode == PARTITION_FRACTIONAL_ODD) pd3dImmediateContext->HSSetShader( g_pHullShaderFracOdd, nullptr, 0 ); pd3dImmediateContext->DSSetShader( g_pDomainShader, nullptr, 0 ); pd3dImmediateContext->GSSetShader( nullptr, nullptr, 0 ); pd3dImmediateContext->PSSetShader( g_pPixelShader, nullptr, 0 ); // Optionally draw the wireframe if( g_bDrawWires ) { pd3dImmediateContext->PSSetShader( g_pSolidColorPS, nullptr, 0 ); pd3dImmediateContext->RSSetState( g_pRasterizerStateWireframe ); } // Set the input assembler // This sample uses patches with 16 control points each // Although the Mobius strip only needs to use a vertex buffer, // you can use an index buffer as well by calling IASetIndexBuffer(). pd3dImmediateContext->IASetInputLayout( g_pPatchLayout ); UINT Stride = sizeof( BEZIER_CONTROL_POINT ); UINT Offset = 0; pd3dImmediateContext->IASetVertexBuffers( 0, 1, &g_pControlPointVB, &Stride, &Offset ); pd3dImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_16_CONTROL_POINT_PATCHLIST ); // Draw the mesh pd3dImmediateContext->Draw( ARRAYSIZE(g_MobiusStrip), 0 ); pd3dImmediateContext->RSSetState( g_pRasterizerStateSolid ); // Render the HUD DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); RenderText(); DXUT_EndPerfEvent(); }
//-------------------------------------------------------------------------------------- //실제 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() ); } }
//-------------------------------------------------------------------------------------- // 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(); } }
//-------------------------------------------------------------------------------------- // 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(); }
//-------------------------------------------------------------------------------------- // 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() ); } }