//-------------------------------------------------------------------------------------- // Initialize the app //-------------------------------------------------------------------------------------- void InitApp() { //g_LightControl.SetLightDirection( D3DXVECTOR3(-0.29f, 0.557f, 0.778f) ); g_LightControl.SetLightDirection( D3DXVECTOR3( -0.789f, 0.527f, 0.316f ) ); g_LightControl.SetButtonMask( MOUSE_MIDDLE_BUTTON ); // Initialize dialogs g_SettingsDlg.Init( &g_DialogResourceManager ); g_HUD.Init( &g_DialogResourceManager ); g_SampleUI.Init( &g_DialogResourceManager ); g_HUD.SetCallback( OnGUIEvent ); int iX = 15; int iY = 10; g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", iX, iY, 125, 22 ); g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", iX, iY += 24, 125, 22 ); g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", iX, iY += 24, 125, 22, VK_F2 ); g_SampleUI.SetCallback( OnGUIEvent ); iX = 15; iY = 10; // Title font for static g_SampleUI.SetFont( 1, L"Arial", 14, FW_NORMAL ); CDXUTElement* pElement = g_SampleUI.GetDefaultElement( DXUT_CONTROL_STATIC, 0 ); if( pElement ) { pElement->iFont = 1; pElement->dwTextFormat = DT_RIGHT | DT_VCENTER; } // Technique g_SampleUI.AddStatic( -1, L"Technique", iX, iY += 24, 115, 22 ); g_SampleUI.AddComboBox( IDC_TECHNIQUE, iX + 125, iY, 150, 22 ); g_SampleUI.GetComboBox( IDC_TECHNIQUE )->SetScrollBarWidth( 0 ); g_SampleUI.GetComboBox( IDC_TECHNIQUE )->AddItem( L"Local-deformable PRT", ( void* )"LDPRT" ); g_SampleUI.GetComboBox( IDC_TECHNIQUE )->AddItem( L"N dot L lighting", ( void* )"NdotL" ); // Animation speed iY += 10; g_SampleUI.AddStatic( -1, L"Animation Speed", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_ANIMATION_SPEED, iX + 125, iY, 125, 22, 0, 3000, 700 ); // Light intensity iY += 10; g_SampleUI.AddStatic( -1, L"Light Intensity", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_LIGHT_SLIDER, iX + 125, iY, 125, 22, 0, 1000, 500 ); g_SampleUI.AddStatic( IDC_ENV_LABEL, L"Env Intensity", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_ENV_SLIDER, iX + 125, iY, 125, 22, 0, 3000, 600 ); // Color transmission iY += 10; g_SampleUI.AddStatic( IDC_RED_TRANSMIT_LABEL, L"Transmit Red", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_RED_TRANSMIT_SLIDER, iX + 125, iY, 125, 22, 0, 3000, 1200 ); g_SampleUI.AddStatic( IDC_GREEN_TRANSMIT_LABEL, L"Transmit Green", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_GREEN_TRANSMIT_SLIDER, iX + 125, iY, 125, 22, 0, 3000, 800 ); g_SampleUI.AddStatic( IDC_BLUE_TRANSMIT_LABEL, L"Transmit Blue", iX, iY += 24, 115, 22 ); g_SampleUI.AddSlider( IDC_BLUE_TRANSMIT_SLIDER, iX + 125, iY, 125, 22, 0, 3000, 350 ); }
// Handle key presses void CALLBACK OnKeyboard( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext ) { if( bKeyDown ) { switch( nChar ) { case VK_F1: gShowHelp = !gShowHelp; break; case VK_TAB: { CDXUTComboBox *comboBox = gSampleUI.GetComboBox(IDC_IMAGEVIEW); if (eImageView_Uncompressed == (intptr_t)comboBox->GetSelectedData()) { comboBox->SetSelectedByData((void*)eImageView_Compressed); } else if (eImageView_Compressed == (intptr_t)comboBox->GetSelectedData()) { comboBox->SetSelectedByData((void*)eImageView_Uncompressed); } gSampleUI.SendEvent(IDC_IMAGEVIEW, true, comboBox); break; } } } }
//-------------------------------------------------------------------------------------- // Update the MSAA sample count combo box for this format //-------------------------------------------------------------------------------------- void UpdateMSAASampleCounts( ID3D10Device* pd3dDevice, DXGI_FORMAT fmt ) { CDXUTComboBox* pComboBox = NULL; bool bResetSampleCount = false; UINT iHighestSampleCount = 0; pComboBox = g_SampleUI.GetComboBox( IDC_SAMPLE_COUNT ); if( !pComboBox ) return; pComboBox->RemoveAllItems(); WCHAR val[10]; for( UINT i = 1; i <= D3D10_MAX_MULTISAMPLE_SAMPLE_COUNT; i++ ) { UINT Quality; if( SUCCEEDED( pd3dDevice->CheckMultisampleQualityLevels( fmt, i, &Quality ) ) && Quality > 0 ) { swprintf_s( val, 10, L"%d", i ); pComboBox->AddItem( val, IntToPtr( i ) ); iHighestSampleCount = i; } else if( g_MSAASampleCount == i ) { bResetSampleCount = true; } } if( bResetSampleCount ) g_MSAASampleCount = iHighestSampleCount; pComboBox->SetSelectedByData( IntToPtr( g_MSAASampleCount ) ); }
void CALLBACK onKeyboard(UINT nchar, bool keyDown, bool altDown, void *userContext) { if (keyDown) switch (nchar) { case VK_TAB: { if (keyDown) showHud = !showHud; break; } case '1': case '2': case '3': case '4': case '5': { hud.GetComboBox(IDC_PRESET)->SetSelectedByIndex(nchar - '1'); onLostDevice(NULL); onResetDevice(DXUTGetD3D9Device(), DXUTGetD3D9BackBufferSurfaceDesc(), NULL); break; } case 'X': hud.GetCheckBox(IDC_PROFILE)->SetChecked(!hud.GetCheckBox(IDC_PROFILE)->GetChecked()); timer->setEnabled(hud.GetCheckBox(IDC_PROFILE)->GetChecked()); break; case 'Z': hud.GetCheckBox(IDC_ANTIALIASING)->SetChecked(!hud.GetCheckBox(IDC_ANTIALIASING)->GetChecked()); break; } }
//-------------------------------------------------------------------------------------- // This callback function will be called at the end of every frame to perform all the // rendering calls for the scene, and it will also be called if the window needs to be // repainted. After this function has returned, DXUT will call // IDirect3DDevice9::Present to display the contents of the next buffer in the swap chain //-------------------------------------------------------------------------------------- void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { // If the settings dialog is being shown, then // render it instead of rendering the app's scene if( g_SettingsDlg.IsActive() ) { g_SettingsDlg.OnRender( fElapsedTime ); return; } HRESULT hr; D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mWorldViewProjection; // Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 141, 153, 191 ), 1.0f, 0 ) ); // Render the scene if( SUCCEEDED( pd3dDevice->BeginScene() ) ) { // Get the projection & view matrix from the camera class mWorld = *g_Camera.GetWorldMatrix(); mView = *g_Camera.GetViewMatrix(); mProj = *g_Camera.GetProjMatrix(); mWorldViewProjection = mWorld * mView * mProj; // Update the effect's variables. V( g_pEffect->SetMatrix( g_hWorldViewProjection, &mWorldViewProjection ) ); V( g_pEffect->SetMatrix( g_hWorld, &mWorld ) ); V( g_pEffect->SetFloat( g_hTime, ( float )fTime ) ); V( g_pEffect->SetValue( g_hCameraPosition, g_Camera.GetEyePt(), sizeof( D3DXVECTOR3 ) ) ); UINT iCurSubset = ( UINT )( INT_PTR )g_SampleUI.GetComboBox( IDC_SUBSET )->GetSelectedData(); // A subset of -1 was arbitrarily chosen to represent all subsets if( iCurSubset == -1 ) { // Iterate through subsets, changing material properties for each for( UINT iSubset = 0; iSubset < g_MeshLoader.GetNumMaterials(); iSubset++ ) { RenderSubset( iSubset ); } } else { RenderSubset( iCurSubset ); } RenderText(); V( g_HUD.OnRender( fElapsedTime ) ); V( g_SampleUI.OnRender( fElapsedTime ) ); V( pd3dDevice->EndScene() ); } }
void initApp() { hud.Init(&dialogResourceManager); hud.SetCallback(onGUIEvent); int iY = 10; hud.AddButton(IDC_TOGGLE_FULLSCREEN, L"Toggle full screen", 35, iY, HUD_WIDTH, 22); iY += 24; hud.AddComboBox(IDC_PRESET, 35, iY += 24, HUD_WIDTH, 22, 0, false); hud.GetComboBox(IDC_PRESET)->AddItem(L"SMAA Low", (LPVOID) 0); hud.GetComboBox(IDC_PRESET)->AddItem(L"SMAA Medium", (LPVOID) 1); hud.GetComboBox(IDC_PRESET)->AddItem(L"SMAA High", (LPVOID) 2); hud.GetComboBox(IDC_PRESET)->AddItem(L"SMAA Ultra", (LPVOID) 3); hud.GetComboBox(IDC_PRESET)->AddItem(L"SMAA Custom", (LPVOID) 4); hud.GetComboBox(IDC_PRESET)->SetSelectedByData((LPVOID) 2); hud.AddComboBox(IDC_DETECTION_MODE, 35, iY += 24, HUD_WIDTH, 22, 0, false); hud.GetComboBox(IDC_DETECTION_MODE)->AddItem(L"Luma edge det.", (LPVOID) 0); hud.GetComboBox(IDC_DETECTION_MODE)->AddItem(L"Color edge det.", (LPVOID) 1); hud.GetComboBox(IDC_DETECTION_MODE)->AddItem(L"Depth edge det.", (LPVOID) 2); hud.AddCheckBox(IDC_ANTIALIASING, L"SMAA Anti-Aliasing", 35, iY += 24, HUD_WIDTH, 22, true); hud.AddCheckBox(IDC_PROFILE, L"Profile", 35, iY += 24, 125, 22, false); wstringstream s; s << L"Threshold: " << commandlineOptions.threshold; hud.AddStatic(IDC_THRESHOLD_LABEL, s.str().c_str(), 35, iY += 24, HUD_WIDTH, 22); hud.AddSlider(IDC_THRESHOLD, 35, iY += 24, HUD_WIDTH, 22, 0, 100, int(100.0f * commandlineOptions.threshold / 0.5f)); hud.GetStatic(IDC_THRESHOLD_LABEL)->SetVisible(false); hud.GetSlider(IDC_THRESHOLD)->SetVisible(false); s = wstringstream(); s << L"Max Search Steps: " << commandlineOptions.searchSteps; hud.AddStatic(IDC_MAX_SEARCH_STEPS_LABEL, s.str().c_str(), 35, iY += 24, HUD_WIDTH, 22); hud.AddSlider(IDC_MAX_SEARCH_STEPS, 35, iY += 24, HUD_WIDTH, 22, 0, 100, int(100.0f * commandlineOptions.searchSteps / 98.0f)); hud.GetStatic(IDC_MAX_SEARCH_STEPS_LABEL)->SetVisible(false); hud.GetSlider(IDC_MAX_SEARCH_STEPS)->SetVisible(false); s = wstringstream(); s << L"Max Diag. Search Steps: " << commandlineOptions.diagSearchSteps; hud.AddStatic(IDC_MAX_SEARCH_STEPS_DIAG_LABEL, s.str().c_str(), 35, iY += 24, HUD_WIDTH, 22); hud.AddSlider(IDC_MAX_SEARCH_STEPS_DIAG, 35, iY += 24, HUD_WIDTH, 22, 0, 100, int(100.0f * commandlineOptions.diagSearchSteps / 20.0f)); hud.GetStatic(IDC_MAX_SEARCH_STEPS_DIAG_LABEL)->SetVisible(false); hud.GetSlider(IDC_MAX_SEARCH_STEPS_DIAG)->SetVisible(false); s = wstringstream(); s << L"Corner Rounding: " << commandlineOptions.cornerRounding; hud.AddStatic(IDC_CORNER_ROUNDING_LABEL, s.str().c_str(), 35, iY += 24, HUD_WIDTH, 22); hud.AddSlider(IDC_CORNER_ROUNDING, 35, iY += 24, HUD_WIDTH, 22, 0, 100, int(100.0f * commandlineOptions.cornerRounding / 100.0f)); hud.GetStatic(IDC_CORNER_ROUNDING_LABEL)->SetVisible(false); hud.GetSlider(IDC_CORNER_ROUNDING)->SetVisible(false); }
void FillProfiles(BOOL DX11Available) { CDXUTComboBox *comboBox = gSampleUI.GetComboBox(IDC_PROFILE); if (DX11Available) { comboBox->AddItem(L"BC6H veryfast", (void *)(CompressImageBC6H_veryfast)); comboBox->AddItem(L"BC6H fast", (void *)(CompressImageBC6H_fast)); comboBox->AddItem(L"BC6H basic", (void *)(CompressImageBC6H_basic)); comboBox->AddItem(L"BC6H slow", (void *)(CompressImageBC6H_slow)); comboBox->AddItem(L"BC6H veryslow", (void *)(CompressImageBC6H_veryslow)); comboBox->SetDropHeight((12-1)*17); } comboBox->SetSelectedByData((void *)(gCompressionFunc)); }
//-------------------------------------------------------------------------------------- // Handle key presses //-------------------------------------------------------------------------------------- void CALLBACK OnKeyboard( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext ) { switch (nChar) { case '1': case '2': case '3': case '4': { int group = (nChar - '1'); auto pComboBox = g_SampleUI.GetComboBox( IDC_GROUP ); assert(pComboBox != NULL); pComboBox->SetSelectedByData( IntToPtr( group ) ); SetViewForGroup( group ); } break; } }
HRESULT CALLBACK onResetDevice(IDirect3DDevice9 *device, const D3DSURFACE_DESC *desc, void *userContext) { HRESULT hr; V_RETURN(dialogResourceManager.OnD3D9ResetDevice()); if (font) V_RETURN(font->OnResetDevice()); timer = new Timer(device); timer->setEnabled(hud.GetCheckBox(IDC_PROFILE)->GetChecked()); timer->setRepetitionsCount(100); SMAA::Preset preset = SMAA::Preset(int(hud.GetComboBox(IDC_PRESET)->GetSelectedData())); if(int(preset) == 4) { setVisibleCustomControls( true); } else { setVisibleCustomControls(false); } smaa = new SMAA(device, desc->Width, desc->Height, preset); V(device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbufferSurface)); V(device->CreateTexture(desc->Width, desc->Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &finalbufferColorTex, NULL)); V(finalbufferColorTex->GetSurfaceLevel(0, &finalbufferColorSurface)); V(device->CreateTexture(desc->Width, desc->Height, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &finalbufferDepthTex, NULL)); V(finalbufferDepthTex->GetSurfaceLevel(0, &finalbufferDepthSurface)); D3DXIMAGE_INFO info; V(D3DXGetImageInfoFromResource(NULL, L"Unigine02.png", &info)); V(D3DXCreateTextureFromResourceEx(device, NULL, L"Unigine02.png", info.Width, info.Height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, &info, NULL, &colorTex)); V(D3DXGetImageInfoFromResource(NULL, L"Unigine02.dds", &info)); V(D3DXCreateTextureFromResourceEx(device, NULL, L"Unigine02.dds", info.Width, info.Height, 1, 0, D3DFMT_R32F, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, &info, NULL, &depthTex)); V_RETURN(D3DXCreateSprite(device, &sprite)); txtHelper = new CDXUTTextHelper(font, sprite, NULL, NULL, 15); hud.SetLocation(desc->Width - 170, 0); hud.SetSize(170, 170); return S_OK; }
// Initialize the app void InitApp() { // Initialize dialogs gD3DSettingsDlg.Init(&gDialogResourceManager); gHUD.Init(&gDialogResourceManager); gSampleUI.Init(&gDialogResourceManager); gHUD.SetCallback(OnGUIEvent); int x = 0; int y = 10; gHUD.AddButton(IDC_TOGGLEFULLSCREEN, L"Toggle full screen", x, y, 170, 23); gHUD.AddButton(IDC_TOGGLEREF, L"Toggle REF (F3)", x, y += 26, 170, 23, VK_F3); gHUD.AddButton(IDC_CHANGEDEVICE, L"Change device (F2)", x, y += 26, 170, 23, VK_F2); gHUD.SetSize( 170, 170 ); gSampleUI.SetCallback(OnGUIEvent); x = 0; y = 0; gSampleUI.AddStatic(IDC_TEXT, L"", x, y, 1, 1); y += 5*22; gSampleUI.AddComboBox(IDC_PROFILE, x, y, 226, 22); y += 26; gSampleUI.AddCheckBox(IDC_MT, L"Multithreaded", x, y, 125, 22, gMultithreaded); gSampleUI.AddButton(IDC_RECOMPRESS, L"Recompress", x + 131, y, 125, 22); y += 26; gSampleUI.AddComboBox(IDC_IMAGEVIEW, x, y, 145, 22); gSampleUI.AddCheckBox(IDC_ALPHA, L"Show Alpha", x + 151, y, 105, 22); y += 26; gSampleUI.AddSlider(IDC_EXPOSURE, x, y, 250, 22); y += 26; gSampleUI.AddButton(IDC_LOAD_TEXTURE, L"Load Texture", x, y, 125, 22); gSampleUI.AddButton(IDC_SAVE_TEXTURE, L"Save Texture", x + 131, y, 125, 22); y += 26; gSampleUI.SetSize( 276, y+150 ); { CDXUTComboBox *comboBox = gSampleUI.GetComboBox(IDC_IMAGEVIEW); comboBox->AddItem(L"Uncompressed", (void *)(eImageView_Uncompressed)); comboBox->AddItem(L"Compressed", (void *)(eImageView_Compressed)); //comboBox->AddItem(L"Error", (void *)(eImageView_Error)); //comboBox->AddItem(L"All", (void *)(eImageView_All)); comboBox->SetSelectedByData((void *)(gImageView)); } gSampleUI.SendEvent(IDC_TEXT, true, gSampleUI.GetStatic(IDC_TEXT)); }
//-------------------------------------------------------------------------------------- // 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 ); // Create other render resources here g_States = std::make_unique<CommonStates>( pd3dDevice ); g_Batch = std::make_unique<PrimitiveBatch<VertexPositionColor>>( pd3dImmediateContext ); g_BatchEffect = std::make_unique<BasicEffect>( pd3dDevice ); g_BatchEffect->SetVertexColorEnabled(true); { void const* shaderByteCode; size_t byteCodeLength; g_BatchEffect->GetVertexShaderBytecode( &shaderByteCode, &byteCodeLength ); hr = pd3dDevice->CreateInputLayout( VertexPositionColor::InputElements, VertexPositionColor::InputElementCount, shaderByteCode, byteCodeLength, &g_pBatchInputLayout ); if( FAILED( hr ) ) return hr; } // Setup the camera's view parameters auto pComboBox = g_SampleUI.GetComboBox( IDC_GROUP ); SetViewForGroup( (pComboBox) ? (int)PtrToInt( pComboBox->GetSelectedData() ) : 0 ); g_HUD.GetButton( IDC_TOGGLEWARP )->SetEnabled( true ); return S_OK; }
//-------------------------------------------------------------------------------------- // LoadRasterizerStates // Create a set of rasterizer states for non-FX state managment. These states // will later be set using RSSetState in OnD3D10FrameRender. //-------------------------------------------------------------------------------------- void LoadRasterizerStates( ID3D10Device* pd3dDevice ) { D3D10_FILL_MODE fill[MAX_RASTERIZER_MODES] = { D3D10_FILL_SOLID, D3D10_FILL_SOLID, D3D10_FILL_SOLID, D3D10_FILL_WIREFRAME, D3D10_FILL_WIREFRAME, D3D10_FILL_WIREFRAME }; D3D10_CULL_MODE cull[MAX_RASTERIZER_MODES] = { D3D10_CULL_NONE, D3D10_CULL_FRONT, D3D10_CULL_BACK, D3D10_CULL_NONE, D3D10_CULL_FRONT, D3D10_CULL_BACK }; for( UINT i = 0; i < MAX_RASTERIZER_MODES; i++ ) { D3D10_RASTERIZER_DESC rasterizerState; rasterizerState.FillMode = fill[i]; rasterizerState.CullMode = cull[i]; rasterizerState.FrontCounterClockwise = false; rasterizerState.DepthBias = false; rasterizerState.DepthBiasClamp = 0; rasterizerState.SlopeScaledDepthBias = 0; rasterizerState.DepthClipEnable = true; rasterizerState.ScissorEnable = false; rasterizerState.MultisampleEnable = false; rasterizerState.AntialiasedLineEnable = false; pd3dDevice->CreateRasterizerState( &rasterizerState, &g_pRasterStates[i] ); g_SampleUI.GetComboBox( IDC_SCENERASTERIZER_MODE )->AddItem( g_szRasterizerModes[i], ( void* )( UINT64 )i ); } }
void CALLBACK onFrameRender(IDirect3DDevice9 *device, double time, float elapsedTime, void *userContext) { HRESULT hr; // IMPORTANT: stencil must be cleared to zero before executing 'smaa->go' V(device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0)); V(device->BeginScene()); // This emulates main pass. mainPass(device); // Run SMAA if (hud.GetCheckBox(IDC_ANTIALIASING)->GetChecked()) { SMAA::Input input = SMAA::Input(int(hud.GetComboBox(IDC_DETECTION_MODE)->GetSelectedData())); int n = hud.GetCheckBox(IDC_PROFILE)->GetChecked()? timer->getRepetitionsCount() : 1; timer->start(); for (int i = 0; i < n; i++) { // This loop is just for profiling. switch (input) { case SMAA::INPUT_LUMA: case SMAA::INPUT_COLOR: smaa->go(finalbufferColorTex, finalbufferColorTex, backbufferSurface, input); break; case SMAA::INPUT_DEPTH: smaa->go(finalbufferDepthTex, finalbufferColorTex, backbufferSurface, input); break; } } timer->clock(L"SMAA"); } else { copy(device); } // Draw the HUD drawHud(elapsedTime); V(device->EndScene()); }
//-------------------------------------------------------------------------------------- // 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, L"media\\cup.obj" ) ); // 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 ); } // 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 ); return S_OK; }
//-------------------------------------------------------------------------------------- // This callback function will be called immediately after the Direct3D device has been // created, which will happen during application initialization and windowed/full screen // toggles. This is the best location to create D3DPOOL_MANAGED resources since these // resources need to be reloaded whenever the device is destroyed. Resources created // here should be released in the OnDestroyDevice callback. //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; WCHAR str[MAX_PATH]; 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 ) ); // Create the mesh and load it with data already gathered from a file V_RETURN( g_MeshLoader.Create( pd3dDevice, L"media\\cup.obj" ) ); // Add the identified material subsets to the UI CDXUTComboBox* pComboBox = g_SampleUI.GetComboBox( IDC_SUBSET ); pComboBox->RemoveAllItems(); pComboBox->AddItem( L"All", ( void* )( INT_PTR )-1 ); for( UINT i = 0; i < g_MeshLoader.GetNumMaterials(); i++ ) { Material* pMaterial = g_MeshLoader.GetMaterial( i ); pComboBox->AddItem( pMaterial->strName, ( void* )( INT_PTR )i ); } // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the // shader debugger. Debugging vertex shaders requires either REF or software vertex // processing, and debugging pixel shaders requires REF. The // D3DXSHADER_FORCE_*_SOFTWARE_NOOPT flag improves the debug experience in the // shader debugger. It enables source level debugging, prevents instruction // reordering, prevents dead code elimination, and forces the compiler to compile // against the next higher available software target, which ensures that the // unoptimized shaders do not exceed the shader model limitations. Setting these // flags will cause slower rendering since the shaders will be unoptimized and // forced into software. See the DirectX documentation for more information about // using the shader debugger. DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; #if defined( DEBUG ) || defined( _DEBUG ) // Set the D3DXSHADER_DEBUG flag to embed debug information in the shaders. // Setting this flag improves the shader debugging experience, but still allows // the shaders to be optimized and to run exactly the way they will run in // the release configuration of this program. dwShaderFlags |= D3DXSHADER_DEBUG; #endif #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif // Read the D3DX effect file V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"MeshFromOBJ.fx" ) ); // If this fails, there should be debug output as to // they the .fx file failed to compile V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, NULL ) ); // Cache the effect handles g_hAmbient = g_pEffect->GetParameterBySemantic( 0, "Ambient" ); g_hDiffuse = g_pEffect->GetParameterBySemantic( 0, "Diffuse" ); g_hSpecular = g_pEffect->GetParameterBySemantic( 0, "Specular" ); g_hOpacity = g_pEffect->GetParameterBySemantic( 0, "Opacity" ); g_hSpecularPower = g_pEffect->GetParameterBySemantic( 0, "SpecularPower" ); g_hLightColor = g_pEffect->GetParameterBySemantic( 0, "LightColor" ); g_hLightPosition = g_pEffect->GetParameterBySemantic( 0, "LightPosition" ); g_hCameraPosition = g_pEffect->GetParameterBySemantic( 0, "CameraPosition" ); g_hTexture = g_pEffect->GetParameterBySemantic( 0, "Texture" ); g_hTime = g_pEffect->GetParameterBySemantic( 0, "Time" ); g_hWorld = g_pEffect->GetParameterBySemantic( 0, "World" ); g_hWorldViewProjection = g_pEffect->GetParameterBySemantic( 0, "WorldViewProjection" ); // 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 ); return S_OK; }
//-------------------------------------------------------------------------------------- // 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; }
void CALLBACK onGUIEvent(UINT event, int controlId, CDXUTControl* control, void *userContext) { switch(controlId) { case IDC_TOGGLE_FULLSCREEN: DXUTToggleFullScreen(); break; case IDC_PRESET: if (event == EVENT_COMBOBOX_SELECTION_CHANGED) { SMAA::Preset selected; selected = SMAA::Preset(int(hud.GetComboBox(IDC_PRESET)->GetSelectedData())); onLostDevice(NULL); onResetDevice(DXUTGetD3D9Device(), DXUTGetD3D9BackBufferSurfaceDesc(), NULL); } break; case IDC_ANTIALIASING: if (event == EVENT_CHECKBOX_CHANGED) timer->reset(); break; case IDC_PROFILE: if (event == EVENT_CHECKBOX_CHANGED) { timer->reset(); timer->setEnabled(hud.GetCheckBox(IDC_PROFILE)->GetChecked()); } break; case IDC_THRESHOLD: if (event == EVENT_SLIDER_VALUE_CHANGED) { CDXUTSlider *slider = hud.GetSlider(IDC_THRESHOLD); int min, max; slider->GetRange(min, max); float scale = float(slider->GetValue()) / (max - min); smaa->setThreshold(scale * 0.5f); wstringstream s; s << L"Threshold: " << scale * 0.5f; hud.GetStatic(IDC_THRESHOLD_LABEL)->SetText(s.str().c_str()); } break; case IDC_MAX_SEARCH_STEPS: if (event == EVENT_SLIDER_VALUE_CHANGED) { CDXUTSlider *slider = hud.GetSlider(IDC_MAX_SEARCH_STEPS); int min, max; slider->GetRange(min, max); float scale = float(slider->GetValue()) / (max - min); smaa->setMaxSearchSteps(int(round(scale * 98.0f))); wstringstream s; s << L"Max Search Steps: " << int(round(scale * 98.0f)); hud.GetStatic(IDC_MAX_SEARCH_STEPS_LABEL)->SetText(s.str().c_str()); } break; case IDC_MAX_SEARCH_STEPS_DIAG: if (event == EVENT_SLIDER_VALUE_CHANGED) { CDXUTSlider *slider = hud.GetSlider(IDC_MAX_SEARCH_STEPS_DIAG); int min, max; slider->GetRange(min, max); float scale = float(slider->GetValue()) / (max - min); smaa->setMaxSearchStepsDiag(int(round(scale * 20.0f))); wstringstream s; s << L"Max Diag. Search Steps: " << int(round(scale * 20.0f)); hud.GetStatic(IDC_MAX_SEARCH_STEPS_DIAG_LABEL)->SetText(s.str().c_str()); } break; case IDC_CORNER_ROUNDING: if (event == EVENT_SLIDER_VALUE_CHANGED) { CDXUTSlider *slider = hud.GetSlider(IDC_CORNER_ROUNDING); int min, max; slider->GetRange(min, max); float scale = float(slider->GetValue()) / (max - min); smaa->setCornerRounding(scale * 100.0f); wstringstream s; s << L"Corner Rounding: " << scale * 100.0f; hud.GetStatic(IDC_CORNER_ROUNDING_LABEL)->SetText(s.str().c_str()); } 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(); }
//-------------------------------------------------------------------------------------- // 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; }
//-------------------------------------------------------------------------------------- // Render the scene using the D3D10 device //-------------------------------------------------------------------------------------- void CALLBACK OnD3D10FrameRender( ID3D10Device* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) { // If the settings dialog is being shown, then // render it instead of rendering the app's scene if( g_SettingsDlg.IsActive() ) { g_SettingsDlg.OnRender( fElapsedTime ); return; } // // Clear the back buffer // float ClearColor[4] = { 0.3f, 0.3f, 0.3f, 1.0f } ; // red, green, blue, alpha ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView(); pd3dDevice->ClearRenderTargetView( pRTV, ClearColor ); // // Clear the depth stencil // ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView(); pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 ); HRESULT hr; D3DXMATRIXA16 mWorld; D3DXMATRIXA16 mView; D3DXMATRIXA16 mProj; D3DXMATRIXA16 mWorldViewProjection; // Get the projection & view matrix from the camera class mWorld = *g_Camera.GetWorldMatrix(); mView = *g_Camera.GetViewMatrix(); mProj = *g_Camera.GetProjMatrix(); mWorldViewProjection = mWorld * mView * mProj; // Update the effect's variables. V( g_pWorldViewProjection->SetMatrix( (float*)&mWorldViewProjection ) ); V( g_pWorld->SetMatrix( (float*)&mWorld ) ); V( g_pTime->SetFloat( (float)fTime ) ); V( g_pCameraPosition->SetFloatVector( (float*)g_Camera.GetEyePt() ) ); // // Set the Vertex Layout // pd3dDevice->IASetInputLayout( g_pVertexLayout ); UINT iCurSubset = ( UINT )( INT_PTR )g_SampleUI.GetComboBox( IDC_SUBSET )->GetSelectedData(); // // Render the mesh // if ( iCurSubset == -1 ) { for ( UINT iSubset = 0; iSubset < g_MeshLoader.GetNumSubsets(); ++iSubset ) { RenderSubset( iSubset ); } } else { RenderSubset( iCurSubset ); } DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); RenderText(); g_HUD.OnRender( fElapsedTime ); g_SampleUI.OnRender( fElapsedTime ); DXUT_EndPerfEvent(); }
//-------------------------------------------------------------------------------------- // This function loads a new technique and all device objects it requires. //-------------------------------------------------------------------------------------- HRESULT LoadTechniqueObjects( const char* szMedia ) { HRESULT hr = S_OK; if( NULL == g_pEffect ) return D3DERR_INVALIDCALL; IDirect3DTexture9* pTexture = NULL; IDirect3DCubeTexture9* pCubeTexture = NULL; IDirect3DDevice9* pDevice = DXUTGetD3D9Device(); WCHAR strFileName[MAX_PATH+1] = {0}; WCHAR strPath[MAX_PATH+1] = {0}; char strTechnique[MAX_PATH] = {0}; // Make sure the technique works char* strComboTech = ( char* )g_SampleUI.GetComboBox( IDC_TECHNIQUE )->GetSelectedData(); strcpy_s( strTechnique, MAX_PATH, strComboTech ); bool bLDPRT = ( strTechnique && ( 0 == strcmp( strTechnique, "LDPRT" ) ) ); // If we're not a signed format, make sure we use a technnique that will unbias if( D3DFMT_Q16W16V16U16 != g_fmtTexture && D3DFMT_Q8W8V8U8 != g_fmtTexture ) strcat_s( strTechnique, MAX_PATH, "_Unbias" ); D3DXHANDLE hTechnique = g_pEffect->GetTechniqueByName( strTechnique ); V_RETURN( g_pEffect->SetTechnique( hTechnique ) ); // Enable/disable LDPRT-only items g_SampleUI.GetStatic( IDC_ENV_LABEL )->SetEnabled( bLDPRT ); g_SampleUI.GetSlider( IDC_ENV_SLIDER )->SetEnabled( bLDPRT ); g_SampleUI.GetSlider( IDC_RED_TRANSMIT_SLIDER )->SetEnabled( bLDPRT ); g_SampleUI.GetSlider( IDC_GREEN_TRANSMIT_SLIDER )->SetEnabled( bLDPRT ); g_SampleUI.GetSlider( IDC_BLUE_TRANSMIT_SLIDER )->SetEnabled( bLDPRT ); g_SampleUI.GetStatic( IDC_RED_TRANSMIT_LABEL )->SetEnabled( bLDPRT ); g_SampleUI.GetStatic( IDC_GREEN_TRANSMIT_LABEL )->SetEnabled( bLDPRT ); g_SampleUI.GetStatic( IDC_BLUE_TRANSMIT_LABEL )->SetEnabled( bLDPRT ); // Load the mesh swprintf_s( strFileName, MAX_PATH, TEXT( "media\\%S" ), szMedia ); V_RETURN( LoadLDPRTData( pDevice, strFileName ) ); // Albedo texture swprintf_s( strFileName, MAX_PATH, TEXT( "media\\%SAlbedo.dds" ), szMedia ); DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, strFileName ); V( D3DXCreateTextureFromFile( pDevice, strPath, &pTexture ) ); g_pEffect->SetTexture( "Albedo", pTexture ); SAFE_RELEASE( pTexture ); // Normal map swprintf_s( strFileName, MAX_PATH, TEXT( "media\\%SNormalMap.dds" ), szMedia ); DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, strFileName ); V( D3DXCreateTextureFromFileEx( pDevice, strPath, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, g_fmtTexture, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pTexture ) ); g_pEffect->SetTexture( "NormalMap", pTexture ); SAFE_RELEASE( pTexture ); // Spherical harmonic basic functions char* pNames[4] = {"YlmCoeff0","YlmCoeff4","YlmCoeff8","YlmCoeff12"}; for( int i = 0; i < 4; i++ ) { D3DXCreateCubeTexture( pDevice, 32, 1, 0, g_fmtCubeMap, D3DPOOL_MANAGED, &pCubeTexture ); D3DXFillCubeTexture( pCubeTexture, myFillBF, ( LPVOID )( INT_PTR )( i * 4 ) ); g_pEffect->SetTexture( pNames[i], pCubeTexture ); SAFE_RELEASE( pCubeTexture ); } 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; } } }