//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { g_pd3dDevice = pd3dDevice; HRESULT hr; ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_D3DSettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); D3DXVECTOR3 vCenter( 0.25767413f, -28.503521f, 111.00689f); FLOAT fObjectRadius = 378.15607f; D3DXMatrixTranslation( &g_mCenterMesh, -vCenter.x, -vCenter.y, -vCenter.z ); D3DXMATRIXA16 m; D3DXMatrixRotationY( &m, D3DX_PI ); g_mCenterMesh *= m; D3DXMatrixRotationX( &m, D3DX_PI / 2.0f ); g_mCenterMesh *= m; // Compile the shaders to a model based on the feature level we acquired ID3DBlob* pVertexShaderBuffer = NULL; ID3DBlob* pGeometryShaderBuffer = NULL; ID3DBlob* pPixelShaderBuffer = NULL; switch( DXUTGetD3D11DeviceFeatureLevel() ) { case D3D_FEATURE_LEVEL_11_0: V_RETURN( CompileShaderFromFile( L"cloth_renderer_VS.hlsl", "VSMain", "vs_5_0" , &pVertexShaderBuffer ) ); V_RETURN( CompileShaderFromFile( L"cloth_renderer_PS.hlsl", "GSMain", "gs_5_0" , &pGeometryShaderBuffer ) ); V_RETURN( CompileShaderFromFile( L"cloth_renderer_PS.hlsl", "PSMain", "ps_5_0" , &pPixelShaderBuffer ) ); break; } // Create the shaders V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), NULL, &g_pVertexShader ) ); V_RETURN( pd3dDevice->CreateGeometryShader( pGeometryShaderBuffer->GetBufferPointer(), pGeometryShaderBuffer->GetBufferSize(), NULL, &g_pGeometryShader ) ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), NULL, &g_pPixelShader ) ); V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), &g_pVertexLayout11 ) ); SAFE_RELEASE( pVertexShaderBuffer ); SAFE_RELEASE( pPixelShaderBuffer ); SAFE_RELEASE( pGeometryShaderBuffer ); // Load the mesh V_RETURN( g_Mesh11.Create( pd3dDevice, L"tiny\\tiny.sdkmesh", true ) ); // Create a sampler state D3D11_SAMPLER_DESC SamDesc; SamDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; SamDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.MipLODBias = 0.0f; SamDesc.MaxAnisotropy = 1; SamDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; SamDesc.BorderColor[0] = SamDesc.BorderColor[1] = SamDesc.BorderColor[2] = SamDesc.BorderColor[3] = 0; SamDesc.MinLOD = 0; SamDesc.MaxLOD = D3D11_FLOAT32_MAX; V_RETURN( pd3dDevice->CreateSamplerState( &SamDesc, &g_pSamLinear ) ); // Setup constant buffers D3D11_BUFFER_DESC Desc; Desc.Usage = D3D11_USAGE_DYNAMIC; Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; Desc.MiscFlags = 0; Desc.ByteWidth = sizeof( CB_VS_PER_OBJECT ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &g_pcbVSPerObject ) ); Desc.ByteWidth = sizeof( CB_PS_PER_OBJECT ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &g_pcbPSPerObject ) ); Desc.ByteWidth = sizeof( CB_PS_PER_FRAME ); V_RETURN( pd3dDevice->CreateBuffer( &Desc, NULL, &g_pcbPSPerFrame ) ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 30.0f, 30.0f, -80.0f ); D3DXVECTOR3 vecAt ( 10.0f, 20.0f, -0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); cloths.resize(numFlags); for( int flagIndex = 0; flagIndex < numFlags; ++flagIndex ) { cloths[flagIndex].create_buffers(clothWidth, clothHeight); } initBullet(); std::wstring flagTexsName[] = { L"atiFlag.bmp", L"amdFlag.bmp", }; int numFlagTexs = 2; WCHAR flagTexs[2][MAX_PATH]; HRESULT res = DXUTFindDXSDKMediaFileCch(flagTexs[0],MAX_PATH, flagTexsName[0].c_str()); res = DXUTFindDXSDKMediaFileCch(flagTexs[1],MAX_PATH, flagTexsName[1].c_str()); for( int flagIndex = 0; flagIndex < numFlags; ++flagIndex ) { cloths[flagIndex].create_texture(flagTexs[flagIndex % numFlagTexs]); cloths[flagIndex].x_offset = 0; cloths[flagIndex].y_offset = 0; cloths[flagIndex].z_offset = 0; } my_capsule.create_buffers(50,40); my_capsule.create_texture(); //Turn off backface culling D3D11_RASTERIZER_DESC rsDesc; ZeroMemory(&rsDesc,sizeof(D3D11_RASTERIZER_DESC) ); rsDesc.CullMode = D3D11_CULL_NONE; rsDesc.FillMode = D3D11_FILL_SOLID; hr = pd3dDevice->CreateRasterizerState(&rsDesc, &g_pRasterizerState); rsDesc.FillMode = D3D11_FILL_WIREFRAME; hr = pd3dDevice->CreateRasterizerState(&rsDesc, &g_pRasterizerStateWF); SAFE_RELEASE(pd3dImmediateContext); return S_OK; }
//-------------------------------------------------------------------------------------- // Release D3D9 resources created in the OnD3D9ResetDevice callback //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9LostDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D9LostDevice(); g_SettingsDlg.OnD3D9LostDevice(); }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED) // and aren't tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); // Initialize the font V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont ) ); // Load the mesh V_RETURN( g_Mesh.Create( pd3dDevice, L"dwarf\\dwarf.x" ) ); // Find the mesh's center, then generate a centering matrix. IDirect3DVertexBuffer9* pVB = NULL; V_RETURN( g_Mesh.m_pMesh->GetVertexBuffer( &pVB ) ); void* pVertices = NULL; hr = pVB->Lock( 0, 0, &pVertices, 0 ); if( FAILED( hr ) ) { SAFE_RELEASE( pVB ); return hr; } hr = D3DXComputeBoundingSphere( ( D3DXVECTOR3* )pVertices, g_Mesh.m_pMesh->GetNumVertices(), D3DXGetFVFVertexSize( g_Mesh.m_pMesh->GetFVF() ), &g_vObjectCenter, &g_fObjectRadius ); pVB->Unlock(); SAFE_RELEASE( pVB ); if( FAILED( hr ) ) return hr; D3DXMatrixTranslation( &g_mCenterWorld, -g_vObjectCenter.x, -g_vObjectCenter.y, -g_vObjectCenter.z ); // Read the D3DX effect file TCHAR str[MAX_PATH]; hr = DXUTFindDXSDKMediaFileCch( str, MAX_PATH, TEXT( "CompiledEffect.fxo" ) ); if( FAILED( hr ) ) { MessageBox( DXUTGetHWND(), TEXT( "Could not locate \"CompiledEffect.fxo\".\n\n" ) TEXT( "This file is created as part of the project build process,\n" ) TEXT( "so the associated project must be compiled inside Visual\n" ) TEXT( "Studio before attempting to run this sample.\n\n" ) TEXT( "If receiving this error even after compiling the project,\n" ) TEXT( "it's likely there was a problem compiling the effect file.\n" ) TEXT( "Check the build log to verify the custom build step was\n" ) TEXT( "run and to look for possible fxc compile errors." ), TEXT( "File Not Found" ), MB_OK ); return E_FAIL; } // Since we are loading a binary file here and this effect has already been compiled, // you can not pass compiler flags here (for example to debug the shaders). // To debug the shaders, you must pass these flags to the compiler that generated the // binary (for example fxc.exe). In this sample, there are 2 extra Visual Studio configurations // called "Debug Shaders" and "Unicode Debug Shaders" that pass the debug shader flags to fxc.exe. V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, D3DXFX_NOT_CLONEABLE, NULL, &g_pEffect, NULL ) ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT) // or that are tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { #ifdef CONSOLE std::cout << "OnD3D9ResetDevice" << std::endl; #endif HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9ResetDevice() ); V_RETURN( g_SettingsDlg.OnD3D9ResetDevice() ); if( g_pFont9 ) V_RETURN( g_pFont9->OnResetDevice() ); if( g_pEffect9 ) V_RETURN( g_pEffect9->OnResetDevice() ); V_RETURN( D3DXCreateSprite( pd3dDevice, &g_pSprite9 ) ); g_pTxtHelper = new CDXUTTextHelper( g_pFont9, g_pSprite9, NULL, NULL, 15 ); g_HUD.SetLocation( pBackBufferSurfaceDesc->Width - 170, 0 ); g_HUD.SetSize( 170, 170 ); g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 350 ); g_SampleUI.SetSize( 170, 300 ); pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); //pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE ); pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE ); pd3dDevice->SetRenderState( D3DRS_LIGHTING, false ); pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x80808080 ); D3DLIGHT9 light; D3DXVECTOR3 vecLightDirUnnormalized( 10.0f, -10.0f, 10.0f ); ZeroMemory( &light, sizeof( D3DLIGHT9 ) ); light.Type = D3DLIGHT_DIRECTIONAL; light.Diffuse.r = 1.0f; light.Diffuse.g = 1.0f; light.Diffuse.b = 1.0f; D3DXVec3Normalize( ( D3DXVECTOR3* )&light.Direction, &vecLightDirUnnormalized ); light.Position.x = 10.0f; light.Position.y = -10.0f; light.Position.z = 10.0f; light.Range = 1000.0f; pd3dDevice->SetLight( 0, &light ); pd3dDevice->LightEnable( 0, TRUE ); // Set the transform matrices D3DXMATRIXA16 matWorld; D3DXMatrixIdentity( &matWorld ); pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld ); // Setup the camera with view & projection matrix D3DXVECTOR3 vecEye( 0.0f, 0.0f, -5.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, 0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 1.0f, 1000.0f ); return S_OK; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D10 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10FrameRender(ID3D10Device* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext) { HRESULT hr; g_device->ClearRenderTargetView(g_vsObj->m_TextureTV, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f)); g_device->ClearRenderTargetView(g_vsObj->m_diffuseTextureTV[0], D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f)); g_device->ClearRenderTargetView(g_vsObj->m_diffuseTextureTV[1], D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f)); g_device->ClearRenderTargetView(g_vsObj->m_otherTextureTV, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f)); g_device->ClearRenderTargetView(g_vsObj->m_distDirTextureTV, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f)); // 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; } if (g_Camera.m_nMouseWheelDelta && g_Camera.m_nZoomButtonMask == MOUSE_WHEEL) { g_vsObj->m_pan /= g_vsObj->m_scale; g_vsObj->m_scale += g_vsObj->m_scale * g_Camera.m_nMouseWheelDelta * 0.2; g_vsObj->m_pan *= g_vsObj->m_scale; g_Camera.m_nMouseWheelDelta = 0; g_vsObj->m_scale = max(0.01, g_vsObj->m_scale); } if ((!g_Camera.IsMouseRButtonDown()) && (g_mouseLButtonDown == true)) { g_mouseLButtonDown = false; } else if (g_Camera.IsMouseRButtonDown()) { g_mouseLButtonDown = true; } else { g_mouseLButtonDown = false; } if (g_Camera.IsBeingDragged()) { float ff = 1.0f; if (g_Camera.IsMouseRButtonDown()) { ff = 0.127f; } float xFac = ff * 4.0f / g_vsObj->m_sizeX; float yFac = ff * 4.0f / g_vsObj->m_sizeY; g_vsObj->m_pan += D3DXVECTOR2(xFac * g_Camera.m_vMouseDelta.x, -yFac * g_Camera.m_vMouseDelta.y); g_Camera.m_vMouseDelta.x = 0; g_Camera.m_vMouseDelta.y = 0; } //g_vsObj->m_polySize = 0.1; //render each polygon in full screen size g_vsObj->RenderDiffusion(pd3dDevice); g_vsObj->Render(pd3dDevice); if (g_showMenue) { DXUT_BeginPerfEvent(DXUT_PERFEVENTCOLOR, L"HUD / Stats"); g_HUD.OnRender(fElapsedTime); g_SampleUI.OnRender(fElapsedTime); RenderText(); DXUT_EndPerfEvent(); } }
static void InitApp() { g_pCamManager = new S3UTCameraManager(); g_pCamManager->ConfigCameras("Cameras.txt"); g_pCamManager->DumpCameraStatus("DumpResult.txt"); g_D3DSettingsDlg.Init( &g_DialogResourceManager ); g_HUD.Init( &g_DialogResourceManager ); g_SampleUI.Init( &g_DialogResourceManager ); g_HUD.SetCallback( OnGUIEvent ); int iY = 10; g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY, 125, 22, VK_F2 ); g_SampleUI.EnableKeyboardInput( true ); g_SampleUI.SetCallback( OnGUIEvent ); iY = 10; g_SampleUI.AddStatic( IDC_SHADOW_ALGORITHM_LABEL, L"Shadow algorithm:", 35, iY, 125, 22 ); CDXUTComboBox *pComboBox; g_SampleUI.AddComboBox( IDC_SHADOW_ALGORITHM, 35, iY += 20, 125, 30, 0, false, &pComboBox); pComboBox->AddItem(L"StandardBP", NULL); pComboBox->AddItem(L"BP_MSSM_KERNEL", NULL); pComboBox->AddItem(L"STD_VSM", NULL); pComboBox->AddItem(L"MipVSM", NULL); pComboBox->AddItem(L"HirBP", NULL); pComboBox->AddItem(L"BPGI", NULL); pComboBox->AddItem(L"NoShadows", NULL); pComboBox->AddItem(L"SingleLight", NULL); pComboBox->AddItem(L"PCSS", NULL); g_SampleUI.AddStatic( IDC_COMMON_LABEL, L"Light Zn", 35, iY += 25, 125, 22 ); g_SampleUI.AddSlider( IDC_LIGHT_ZN, 160, iY, 124, 22, 0, 100, g_fCtrledLightZn*5 ); g_SampleUI.AddStatic( IDC_COMMON_LABEL, L"Light Zf", 35, iY += 25, 125, 22 ); g_SampleUI.AddSlider( IDC_LIGHT_ZF, 160, iY, 124, 22, 0, 100, g_fCtrledLightZf - 10 ); g_SampleUI.AddStatic( IDC_COMMON_LABEL, L"Light FOV", 35, iY += 25, 125, 22 ); g_SampleUI.AddSlider( IDC_LIGHT_FOV, 160, iY, 124, 22, 0, 100, g_fCtrledLightFov * 100 ); g_SampleUI.AddStatic( IDC_LIGHT_SIZE_LABEL, L"Light source size:", 35, iY += 25, 125, 22 ); g_SampleUI.AddSlider( IDC_LIGHT_SIZE, 160, iY, 124, 22, 0, 100, 0 ); g_SampleUI.AddStatic(IDC_COMMON_LABEL, L"NumLightSample", 35, iY += 25, 125, 22 ); g_SampleUI.AddSlider(IDC_NUM_LIGHT_SAMPLE, 160, iY, 124, 22, 0, 16, 0 ); g_SampleUI.AddStatic(IDC_COMMON_LABEL, L"fDefaultDepthBias", 35, iY += 25, 125, 22 ); g_SampleUI.AddSlider(IDC_fDefaultDepthBias, 160, iY, 124, 22, 0, 100, 40 ); g_SampleUI.AddStatic(IDC_COMMON_LABEL, L"3rd depth delta", 35, iY += 25, 125, 22 ); g_SampleUI.AddSlider(IDC_BIAS_3RD_DEPTH, 160, iY, 124, 22, 0, 100, 40 ); g_SampleUI.AddStatic(IDC_COMMON_LABEL, L"1st depth delta", 35, iY += 25, 125, 22 ); g_SampleUI.AddSlider(IDC_BIAS_1ST_DEPTH, 160, iY, 124, 22, 0, 100, 40 ); g_SampleUI.AddCheckBox( IDC_BTEXTURED, L"Enable Texturing", 15, iY += 25, 124, 22, false); g_SampleUI.AddCheckBox( IDC_SHOW_3DWIDGET, L"Show 3D Widget", 150, iY, 124, 22, true); g_SampleUI.AddCheckBox( IDC_BMOVECAMERA, L"Move Camera", 15, iY += 25, 124, 22, true); g_SampleUI.AddCheckBox( IDC_FRAME_DUMP, L"Dump Frame", 150, iY, 124, 22, false); g_SampleUI.AddCheckBox( IDC_BDUMP_SHADOWMAP, L"Dump Shadow Map", 15, iY+=25, 124, 22, false); g_SampleUI.AddCheckBox( IDC_BDUMP_LIGHT_PAR, L"Dump Light Para", 150, iY, 124, 22, false); g_SampleUI.AddCheckBox( IDC_STATIC, L"Freeze Model", 15, iY += 25, 124, 22, false); g_SampleUI.AddCheckBox( IDC_ANIMATE, L"Show Animated Model", 150, iY, 124, 22, false); g_SampleUI.AddCheckBox( IDC_SCENE, L"Show scene", 15, iY += 25, 124, 22, true); g_SampleUI.AddCheckBox( IDC_FAN, L"Show Fan", 150, iY, 124, 22, false); g_CameraUI.Init( &g_DialogResourceManager ); g_CameraUI.EnableKeyboardInput( true ); g_CameraUI.SetCallback( OnGUIEvent ); { g_pCamManager->SetupCameraUI( g_CameraUI ); } }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10CreateDevice(ID3D10Device* pDev10, const DXGI_SURFACE_DESC *pBackBufferSurfaceDesc, void* pUserContext) { HRESULT hr; g_pSkyBox = new S3UTSkybox(); //g_pEnvMap = new HDRCubeTexture; V_RETURN(DXUTSetMediaSearchPath(L"..\\Source\\SoftShadows")); V_RETURN(g_DialogResourceManager.OnD3D10CreateDevice(pDev10)); V_RETURN(g_D3DSettingsDlg.OnD3D10CreateDevice(pDev10)); V_RETURN(D3DX10CreateFont(pDev10, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont10)); g_SampleUI.GetSlider(IDC_LIGHT_SIZE)->SetValue((int)(g_fFilterSize * 200.0)); g_SampleUI.GetComboBox(IDC_SHADOW_ALGORITHM)->SetSelectedByIndex(ssmap.bAccurateShadow == true ? 0 : 1); switch( ShadowAlgorithm ) { case STANDARD_BP: g_ABP.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); break; case BP_MSSM_KERNEL: g_BPMSSMKernel.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); break; case STD_VSM: g_StdVSM.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); break; case MIP_VSM: g_MipVSM.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); break; case HIR_BP: g_HBP.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); break; case BP_GI: g_BPGI.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); break; case STD_PCSS: g_PCSS.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); break; default: break; } g_NoShadow.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_Final.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_GBuffer.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_ScrQuadRender.OnD3D10CreateDevice(g_ABP.m_pEffect,pDev10,pBackBufferSurfaceDesc,pUserContext); ssmap.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_Widget.OnD3D10CreateDevice( pDev10,pBackBufferSurfaceDesc,pUserContext ); g_Blender.OnD3D10CreateDevice( pDev10,pBackBufferSurfaceDesc,pUserContext ); V_RETURN(D3DX10CreateSprite(pDev10, 512, &g_pSprite10)); {//must be after g_ABP create a device,because they uses the members of g_ABP static const D3D10_INPUT_ELEMENT_DESC scenemeshlayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; if (g_MeshLight.Create(pDev10, L"arrow.x", (D3D10_INPUT_ELEMENT_DESC*)scenemeshlayout, 3) != S_OK) { MessageBox(DXUTGetHWND(), L"Could not load geometry from arrow.x", L"Error", MB_OK); exit(0); } D3D10_PASS_DESC PassDesc; V_RETURN(g_NoShadow.m_pEffect->GetTechniqueByName("RenderAcc")->GetPassByIndex(0)->GetDesc(&PassDesc)); V_RETURN(pDev10->CreateInputLayout(scenemeshlayout, 3, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pMaxLayout)); } LoadNewModel(); //V_RETURN( DXUTFindDXSDKMediaFileCch( g_EnvMapFilePath, MAX_PATH_STR, g_DefaultEnvMapName[0] ) ); //g_pEnvMap->OnCreateDevice(pDev10, g_EnvMapFilePath, DXGI_FORMAT_R8G8B8A8_UNORM); g_pSkyBox->OnCreateDevice( pDev10 ); // g_pSkyBox->SetTexture( g_pEnvMap->m_TextureRV ); g_pFloatBufferSurfaceDesc.SampleDesc.Count = pBackBufferSurfaceDesc->SampleDesc.Count; g_pFloatBufferSurfaceDesc.SampleDesc.Quality = pBackBufferSurfaceDesc->SampleDesc.Quality; D3DXVECTOR3 vTmp = D3DXVECTOR3(1, 2, 3); D3DXVec3Normalize(&g_vLightDir, &vTmp); SAFE_RELEASE(g_pRenderState); D3D10_RASTERIZER_DESC RasterizerState; RasterizerState.FillMode = D3D10_FILL_SOLID; RasterizerState.CullMode = D3D10_CULL_FRONT; RasterizerState.FrontCounterClockwise = true; RasterizerState.DepthBias = false; RasterizerState.DepthBiasClamp = 0.1; RasterizerState.SlopeScaledDepthBias = 0; RasterizerState.DepthClipEnable = true; RasterizerState.ScissorEnable = false; RasterizerState.MultisampleEnable = false; RasterizerState.AntialiasedLineEnable = false; V(pDev10->CreateRasterizerState(&RasterizerState, &g_pRenderState)); SAFE_RELEASE(g_pDSState); D3D10_DEPTH_STENCIL_DESC DSState; ZeroMemory(&DSState, sizeof(DSState)); DSState.DepthEnable = true; DSState.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL; DSState.DepthFunc = D3D10_COMPARISON_LESS_EQUAL; V(pDev10->CreateDepthStencilState(&DSState, &g_pDSState)); //light management for( int light_idx = 0; light_idx < NUM_LIGHT; ++light_idx ) { g_pLightLumiBuffer[light_idx] = new RenderObject( "RenderScreenPixelPos" ); g_pLightLumiBuffer[light_idx] ->OnD3D10CreateDevice( NULL,pDev10, pBackBufferSurfaceDesc, pUserContext); } g_pBlendBuffer = new RenderObject( "RenderScreenPixelPos" ); g_pBlendBuffer->OnD3D10CreateDevice( NULL,pDev10, pBackBufferSurfaceDesc, pUserContext); g_pWidgetBuffer = new RenderObject( "RenderScreenPixelPos" );//fake tech name,we dont want to use it. We only want to utilize its buffers g_pWidgetBuffer->OnD3D10CreateDevice( NULL,pDev10, pBackBufferSurfaceDesc, pUserContext); //-------------------------------------------------------------------------------------------------------- return S_OK; }
// Create any D3D11 resources that aren't dependent on the back buffer HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN(gDialogResourceManager.OnD3D11CreateDevice(pd3dDevice, pd3dImmediateContext)); V_RETURN(gD3DSettingsDlg.OnD3D11CreateDevice(pd3dDevice)); gTxtHelper = new CDXUTTextHelper(pd3dDevice, pd3dImmediateContext, &gDialogResourceManager, 15); // Create a vertex shader. ID3DBlob* vertexShaderBuffer = NULL; V_RETURN(CompileShaderFromFile(L"shaders.hlsl", "PassThroughVS", "vs_4_0", &vertexShaderBuffer)); V_RETURN(pd3dDevice->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &gVertexShader)); // Create a pixel shader that renders the composite frame. ID3DBlob* pixelShaderBuffer = NULL; V_RETURN(CompileShaderFromFile(L"shaders.hlsl", "RenderFramePS", "ps_4_0", &pixelShaderBuffer)); V_RETURN(pd3dDevice->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &gRenderFramePS)); // Create a pixel shader that renders the error texture. V_RETURN(CompileShaderFromFile(L"shaders.hlsl", "RenderTexturePS", "ps_4_0", &pixelShaderBuffer)); V_RETURN(pd3dDevice->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &gRenderTexturePS)); // Create a pixel shader that shows alpha V_RETURN(CompileShaderFromFile(L"shaders.hlsl", "RenderAlphaPS", "ps_4_0", &pixelShaderBuffer)); V_RETURN(pd3dDevice->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &gRenderAlphaPS)); // Create our vertex input layout const D3D11_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; V_RETURN(pd3dDevice->CreateInputLayout(layout, ARRAYSIZE(layout), vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &gVertexLayout)); SAFE_RELEASE(vertexShaderBuffer); SAFE_RELEASE(pixelShaderBuffer); // Create a vertex buffer for three textured quads. D3DXVECTOR2 quadSize(0.32f, 0.32f); D3DXVECTOR2 quadOrigin(-0.66f, -0.0f); Vertex tripleQuadVertices[18]; ZeroMemory(tripleQuadVertices, sizeof(tripleQuadVertices)); for(int i = 0; i < 18; i += 6) { tripleQuadVertices[i].position = D3DXVECTOR3(quadOrigin.x - quadSize.x, quadOrigin.y + quadSize.y, 0.0f); tripleQuadVertices[i].texCoord = D3DXVECTOR2(0.0f, 0.0f); tripleQuadVertices[i + 1].position = D3DXVECTOR3(quadOrigin.x + quadSize.x, quadOrigin.y + quadSize.y, 0.0f); tripleQuadVertices[i + 1].texCoord = D3DXVECTOR2(1.0f, 0.0f); tripleQuadVertices[i + 2].position = D3DXVECTOR3(quadOrigin.x + quadSize.x, quadOrigin.y - quadSize.y, 0.0f); tripleQuadVertices[i + 2].texCoord = D3DXVECTOR2(1.0f, 1.0f); tripleQuadVertices[i + 3].position = D3DXVECTOR3(quadOrigin.x + quadSize.x, quadOrigin.y - quadSize.y, 0.0f); tripleQuadVertices[i + 3].texCoord = D3DXVECTOR2(1.0f, 1.0f); tripleQuadVertices[i + 4].position = D3DXVECTOR3(quadOrigin.x - quadSize.x, quadOrigin.y - quadSize.y, 0.0f); tripleQuadVertices[i + 4].texCoord = D3DXVECTOR2(0.0f, 1.0f); tripleQuadVertices[i + 5].position = D3DXVECTOR3(quadOrigin.x - quadSize.x, quadOrigin.y + quadSize.y, 0.0f); tripleQuadVertices[i + 5].texCoord = D3DXVECTOR2(0.0f, 0.0f); quadOrigin.x += 0.66f; } D3D11_BUFFER_DESC bufDesc; ZeroMemory(&bufDesc, sizeof(bufDesc)); bufDesc.Usage = D3D11_USAGE_DEFAULT; bufDesc.ByteWidth = sizeof(tripleQuadVertices); bufDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufDesc.CPUAccessFlags = 0; D3D11_SUBRESOURCE_DATA data; ZeroMemory(&data, sizeof(data)); data.pSysMem = tripleQuadVertices; V_RETURN(pd3dDevice->CreateBuffer(&bufDesc, &data, &gVertexBuffer)); // Create a vertex buffer for a single textured quad. quadSize = D3DXVECTOR2(1.0f, 1.0f); quadOrigin = D3DXVECTOR2(0.0f, 0.0f); Vertex singleQuadVertices[6]; singleQuadVertices[0].position = D3DXVECTOR3(quadOrigin.x - quadSize.x, quadOrigin.y + quadSize.y, 0.0f); singleQuadVertices[0].texCoord = D3DXVECTOR2(0.0f, 0.0f); singleQuadVertices[1].position = D3DXVECTOR3(quadOrigin.x + quadSize.x, quadOrigin.y + quadSize.y, 0.0f); singleQuadVertices[1].texCoord = D3DXVECTOR2(1.0f, 0.0f); singleQuadVertices[2].position = D3DXVECTOR3(quadOrigin.x + quadSize.x, quadOrigin.y - quadSize.y, 0.0f); singleQuadVertices[2].texCoord = D3DXVECTOR2(1.0f, 1.0f); singleQuadVertices[3].position = D3DXVECTOR3(quadOrigin.x + quadSize.x, quadOrigin.y - quadSize.y, 0.0f); singleQuadVertices[3].texCoord = D3DXVECTOR2(1.0f, 1.0f); singleQuadVertices[4].position = D3DXVECTOR3(quadOrigin.x - quadSize.x, quadOrigin.y - quadSize.y, 0.0f); singleQuadVertices[4].texCoord = D3DXVECTOR2(0.0f, 1.0f); singleQuadVertices[5].position = D3DXVECTOR3(quadOrigin.x - quadSize.x, quadOrigin.y + quadSize.y, 0.0f); singleQuadVertices[5].texCoord = D3DXVECTOR2(0.0f, 0.0f); ZeroMemory(&bufDesc, sizeof(bufDesc)); bufDesc.Usage = D3D11_USAGE_DEFAULT; bufDesc.ByteWidth = sizeof(singleQuadVertices); bufDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufDesc.CPUAccessFlags = 0; ZeroMemory(&data, sizeof(data)); data.pSysMem = singleQuadVertices; V_RETURN(pd3dDevice->CreateBuffer(&bufDesc, &data, &gQuadVB)); // Create a constant buffer ZeroMemory(&bufDesc, sizeof(bufDesc)); bufDesc.ByteWidth = sizeof( VS_CONSTANT_BUFFER ); bufDesc.Usage = D3D11_USAGE_DYNAMIC; bufDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; bufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; V_RETURN(pd3dDevice->CreateBuffer(&bufDesc, NULL, &gConstantBuffer)); // Create a sampler state D3D11_SAMPLER_DESC SamDesc; SamDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; SamDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; SamDesc.MipLODBias = 0.0f; SamDesc.MaxAnisotropy = 1; SamDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; SamDesc.BorderColor[0] = SamDesc.BorderColor[1] = SamDesc.BorderColor[2] = SamDesc.BorderColor[3] = 0; SamDesc.MinLOD = 0; SamDesc.MaxLOD = D3D11_FLOAT32_MAX; V_RETURN(pd3dDevice->CreateSamplerState(&SamDesc, &gSamPoint)); // Load and initialize the textures. WCHAR path[MAX_PATH]; V_RETURN(DXUTFindDXSDKMediaFileCch(path, MAX_PATH, L"Images\\Desk_21_hdr.dds")); V_RETURN(CreateTextures(path)); // Update the UI's texture width and height. gSampleUI.SendEvent(IDC_TEXT, true, gSampleUI.GetStatic(IDC_TEXT)); return S_OK; }
// Render the scene using the D3D11 device void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime, float fElapsedTime, void* pUserContext ) { // Recompress the texture gFrameDelay frames after the app has started. This produces more accurate timing of the // compression algorithm. if(gFrameNum == gFrameDelay) { RecompressTexture(); gSampleUI.SendEvent(IDC_TEXT, true, gSampleUI.GetStatic(IDC_TEXT)); gFrameNum++; } else if(gFrameNum < gFrameDelay) { gFrameNum++; } // If the settings dialog is being shown, then render it instead of rendering the app's scene if( gD3DSettingsDlg.IsActive() ) { gD3DSettingsDlg.OnRender( fElapsedTime ); return; } // Clear the render target and depth stencil float ClearColor[4] = { 0.02f, 0.02f, 0.02f, 1.0f }; ID3D11RenderTargetView* pRTV = DXUTGetD3D11RenderTargetView(); pd3dImmediateContext->ClearRenderTargetView( pRTV, ClearColor ); ID3D11DepthStencilView* pDSV = DXUTGetD3D11DepthStencilView(); pd3dImmediateContext->ClearDepthStencilView( pDSV, D3D11_CLEAR_DEPTH, 1.0, 0 ); // Set the input layout. pd3dImmediateContext->IASetInputLayout( gVertexLayout ); // Set the vertex buffer. UINT stride = sizeof( Vertex ); UINT offset = 0; if (gImageView == eImageView_All) { pd3dImmediateContext->IASetVertexBuffers( 0, 1, &gVertexBuffer, &stride, &offset ); } else { pd3dImmediateContext->IASetVertexBuffers( 0, 1, &gQuadVB, &stride, &offset ); } // Set the primitive topology pd3dImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); // Update the Constant Buffer D3D11_MAPPED_SUBRESOURCE MappedResource; pd3dImmediateContext->Map( gConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource ); VS_CONSTANT_BUFFER* pConstData = ( VS_CONSTANT_BUFFER* )MappedResource.pData; ZeroMemory(pConstData, sizeof(VS_CONSTANT_BUFFER)); SetView(&pConstData->mView); pConstData->exposure = powf(2.0, gLog2Exposure); pd3dImmediateContext->Unmap( gConstantBuffer, 0 ); // Set the shaders ID3D11Buffer* pBuffers[1] = { gConstantBuffer }; pd3dImmediateContext->VSSetConstantBuffers( 0, 1, pBuffers ); pd3dImmediateContext->VSSetShader( gVertexShader, NULL, 0 ); if (gSampleUI.GetCheckBox(IDC_ALPHA)->GetChecked()) { pd3dImmediateContext->PSSetShader( gRenderAlphaPS, NULL, 0 ); } else { pd3dImmediateContext->PSSetShader( gRenderFramePS, NULL, 0 ); } pd3dImmediateContext->PSSetConstantBuffers(0, 1, pBuffers ); // Set the texture sampler. pd3dImmediateContext->PSSetSamplers( 0, 1, &gSamPoint ); // Render the textures. if (gImageView == eImageView_Uncompressed || gImageView == eImageView_All ) { pd3dImmediateContext->PSSetShaderResources( 0, 1, &gUncompressedSRV ); } else if (gImageView == eImageView_Compressed) { pd3dImmediateContext->PSSetShaderResources( 0, 1, &gCompressedSRV ); } else if (gImageView == eImageView_Error) { pd3dImmediateContext->PSSetShaderResources( 0, 1, &gErrorSRV ); } pd3dImmediateContext->Draw( 6, 0 ); if (gImageView == eImageView_All) { pd3dImmediateContext->PSSetShaderResources( 0, 1, &gCompressedSRV ); pd3dImmediateContext->Draw( 6, 6 ); pd3dImmediateContext->PSSetShaderResources( 0, 1, &gErrorSRV ); pd3dImmediateContext->Draw( 6, 12 ); } DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); HRESULT hr; V(gHUD.OnRender( fElapsedTime )); V(gSampleUI.OnRender( fElapsedTime )); RenderText(); DXUT_EndPerfEvent(); }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr = S_OK; auto pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_SettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _DEBUG // Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DCOMPILE_DEBUG; // Disable optimizations to further improve shader debugging dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif #if D3D_COMPILER_VERSION >= 46 // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Tutorial11.fx" ) ); V_RETURN( D3DX11CompileEffectFromFile( str, nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, dwShaderFlags, 0, pd3dDevice, &g_pEffect, nullptr) ); #else ID3DBlob* pEffectBuffer = nullptr; V_RETURN( DXUTCompileFromFile( L"Tutorial11.fx", nullptr, "none", "fx_5_0", dwShaderFlags, 0, &pEffectBuffer ) ); hr = D3DX11CreateEffectFromMemory( pEffectBuffer->GetBufferPointer(), pEffectBuffer->GetBufferSize(), 0, pd3dDevice, &g_pEffect ); SAFE_RELEASE( pEffectBuffer ); if ( FAILED(hr) ) return hr; #endif // Obtain the technique g_pTechnique = g_pEffect->GetTechniqueByName( "Render" ); // Obtain the variables g_ptxDiffuseVariable = g_pEffect->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); g_pWorldVariable = g_pEffect->GetVariableByName( "World" )->AsMatrix(); g_pViewVariable = g_pEffect->GetVariableByName( "View" )->AsMatrix(); g_pProjectionVariable = g_pEffect->GetVariableByName( "Projection" )->AsMatrix(); g_pWavinessVariable = g_pEffect->GetVariableByName( "Waviness" )->AsScalar(); g_pTimeVariable = g_pEffect->GetVariableByName( "Time" )->AsScalar(); // Set Waviness g_pWavinessVariable->SetFloat( g_fModelWaviness ); // Define the input layout D3D11_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = ARRAYSIZE( layout ); // Create the input layout D3DX11_PASS_DESC PassDesc; V_RETURN( g_pTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc ) ); V_RETURN( pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout ) ); // Set the input layout pd3dImmediateContext->IASetInputLayout( g_pVertexLayout ); // Load the mesh V_RETURN( g_Mesh.Create( pd3dDevice, L"Tiny\\tiny.sdkmesh" ) ); // Initialize the world matrices g_World = XMMatrixIdentity(); // Setup the camera's view parameters static const XMVECTORF32 s_Eye = { 0.0f, 3.0f, -800.0f, 0.f }; static const XMVECTORF32 s_At = { 0.0f, 1.0f, 0.0f, 0.f }; g_Camera.SetViewParams( s_Eye, s_At ); return S_OK; }
// Handles the GUI events void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext ) { switch( nControlID ) { case IDC_TOGGLEFULLSCREEN: { DXUTToggleFullScreen(); break; } case IDC_TOGGLEREF: { DXUTToggleREF(); break; } case IDC_CHANGEDEVICE: { gD3DSettingsDlg.SetActive( !gD3DSettingsDlg.IsActive() ); break; } case IDC_TEXT: case IDC_EXPOSURE: { gLog2Exposure = (gSampleUI.GetSlider(IDC_EXPOSURE)->GetValue() / 100.0f - 0.5f) * 33.33333; WCHAR wstr[MAX_PATH]; swprintf_s(wstr, MAX_PATH, L"Texture Size: %d x %d\n" L"logRGB L1: %.2f%%\n" //L"logRGB RMSE: %.4f\n" //L"Relative error: %.2f%%\n" //L"mPSNR: %.2f%%\n" L"Exposure: %.2f\n" L"Compression Time: %0.2f ms\n" L"Compression Rate: %0.2f Mp/s\n", gTexWidth, gTexHeight, gError, gLog2Exposure, gCompTime, gCompRate); gSampleUI.GetStatic(IDC_TEXT)->SetText(wstr); break; } case IDC_MT: { // Shut down all previous threading abilities. DestroyThreads(); gMultithreaded = gSampleUI.GetCheckBox(IDC_MT)->GetChecked(); if (gMultithreaded) { InitWin32Threads(); } // Recompress the texture. RecompressTexture(); gSampleUI.SendEvent(IDC_TEXT, true, gSampleUI.GetStatic(IDC_TEXT)); break; } case IDC_PROFILE: { gCompressionFunc = (CompressionFunc*)gSampleUI.GetComboBox(IDC_PROFILE)->GetSelectedData(); // Recompress the texture. RecompressTexture(); gSampleUI.SendEvent(IDC_TEXT, true, gSampleUI.GetStatic(IDC_TEXT)); break; } case IDC_LOAD_TEXTURE: { // Store the current working directory. TCHAR workingDirectory[MAX_PATH]; GetCurrentDirectory(MAX_PATH, workingDirectory); // Open a file dialog. OPENFILENAME openFileName; WCHAR file[MAX_PATH]; file[0] = 0; ZeroMemory(&openFileName, sizeof(OPENFILENAME)); openFileName.lStructSize = sizeof(OPENFILENAME); openFileName.lpstrFile = file; openFileName.nMaxFile = MAX_PATH; openFileName.lpstrFilter = L"DDS\0*.dds\0\0"; openFileName.nFilterIndex = 1; openFileName.lpstrInitialDir = NULL; openFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if(GetOpenFileName(&openFileName)) { //CreateTextures(openFileName.lpstrFile); SAFE_RELEASE(gUncompressedSRV); LoadTexture(openFileName.lpstrFile); } // Restore the working directory. GetOpenFileName changes the current working directory which causes problems with relative paths to assets. SetCurrentDirectory(workingDirectory); RecompressTexture(); gSampleUI.SendEvent(IDC_TEXT, true, gSampleUI.GetStatic(IDC_TEXT)); break; } case IDC_RECOMPRESS: { // Recompress the texture. RecompressTexture(); gSampleUI.SendEvent(IDC_TEXT, true, gSampleUI.GetStatic(IDC_TEXT)); break; } case IDC_SAVE_TEXTURE: { // Store the current working directory. TCHAR workingDirectory[MAX_PATH]; GetCurrentDirectory(MAX_PATH, workingDirectory); // Open a file dialog. OPENFILENAME openFileName; WCHAR file[MAX_PATH]; file[0] = 0; ZeroMemory(&openFileName, sizeof(OPENFILENAME)); openFileName.lStructSize = sizeof(OPENFILENAME); openFileName.lpstrFile = file; openFileName.nMaxFile = MAX_PATH; openFileName.lpstrFilter = L"DDS\0*.dds\0\0"; openFileName.lpstrDefExt = L"dds"; openFileName.nFilterIndex = 1; openFileName.lpstrInitialDir = NULL; openFileName.Flags = OFN_PATHMUSTEXIST; if(GetSaveFileName(&openFileName)) { SaveTexture(gCompressedSRV,openFileName.lpstrFile); } // Restore the working directory. GetOpenFileName changes the current working directory which causes problems with relative paths to assets. SetCurrentDirectory(workingDirectory); break; } case IDC_IMAGEVIEW: { gImageView = (EImageView)(INT_PTR)gSampleUI.GetComboBox(IDC_IMAGEVIEW)->GetSelectedData(); break; } } }
//-------------------------------------------------------------------------------------- // 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_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(); } }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; auto pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_SettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); // Read the HLSL file // You should use the lowest possible shader profile for your shader to enable various feature levels. These // shaders are simple enough to work well within the lowest possible profile, and will run on all feature levels DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #ifdef _DEBUG // Disable optimizations to further improve shader debugging dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif ID3DBlob* pVertexShaderBuffer = nullptr; V_RETURN( DXUTCompileFromFile( L"SimpleSample.hlsl", nullptr, "RenderSceneVS", "vs_4_0_level_9_1", dwShaderFlags, 0, &pVertexShaderBuffer ) ); ID3DBlob* pPixelShaderBuffer = nullptr; V_RETURN( DXUTCompileFromFile( L"SimpleSample.hlsl", nullptr, "RenderScenePS", "ps_4_0_level_9_1", dwShaderFlags, 0, &pPixelShaderBuffer ) ); // Create the shaders V_RETURN( pd3dDevice->CreateVertexShader( pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), nullptr, &g_pVertexShader11 ) ); DXUT_SetDebugName( g_pVertexShader11, "RenderSceneVS" ); V_RETURN( pd3dDevice->CreatePixelShader( pPixelShaderBuffer->GetBufferPointer(), pPixelShaderBuffer->GetBufferSize(), nullptr, &g_pPixelShader11 ) ); DXUT_SetDebugName( g_pPixelShader11, "RenderScenePS" ); // Create a layout for the object data const D3D11_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; V_RETURN( pd3dDevice->CreateInputLayout( layout, ARRAYSIZE( layout ), pVertexShaderBuffer->GetBufferPointer(), pVertexShaderBuffer->GetBufferSize(), &g_pLayout11 ) ); DXUT_SetDebugName( g_pLayout11, "Primary" ); // No longer need the shader blobs SAFE_RELEASE( pVertexShaderBuffer ); SAFE_RELEASE( pPixelShaderBuffer ); // Create state objects D3D11_SAMPLER_DESC samDesc = {}; samDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samDesc.AddressU = samDesc.AddressV = samDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samDesc.MaxAnisotropy = 1; samDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; samDesc.MaxLOD = D3D11_FLOAT32_MAX; V_RETURN( pd3dDevice->CreateSamplerState( &samDesc, &g_pSamLinear ) ); DXUT_SetDebugName( g_pSamLinear, "Linear" ); // Create constant buffers D3D11_BUFFER_DESC cbDesc = {}; cbDesc.Usage = D3D11_USAGE_DYNAMIC; cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; cbDesc.ByteWidth = sizeof( CB_VS_PER_OBJECT ); V_RETURN( pd3dDevice->CreateBuffer( &cbDesc, nullptr, &g_pcbVSPerObject11 ) ); DXUT_SetDebugName( g_pcbVSPerObject11, "CB_VS_PER_OBJECT" ); cbDesc.ByteWidth = sizeof( CB_VS_PER_FRAME ); V_RETURN( pd3dDevice->CreateBuffer( &cbDesc, nullptr, &g_pcbVSPerFrame11 ) ); DXUT_SetDebugName( g_pcbVSPerFrame11, "CB_VS_PER_FRAME" ); // Create other render resources here // Setup the camera's view parameters static const XMVECTORF32 s_vecEye = { 0.0f, 0.0f, -5.0f, 0.f }; g_Camera.SetViewParams( s_vecEye, g_XMZero ); g_HUD.GetButton( IDC_TOGGLEWARP )->SetEnabled( true ); return S_OK; }
//-------------------------------------------------------------------------------------- // Initialize the app //-------------------------------------------------------------------------------------- void InitApp() { g_SettingsDlg.Init( &g_DialogResourceManager ); }
//-------------------------------------------------------------------------------------- // Create any D3D11 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; static bool bFirstOnCreateDevice = true; // Warn the user that in order to support CS4x, a non-hardware device has been created, continue or quit? if ( DXUTGetDeviceSettings().d3d11.DriverType != D3D_DRIVER_TYPE_HARDWARE && bFirstOnCreateDevice ) { if ( MessageBox( 0, L"CS4x capability is missing. "\ L"In order to continue, a non-hardware device has been created, "\ L"it will be very slow, continue?", L"Warning", MB_ICONEXCLAMATION | MB_YESNO ) != IDYES ) return E_FAIL; } bFirstOnCreateDevice = false; ID3D11DeviceContext* pd3dImmediateContext = DXUTGetD3D11DeviceContext(); V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) ); V_RETURN( g_D3DSettingsDlg.OnD3D11CreateDevice( pd3dDevice ) ); V_RETURN( g_Tessellator.OnD3D11CreateDevice( pd3dDevice ) ); g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 ); // find the file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"BaseMesh.obj" ) ); std::wifstream ifs( str ); WCHAR line[256] = {0}; CGrowableArray<D3DXVECTOR4> initdata; // Parse the .obj file. Both triangle faces and quad faces are supported. // Only v and f tags are processed, other tags like vn, vt etc are ignored. { CGrowableArray<D3DXVECTOR4> v; while ( ifs >> line ) { if ( 0 == wcscmp( line, L"#" ) ) ifs.getline( line, 255 ); else if ( 0 == wcscmp( line, L"v" ) ) { D3DXVECTOR4 pos; ifs >> pos.x >> pos.y >> pos.z; pos.w = 1; v.Add( pos ); } } ifs.clear( 0 ); ifs.seekg( 0 ); while ( ifs >> line ) { if ( 0 == wcscmp( line, L"#" ) ) ifs.getline( line, 255 ); else if ( 0 == wcscmp( line, L"f" ) ) { ifs.getline( line, 255 ); std::wstringstream ss(line); int idx[4], i = 0; while ( ss >> line ) { std::wstringstream ss(line); ss >> idx[i++]; } initdata.Add( v[idx[0]-1] ); initdata.Add( v[idx[1]-1] ); initdata.Add( v[idx[2]-1] ); if ( i >= 4 ) // quad face? { initdata.Add( v[idx[2]-1] ); initdata.Add( v[idx[3]-1] ); initdata.Add( v[idx[0]-1] ); } } }
//-------------------------------------------------------------------------------------- // Release D3D9 resources created in the OnD3D9CreateDevice callback //-------------------------------------------------------------------------------------- void CALLBACK OnD3D9DestroyDevice( void* pUserContext ) { SAFE_DELETE( g_pMain ); g_DialogResourceManager.OnD3D9DestroyDevice(); g_SettingsDlg.OnD3D9DestroyDevice(); }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10CreateDevice( ID3D10Device* pd3dDevice, const DXGI_SURFACE_DESC* pBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( D3DX10CreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont10 ) ); V_RETURN( D3DX10CreateSprite( pd3dDevice, 512, &g_pSprite10 ) ); g_pTxtHelper = new CDXUTTextHelper( NULL, NULL, g_pFont10, g_pSprite10, 15 ); // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"MeshFromOBJ10.fx" ) ); DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3D10_SHADER_DEBUG; #endif V_RETURN( D3DX10CreateEffectFromFile( str, NULL, NULL, "fx_4_0", dwShaderFlags, 0, pd3dDevice, NULL, NULL, &g_pEffect10, NULL, NULL ) ); // Obtain the technique g_pTechnique = g_pEffect10->GetTechniqueByName( "NoSpecular" ); g_ptxDiffuseVariable = g_pEffect10->GetVariableByName( "g_MeshTexture" )->AsShaderResource(); g_pAmbient = g_pEffect10->GetVariableByName( "g_vMaterialAmbient" )->AsVector(); g_pDiffuse = g_pEffect10->GetVariableByName( "g_vMaterialDiffuse" )->AsVector(); g_pSpecular = g_pEffect10->GetVariableByName( "g_vMaterialSpecular" )->AsVector(); g_pOpacity = g_pEffect10->GetVariableByName( "g_fMaterialAlpha" )->AsScalar(); g_pSpecularPower = g_pEffect10->GetVariableByName( "g_nMaterialShininess" )->AsScalar(); g_pLightColor = g_pEffect10->GetVariableByName( "g_vLightColor" )->AsVector(); g_pLightPosition = g_pEffect10->GetVariableByName( "g_vLightPosition" )->AsVector(); g_pCameraPosition = g_pEffect10->GetVariableByName( "g_vCameraPosition" )->AsVector(); g_pTime = g_pEffect10->GetVariableByName( "g_fTime" )->AsScalar(); g_pWorld = g_pEffect10->GetVariableByName( "g_mWorld" )->AsMatrix(); g_pWorldViewProjection = g_pEffect10->GetVariableByName( "g_mWorldViewProjection" )->AsMatrix(); // Define the input layout const D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, } ; UINT numElements = sizeof( layout ) / sizeof( layout[0] ); // Create the input layout D3D10_PASS_DESC PassDesc; g_pTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout ) ); pd3dDevice->IASetInputLayout( g_pVertexLayout ); // Load the mesh V_RETURN( g_MeshLoader.Create( pd3dDevice, MESHFILEPATH ) ); // Add the identified subsets to the UI CDXUTComboBox* pComboBox = g_SampleUI.GetComboBox( IDC_SUBSET ); pComboBox->RemoveAllItems(); pComboBox->AddItem( L"All", ( void* )( INT_PTR )-1 ); for ( UINT subset = 0; subset < g_MeshLoader.GetNumSubsets(); ++subset ) { Material* pMaterial = g_MeshLoader.GetSubsetMaterial( subset ); pComboBox->AddItem( pMaterial->strName, ( void* )( INT_PTR )subset ); } // Store the correct technique for each material for ( UINT i = 0; i < g_MeshLoader.GetNumMaterials(); ++i ) { Material* pMaterial = g_MeshLoader.GetMaterial( i ); const char* strTechnique = ""; if( pMaterial->pTextureRV10 && pMaterial->bSpecular ) strTechnique = "TexturedSpecular"; else if( pMaterial->pTextureRV10 && !pMaterial->bSpecular ) strTechnique = "TexturedNoSpecular"; else if( !pMaterial->pTextureRV10 && pMaterial->bSpecular ) strTechnique = "Specular"; else if( !pMaterial->pTextureRV10 && !pMaterial->bSpecular ) strTechnique = "NoSpecular"; pMaterial->pTechnique = g_pEffect10->GetTechniqueByName( strTechnique ); } LoadRasterizerStates(pd3dDevice); // Load the mesh into dump g_DumpMesh = new DumpMesh(pd3dDevice, g_MeshLoader.GetMesh()); g_DumpMesh->DumpVertices(); g_DumpMesh->DumpIndices(); // Construct the mesh g_AABBConstructor = new txAABBConstructor(pd3dDevice,g_DumpMesh->GetVertexCache(),g_DumpMesh->GetIndexCache()); g_FunctionDraw = new txFunctionMeshDisplay(pd3dDevice); // Create a cube //CreateCube(pd3dDevice); //g_pBox = new Box(); //g_pBox->init(pd3dDevice, 1.0f); //g_pBox->customizeInit(pd3dDevice, D3DXVECTOR3(0.0f,0.0f,0.0f),D3DXVECTOR3(1.0f,1.0f,1.0f)); g_Parabola = new txFunctionUVBase(-2.0,2.0,-2.0,2.0,2,2); g_Parabola->Discrete(txUVFunctionZoo::Parabola); g_ParabolaDis = new txFunctionDrawor(g_Parabola->GetVList(), g_Parabola->M(), g_Parabola->N(),pd3dDevice); //g_UVImport = new txUVSurfaceImport(); //g_UVImport->ConstructSurfaceFromFile("D:\\data\\screwsurface.xyz"); //g_UVFileDisp = new txFunctionDrawor(g_UVImport->GetVList(), g_UVImport->M(), g_UVImport->N(),pd3dDevice); std::vector<txVec3> temv; D3DXVECTOR32txVec3(g_Parabola->GetVList(),temv); txSurface surface(temv, g_Parabola->M(), g_Parabola->N()); txICollisionshapeMesh meshmodel(surface); meshmodel.Build(); txPlane plane0(0,0,1,10); std::vector<txPlane> planes; planes.push_back(plane0); txPlaneCollisionShape planecoll(planes); planecoll.Collide(meshmodel); const size_t AABBLevelCount = g_AABBConstructor->GetAABBLevelCount(); // Add the AABB Levels CDXUTComboBox* pLevelComboBox = g_SampleUI.GetComboBox( IDC_AABBSUBLEVEL ); pLevelComboBox->RemoveAllItems(); wchar_t sz[32]; for (size_t i=0; i<AABBLevelCount-1; i++){ //itoa(i,temp,10); swprintf(sz,31,L"%d",i); pLevelComboBox->AddItem( sz, ( void* )( UINT64 )i ); } // Setup the camera's view parameters D3DXVECTOR3 vecEye( 2.0f, 1.0f, 0.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.0f, -0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); D3D10_BLEND_DESC blendDesc = {0}; blendDesc.AlphaToCoverageEnable = false; blendDesc.BlendEnable[0] = true; blendDesc.SrcBlend = D3D10_BLEND_SRC_ALPHA; blendDesc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; blendDesc.BlendOp = D3D10_BLEND_OP_ADD; blendDesc.SrcBlendAlpha = D3D10_BLEND_ONE; blendDesc.DestBlendAlpha = D3D10_BLEND_ZERO; blendDesc.BlendOpAlpha = D3D10_BLEND_OP_ADD; blendDesc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; HR(pd3dDevice->CreateBlendState(&blendDesc, &g_TransparentBS)); return S_OK; }
//-------------------------------------------------------------------------------------- // Handles the GUI events //-------------------------------------------------------------------------------------- void CALLBACK OnGUIEvent(UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext) { switch( nControlID ) { case IDC_CHANGEDEVICE: g_D3DSettingsDlg.SetActive( !g_D3DSettingsDlg.IsActive() ); break; case IDC_LIGHT_SIZE: break; case IDC_SHADOW_ALGORITHM: OldShadowAlgorithm = ShadowAlgorithm; ShadowAlgorithm = ((CDXUTComboBox*)pControl)->GetSelectedIndex(); break; case IDC_LIGHT_ZN: //g_Widget.SetLightZn( (float)g_SampleUI.GetSlider(IDC_LIGHT_ZN)->GetValue()/2 ); break; case IDC_LIGHT_ZF: //g_Widget.SetLightZf( (float)g_SampleUI.GetSlider(IDC_LIGHT_ZF)->GetValue()+30 ); break; case IDC_LIGHT_FOV: //g_Widget.SetLightFov((float)g_SampleUI.GetSlider(IDC_LIGHT_FOV)->GetValue()/10); break; case IDC_fDefaultDepthBias: g_fDefaultDepthBias = (float)g_SampleUI.GetSlider(IDC_fDefaultDepthBias)->GetValue()/100; break; case IDC_fDepthBiasHammer: g_fDepthBiasHammer = (float)g_SampleUI.GetSlider(IDC_fDepthBiasHammer)->GetValue()/4000.0; break; case IDC_fDepthBiasLeftForearm: g_fDepthBiasLeftForearm = (float)g_SampleUI.GetSlider(IDC_fDepthBiasLeftForearm)->GetValue()/4000.0; break; case IDC_fDepthBiasRightForearm: g_fDepthBiasRightForearm = (float)g_SampleUI.GetSlider(IDC_fDepthBiasRightForearm)->GetValue()/4000.0; break; case IDC_fDepthBiasLeftShoulder: g_fDepthBiasLeftShoulder = (float)g_SampleUI.GetSlider(IDC_fDepthBiasLeftShoulder)->GetValue()/4000.0; break; case IDC_fDepthBiasRightShoulder: g_fDepthBiasRightShoulder = (float)g_SampleUI.GetSlider(IDC_fDepthBiasRightShoulder)->GetValue()/4000.0; break; case IDC_fDepthBiasBlackPlate: g_fDepthBiasBlackPlate = (float)g_SampleUI.GetSlider(IDC_fDepthBiasBlackPlate)->GetValue()/4000.0; break; case IDC_fDepthBiasHelmet: g_fDepthBiasHelmet = (float)g_SampleUI.GetSlider(IDC_fDepthBiasHelmet)->GetValue()/4000.0; break; case IDC_fDepthBiasEyes: g_fDepthBiasEyes = (float)g_SampleUI.GetSlider(IDC_fDepthBiasEyes)->GetValue()/4000.0; break; case IDC_fDepthBiasBelt: g_fDepthBiasBelt = (float)g_SampleUI.GetSlider(IDC_fDepthBiasBelt)->GetValue()/4000.0; break; case IDC_fDepthBiasLeftThigh: g_fDepthBiasLeftThigh = (float)g_SampleUI.GetSlider(IDC_fDepthBiasLeftThigh)->GetValue()/4000.0; break; case IDC_fDepthBiasRightThigh: g_fDepthBiasRightThigh = (float)g_SampleUI.GetSlider(IDC_fDepthBiasRightThigh)->GetValue()/4000.0; break; case IDC_fDepthBiasLeftShin: g_fDepthBiasLeftShin = (float)g_SampleUI.GetSlider(IDC_fDepthBiasLeftShin)->GetValue()/4000.0; break; case IDC_fDepthBiasRightShin: g_fDepthBiasRightShin = (float)g_SampleUI.GetSlider(IDC_fDepthBiasRightShin)->GetValue()/4000.0; break; case IDC_fDepthBiasObject0: g_fDepthBiasObject0 = (float)g_SampleUI.GetSlider(IDC_fDepthBiasObject0)->GetValue()/4000.0; break; case IDC_NUM_LIGHT_SAMPLE: g_nNumLightSample = 2*(int)g_SampleUI.GetSlider(IDC_NUM_LIGHT_SAMPLE)->GetValue(); break; case IDC_BIAS_3RD_DEPTH: g_f3rdDepthDelta = (float)g_SampleUI.GetSlider(IDC_BIAS_3RD_DEPTH)->GetValue()/100; break; case IDC_BIAS_1ST_DEPTH: g_f1stDepthDelta = (float)g_SampleUI.GetSlider(IDC_BIAS_3RD_DEPTH)->GetValue()/500; break; } }
//-------------------------------------------------------------------------------------- // 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(); }
//-------------------------------------------------------------------------------------- void CALLBACK OnD3D10FrameRender(ID3D10Device* pDev10, double fTime, float fElapsedTime, void* pUserContext) { HRESULT hr; g_pCamManager->SyncToCameraUI(g_CameraUI); //dont delete, used no only here const DXGI_SURFACE_DESC *pBackBufferSurfaceDesc = DXUTGetDXGIBackBufferSurfaceDesc(); if( ShadowAlgorithm != OldShadowAlgorithm ) { OldShadowAlgorithm = ShadowAlgorithm; switch( ShadowAlgorithm ) { case STANDARD_BP: g_ABP.OnD3D10DestroyDevice(); break; case BP_MSSM_KERNEL: g_BPMSSMKernel.OnD3D10DestroyDevice(); break; case STD_VSM: g_StdVSM.OnD3D10DestroyDevice(); break; case MIP_VSM: g_MipVSM.OnD3D10DestroyDevice(); break; case HIR_BP: g_HBP.OnD3D10DestroyDevice(); break; case BP_GI: g_BPGI.OnD3D10DestroyDevice(); break; case STD_PCSS: g_PCSS.OnD3D10DestroyDevice(); break; default: break; }; switch( ShadowAlgorithm ) { case STANDARD_BP: g_ABP.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_ABP.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext ); break; case BP_MSSM_KERNEL: g_BPMSSMKernel.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_BPMSSMKernel.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext ); break; case STD_VSM: g_StdVSM.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_StdVSM.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext ); break; case MIP_VSM: g_MipVSM.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_MipVSM.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext ); break; case HIR_BP: g_HBP.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_HBP.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext ); break; case BP_GI: g_BPGI.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_BPGI.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext ); break; case STD_PCSS: g_PCSS.OnD3D10CreateDevice(pDev10,pBackBufferSurfaceDesc,pUserContext); g_PCSS.OnD3D10SwapChainResized(pDev10,NULL,pBackBufferSurfaceDesc,pUserContext ); break; default: break; }; } //begin light and view pos management { static double old_fTime = 0.001; fTime = old_fTime; old_fTime += 0.02; static double oldTime = 0; static unsigned old_iSta = 0; static double stop_time = 0; static double total_stop_time = 0; double tmp = fTime; unsigned iSta = g_SampleUI.GetCheckBox(IDC_STATIC)->GetChecked(); if( 0 == old_iSta && 1 == iSta )//turn to be static { stop_time = fTime - total_stop_time; } if( 1 == iSta ) { total_stop_time += ( fTime - oldTime ); fTime = stop_time; } if( 0 == iSta ) { fTime -= total_stop_time; } old_iSta = iSta; oldTime = tmp; }//end light and view pos management S3UTCamera& g_CameraRef = *(g_pCamManager->ActiveEye()); // compute view matrix D3DXMATRIX mTmp, mWorldView, mWorldViewProj, mWorldViewInv; D3DXMatrixInverse(&mTmp, NULL, g_CameraRef.GetWorldMatrix()); D3DXMatrixMultiply(&mWorldView, &mTmp, g_CameraRef.GetViewMatrix()); // correct near/far clip planes according to camera location D3DXVECTOR3 vBox[2]; float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height; // clear depth and color ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView(); pDev10->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0); ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView(); if( g_D3DSettingsDlg.IsActive() ) { g_D3DSettingsDlg.OnRender( fElapsedTime ); return; } Parameters para; para.fLightZn = g_fCtrledLightZn; float biases[15]; biases[0] = g_fDepthBiasObject0; biases[1] = g_fDefaultDepthBias; biases[2] = g_fDepthBiasHammer; biases[3] = g_fDepthBiasLeftForearm; biases[4] = g_fDepthBiasRightForearm; biases[5] = g_fDepthBiasLeftShoulder; biases[6] = g_fDepthBiasRightShoulder; biases[7] = g_fDepthBiasBlackPlate; biases[8] = g_fDepthBiasHelmet; biases[9] = g_fDepthBiasEyes; biases[10] = g_fDepthBiasBelt; biases[11] = g_fDepthBiasLeftThigh; biases[12] = g_fDepthBiasRightThigh; biases[13] = g_fDepthBiasLeftShin; biases[14] = g_fDepthBiasRightShin; g_MeshScene.set_biases(biases,15); float light_size[NUM_LIGHT] = LIGHT_SIZE; float light_ZNS[NUM_LIGHT] = LIGHT_ZNS; float light_view_angle[NUM_LIGHT] = LIGHT_VIEW_ANGLES; D3DXVECTOR4 light_color[NUM_LIGHT] = LIGHT_COLOR; bool render_ogre = g_SampleUI.GetCheckBox( IDC_ANIMATE )->GetChecked(); bool render_scene = g_SampleUI.GetCheckBox( IDC_SCENE )->GetChecked(); bool render_fan = g_SampleUI.GetCheckBox( IDC_FAN )->GetChecked(); float ClearColor[4] = { 0, 0, 0, 1 }; pDev10->ClearRenderTargetView(g_pBlendBuffer->m_pRTView, ClearColor); //use alpha val 1 to represent untouched pixels pDev10->ClearRenderTargetView(g_pWidgetBuffer->m_pRTView, ClearColor); //light management ID3D10RenderTargetView *p_RTV; ID3D10ShaderResourceView *p_SRV; static float light_scale_factor = 0.2; static float ls_incre = 0.04; if( g_LightVary == true || g_fFilterSize < g_fFilterSizeCtrl ) { g_fFilterSize -= ls_incre; if( g_fFilterSize < 0.1 || g_fFilterSize > g_fFilterSizeCtrl ) ls_incre = -ls_incre; } else { g_fFilterSize = g_fFilterSizeCtrl; } g_fFilterSize = g_fFilterSizeCtrl; //light pos is not modified here. this light pos stored in 3DWidget is only synced with light camera after rendering of the light //if you try to modify light pos here, light would be freezed, of course, this is a bug introduced by bad design D3DXVECTOR3 vTmp; // render GBuffer pDev10->OMSetDepthStencilState(g_pDSState,0); g_GBuffer.OnD3D10FrameRender( true, true, g_SampleUI, g_MeshScene, g_CameraRef, pDev10, fTime, fElapsedTime, pUserContext ); g_Blender.OriginalSampleMask = 0; pDev10->OMGetBlendState( &g_Blender.pOriginalBlendState, g_Blender.OriginalBlendFactor, &g_Blender.OriginalSampleMask ); float BlendBufferClearColor[4] = { 1, 1, 0, 1 }; bool isFirstPass = true; for( int cam_idx = 0; cam_idx < g_pCamManager->CameraCount(); ++cam_idx ) { // rendering a subdivided light if( g_pCamManager->Camera(cam_idx)->GetCamType() == S3UTCamera::eLight&& g_pCamManager->Camera(cam_idx)->IsActive() ) { float scaled_half_light_size = (g_pCamManager->Camera(cam_idx)->GetLightSize()*LIGHT_SCALE_FACTOR); float fStartPt = -scaled_half_light_size; float fInterval = 2 * scaled_half_light_size / g_nNumLightSample; float fSubLightSize = g_pCamManager->Camera(cam_idx)->GetLightSize(); if( g_nNumLightSample > 0 ) { fSubLightSize = fSubLightSize / g_nNumLightSample; } if( g_nNumLightSample == 0 ) { g_nNumLightSample = 1; } static float total_light_x_incre = 0; static int light_mov_dir = 0; float shadow_factor = 0.8/(g_nNumLightSample * g_nNumLightSample); for( int ix = 0; ix < g_nNumLightSample; ++ix ) { for( int iy = 0; iy < g_nNumLightSample; ++iy ) { D3DXVECTOR3 vLight = *g_pCamManager->Camera(cam_idx)->GetEyePt(); S3UTCamera local_cam = *g_pCamManager->Camera(cam_idx); D3DXVECTOR3 vTrans( fStartPt+(ix+0.5)*fInterval,fStartPt+(iy+0.5)*fInterval,0 ); if( g_nNumLightSample == 1 ) { vTrans = D3DXVECTOR3(0,0,0); } D3DXMATRIX mInvLightView; D3DXVECTOR4 tmp_light_pos; D3DXMatrixInverse(&mInvLightView, NULL, local_cam.GetViewMatrix()); D3DXVec3Transform(&tmp_light_pos, &vTrans, &mInvLightView ); D3DXVECTOR3 tmp_light_pos_3(tmp_light_pos.x,tmp_light_pos.y,tmp_light_pos.z); D3DXVECTOR3 vLookAt = *local_cam.GetLookAtPt(); local_cam.SetViewParams( &tmp_light_pos_3, &vLookAt ); g_MeshScene.set_parameters( render_ogre, render_scene, render_fan, false ); D3DXMATRIX mLightView; // here we compute light viewprojection so that light oversees the whole scene D3DXMATRIX mTranslate; D3DXMatrixInverse(&mTranslate, NULL, local_cam.GetWorldMatrix()); D3DXMatrixMultiply(&mLightView, &mTranslate, local_cam.GetViewMatrix()); unsigned iTmp = g_SampleUI.GetCheckBox(IDC_BDUMP_SHADOWMAP)->GetChecked(); ssmap.Render(pDev10, &g_MeshScene, local_cam,fTime,fElapsedTime,iTmp); pDev10->RSSetState(g_pRenderState); p_RTV = g_pBlendBuffer->m_pRTView; float NewBlendFactor[4] = {0,0,0,0}; //alpha blending initial; if( isFirstPass ) { isFirstPass = false; pDev10->OMSetBlendState( g_Blender.m_pSceneBlendStateInitial, NewBlendFactor, 0xffffffff ); } else { pDev10->OMSetBlendState( g_Blender.m_pSceneBlendStateOn, NewBlendFactor, 0xffffffff ); } if( ShadowAlgorithm == BP_GI ) { V(g_BPGI.m_pEffect->GetVariableByName("g_fLumiFactor")->AsScalar()->SetFloat( shadow_factor )); g_BPGI.set_parameters( para,p_RTV,&light_color[0] ); g_BPGI.set_input_buffer( &g_GBuffer ); g_BPGI.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext); } else if( ShadowAlgorithm == STANDARD_BP ) { V(g_ABP.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor )); g_ABP.set_parameters( para,p_RTV,&light_color[0] ); g_ABP.set_input_buffer( &g_GBuffer ); g_ABP.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext); } else if( ShadowAlgorithm == BP_MSSM_KERNEL ) { V(g_BPMSSMKernel.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor )); g_BPMSSMKernel.set_parameters( para,p_RTV,&light_color[0] ); g_BPMSSMKernel.set_input_buffer( &g_GBuffer ); g_BPMSSMKernel.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext); } else if( ShadowAlgorithm == STD_VSM ) { V(g_StdVSM.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor )); g_StdVSM.set_bias( g_fDefaultDepthBias,g_f3rdDepthDelta, g_f1stDepthDelta ); g_StdVSM.set_parameters( para,p_RTV,&light_color[0] ); g_StdVSM.set_input_buffer( &g_GBuffer ); g_StdVSM.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext); } else if( ShadowAlgorithm == MIP_VSM ) { V(g_MipVSM.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor )); g_MipVSM.set_parameters( para,p_RTV,&light_color[0] ); g_MipVSM.set_input_buffer( &g_GBuffer ); g_MipVSM.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext); } else if( ShadowAlgorithm == STD_PCSS ) { V(g_PCSS.m_pEffect->GetVariableByName("fLumiFactor")->AsScalar()->SetFloat( shadow_factor )); g_PCSS.set_parameters( para,p_RTV,&light_color[0] ); g_PCSS.set_input_buffer( &g_GBuffer ); g_PCSS.OnD3D10FrameRender(render_ogre,render_scene,g_SampleUI,g_MeshScene,fSubLightSize,ssmap,g_CameraRef,local_cam,pDev10,fTime,fElapsedTime,pUserContext); } //alpha blending restore pDev10->OMSetBlendState( g_Blender.pOriginalBlendState, g_Blender.OriginalBlendFactor, g_Blender.OriginalSampleMask ); if( g_SampleUI.GetCheckBox( IDC_SHOW_3DWIDGET )->GetChecked() ) { if( g_pCamManager->Camera(cam_idx)->GetCamType() == S3UTCamera::eLight&& g_pCamManager->Camera(cam_idx)->IsActive() ) { pDev10->OMSetRenderTargets(1, &(g_pWidgetBuffer->m_pRTView), g_GBuffer.m_pDepthBuffer->m_pDSView); g_Widget.m_pSsmap = &ssmap; g_Widget.OnD3D10FrameRender(pDev10,g_CameraRef,local_cam); } } } } } } //----------------------------------------------------------------------------------- D3DXMATRIX mMatrixScale; D3DXMATRIX mMatrixScaleWVP; D3DXMatrixScaling( &mMatrixScale,(FLOAT)5,(FLOAT)5,(FLOAT)5 ); D3DXMatrixMultiply( &mMatrixScaleWVP, &mMatrixScale, &mWorldViewProj ); ID3D10RenderTargetView* pOrigRTV = DXUTGetD3D10RenderTargetView(); pDev10->OMSetRenderTargets(1,&pOrigRTV,NULL); float FinalClearColor[4] = { 1, 0, 0, 1 }; pDev10->ClearRenderTargetView(pOrigRTV, FinalClearColor); g_MeshScene.set_parameters( render_ogre,render_scene, render_fan ); g_Final.set_parameters( para, pOrigRTV, NULL, g_pBlendBuffer->m_pSRView, g_pWidgetBuffer->m_pSRView ); S3UTCamera& g_LCameraRef = g_LCamera[0]; g_Final.set_input_buffer( &g_GBuffer ); g_pSkyBox->OnFrameRender( mMatrixScaleWVP ); g_Final.OnD3D10FrameRender(g_SampleUI,g_MeshScene,g_fFilterSize,ssmap,g_CameraRef,g_LCameraRef,pDev10,fTime,fElapsedTime,pUserContext); g_LCameraRef.SetProjParams(g_fCtrledLightFov, 1.0, g_fCtrledLightZn, g_fCtrledLightZf); //if( g_SampleUI.GetCheckBox( IDC_SHOW_3DWIDGET )->GetChecked() ) //{ // for( int cam_idx = 0; cam_idx < g_pCamManager->CameraCount(); ++cam_idx ) // { // // rendering a subdivided light // if( g_pCamManager->Camera(cam_idx)->GetCamType() == S3UTCamera::eLight&& // g_pCamManager->Camera(cam_idx)->IsActive() ) // { // g_Widget.m_pSsmap = &ssmap; // g_Widget.OnD3D10FrameRender(pDev10,g_CameraRef,*(g_pCamManager->Camera(cam_idx))); // } // } //} // render UI if (g_bShowUI) { RenderText(); g_SampleUI.OnRender(fElapsedTime); //g_HUD.OnRender(fElapsedTime); } if( g_bShowLightUI ) { g_CameraUI.OnRender(fElapsedTime); } if( g_SampleUI.GetCheckBox( IDC_FRAME_DUMP )->GetChecked() ) { static int g_Frame = 0; IDXGISwapChain* pSwapChain = DXUTGetDXGISwapChain(); ID3D10Texture2D* pRT; pSwapChain->GetBuffer(0, __uuidof(pRT), reinterpret_cast<void**>(&pRT)); WCHAR filename[32]; StringCchPrintf(filename, 100, L"DumpedImages\\screenshot%.3d.jpg", g_Frame); D3DX10SaveTextureToFile(pRT, D3DX10_IFF_JPG, filename); SAFE_RELEASE(pRT); ++g_Frame; } }
//-------------------------------------------------------------------------------------- // Initialize the app //-------------------------------------------------------------------------------------- void InitApp() { // Initialize dialogs g_SettingsDlg.Init( &g_DialogResourceManager ); g_HUD.Init( &g_DialogResourceManager ); g_SampleUI.Init( &g_DialogResourceManager ); g_HUD.SetCallback( OnGUIEvent ); int iY = 10; g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 35, iY, 125, 22 ); g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 35, iY += 24, 125, 22 ); g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 35, iY += 24, 125, 22, VK_F2 ); g_SampleUI.SetCallback( OnGUIEvent ); g_SampleUI.SetFont( 1, L"Comic Sans MS", 24, FW_NORMAL ); g_SampleUI.SetFont( 2, L"Courier New", 16, FW_NORMAL ); // Static g_SampleUI.AddStatic( IDC_STATIC, L"This is a static control.", 0, 0, 200, 30 ); g_SampleUI.AddStatic( IDC_OUTPUT, L"This static control provides feedback for your action. It will change as you interact with the UI controls.", 20, 50, 620, 300 ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetTextColor( D3DCOLOR_ARGB( 255, 255, 0, 0 ) ); // Change color to red g_SampleUI.GetStatic( IDC_STATIC )->SetTextColor( D3DCOLOR_ARGB( 255, 0, 255, 0 ) ); // Change color to green g_SampleUI.GetControl( IDC_OUTPUT )->GetElement( 0 )->dwTextFormat = DT_LEFT | DT_TOP | DT_WORDBREAK; g_SampleUI.GetControl( IDC_OUTPUT )->GetElement( 0 )->iFont = 2; g_SampleUI.GetControl( IDC_STATIC )->GetElement( 0 )->dwTextFormat = DT_CENTER | DT_VCENTER | DT_WORDBREAK; // Buttons g_SampleUI.AddButton( IDC_ENABLEIME, L"Enable (I)ME", 30, 390, 80, 35, L'I' ); g_SampleUI.AddButton( IDC_DISABLEIME, L"Disable I(M)E", 30, 430, 80, 35, L'M' ); // Edit box g_SampleUI.AddEditBox( IDC_EDITBOX1, L"Edit control with default styles. Type text here and press Enter", 20, 440, 600, 32 ); // IME-enabled edit box CDXUTIMEEditBox* pIMEEdit; CDXUTIMEEditBox::InitDefaultElements( &g_SampleUI ); if( SUCCEEDED( CDXUTIMEEditBox::CreateIMEEditBox( &g_SampleUI, IDC_EDITBOX2, L"IME-capable edit control with custom styles. Type and press Enter", 20, 390, 600, 45, false, &pIMEEdit ) ) ) { g_SampleUI.AddControl( pIMEEdit ); pIMEEdit->GetElement( 0 )->iFont = 1; pIMEEdit->GetElement( 1 )->iFont = 1; pIMEEdit->GetElement( 9 )->iFont = 1; pIMEEdit->GetElement( 0 )->TextureColor.Init( D3DCOLOR_ARGB( 128, 255, 255, 255 ) ); // Transparent center pIMEEdit->SetBorderWidth( 7 ); pIMEEdit->SetTextColor( D3DCOLOR_ARGB( 255, 64, 64, 64 ) ); pIMEEdit->SetCaretColor( D3DCOLOR_ARGB( 255, 64, 64, 64 ) ); pIMEEdit->SetSelectedTextColor( D3DCOLOR_ARGB( 255, 255, 255, 255 ) ); pIMEEdit->SetSelectedBackColor( D3DCOLOR_ARGB( 255, 40, 72, 72 ) ); } // Slider g_SampleUI.AddSlider( IDC_SLIDER, 200, 450, 200, 24, 0, 100, 50, false ); // Checkbox g_SampleUI.AddCheckBox( IDC_CHECKBOX, L"This is a checkbox with hotkey. Press 'C' to toggle the check state.", 170, 450, 350, 24, false, L'C', false ); g_SampleUI.AddCheckBox( IDC_CLEAREDIT, L"This checkbox controls whether edit control text is cleared when Enter is pressed. (T)", 170, 460, 450, 24, false, L'T', false ); // Combobox CDXUTComboBox* pCombo; g_SampleUI.AddComboBox( IDC_COMBOBOX, 0, 0, 200, 24, L'O', false, &pCombo ); if( pCombo ) { pCombo->SetDropHeight( 100 ); pCombo->AddItem( L"Combobox item (O)", ( LPVOID )0x11111111 ); pCombo->AddItem( L"Placeholder (O)", ( LPVOID )0x12121212 ); pCombo->AddItem( L"One more (O)", ( LPVOID )0x13131313 ); pCombo->AddItem( L"I can't get enough (O)", ( LPVOID )0x14141414 ); pCombo->AddItem( L"Ok, last one, I promise (O)", ( LPVOID )0x15151515 ); } // Radio buttons g_SampleUI.AddRadioButton( IDC_RADIO1A, 1, L"Radio group 1 Amy (1)", 0, 50, 220, 24, false, L'1' ); g_SampleUI.AddRadioButton( IDC_RADIO1B, 1, L"Radio group 1 Brian (2)", 0, 50, 220, 24, false, L'2' ); g_SampleUI.AddRadioButton( IDC_RADIO1C, 1, L"Radio group 1 Clark (3)", 0, 50, 220, 24, false, L'3' ); g_SampleUI.AddRadioButton( IDC_RADIO2A, 2, L"Single (4)", 0, 50, 90, 24, false, L'4' ); g_SampleUI.AddRadioButton( IDC_RADIO2B, 2, L"Double (5)", 0, 50, 90, 24, false, L'5' ); g_SampleUI.AddRadioButton( IDC_RADIO2C, 2, L"Triple (6)", 0, 50, 90, 24, false, L'6' ); // List box g_SampleUI.AddListBox( IDC_LISTBOX, 30, 400, 200, 150, 0 ); for( int i = 0; i < 15; ++i ) { WCHAR wszText[50]; swprintf_s( wszText, 50, L"Single-selection listbox item %d", i ); g_SampleUI.GetListBox( IDC_LISTBOX )->AddItem( wszText, ( LPVOID )( size_t )i ); } g_SampleUI.AddListBox( IDC_LISTBOXM, 30, 400, 200, 150, CDXUTListBox::MULTISELECTION ); for( int i = 0; i < 30; ++i ) { WCHAR wszText[50]; swprintf_s( wszText, 50, L"Multi-selection listbox item %d", i ); g_SampleUI.GetListBox( IDC_LISTBOXM )->AddItem( wszText, ( LPVOID )( size_t )i ); } }
//-------------------------------------------------------------------------------------- // 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, 0, 0, 0 ), 1.0f, 0 ) ); //V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE( 0.0f, 0.0f, 0.0f, 1.0f ), 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(); D3DXMatrixIdentity(&mWorld); //mWorldViewProjection = mWorld * mView * mProj; //* pd3dDevice->SetTransform(D3DTS_WORLD, &mWorld); pd3dDevice->SetTransform(D3DTS_VIEW, &mView); pd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj); //*/ /* pd3dDevice->SetTransform(D3DTS_PROJECTION, &g_pCamera->m_mProj); pd3dDevice->SetTransform(D3DTS_VIEW, &g_pCamera->m_mView); pd3dDevice->SetTransform(D3DTS_WORLD, &mWorld); //*/ // 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_mWorldViewProjection", &mWorldViewProjection ) ); V( g_pEffect9->SetMatrix( "g_mWorld", &mWorld ) ); V( g_pEffect9->SetFloat( "g_fTime", ( float )fTime ) ); */ pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_COLORVALUE( 0.8f, 0.8f, 0.8f, 1.0f )); pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, false ); pd3dDevice->SetRenderState( D3DRS_COLORVERTEX, true ); DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"Draw scene" ); particleSystem.render(); DXUT_EndPerfEvent(); 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() ); } }
//-------------------------------------------------------------------------------------- // Handles the GUI events //-------------------------------------------------------------------------------------- void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext ) { WCHAR wszOutput[1024]; switch( nControlID ) { case IDC_TOGGLEFULLSCREEN: DXUTToggleFullScreen(); break; case IDC_TOGGLEREF: DXUTToggleREF(); break; case IDC_CHANGEDEVICE: g_SettingsDlg.SetActive( !g_SettingsDlg.IsActive() ); break; case IDC_ENABLEIME: CDXUTIMEEditBox::SetImeEnableFlag( true ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( L"You clicked the 'Enable IME' button.\nIME text input is enabled for IME-capable edit controls." ); break; case IDC_DISABLEIME: CDXUTIMEEditBox::SetImeEnableFlag( false ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( L"You clicked the 'Disable IME' button.\nIME text input is disabled for IME-capable edit controls." ); break; case IDC_EDITBOX1: case IDC_EDITBOX2: switch( nEvent ) { case EVENT_EDITBOX_STRING: { swprintf_s( wszOutput, 1024, L"You have pressed Enter in edit control (ID %u).\nThe content of the edit control is:\n\"%s\"", nControlID, ( ( CDXUTEditBox* )pControl )->GetText() ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); // Clear the text if needed if( g_SampleUI.GetCheckBox( IDC_CLEAREDIT )->GetChecked() ) ( ( CDXUTEditBox* )pControl )->SetText( L"" ); break; } case EVENT_EDITBOX_CHANGE: { swprintf_s( wszOutput, 1024, L"You have changed the content of an edit control (ID %u).\nIt is now:\n\"%s\"", nControlID, ( ( CDXUTEditBox* )pControl )->GetText() ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; } } break; case IDC_SLIDER: swprintf_s( wszOutput, 1024, L"You adjusted the slider control.\nThe new value reported is %d", ( ( CDXUTSlider* )pControl )->GetValue() ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; case IDC_CHECKBOX: swprintf_s( wszOutput, 1024, L"You %s the upper check box.", ( ( CDXUTCheckBox* )pControl )->GetChecked() ? L"checked" : L"cleared" ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; case IDC_CLEAREDIT: swprintf_s( wszOutput, 1024, L"You %s the lower check box.\nNow edit controls will %s", ( ( CDXUTCheckBox* )pControl )->GetChecked() ? L"checked" : L"cleared", ( ( CDXUTCheckBox* )pControl )->GetChecked() ? L"be cleared when you press Enter to send the text" : L"retain the text context when you press Enter to send the text" ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; case IDC_COMBOBOX: { DXUTComboBoxItem* pItem = ( ( CDXUTComboBox* )pControl )->GetSelectedItem(); if( pItem ) { swprintf_s( wszOutput, 1024, L"You selected a new item in the combobox.\nThe new item is \"%s\" and the associated data value is 0x%p", pItem->strText, pItem->pData ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); } break; } case IDC_RADIO1A: case IDC_RADIO1B: case IDC_RADIO1C: swprintf_s( wszOutput, 1024, L"You selected a new radio button in the UPPER radio group.\nThe new button is \"%s\"", ( ( CDXUTRadioButton* )pControl )->GetText() ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; case IDC_RADIO2A: case IDC_RADIO2B: case IDC_RADIO2C: swprintf_s( wszOutput, 1024, L"You selected a new radio button in the LOWER radio group.\nThe new button is \"%s\"", ( ( CDXUTRadioButton* )pControl )->GetText() ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; case IDC_LISTBOX: switch( nEvent ) { case EVENT_LISTBOX_ITEM_DBLCLK: { DXUTListBoxItem* pItem = ( ( CDXUTListBox* )pControl )->GetItem( ( ( CDXUTListBox* )pControl )->GetSelectedIndex( -1 ) ); swprintf_s( wszOutput, 1024, L"You double clicked an item in the left list box. The item is\n\"%s\"", pItem ? pItem->strText : L"" ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; } case EVENT_LISTBOX_SELECTION: { swprintf_s( wszOutput, 1024, L"You changed the selection in the left list box. The selected item is %d", ( ( CDXUTListBox* )pControl )->GetSelectedIndex() ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; } } break; case IDC_LISTBOXM: switch( nEvent ) { case EVENT_LISTBOX_ITEM_DBLCLK: { DXUTListBoxItem* pItem = ( ( CDXUTListBox* )pControl )->GetItem( ( ( CDXUTListBox* )pControl )->GetSelectedIndex( -1 ) ); swprintf_s( wszOutput, 1024, L"You double clicked an item in the right list box. The item is\n\"%s\"", pItem ? pItem->strText : L"" ); g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; } case EVENT_LISTBOX_SELECTION: { swprintf_s( wszOutput, 1024, L"You changed the selection in the right list box. The selected item(s) are\n" ); int nSelected = -1; while( ( nSelected = ( ( CDXUTListBox* )pControl )->GetSelectedIndex( nSelected ) ) != -1 ) { swprintf_s( wszOutput + lstrlenW( wszOutput ), 1024 - lstrlenW( wszOutput ), L"%d,", nSelected ); } // Remove the trailing comma if one exists. if( wszOutput[lstrlenW( wszOutput ) - 1] == L',' ) wszOutput[lstrlenW( wszOutput ) - 1] = L'\0'; g_SampleUI.GetStatic( IDC_OUTPUT )->SetText( wszOutput ); break; } } break; } }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10CreateDevice( ID3D10Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( g_D3DSettingsDlg.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( D3DX10CreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont10 ) ); V_RETURN( D3DX10CreateSprite( pd3dDevice, MAX_SPRITES, &g_pSprite10 ) ); V_RETURN( CDXUTDirectionWidget::StaticOnD3D10CreateDevice( pd3dDevice ) ); // Read the D3DX effect file WCHAR str[MAX_PATH]; DWORD dwShaderFlags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3D10_SHADER_DEBUG; #endif V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Exercise06.fx" ) ); V_RETURN( D3DX10CreateEffectFromFile( str, NULL, NULL, "fx_4_0", dwShaderFlags, 0, pd3dDevice, NULL, NULL, &g_pEffect10, NULL, NULL ) ); // Obtain the technique handles g_pRenderTextured = g_pEffect10->GetTechniqueByName( "RenderTextured" ); g_pGrowBranches = g_pEffect10->GetTechniqueByName( "GrowBranches" ); // Obtain the parameter handles g_pmWorldViewProj = g_pEffect10->GetVariableByName( "g_mWorldViewProj" )->AsMatrix(); g_pmWorld = g_pEffect10->GetVariableByName( "g_mWorld" )->AsMatrix(); g_pmWorldView = g_pEffect10->GetVariableByName( "g_mWorldView" )->AsMatrix(); g_pViewSpaceLightDir = g_pEffect10->GetVariableByName( "g_ViewSpaceLightDir" )->AsVector(); g_pLengthModifier = g_pEffect10->GetVariableByName( "LengthModifier" )->AsScalar(); g_pSpreadModifier = g_pEffect10->GetVariableByName( "SpreadModifier" )->AsScalar(); g_pShrinkModifier = g_pEffect10->GetVariableByName( "ShrinkModifier" )->AsScalar(); g_pExtinctionRate = g_pEffect10->GetVariableByName( "ExtinctionRate" )->AsScalar(); g_pDiffuseTex = g_pEffect10->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); g_pRandomBuf = g_pEffect10->GetVariableByName( "g_RandomBuffer" )->AsShaderResource(); // Define our vertex data layout const D3D10_INPUT_ELEMENT_DESC layout[] = { { "POS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "GROWAMT", 0, DXGI_FORMAT_R32_FLOAT, 0, 32, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; D3D10_PASS_DESC PassDesc; UINT numElements = sizeof( layout ) / sizeof( D3D10_INPUT_ELEMENT_DESC ); g_pRenderTextured->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout ) ); // Load our IB and VB with mesh data V_RETURN( LoadMesh( pd3dDevice ) ); // Create our StreamTo and DrawFrom buffers const UINT MaxVertices = 300000; D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = MaxVertices * g_VertStride; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER | D3D10_BIND_STREAM_OUTPUT; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; V_RETURN( pd3dDevice->CreateBuffer( &bd, NULL, &g_pStreamTo ) ); V_RETURN( pd3dDevice->CreateBuffer( &bd, NULL, &g_pDrawFrom ) ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -15.0f ); D3DXVECTOR3 vecAt( 0.0f,0.0f,0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); g_pLengthModifier->SetFloat( g_LengthModifier ); g_pSpreadModifier->SetFloat( g_SpreadModifier ); g_pShrinkModifier->SetFloat( g_ShrinkModifier ); g_pExtinctionRate->SetFloat( g_ExtinctionRate ); //----------------------------------------------------------------------------------------- // o/__ <-- Create a buffer full of random vectors. These will be indexed by our // | (\ tree generation algorithm to give the tree a more natural look. //----------------------------------------------------------------------------------------- int iNumRandValues = 1024; srand( 200 ); //create the data D3D10_SUBRESOURCE_DATA InitData; InitData.pSysMem = new float[iNumRandValues * 4]; if( !InitData.pSysMem ) return E_OUTOFMEMORY; for( int i = 0; i < iNumRandValues * 3; i++ ) { ( ( float* )InitData.pSysMem )[i] = float( ( rand() % 10000 ) - 5000 ) / 5000.0f; } D3D10_BUFFER_DESC bufferDesc; bufferDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE; bufferDesc.ByteWidth = 1024 * 4 * sizeof( float ); bufferDesc.CPUAccessFlags = 0; bufferDesc.MiscFlags = 0; bufferDesc.Usage = D3D10_USAGE_DEFAULT; V_RETURN( pd3dDevice->CreateBuffer( &bufferDesc, &InitData, &g_pRandomBuffer ) ); D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc; ZeroMemory( &SRVDesc, sizeof( SRVDesc ) ); SRVDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_BUFFER; SRVDesc.Buffer.ElementOffset = 0; SRVDesc.Buffer.ElementWidth = 1024; V_RETURN( pd3dDevice->CreateShaderResourceView( g_pRandomBuffer, &SRVDesc, &g_pRandomBufferRV ) ); SAFE_DELETE_ARRAY( InitData.pSysMem ); return S_OK; }
//-------------------------------------------------------------------------------------- // Create any D3D10 resources that aren't dependant on the back buffer //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D10CreateDevice( ID3D10Device* pd3dDevice, const DXGI_SURFACE_DESC* pBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( g_D3DSettingsDlg.OnD3D10CreateDevice( pd3dDevice ) ); V_RETURN( D3DX10CreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont ) ); V_RETURN( D3DX10CreateSprite( pd3dDevice, 512, &g_pSprite ) ); g_pTxtHelper = new CDXUTTextHelper( NULL, NULL, g_pFont, g_pSprite, 15 ); DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3D10_SHADER_DEBUG; #endif // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Tutorial13.fx" ) ); V_RETURN( D3DX10CreateEffectFromFile( str, NULL, NULL, "fx_4_0", dwShaderFlags, 0, pd3dDevice, NULL, NULL, &g_pEffect, NULL, NULL ) ); // Obtain the technique g_pTechnique = g_pEffect->GetTechniqueByName( "Render" ); // Obtain the variables g_ptxDiffuseVariable = g_pEffect->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); g_pWorldVariable = g_pEffect->GetVariableByName( "World" )->AsMatrix(); g_pViewVariable = g_pEffect->GetVariableByName( "View" )->AsMatrix(); g_pProjectionVariable = g_pEffect->GetVariableByName( "Projection" )->AsMatrix(); g_pExplodeVariable = g_pEffect->GetVariableByName( "Explode" )->AsScalar(); // Set Waviness g_pExplodeVariable->SetFloat( g_fExplode ); // Define the input layout const D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; UINT numElements = sizeof( layout ) / sizeof( layout[0] ); // Create the input layout D3D10_PASS_DESC PassDesc; g_pTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pVertexLayout ) ); // Set the input layout pd3dDevice->IASetInputLayout( g_pVertexLayout ); // Load the mesh V_RETURN( g_Mesh.Create( pd3dDevice, L"Tiny\\tiny.sdkmesh", true ) ); // Initialize the world matrices D3DXMatrixIdentity( &g_World ); // Initialize the camera D3DXVECTOR3 Eye( 0.0f, 0.0f, -800.0f ); D3DXVECTOR3 At( 0.0f, 0.0f, 0.0f ); g_Camera.SetViewParams( &Eye, &At ); return S_OK; }
//-------------------------------------------------------------------------------------- // Initialize the app //-------------------------------------------------------------------------------------- void InitApp() { camera = new CCamera; manipulator = new CManipulator; g_SettingsDlg.Init( &g_DialogResourceManager ); g_HUD.Init( &g_DialogResourceManager ); g_SampleUI.Init( &g_DialogResourceManager ); //g_HUD g_HUD.SetCallback( OnGUIEvent ); g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 5, 10, 125, 22, VK_F12 ); //fonts g_HUD.SetFont( 1, L"Arial", 20, FW_BOLD ); g_HUD.SetFont( 2, L"Arial", 16, FW_NORMAL ); g_HUD.AddEditBox( IDC_NEWFILE, L"testconfig.ini", 150, 10, 120, 30 ); g_HUD.AddButton( IDC_NEWFILE_BTN, L"New", 280, 15, 60, 20); g_HUD.AddEditBox( IDC_LOADFILE, L"testconfig.ini", 150, 40, 120, 30 ); g_HUD.AddButton( IDC_LOADFILE_BTN, L"Load", 280, 45, 60, 20); g_HUD.AddEditBox( IDC_SAVEFILE, L"testconfig.ini", 150, 70, 120, 30 ); g_HUD.AddButton( IDC_SAVEFILE_BTN, L"Save", 280, 75, 60, 20); g_HUD.AddEditBox( IDC_CMD, L"", 5, 5, 300, 30 ); g_HUD.AddStatic( IDC_STATIC7, L"status", 5, 5, 300, 30 ); g_HUD.GetStatic( IDC_STATIC7 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_HUD.GetStatic( IDC_STATIC7 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_HUD.GetStatic( IDC_STATIC7 )->GetElement(0)->iFont = 2; //g_SampleUI g_SampleUI.SetCallback( OnGUIEvent ); int iY = 10; g_SampleUI.SetVisible(false); //fonts g_SampleUI.SetFont( 1, L"Arial", 20, FW_BOLD ); g_SampleUI.SetFont( 2, L"Arial", 16, FW_NORMAL ); g_SampleUI.AddStatic( IDC_OBJNAME, L"Pick object", 0, 0, 200, 30 ); g_SampleUI.GetStatic( IDC_OBJNAME )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_OBJNAME )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_OBJNAME )->GetElement(0)->iFont = 1; g_SampleUI.AddStatic( IDC_STATIC7, L"angleX:",210,0,50,30 ); g_SampleUI.GetStatic( IDC_STATIC7 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC7 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC7 )->GetElement(0)->iFont = 2; g_SampleUI.AddStatic( IDC_STATIC8, L"angleY:",260,0,50,30 ); g_SampleUI.GetStatic( IDC_STATIC8 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC8 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC8 )->GetElement(0)->iFont = 2; //width edit box g_SampleUI.AddStatic( IDC_STATIC1, L"Width", 0, 50, 200, 30 ); g_SampleUI.GetStatic( IDC_STATIC1 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC1 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC1 )->GetElement(0)->iFont = 2; g_SampleUI.AddEditBox( IDC_WIDTH, L"", 70, 45, 120, 30 ); g_SampleUI.AddSlider( IDC_WIDTH_SLIDER, 200, 50, 150, 24, 1, 75, 20, false ); //length edit box g_SampleUI.AddStatic( IDC_STATIC2, L"Length", 0, 80, 200, 30 ); g_SampleUI.GetStatic( IDC_STATIC2 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC2 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC2 )->GetElement(0)->iFont = 2; g_SampleUI.AddEditBox( IDC_LENGTH, L"", 70, 75, 120, 30 ); g_SampleUI.AddSlider( IDC_LENGTH_SLIDER, 200, 80, 150, 24, 1, 400, 100, false ); //angle restrictions g_SampleUI.AddStatic( IDC_STATIC3, L"Angles restrictions", 0, 110, 200, 30 ); g_SampleUI.GetStatic( IDC_STATIC3 )->GetElement( 0 )->dwTextFormat = DT_LEFT | DT_TOP; g_SampleUI.GetStatic( IDC_STATIC3 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC3 )->GetElement(0)->iFont = 2; g_SampleUI.AddStatic( IDC_STATIC4, L"X", 20, 130, 200, 30 ); g_SampleUI.GetStatic( IDC_STATIC4 )->GetElement( 0 )->dwTextFormat = DT_LEFT | DT_TOP; g_SampleUI.GetStatic( IDC_STATIC4 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC4 )->GetElement(0)->iFont = 2; g_SampleUI.AddStatic( IDC_STATIC5, L"Y", 20, 160, 200, 30 ); g_SampleUI.GetStatic( IDC_STATIC5 )->GetElement( 0 )->dwTextFormat = DT_LEFT | DT_TOP; g_SampleUI.GetStatic( IDC_STATIC5 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC5 )->GetElement(0)->iFont = 2; g_SampleUI.AddEditBox( IDC_ANGLE_RESTRICT_XL, L"", 70, 125, 100, 30 ); g_SampleUI.AddButton( IDC_ANGLE_RESTRICT_XL_BTN, L"A", 170, 130, 22, 22 ); g_SampleUI.AddEditBox( IDC_ANGLE_RESTRICT_XU, L"", 220, 125, 100, 30 ); g_SampleUI.AddButton( IDC_ANGLE_RESTRICT_XU_BTN, L"A", 320, 130, 22, 22 ); g_SampleUI.AddEditBox( IDC_ANGLE_RESTRICT_YL, L"", 70, 155, 100, 30 ); g_SampleUI.AddButton( IDC_ANGLE_RESTRICT_YL_BTN, L"A", 170, 160, 22, 22 ); g_SampleUI.AddEditBox( IDC_ANGLE_RESTRICT_YU, L"", 220, 155, 100, 30 ); g_SampleUI.AddButton( IDC_ANGLE_RESTRICT_YU_BTN, L"A", 320, 160, 22, 22 ); //add button g_SampleUI.AddButton( IDC_ADDCHAIN, L"Add", 15, 200, 100, 22 ); g_SampleUI.AddButton( IDC_REMOVECHAIN, L"Remove", 115, 200, 100, 22 ); //offset g_SampleUI.AddStatic( IDC_STATIC6, L"Offset", 0, 230, 70, 30 ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC6 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement(0)->iFont = 2; g_SampleUI.AddEditBox( IDC_OFFSET_X, L"", 70, 225, 80, 30 ); g_SampleUI.AddEditBox( IDC_OFFSET_Y, L"", 160, 225, 80, 30 ); g_SampleUI.AddEditBox( IDC_OFFSET_Z, L"", 250, 225, 80, 30 ); //physical stuff g_SampleUI.AddStatic( IDC_STATIC6, L"Displace", 0, 265, 70, 30 ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC6 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement(0)->iFont = 2; g_SampleUI.AddEditBox(IDC_DISPLACE, L"", 70, 265, 80, 30); g_SampleUI.AddStatic( IDC_STATIC6, L"Direction", 0, 305, 70, 30 ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC6 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement(0)->iFont = 2; g_SampleUI.AddEditBox(IDC_DIRECTION, L"", 70, 305, 80, 30); g_SampleUI.AddStatic( IDC_STATIC6, L"Coefficient", 0, 345, 70, 30 ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC6 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement(0)->iFont = 2; g_SampleUI.AddEditBox(IDC_COEFFICIENT, L"", 70, 345, 80, 30); g_SampleUI.AddStatic( IDC_STATIC6, L"Mass", 0, 385, 70, 30 ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC6 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement(0)->iFont = 2; g_SampleUI.AddEditBox(IDC_MASS, L"", 70, 385, 80, 30); g_SampleUI.AddStatic( IDC_STATIC6, L"Tilt", 0, 425, 70, 30 ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC6 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement(0)->iFont = 2; g_SampleUI.AddCheckBox( IDC_TILT, L"", 60, 425, 30, 30 ); g_SampleUI.AddStatic( IDC_STATIC6, L"Pressure", 100, 425, 70, 30 ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC6 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement(0)->iFont = 2; g_SampleUI.AddCheckBox( IDC_PRESSURE, L"", 160, 425, 30, 30 ); g_SampleUI.AddStatic( IDC_STATIC6, L"Laser", 200, 425, 70, 30 ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement( 0 )->dwTextFormat = DT_LEFT; g_SampleUI.GetStatic( IDC_STATIC6 )->SetTextColor( D3DCOLOR_ARGB( 255, 220, 220, 220 ) ); g_SampleUI.GetStatic( IDC_STATIC6 )->GetElement(0)->iFont = 2; g_SampleUI.AddCheckBox( IDC_LASER, L"", 260, 425, 30, 30 ); }
//-------------------------------------------------------------------------------------- // 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(); }
//-------------------------------------------------------------------------------------- // 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[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_mCenterMesh * *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_pEffect9->SetValue( g_hLightDir, vLightDir, sizeof( D3DXVECTOR3 ) * MAX_LIGHTS ) ); V( g_pEffect9->SetValue( g_hLightDiffuse, 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_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() ); } }
//-------------------------------------------------------------------------------------- // Release D3D11 resources created in OnD3D11CreateDevice //-------------------------------------------------------------------------------------- void CALLBACK OnD3D11DestroyDevice( void* pUserContext ) { g_DialogResourceManager.OnD3D11DestroyDevice(); g_D3DSettingsDlg.OnD3D11DestroyDevice(); DXUTGetGlobalResourceCache().OnDestroyDevice(); SAFE_DELETE( g_pTxtHelper ); g_Mesh11.Destroy(); SAFE_RELEASE(g_pGeometryShader); SAFE_RELEASE( g_pVertexLayout11 ); SAFE_RELEASE( g_pVertexBuffer ); SAFE_RELEASE( g_pVertexShader ); SAFE_RELEASE( g_pPixelShader ); SAFE_RELEASE( g_pSamLinear ); SAFE_RELEASE( g_pcbVSPerObject ); SAFE_RELEASE( g_pcbPSPerObject ); SAFE_RELEASE( g_pcbPSPerFrame ); SAFE_RELEASE( g_pRasterizerState ); SAFE_RELEASE( g_pRasterizerStateWF ); for( int flagIndex = 0; flagIndex < numFlags; ++flagIndex ) { cloths[flagIndex].destroy(); } my_capsule.destroy(); // Shouldn't need to delete this as it's just a soft body and will be deleted later by the collision object cleanup. //for( int flagIndex = 0; flagIndex < m_flags.size(); ++flagIndex ) //{ //delete m_flags[flagIndex]; //} //cleanup in the reverse order of creation/initialization if( g_defaultSolver ) delete g_defaultSolver; if( g_cpuSolver ) delete g_cpuSolver; if( g_dx11Solver ) delete g_dx11Solver; if( g_dx11SIMDSolver ) delete g_dx11SIMDSolver; if( g_softBodyOutput ) delete g_softBodyOutput; for(int i=0; i< m_collisionShapes.size(); i++) delete m_collisionShapes[i]; //remove the rigidbodies from the dynamics world and delete them int i; for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--) { btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i]; btRigidBody* body = btRigidBody::upcast(obj); if (body && body->getMotionState()) { delete body->getMotionState(); } m_dynamicsWorld->removeCollisionObject( obj ); delete obj; } delete m_dynamicsWorld; delete m_solver; delete m_broadphase; delete m_dispatcher; delete m_collisionConfiguration; }