//-------------------------------------------------------------------------------------- // 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 ) ); }
//-------------------------------------------------------------------------------------- // 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* 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; }
//-------------------------------------------------------------------------------------- // 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; }