//-------------------------------------------------------------------------------------- // Find and compile the specified shader //-------------------------------------------------------------------------------------- HRESULT CompileShaderFromFile( WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut ) { HRESULT hr = S_OK; // find the file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, szFileName ) ); DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _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; #endif ID3DBlob* pErrorBlob; hr = D3DX11CompileFromFile( str, NULL, NULL, szEntryPoint, szShaderModel, dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL ); if( FAILED(hr) ) { if( pErrorBlob != NULL ) OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() ); SAFE_RELEASE( pErrorBlob ); return hr; } SAFE_RELEASE( pErrorBlob ); return S_OK; }
//-------------------------------------------------------------------------------------- // This function loads the mesh and LDPRT data. It also centers and optimizes the // mesh for the graphics card's vertex cache. //-------------------------------------------------------------------------------------- HRESULT LoadLDPRTData( IDirect3DDevice9* pd3dDevice, WCHAR* strFilePrefixIn ) { WCHAR str[MAX_PATH]; WCHAR strFileName[MAX_PATH]; WCHAR strFilePrefix[MAX_PATH]; HRESULT hr; // Load the mesh with D3DX and get back a ID3DXMesh*. For this // sample we'll ignore the X file's embedded materials since we know // exactly the model we're loading. See the mesh samples such as // "OptimizedMesh" for a more generic mesh loading example. swprintf_s( strFilePrefix, MAX_PATH, TEXT( "%s" ), strFilePrefixIn ); strFilePrefix[MAX_PATH - 1] = 0; swprintf_s( strFileName, MAX_PATH, TEXT( "%s.x" ), strFilePrefix ); strFileName[MAX_PATH - 1] = 0; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, strFileName ) ); CAllocateHierarchy Alloc; // Delete existing resources if( g_pFrameRoot ) { D3DXFrameDestroy( g_pFrameRoot, &Alloc ); g_pFrameRoot = NULL; } // Create hierarchy V_RETURN( D3DXLoadMeshHierarchyFromX( str, D3DXMESH_MANAGED, pd3dDevice, &Alloc, NULL, &g_pFrameRoot, &g_pAnimController ) ); SetupBoneMatrixPointers( g_pFrameRoot, g_pFrameRoot ); return S_OK; }
HRESULT LoadMesh(IDirect3DDevice9* pd3dDevice, WCHAR* strFileName, ID3DXMesh** ppMesh) { ID3DXMesh* pMesh = NULL; WCHAR str[MAX_PATH]; HRESULT hr; V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, strFileName)); V_RETURN(D3DXLoadMeshFromX(str, D3DXMESH_MANAGED, pd3dDevice, NULL, NULL, NULL, NULL, &pMesh)); DWORD* rgdwAdjacency = NULL; if(!(pMesh->GetFVF() & D3DFVF_NORMAL)) { ID3DXMesh* pTempMesh; V(pMesh->CloneMeshFVF(pMesh->GetOptions(), pMesh->GetFVF() | D3DFVF_NORMAL, pd3dDevice, &pTempMesh)); V(D3DXComputeNormals(pTempMesh, NULL)); SAFE_RELEASE(pMesh); pMesh = pTempMesh; } rgdwAdjacency = new DWORD[pMesh->GetNumFaces() * 3]; if(rgdwAdjacency == NULL) return E_OUTOFMEMORY; V(pMesh->GenerateAdjacency(1e-6f, rgdwAdjacency)); V(pMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE, rgdwAdjacency, NULL, NULL, NULL)); delete []rgdwAdjacency; *ppMesh = pMesh; return S_OK; }
HRESULT D3DEngine::OnCreateDevice(ID3D10Device* d3d_device) { HRESULT hr; // Find the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, kFileTutFX)); 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, d3d_device, NULL, NULL, &effect_, NULL, NULL ) ); // techniqueを入手 technique_ = effect_->GetTechniqueByName( "Render" ); diffuse_variable_ = effect_->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); world_variable_ = effect_->GetVariableByName( "World" )->AsMatrix(); view_variable_ = effect_->GetVariableByName( "View" )->AsMatrix(); projection_variable_ = effect_->GetVariableByName( "Projection" )->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; technique_->GetPassByIndex( 0 )->GetDesc( &PassDesc ); V_RETURN( d3d_device->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &vertex_layout_ ) ); // Set the input layout d3d_device->IASetInputLayout( vertex_layout_ ); // Load the mesh V_RETURN(mesh_.Create(d3d_device, kFileTinyMesh, true)); // Initialize the world matrices D3DXMatrixIdentity( &world_ ); // Initialize the view matrix D3DXVECTOR3 Eye( 0.0f, 3.0f, -500.0f ); D3DXVECTOR3 At( 0.0f, 1.0f, 0.0f ); D3DXVECTOR3 Up( 0.0f, 1.0f, 0.0f ); D3DXMatrixLookAtLH( &view_, &Eye, &At, &Up ); // Update Variables that never change view_variable_->SetMatrix( ( float* )&view_ ); return S_OK; }
//-------------------------------------------------------------------------------------- //LoadShader //-------------------------------------------------------------------------------------- HRESULT CEffect::LoadShader(WCHAR *a_Filename, IDirect3DDevice9 *a_pDevice) { HRESULT hr; ID3DXBuffer *errorBuffer; WCHAR str[MAX_PATH]; m_pDevice = a_pDevice; //delete old effect if multiple calls to load shader are made SAFE_RELEASE(m_pEffect); // 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 = 0; #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 hr = DXUTFindDXSDKMediaFileCch( str, MAX_PATH, a_Filename ); // search DX search paths for .fx file if(FAILED(hr)) { _snwprintf_s(m_ErrorMsg, MAX_ERROR_MSG, MAX_ERROR_MSG, L"Error: Can't Find Shader %s in any search paths.", a_Filename); return STG_E_FILENOTFOUND; } //load and compile .fx file // If this fails, there should be debug output as to // why the .fx file failed to compile hr = D3DXCreateEffectFromFile( m_pDevice, str, NULL, NULL, dwShaderFlags, NULL, &m_pEffect, &errorBuffer ); //return failure and record error message if failure if(FAILED(hr)) { //error is in 8-bit character format, so for swprintf to use the string, %S (capital S) is used. _snwprintf_s(m_ErrorMsg, MAX_ERROR_MSG, MAX_ERROR_MSG, L"Shader Compile Error: %S.", errorBuffer->GetBufferPointer() ); errorBuffer->Release(); return hr; } _snwprintf_s(m_ErrorMsg, MAX_ERROR_MSG, MAX_ERROR_MSG, L"FX file %s Compiled Successfully.", a_Filename); return S_OK; }
//----------------------------------------------------------------------------- HRESULT CDXUTMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice, LPCWSTR strFilename ) { WCHAR strPath[MAX_PATH]; LPD3DXBUFFER pAdjacencyBuffer = NULL; LPD3DXBUFFER pMtrlBuffer = NULL; HRESULT hr; // Cleanup previous mesh if any Destroy(); // Find the path for the file, and convert it to ANSI (for the D3DX API) DXUTFindDXSDKMediaFileCch( strPath, sizeof(strPath) / sizeof(WCHAR), strFilename ); // Load the mesh if( FAILED( hr = D3DXLoadMeshFromX( strPath, D3DXMESH_MANAGED, pd3dDevice, &pAdjacencyBuffer, &pMtrlBuffer, NULL, &m_dwNumMaterials, &m_pMesh ) ) ) { return hr; } // Optimize the mesh for performance if( FAILED( hr = m_pMesh->OptimizeInplace( D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) ) { SAFE_RELEASE( pAdjacencyBuffer ); SAFE_RELEASE( pMtrlBuffer ); return hr; } // Set strPath to the path of the mesh file WCHAR *pLastBSlash = wcsrchr( strPath, L'\\' ); if( pLastBSlash ) *(pLastBSlash + 1) = L'\0'; else *strPath = L'\0'; D3DXMATERIAL* d3dxMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer(); hr = CreateMaterials( strPath, pd3dDevice, d3dxMtrls, m_dwNumMaterials ); SAFE_RELEASE( pAdjacencyBuffer ); SAFE_RELEASE( pMtrlBuffer ); // Extract data from m_pMesh for easy access D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE]; m_dwNumVertices = m_pMesh->GetNumVertices(); m_dwNumFaces = m_pMesh->GetNumFaces(); m_dwBytesPerVertex = m_pMesh->GetNumBytesPerVertex(); m_pMesh->GetIndexBuffer( &m_pIB ); m_pMesh->GetVertexBuffer( &m_pVB ); m_pMesh->GetDeclaration( decl ); pd3dDevice->CreateVertexDeclaration( decl, &m_pDecl ); return hr; }
// // MeshNode::VOnRestore - 3rd Edition, Chapter 14, page 506 // // This function loads the Mesh and ensures the Mesh has normals; it also optimizes the // Mesh for the graphics card's vertex cache, which improves performance by organizing // the internal triangle list for less cache misses. // HRESULT D3DMeshNode9::VOnRestore(Scene *pScene) { if (m_XFileName.empty()) { SetRadius(CalcBoundingSphere()); return D3DSceneNode9::VOnRestore(pScene); } // Change post press - release the Mesh only if we have a valid Mesh file name to load. // Otherwise we likely created it on our own, and needs to be kept. SAFE_RELEASE(m_pMesh); WCHAR str[MAX_PATH]; HRESULT hr; // Load the Mesh with D3DX and get back a ID3DXMesh*. For this // sample we'll ignore the X file's embedded materials since we know // exactly the model we're loading. See the Mesh samples such as // "OptimizedMesh" for a more generic Mesh loading example. V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, m_XFileName.c_str() ) ); V_RETURN( D3DXLoadMeshFromX(str, D3DXMESH_MANAGED, DXUTGetD3D9Device(), NULL, NULL, NULL, NULL, &m_pMesh) ); DWORD *rgdwAdjacency = NULL; // Make sure there are normals which are required for lighting if( !(m_pMesh->GetFVF() & D3DFVF_NORMAL) ) { ID3DXMesh* pTempMesh; V( m_pMesh->CloneMeshFVF( m_pMesh->GetOptions(), m_pMesh->GetFVF() | D3DFVF_NORMAL, DXUTGetD3D9Device(), &pTempMesh ) ); V( D3DXComputeNormals( pTempMesh, NULL ) ); SAFE_RELEASE( m_pMesh ); m_pMesh = pTempMesh; } // Optimize the Mesh for this graphics card's vertex cache // so when rendering the Mesh's triangle list the vertices will // cache hit more often so it won't have to re-execute the vertex shader // on those vertices so it will improve perf. rgdwAdjacency = GCC_NEW DWORD[m_pMesh->GetNumFaces() * 3]; if( rgdwAdjacency == NULL ) return E_OUTOFMEMORY; V( m_pMesh->ConvertPointRepsToAdjacency(NULL, rgdwAdjacency) ); V( m_pMesh->OptimizeInplace(D3DXMESHOPT_VERTEXCACHE, rgdwAdjacency, NULL, NULL, NULL) ); SAFE_DELETE_ARRAY(rgdwAdjacency); SetRadius(CalcBoundingSphere()); return D3DSceneNode9::VOnRestore(pScene); }
//-------------------------------------------------------------------------------------- // 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; g_device = pd3dDevice; 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, 512, &g_pSprite10)); g_pTxtHelper = new CDXUTTextHelper(NULL, NULL, g_pFont10, g_pSprite10, 15); V_RETURN(CDXUTDirectionWidget::StaticOnD3D10CreateDevice(pd3dDevice)); // Read the D3DX effect file WCHAR str[MAX_PATH]; ID3D10Blob* pErrBlob = NULL; V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, L"Effect_Undistort.fx")); hr = D3DX10CreateEffectFromFile(str, NULL, NULL, "fx_4_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, pd3dDevice, NULL, NULL, &g_pEffect10, &pErrBlob, NULL); if (FAILED(hr)) { std::string errStr((LPCSTR)pErrBlob->GetBufferPointer(), pErrBlob->GetBufferSize()); WCHAR err[256]; MultiByteToWideChar(CP_ACP, 0, errStr.c_str(), (int)errStr.size(), err, errStr.size()); MessageBox(NULL, (LPCWSTR)err, L"Error", MB_OK); return hr; } // Setup the vector image and the display UINT width = (DXUTIsAppRenderingWithD3D9()) ? DXUTGetD3D9BackBufferSurfaceDesc()->Width : DXUTGetDXGIBackBufferSurfaceDesc()->Width; UINT height = (DXUTIsAppRenderingWithD3D9()) ? DXUTGetD3D9BackBufferSurfaceDesc()->Height : DXUTGetDXGIBackBufferSurfaceDesc()->Height; g_vsObj = new VSObject(pd3dDevice); D3D10_RASTERIZER_DESC rasterizerState; rasterizerState.FillMode = D3D10_FILL_SOLID; rasterizerState.CullMode = D3D10_CULL_NONE; rasterizerState.FrontCounterClockwise = true; 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_pRasterState); return S_OK; }
//----------------------------------------------------------------------------- HRESULT CSkybox::OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice, float fSize, IDirect3DCubeTexture9* pCubeTexture, WCHAR* strEffectFileName ) { HRESULT hr; m_pd3dDevice = pd3dDevice; m_fSize = fSize; m_pEnvironmentMap = pCubeTexture; // 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 WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, strEffectFileName ) ); // 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, &m_pEffect, NULL ) ); // Create vertex declaration V_RETURN( pd3dDevice->CreateVertexDeclaration( g_aSkyboxDecl, &m_pVertexDecl ) ); return S_OK; }
//----------------------------------------------------------------------------- HRESULT CSkybox::OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice, float fSize, WCHAR* strCubeMapFile, WCHAR* strEffectFileName ) { HRESULT hr; WCHAR strPath[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( strPath, MAX_PATH, strCubeMapFile ) ); IDirect3DCubeTexture9* pCubeTexture = NULL; V_RETURN( D3DXCreateCubeTextureFromFileEx( pd3dDevice, strPath, D3DX_DEFAULT, 1, 0, D3DFMT_A16B16G16R16F, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &pCubeTexture ) ); return OnCreateDevice( pd3dDevice, fSize, pCubeTexture, strEffectFileName ); }
HRESULT WINAPI NVUTFindDXSDKMediaFileCch( WCHAR* strDestPath, int cchDest, LPCWSTR strFilename ) { HRESULT hr = DXUTFindDXSDKMediaFileCch( strDestPath, cchDest, strFilename ); if (FAILED(hr)) { WCHAR strFullError[MAX_PATH] = L"The file "; StringCchCat( strFullError, MAX_PATH, strFilename ); StringCchCat( strFullError, MAX_PATH, L" was not found in the search path.\nCannot continue - exiting." ); MessageBoxW(NULL, strFullError, L"Media file not found", MB_OK); exit(0); } return hr; }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED) // and aren't tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont9 ) ); // Read the D3DX effect file WCHAR str[MAX_PATH]; DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE | D3DXFX_LARGEADDRESSAWARE; #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"DDSWithoutD3DX.fx" ) ); V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect9, NULL ) ); g_hRenderScene = g_pEffect9->GetTechniqueByName( "RenderScene" ); g_hmWorld = g_pEffect9->GetParameterByName( NULL, "g_mWorld" ); g_hmWorldViewProjection = g_pEffect9->GetParameterByName( NULL, "g_mWorldViewProjection" ); g_htxDiffuse = g_pEffect9->GetParameterByName( NULL, "g_txDiffuse" ); // Create a decl for the object data. D3DVERTEXELEMENT9 declDesc[] = { {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0}, {0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, {0xFF,0,D3DDECLTYPE_UNUSED, 0,0,0}// D3DDECL_END }; V_RETURN( pd3dDevice->CreateVertexDeclaration( declDesc, &g_pDecl9 ) ); // 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; }
//-------------------------------------------------------------------------------------- // Load an effect file //-------------------------------------------------------------------------------------- HRESULT LoadEffect10( ID3D10Device* pd3dDevice ) { HRESULT hr = S_OK; SAFE_RELEASE( g_pEffect10 ); g_fLastWriteTime = GetFileTime( g_strEffect ); // Read the D3DX effect file WCHAR str[MAX_PATH]; DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; dwShaderFlags |= D3DXSHADER_DEBUG; UINT uFlags = D3D10_SHADER_ENABLE_BACKWARDS_COMPATIBILITY | D3D10_SHADER_DEBUG; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"Exercise01.fx" ) ); V_RETURN( D3DX10CreateEffectFromFile( str, NULL, NULL, "fx_4_0", uFlags, 0, pd3dDevice, NULL, NULL, &g_pEffect10, NULL, NULL ) ); // Get the effect variable handles g_pRenderTerrain = g_pEffect10->GetTechniqueByName( "RenderTerrain10" ); g_pRenderBall = g_pEffect10->GetTechniqueByName( "RenderBall10" ); g_pRenderGrass = g_pEffect10->GetTechniqueByName( "RenderGrass10" ); g_pRenderSky = g_pEffect10->GetTechniqueByName( "RenderSky10" ); g_pmWorldViewProj = g_pEffect10->GetVariableByName( "g_mWorldViewProj" )->AsMatrix(); g_pmViewProj = g_pEffect10->GetVariableByName( "g_mViewProj" )->AsMatrix(); g_pmWorld = g_pEffect10->GetVariableByName( "g_mWorld" )->AsMatrix(); g_pvWorldLightDir = g_pEffect10->GetVariableByName( "g_vWorldLightDir" )->AsVector(); g_pfTime = g_pEffect10->GetVariableByName( "g_fTime" )->AsScalar(); g_pfElapsedTime = g_pEffect10->GetVariableByName( "g_fElapsedTime" )->AsScalar(); g_pvColor = g_pEffect10->GetVariableByName( "g_vColor" )->AsVector(); g_pfWorldScale = g_pEffect10->GetVariableByName( "g_fWorldScale" )->AsScalar(); g_pfHeightScale = g_pEffect10->GetVariableByName( "g_fHeightScale" )->AsScalar(); g_pvEyePt = g_pEffect10->GetVariableByName( "g_vEyePt" )->AsVector(); g_pfFadeStart = g_pEffect10->GetVariableByName( "g_fFadeStart" )->AsScalar(); g_pfFadeEnd = g_pEffect10->GetVariableByName( "g_fFadeEnd" )->AsScalar(); g_ptxDiffuse = g_pEffect10->GetVariableByName( "g_txDiffuse" )->AsShaderResource(); g_ptxNormal = g_pEffect10->GetVariableByName( "g_txNormal" )->AsShaderResource(); g_ptxHeight = g_pEffect10->GetVariableByName( "g_txHeight" )->AsShaderResource(); g_ptxDirt = g_pEffect10->GetVariableByName( "g_txDirt" )->AsShaderResource(); g_ptxGrass = g_pEffect10->GetVariableByName( "g_txGrass" )->AsShaderResource(); g_ptxMask = g_pEffect10->GetVariableByName( "g_txMask" )->AsShaderResource(); g_ptxShadeNormals = g_pEffect10->GetVariableByName( "g_txShadeNormals" )->AsShaderResource(); return hr; }
//-------------------------------------------------------------------------------------- // This function loads the mesh and ensures the mesh has normals; it also optimizes the // mesh for the graphics card's vertex cache, which improves performance by organizing // the internal triangle list for less cache misses. //-------------------------------------------------------------------------------------- HRESULT LoadMesh( IDirect3DDevice9* pd3dDevice, WCHAR* strFileName, ID3DXMesh** ppMesh ) { ID3DXMesh* pMesh = NULL; WCHAR str[MAX_PATH]; HRESULT hr; // Load the mesh with D3DX and get back a ID3DXMesh*. For this // sample we'll ignore the X file's embedded materials since we know // exactly the model we're loading. See the mesh samples such as // "OptimizedMesh" for a more generic mesh loading example. V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, strFileName ) ); V_RETURN( D3DXLoadMeshFromX( str, D3DXMESH_MANAGED, pd3dDevice, NULL, NULL, NULL, NULL, &pMesh ) ); DWORD* rgdwAdjacency = NULL; // Make sure there are normals which are required for lighting if( !( pMesh->GetFVF() & D3DFVF_NORMAL ) ) { ID3DXMesh* pTempMesh; V( pMesh->CloneMeshFVF( pMesh->GetOptions(), pMesh->GetFVF() | D3DFVF_NORMAL, pd3dDevice, &pTempMesh ) ); V( D3DXComputeNormals( pTempMesh, NULL ) ); SAFE_RELEASE( pMesh ); pMesh = pTempMesh; } // Optimize the mesh for this graphics card's vertex cache // so when rendering the mesh's triangle list the vertices will // cache hit more often so it won't have to re-execute the vertex shader // on those vertices so it will improve perf. rgdwAdjacency = new DWORD[pMesh->GetNumFaces() * 3]; if( rgdwAdjacency == NULL ) return E_OUTOFMEMORY; V( pMesh->GenerateAdjacency( 1e-6f, rgdwAdjacency ) ); V( pMesh->OptimizeInplace( D3DXMESHOPT_VERTEXCACHE, rgdwAdjacency, NULL, NULL, NULL ) ); delete []rgdwAdjacency; *ppMesh = pMesh; return S_OK; }
//-------------------------------------------------------------------------------------- // 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; CDXUTIMEEditBox::Initialize( DXUTGetHWND() ); 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 ) ); DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; #if defined( DEBUG ) || defined( _DEBUG ) 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 WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"CustomUI.fx" ) ); V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, NULL ) ); g_Mesh.Create( pd3dDevice, L"misc\\cell.x" ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 1.5f, -7.0f ); D3DXVECTOR3 vecAt ( 0.0f, 0.2f, 0.0f ); D3DXVECTOR3 vecUp ( 0.0f, 1.0f, 0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); D3DXMatrixLookAtLH( &g_mView, &vecEye, &vecAt, &vecUp ); return S_OK; }
//-------------------------------------------------------------------------------------- //메쉬 불러오는 함수 //-------------------------------------------------------------------------------------- HRESULT LoadMesh( IDirect3DDevice9* pd3dDevice, WCHAR* strFileName, ID3DXMesh** ppMesh ) { ID3DXMesh* pMesh = NULL; WCHAR str[MAX_PATH]; HRESULT hr; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, strFileName ) ); V_RETURN( D3DXLoadMeshFromX( str, D3DXMESH_MANAGED, pd3dDevice, NULL, NULL, NULL, NULL, &pMesh ) ); DWORD* rgdwAdjacency = NULL; // mesh에 노말벡터 생성하는 코드 if( !( pMesh->GetFVF() & D3DFVF_NORMAL ) ) { ID3DXMesh* pTempMesh; V( pMesh->CloneMeshFVF( pMesh->GetOptions(), pMesh->GetFVF() | D3DFVF_NORMAL, pd3dDevice, &pTempMesh ) ); V( D3DXComputeNormals( pTempMesh, NULL ) ); SAFE_RELEASE( pMesh ); pMesh = pTempMesh; } //성능 향상을 도모하고자 인접 정보를 기록 //각 mesh(삼각형) 정보 테이블을 가지는 형태 //해당 정보는 pMesh가 가지고 있음 //각 mesh 정보는 인접할 수 있는 점의 최대 개수가 3개 이내임을 활용해 효율적으로 vertex 운영 rgdwAdjacency = new DWORD[pMesh->GetNumFaces() * 3]; if( rgdwAdjacency == NULL ) return E_OUTOFMEMORY; V( pMesh->GenerateAdjacency( 1e-6f, rgdwAdjacency ) ); //버텍스 캐쉬를 활용하는 것 V( pMesh->OptimizeInplace( D3DXMESHOPT_VERTEXCACHE, rgdwAdjacency, NULL, NULL, NULL ) ); delete []rgdwAdjacency; //callback out 변수에 최종 값 저장 *ppMesh = pMesh; return S_OK; }
//----------------------------------------------------------------------------- // Name: // Desc: //----------------------------------------------------------------------------- HRESULT CDXUTMesh::Create( LPDIRECT3DDEVICE9 pd3dDevice, LPCWSTR strFilename ) { WCHAR strPath[MAX_PATH]; LPD3DXBUFFER pAdjacencyBuffer = NULL; LPD3DXBUFFER pMtrlBuffer = NULL; HRESULT hr; // Find the path for the file, and convert it to ANSI (for the D3DX API) DXUTFindDXSDKMediaFileCch( strPath, sizeof(strPath) / sizeof(WCHAR), strFilename ); // Load the mesh if( FAILED( hr = D3DXLoadMeshFromX( strPath, D3DXMESH_SYSTEMMEM, pd3dDevice, &pAdjacencyBuffer, &pMtrlBuffer, NULL, &m_dwNumMaterials, &m_pSysMemMesh ) ) ) { return hr; } // Optimize the mesh for performance if( FAILED( hr = m_pSysMemMesh->OptimizeInplace( D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE, (DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) ) { SAFE_RELEASE( pAdjacencyBuffer ); SAFE_RELEASE( pMtrlBuffer ); return hr; } // Set strPath to the path of the mesh file WCHAR *pLastBSlash = wcsrchr( strPath, L'\\' ); if( pLastBSlash ) *(pLastBSlash + 1) = L'\0'; else *strPath = L'\0'; hr = CreateMaterials( strPath, pd3dDevice, pAdjacencyBuffer, pMtrlBuffer ); SAFE_RELEASE( pAdjacencyBuffer ); SAFE_RELEASE( pMtrlBuffer ); return hr; }
HRESULT FindAndLoadTexture(IDirect3DDevice9* pd3dDevice, WCHAR const* fname, IDirect3DTexture9** ppTexture) { WCHAR str[MAX_PATH]; HRESULT hr = DXUTFindDXSDKMediaFileCch(str, MAX_PATH, fname); if (FAILED(hr)) { PrintfError(L"Can't find file %s.", fname); return hr; } hr = D3DXCreateTextureFromFile(pd3dDevice, str, ppTexture); if (FAILED(hr)) { PrintfError(L"Error creating texture from file %s.", fname); return hr; } return hr; }
//-------------------------------------------------------------------------------------- // Helper to load a VB and IB from a mesh //-------------------------------------------------------------------------------------- HRESULT LoadMesh( ID3D10Device* pd3dDevice ) { HRESULT hr = S_OK; struct OLD_VERT { D3DXVECTOR3 Pos; D3DXVECTOR3 Norm; D3DXVECTOR2 Tex; }; V_RETURN( g_Mesh.Create( pd3dDevice, L"rook.sdkmesh", true ) ); // Load the Texture WCHAR strPath[MAX_PATH] = {0}; DXUTFindDXSDKMediaFileCch( strPath, sizeof( strPath ) / sizeof( WCHAR ), L"rook_diff.dds" ); hr = D3DX10CreateShaderResourceViewFromFile( pd3dDevice, strPath, NULL, NULL, &g_pMeshTexRV, NULL ); return hr; }
//-------------------------------------------------------------------------------------- // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED) // and aren't tied to the back buffer size //-------------------------------------------------------------------------------------- HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { HRESULT hr; V_RETURN( g_DialogResourceManager.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( g_SettingsDlg.OnD3D9CreateDevice( pd3dDevice ) ); V_RETURN( D3DXCreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_pFont9 ) ); // Read the D3DX effect file WCHAR str[MAX_PATH]; DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE; #ifdef DEBUG_VS dwShaderFlags |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif #ifdef DEBUG_PS dwShaderFlags |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; #endif #ifdef D3DXFX_LARGEADDRESS_HANDLE dwShaderFlags |= D3DXFX_LARGEADDRESSAWARE; #endif V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"SimpleSample.fx" ) ); V_RETURN( D3DXCreateEffectFromFile( pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect9, NULL ) ); g_hmWorldViewProjection = g_pEffect9->GetParameterByName( NULL, "g_mWorldViewProjection" ); g_hmWorld = g_pEffect9->GetParameterByName( NULL, "g_mWorld" ); g_hfTime = g_pEffect9->GetParameterByName( NULL, "g_fTime" ); // 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 ) { 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 ); // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / ( FLOAT )pBackBufferSurfaceDesc->Height; g_Camera.SetProjParams( D3DX_PI / 4, fAspectRatio, 0.1f, 1000.0f ); g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height ); 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 ); // load the mesh V_RETURN( g_Mesh.Create( pd3dDevice, L"misc\\ball.sdkmesh" ) ); // Load the texture WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"misc\\seafloor.dds" ) ); V_RETURN( CreateDDSTextureFromFile( pd3dDevice, str, &g_pTexture9 ) ); if( DXUTIsWindowed() ) g_SampleUI.GetButton( IDC_LOAD_TEXTURE )->SetEnabled( true ); else g_SampleUI.GetButton( IDC_LOAD_TEXTURE )->SetEnabled( false ); return S_OK; }
//**************************************************************************// // Compile the shader file. These files aren't pre-compiled (well, not // // here, and are compiled on he fly). // //**************************************************************************// HRESULT VertexAndPixelShaders::CompileShaderFromFile(WCHAR* szFileName, // File Name LPCSTR szEntryPoint, // Name of shader LPCSTR szShaderModel, // Shader model ID3DBlob** ppBlobOut) // Blob returned { HRESULT hr = S_OK; // find the file WCHAR str[MAX_PATH]; V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, szFileName)); DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _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; #endif ID3DBlob* pErrorBlob; hr = D3DX11CompileFromFile(str, NULL, NULL, szEntryPoint, szShaderModel, dwShaderFlags, 0, NULL, ppBlobOut, &pErrorBlob, NULL); if (FAILED(hr)) { WCHAR errorCharsW[200]; if (pErrorBlob != NULL) { charStrToWideChar(errorCharsW, (char *)pErrorBlob->GetBufferPointer()); MessageBox(0, errorCharsW, L"Error", 0); } if (pErrorBlob) pErrorBlob->Release(); return hr; } SAFE_RELEASE(pErrorBlob); return S_OK; }
//----------------------------------------------------------------------------- HRESULT CDXUTMeshFile::Create( LPDIRECT3DDEVICE9 pd3dDevice, LPCWSTR strFilename ) { LPD3DXFILE pDXFile = NULL; LPD3DXFILEENUMOBJECT pEnumObj = NULL; LPD3DXFILEDATA pFileData = NULL; HRESULT hr; SIZE_T cChildren; // Create a x file object if( FAILED( hr = D3DXFileCreate( &pDXFile ) ) ) return E_FAIL; // Register templates for d3drm and patch extensions. if( FAILED( hr = pDXFile->RegisterTemplates( (void*)D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES ) ) ) { SAFE_RELEASE( pDXFile ); return E_FAIL; } // Find the path to the file, and convert it to ANSI (for the D3DXOF API) WCHAR strPath[MAX_PATH]; CHAR strPathANSI[MAX_PATH]; DXUTFindDXSDKMediaFileCch( strPath, sizeof(strPath) / sizeof(WCHAR), strFilename ); WideCharToMultiByte( CP_ACP, 0, strPath, -1, strPathANSI, MAX_PATH, NULL, NULL ); strPathANSI[MAX_PATH-1] = 0; // Create enum object hr = pDXFile->CreateEnumObject( (void*)strPathANSI, D3DXF_FILELOAD_FROMFILE, &pEnumObj ); if( FAILED(hr) ) { SAFE_RELEASE( pDXFile ); return hr; } // Enumerate top level objects (which are always frames) pEnumObj->GetChildren(&cChildren); for (UINT iChild = 0; iChild < cChildren; iChild++) { hr = pEnumObj->GetChild(iChild, &pFileData); if (FAILED(hr)) return hr; hr = LoadFrame( pd3dDevice, pFileData, this ); SAFE_RELEASE( pFileData ); if( FAILED(hr) ) { SAFE_RELEASE( pEnumObj ); SAFE_RELEASE( pDXFile ); return E_FAIL; } } SAFE_RELEASE( pFileData ); SAFE_RELEASE( pEnumObj ); SAFE_RELEASE( pDXFile ); return S_OK; }
//----------------------------------------------------------------------------- HRESULT CDXUTMesh::CreateMaterials( LPCWSTR strPath, IDirect3DDevice9 *pd3dDevice, D3DXMATERIAL* d3dxMtrls, DWORD dwNumMaterials ) { // Get material info for the mesh // Get the array of materials out of the buffer m_dwNumMaterials = dwNumMaterials; if( d3dxMtrls && m_dwNumMaterials > 0 ) { // Allocate memory for the materials and textures m_pMaterials = new D3DMATERIAL9[m_dwNumMaterials]; if( m_pMaterials == NULL ) return E_OUTOFMEMORY; m_pTextures = new LPDIRECT3DBASETEXTURE9[m_dwNumMaterials]; if( m_pTextures == NULL ) return E_OUTOFMEMORY; m_strMaterials = new CHAR[m_dwNumMaterials][MAX_PATH]; if( m_strMaterials == NULL ) return E_OUTOFMEMORY; // Copy each material and create its texture for( DWORD i=0; i<m_dwNumMaterials; i++ ) { // Copy the material m_pMaterials[i] = d3dxMtrls[i].MatD3D; m_pTextures[i] = NULL; // Create a texture if( d3dxMtrls[i].pTextureFilename ) { StringCchCopyA( m_strMaterials[i], MAX_PATH, d3dxMtrls[i].pTextureFilename ); WCHAR strTexture[MAX_PATH]; WCHAR strTextureTemp[MAX_PATH]; D3DXIMAGE_INFO ImgInfo; // First attempt to look for texture in the same folder as the input folder. MultiByteToWideChar( CP_ACP, 0, d3dxMtrls[i].pTextureFilename, -1, strTextureTemp, MAX_PATH ); strTextureTemp[MAX_PATH-1] = 0; StringCchCopy( strTexture, MAX_PATH, strPath ); StringCchCat( strTexture, MAX_PATH, strTextureTemp ); // Inspect the texture file to determine the texture type. if( FAILED( D3DXGetImageInfoFromFile( strTexture, &ImgInfo ) ) ) { // Search the media folder if( FAILED( DXUTFindDXSDKMediaFileCch( strTexture, MAX_PATH, strTextureTemp ) ) ) continue; // Can't find. Skip. D3DXGetImageInfoFromFile( strTexture, &ImgInfo ); } // Call the appropriate loader according to the texture type. switch( ImgInfo.ResourceType ) { case D3DRTYPE_TEXTURE: { IDirect3DTexture9 *pTex; if( SUCCEEDED( D3DXCreateTextureFromFile( pd3dDevice, strTexture, &pTex ) ) ) { // Obtain the base texture interface pTex->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&m_pTextures[i] ); // Release the specialized instance pTex->Release(); } break; } case D3DRTYPE_CUBETEXTURE: { IDirect3DCubeTexture9 *pTex; if( SUCCEEDED( D3DXCreateCubeTextureFromFile( pd3dDevice, strTexture, &pTex ) ) ) { // Obtain the base texture interface pTex->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&m_pTextures[i] ); // Release the specialized instance pTex->Release(); } break; } case D3DRTYPE_VOLUMETEXTURE: { IDirect3DVolumeTexture9 *pTex; if( SUCCEEDED( D3DXCreateVolumeTextureFromFile( pd3dDevice, strTexture, &pTex ) ) ) { // Obtain the base texture interface pTex->QueryInterface( IID_IDirect3DBaseTexture9, (LPVOID*)&m_pTextures[i] ); // Release the specialized instance pTex->Release(); } break; } } } } } return S_OK; }
//-------------------------------------------------------------------------------------- // 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; }
//-------------------------------------------------------------------------------------- // 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; 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 ) ); // 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 // Determine which LDPRT texture and SH coefficient cubemap formats are supported IDirect3D9* pD3D = DXUTGetD3D9Object(); D3DCAPS9 Caps; pd3dDevice->GetDeviceCaps( &Caps ); D3DDISPLAYMODE DisplayMode; pd3dDevice->GetDisplayMode( 0, &DisplayMode ); GetSupportedTextureFormat( pD3D, &Caps, DisplayMode.Format, &g_fmtTexture, &g_fmtCubeMap ); if( D3DFMT_UNKNOWN == g_fmtTexture || D3DFMT_UNKNOWN == g_fmtCubeMap ) return E_FAIL; // Create the skybox g_Skybox.OnCreateDevice( pd3dDevice, 50, L"Light Probes\\rnl_cross.dds", L"SkyBox.fx" ); V( D3DXSHProjectCubeMap( 6, g_Skybox.GetEnvironmentMap(), g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ) ); // Now compute the SH projection of the skybox... LPDIRECT3DCUBETEXTURE9 pSHCubeTex = NULL; V( D3DXCreateCubeTexture( pd3dDevice, 256, 1, 0, D3DFMT_A16B16G16R16F, D3DPOOL_MANAGED, &pSHCubeTex ) ); SHCubeProj projData; projData.Init( g_fSkyBoxLightSH[0], g_fSkyBoxLightSH[1], g_fSkyBoxLightSH[2] ); V( D3DXFillCubeTexture( pSHCubeTex, SHCubeFill, &projData ) ); g_Skybox.InitSH( pSHCubeTex ); // Read the D3DX effect file WCHAR str[MAX_PATH]; V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, TEXT( "LocalDeformablePRT.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 ) ); V_RETURN( LoadTechniqueObjects( "bat" ) ); V_RETURN( g_LightControl.StaticOnD3D9CreateDevice( pd3dDevice ) ); g_LightControl.SetRadius( 2.0f ); // 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 ); // Set the model's initial orientation D3DXQUATERNION quatRotation; D3DXQuaternionRotationYawPitchRoll( &quatRotation, -0.5f, 0.7f, 0.0f ); g_Camera.SetWorldQuat( quatRotation ); return hr; }
//-------------------------------------------------------------------------------------- // 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; }
//-------------------------------------------------------------------------------------- // 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) { #ifdef _PERFORMANCE ::SetThreadAffinityMask(::GetCurrentThread(), 1); PerfManager::createTheOne(); grp::setProfiler(PerfManager::getTheOne()); #endif HRESULT hr; V_RETURN(g_DialogResourceManager.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)); // 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 WCHAR str[MAX_PATH]; V_RETURN(DXUTFindDXSDKMediaFileCch(str, MAX_PATH, L"Test/Demo.fx")); // If this fails, there should be debug output as to // why the .fx file failed to compile LPD3DXBUFFER pError = NULL; D3DXCreateEffectFromFile(pd3dDevice, str, NULL, NULL, dwShaderFlags, NULL, &g_pEffect, &pError); if (NULL != pError) { char* szErr = (char*)(pError->GetBufferPointer()); assert(false); } V(g_pEffect->SetFloat("g_fAmbient", g_fAmbient)); V(g_pEffect->SetFloat("g_fDiffuse", g_fDiffuse)); // Setup the camera's view parameters g_camera.setControlMode(DemoCamera::THIRD_PERSON); g_camera.setViewDistance(3.0f); g_camera.setYaw(3.1415926f); //g_camera.setRoll(3.14159f/4); #if (DEMO_RIGHT_HAND_COORD) g_camera.setViewParams(grp::Vector3(-2.0f, -2.0f, 1.0f), grp::Vector3(0.0f, 0.0f, 1.0f), grp::Vector3(0.0f, 0.0f, 1.0f)); g_camera.setCoordinateSystem(DemoCamera::RIGHT_HAND); g_camera.setUpAxis(DemoCamera::Z_UP); #else g_camera.setViewParams(grp::Vector3(-2.0f, 1.0f, -2.0f), grp::Vector3(0.0f, 1.0f, 0.0f), grp::Vector3(0.0f, 1.0f, 0.0f)); #endif // Setup the camera's projection parameters float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height; g_camera.setPerspectiveParams(D3DX_PI/4, fAspectRatio, 0.001f, 200.0f); g_fileLoader = new MultithreadFileLoader(pd3dDevice); g_fileLoader->enableMultithread(false); g_resourceManager = new MultithreadResManager(g_fileLoader); grp::initialize(NULL, g_fileLoader, NULL, g_resourceManager); g_character = new DemoCharacter(L"Test/warrior.gmd", pd3dDevice); //g_character->setGpuSkinning(true); g_model = g_character->getModel(); if (g_model == NULL) { return E_FAIL; } //g_model->enableMeshLod(true); //g_model->enableWeightLod(true); //g_model->enableSkeletonLod(true); //g_model->setLodTolerance(0.1f); #if (DEMO_RIGHT_HAND_COORD) grp::Matrix coordTransform(grp::Matrix::IDENTITY); #else grp::Matrix coordTransform(grp::Matrix::ZERO); coordTransform._11 = 1.0f; coordTransform._23 = 1.0f; coordTransform._32 = 1.0f; coordTransform._44 = 1.0f; #endif g_model->setTransform(coordTransform);// * translateMatrix); g_model->playAnimation(L"fight", grp::ANIMATION_LOOP, 0, 0); g_model->playAnimation(L"walk", grp::ANIMATION_LOOP, 0, 0); g_model->playAnimation(L"run", grp::ANIMATION_LOOP, 0, 0); setAnimationWeight(); return S_OK; }
//-------------------------------------------------------------------------------------- // 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"Exercise03.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_pRenderPiece = g_pEffect10->GetTechniqueByName( "RenderPiece" ); // Obtain the parameter handles g_pmWorldViewProj = g_pEffect10->GetVariableByName( "g_mWorldViewProj" )->AsMatrix(); g_pmWorldView = g_pEffect10->GetVariableByName( "g_mWorldView" )->AsMatrix(); g_pmWorld = g_pEffect10->GetVariableByName( "g_mWorld" )->AsMatrix(); g_pmProj = g_pEffect10->GetVariableByName( "g_mProj" )->AsMatrix(); g_pViewSpaceLightDir = g_pEffect10->GetVariableByName( "g_ViewSpaceLightDir" )->AsVector(); g_pDiffuseTex = g_pEffect10->GetVariableByName( "g_txDiffuse" )->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 }, }; UINT numElements = sizeof( layout ) / sizeof( layout[0] ); D3D10_PASS_DESC PassDesc; g_pRenderPiece->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 ) ); // Setup the camera's view parameters D3DXVECTOR3 vecEye( 0.0f, 0.0f, -8.0f ); D3DXVECTOR3 vecAt( 0.0f,0.0f,0.0f ); g_Camera.SetViewParams( &vecEye, &vecAt ); return S_OK; }
//-------------------------------------------------------------------------------------- // Helper to load a VB and IB from a mesh //-------------------------------------------------------------------------------------- HRESULT LoadMesh( ID3D10Device* pd3dDevice ) { static float treeVertexData[] = { 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.000000f, -1.834569f, 0.000000f, 0.000000f, -1.000000f, 0.000000f, 0.232397f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, -0.237213f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, 0.605192f, 0.248206f, -0.595694f, 0.763899f, 0.017676f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, 0.374029f, -0.649812f, -0.595694f, 0.472116f, 0.174170f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, -0.514807f, -1.834569f, -0.374029f, -0.649812f, -0.595694f, -0.472116f, 0.315994f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, 0.762787f, -0.194237f, 0.000000f, 0.196639f, -1.834569f, -0.605192f, 0.248206f, -0.595694f, -0.763899f, 0.480517f, 0.999001f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, 0.762787f, -0.194237f, 0.000000f, 0.636337f, -1.834569f, 0.000000f, 0.803211f, -0.595694f, 0.000000f, 0.762787f, 0.999001f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, 0.900607f, -0.999001f, 0.600000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, 0.926464f, -0.753354f, 0.600000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, 0.821507f, -0.753354f, 0.600000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.600000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.600000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.600000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.600000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.600000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.600000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, -0.073536f, -0.753354f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, 0.926464f, -0.753354f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, 0.762787f, -0.194237f, 0.000000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, 0.821507f, -0.753354f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, -0.178493f, -0.753354f, 0.000000f, 0.636337f, 0.165431f, 0.000000f, 0.965927f, 0.072050f, -0.248584f, -0.237213f, -0.194237f, 0.000000f, 1.242701f, 1.102575f, 0.440550f, 0.967456f, 0.171919f, -0.185666f, -0.178493f, -0.753354f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, -0.099393f, -0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, -0.099393f, -0.999001f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.606364f, 1.514307f, 0.440550f, -0.245871f, 0.952700f, -0.178636f, -0.099393f, -0.999001f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.803003f, 1.102575f, 1.045742f, 0.122381f, 0.171919f, 0.977480f, -0.073536f, -0.753354f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.000000f, -0.514807f, 0.165431f, -0.374029f, -0.927565f, 0.072050f, -0.366649f, 0.315994f, -0.194237f, 0.000000f, -0.707816f, 0.946384f, -0.968049f, -0.891821f, 0.171919f, -0.418450f, 0.371931f, -0.660168f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.193009f, 1.358116f, -0.594020f, 0.093915f, 0.952699f, 0.289039f, 0.391077f, -0.905815f, 0.000000f, 0.196639f, 0.165431f, -0.605192f, 0.534905f, 0.072050f, -0.841834f, 0.480517f, -0.194237f, 0.000000f, 0.003630f, 0.946384f, -1.199212f, 0.475540f, 0.171919f, -0.862732f, 0.464690f, -0.660168f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.000000f, 0.196639f, 0.165431f, 0.605192f, 0.303425f, -0.189382f, 0.933846f, 0.017676f, -0.194237f, 0.000000f, 0.022931f, 0.868289f, 1.139810f, 0.475540f, 0.171919f, 0.862732f, 0.032802f, -0.613575f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.000000f, 0.000000f, 0.577163f, 0.000000f, -0.076035f, 0.969256f, -0.234012f, 0.232397f, -0.439883f, 0.000000f, -0.173708f, 1.280021f, 0.534618f, 0.093915f, 0.952699f, -0.289039f, 0.105600f, -0.859222f, 0.000000f, -0.514807f, 0.165431f, 0.374029f, -0.927565f, 0.072050f, 0.366649f, 0.174170f, -0.194237f, 0.000000f, -0.688515f, 0.868289f, 0.908647f, -0.891821f, 0.171919f, 0.418449f, 0.126479f, -0.613575f, 0.000000f, }; static DWORD treeIndexData[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113 }; HRESULT hr = S_OK; g_NumIndices = sizeof( treeIndexData ) / sizeof( DWORD ); g_VertStride = sizeof( TREE_VERTEX ); D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( treeVertexData ); bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; InitData.pSysMem = treeVertexData; V_RETURN( pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVB ) ); bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof( treeIndexData ); bd.BindFlags = D3D10_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; InitData.pSysMem = treeIndexData; V_RETURN( pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIB ) ); // Load the Texture WCHAR strPath[MAX_PATH] = {0}; DXUTFindDXSDKMediaFileCch( strPath, sizeof( strPath ) / sizeof( WCHAR ), L"bark_diff.dds" ); hr = D3DX10CreateShaderResourceViewFromFile( pd3dDevice, strPath, NULL, NULL, &g_pMeshTexRV, NULL ); return hr; }