void D3D10Model::LoadTextures(ID3D10Device* pdev) { TCHAR path[128]; m_pD3D10Material = new D3D10Material[m_numMaterials]; for (DWORD i=0; i<m_numMaterials; i++) { m_pD3D10Material[i].diffuseMap = NULL; m_pD3D10Material[i].bumpMap = NULL; m_pD3D10Material[i].diffuseMapView = NULL; m_pD3D10Material[i].bumpMapView = NULL; _stprintf(path, _T("%s%s"), m_texturePath, m_pMaterial[i].diffuseMapFile); if (!FAILED(D3DX10CreateTextureFromFile(pdev, path, NULL, NULL, (ID3D10Resource **)&m_pD3D10Material[i].diffuseMap, NULL))) { D3D10_TEXTURE2D_DESC desc; m_pD3D10Material[i].diffuseMap->GetDesc(&desc); D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc; ZeroMemory(&srvDesc, sizeof(srvDesc)); srvDesc.Format = desc.Format; srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MipLevels = desc.MipLevels; pdev->CreateShaderResourceView(m_pD3D10Material[i].diffuseMap, &srvDesc, &m_pD3D10Material[i].diffuseMapView); } _stprintf(path, _T("%s%s"), m_texturePath, m_pMaterial[i].bumpMapFile); if (!FAILED(D3DX10CreateTextureFromFile(pdev, path, NULL, NULL, (ID3D10Resource **)&m_pD3D10Material[i].bumpMap, NULL))) { D3D10_TEXTURE2D_DESC desc; m_pD3D10Material[i].diffuseMap->GetDesc(&desc); D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc; ZeroMemory(&srvDesc, sizeof(srvDesc)); srvDesc.Format = desc.Format; srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MipLevels = desc.MipLevels; pdev->CreateShaderResourceView(m_pD3D10Material[i].bumpMap, &srvDesc, &m_pD3D10Material[i].bumpMapView); } } }
bool ds::Texture::Load( const ds::String& Filename, DWORD Type ) { Release( ); if( !ds::Render::GetDevice( ) ) { dsPushMessage( ErrD3DeviceNotCreated ); return false; } D3DX10_IMAGE_LOAD_INFO l_Info = D3DX10_IMAGE_LOAD_INFO( ); CreateInfo( Type, l_Info ); ID3D10Resource* l_Resource = nullptr; HRESULT l_Result = D3DX10CreateTextureFromFile( ds::Render::Device, Filename.c_str( ), &l_Info, nullptr, &l_Resource, nullptr ); if( FAILED( l_Result ) ) { dsPushError( ErrTextureLoad, Filename.c_str( ) ); return false; } l_Resource->QueryInterface( __uuidof( ID3D10Texture2D ), ( LPVOID* )&Texture2D ); l_Resource->Release( ); D3D10_TEXTURE2D_DESC l_Desc; Texture2D->GetDesc( &l_Desc ); Size = ds::Size( l_Desc.Width, l_Desc.Height ); if( Type & RenderTarget ) { D3D10_RENDER_TARGET_VIEW_DESC l_Target; l_Target.Format = l_Info.Format; l_Target.ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2D; l_Target.Texture2D.MipSlice = 0; ds::Render::Device->CreateRenderTargetView( Texture2D, &l_Target, &TargetView ); } ShaderView[ 0 ] = CreateResourceView( this ); return true; }
//Textures Methods HTexture* D3DCore_Impl::Texture_Load( LPCWSTR FileName ) { ID3D10Texture2D* texture2D = NULL; ID3D10Resource* pD3D10Resource = NULL; // Loads the texture into a temporary ID3D10Resource object HR( D3DX10CreateTextureFromFile(md3dDevice, LPCWSTR(FileName), NULL, NULL, &pD3D10Resource, NULL) ); // Translates the ID3D10Resource object into a ID3D10Texture2D object HR( pD3D10Resource ->QueryInterface(__uuidof( ID3D10Texture2D), (LPVOID*)&texture2D) ); HR( pD3D10Resource ->Release() ); mTextureList.push_back( texture2D ); // returns the ID3D10Texture2D object return texture2D; }
Texture2D* TextureLoader::Load(ID3D10Device *pDXDevice, const tstring& assetName, bool skipLoadDefault, bool cpuReadAccess) { if ( m_pAssets->IsAssetPresent(assetName + (cpuReadAccess?_T("_READ"):_T("")))) { //#if defined DEBUG || _DEBUG //cout << "Using Existing Texture.\n"; //#endif } else { D3DX10_IMAGE_LOAD_INFO info; info.Width = D3DX10_DEFAULT; info.Height = D3DX10_DEFAULT; info.Depth = D3DX10_DEFAULT; info.FirstMipLevel = cpuReadAccess?0:D3DX10_DEFAULT; info.MipLevels = cpuReadAccess?0:D3DX10_DEFAULT; info.Usage = cpuReadAccess?D3D10_USAGE_STAGING:D3D10_USAGE_IMMUTABLE; info.BindFlags = cpuReadAccess?0:D3DX10_DEFAULT; info.CpuAccessFlags = cpuReadAccess?D3D10_CPU_ACCESS_READ:0; info.MiscFlags = D3DX10_DEFAULT; info.Format = DXGI_FORMAT_R8G8B8A8_UNORM; info.Filter = D3DX10_DEFAULT; info.MipFilter = D3DX10_DEFAULT; ID3D10Resource* pTexture; HRESULT hr = D3DX10CreateTextureFromFile(pDXDevice, assetName.c_str(), &info, NULL, &pTexture, NULL); if(hr != S_OK) { PANIC("Loading texture '" + string(assetName.begin(), assetName.end()) + "' Failed!"); if (skipLoadDefault == true) return 0; else return LoadDefaultBlack(pDXDevice); } Texture2D* tex = new Texture2D(pDXDevice, pTexture); m_pAssets->AddAsset(assetName + (cpuReadAccess?_T("_READ"):_T("")), tex); } return m_pAssets->GetAsset(assetName + (cpuReadAccess?_T("_READ"):_T(""))); }
////////////////////////////////////////////////////////////////////////// // // Function: LoadTexture // // Last Modified: 03/08/2007 // // Purpose: Loads a texture into memory and returns the index of // where it is in the vector // // In: filename of the texture to load into memory // // Out: index of the texture loaded // ////////////////////////////////////////////////////////////////////////// int CTextureManager10::LoadTexture(const char *szFileName) { //check teh filename pointer if(!szFileName) return 0; //Loop through the Filename vector for(int i = 0; i < (int)m_vFileNames.size(); i++) { //if we find the file is already loaded, just return the index if(strcmp(m_vFileNames[i], szFileName) == 0) return i; } ID3D10Resource *d3dTempTexture; //Create the texture, if it fails return 0 if(FAILED(D3DX10CreateTextureFromFile(g_pd3dDevice, szFileName, NULL, NULL, &d3dTempTexture, NULL))) { return 0; } //setup the new texture tTexture *pNewTexture = new tTexture; pNewTexture->d3dTexture = d3dTempTexture; char *szNewFileName = new char[128]; pNewTexture->szFileName = new char[128]; strcpy(szNewFileName, szFileName); strcpy(pNewTexture->szFileName, szFileName); //Push texture pointer into the texture vector m_vTextures.push_back(pNewTexture); //Push filename into the filename vector m_vFileNames.push_back(szNewFileName); //Return the index return (int)(m_vTextures.size() - 1); }
////////////////////////////////////////////////////////////////////////// // // Function: LoadTexture // // Last Modified: 03/08/2007 // // Purpose: Loads a texture into memory and returns the index of // where it is in the vector // // Notes: don't touch! =o) // ////////////////////////////////////////////////////////////////////////// int CTextureManager10::LoadTexture(const char *szFileName, ID3D10Resource **d3dTexture) { //check teh filename pointer if(!szFileName) { //set it to the default texture if the pointer is bad *d3dTexture = m_vTextures[0]->d3dTexture; return 0; } //Loop through the Filename vector for(int i = 0; i < (int)m_vFileNames.size(); i++) { //if we find the file is already loaded, just return the index if(strcmp(m_vFileNames[i], szFileName) == 0) { *d3dTexture = m_vTextures[i]->d3dTexture; return i; } } //Create the texture, if it fails return 0 and the default texture if(FAILED(D3DX10CreateTextureFromFile(g_pd3dDevice, szFileName, NULL, NULL, d3dTexture, NULL))) { *d3dTexture = m_vTextures[0]->d3dTexture; return 0; } char *szNewFileName = new char[128]; strcpy(szNewFileName, szFileName); //Push filename into the filename vector m_vFileNames.push_back(szNewFileName); //Return the index return (int)(m_vTextures.size() - 1); }
Texture1D* Texture1D::FromFile(LPCTSTR pFileName, Renderer* pRender) { ID3D10Resource* pResource = NULL; HRESULT hr = D3DX10CreateTextureFromFile( pRender->GetDevice(), //[in] ID3D10Device *pDevice, pFileName, //[in] LPCTSTR pSrcFile, NULL, //[in] D3DX10_IMAGE_LOAD_INFO *pLoadInfo, NULL, //[in] ID3DX10ThreadPump *pPump, &pResource, //[out] ID3D10Resource **ppTexture, NULL //[out] HRESULT *pHResult ); if (SUCCEEDED(hr)) { ID3D10Texture1D* ptr = NULL; hr = pResource->QueryInterface(IID_ID3D10Texture1D, (LPVOID*)&ptr); pResource->Release(); if (SUCCEEDED(hr)) { Texture1D* pTexture1D = new Texture1D(ptr); return pTexture1D; } } return NULL; }
SCTReturn SCTTextureD3D10::Initialize(const WCHAR *filename, bool asCubeMap) { // Load the texture HRESULT result; if(asCubeMap) { D3DX10_IMAGE_LOAD_INFO loadInfo; loadInfo.MiscFlags = D3D10_RESOURCE_MISC_TEXTURECUBE; ID3D10Texture2D* tex = 0; result = D3DX10CreateTextureFromFile(mpDevice, filename, &loadInfo, 0, (ID3D10Resource**)&tex, 0); D3D10_TEXTURE2D_DESC texDesc; tex->GetDesc(&texDesc); D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc; viewDesc.Format = texDesc.Format; viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURECUBE; viewDesc.TextureCube.MipLevels = texDesc.MipLevels; viewDesc.TextureCube.MostDetailedMip = 0; result = mpDevice->CreateShaderResourceView(tex, &viewDesc, &mpShaderResourceView); ReleaseCOM(tex); } else { result = D3DX10CreateShaderResourceViewFromFile(mpDevice, filename, NULL, NULL, &mpShaderResourceView, NULL); } if(result != S_OK) return FAIL; return OK; }
void ColoredCubeApp::initApp() { D3DApp::initApp(); myOutFile.open("debug.txt"); fx::InitAll(md3dDevice); mBox.init(md3dDevice, 1.0f); mPlane.init(md3dDevice, 1.0f); //we send scale of 1.0f what does that mean?? mTree.initObject(md3dDevice); std::string treeFileName = "MediumPolyTree.3ds"; mTree.load(md3dDevice, treeFileName); mTree.createTexturesAll(L"LeafCol.jpg", L"LeafAlpha.jpg", L"mySpec.jpg", L"LeafnormalX_normals.PNG"); mTree.setCubeMap(Object::createCubeTex(md3dDevice, L"grassenvmap1024.dds")); mTree.rotate(-1.5f,0.0f,0.0f); mTree.translate(3.0f,-3.0f,-2.6f); mObjBox.initObject(md3dDevice); std::string boxFileName = "man2.fbx"; mObjBox.load(md3dDevice, boxFileName); mObjBox.createTexturesAll( L"manD.jpg", L"defaultAlpha.jpg", L"defaultspec.dds", L"manN.jpg"); mObjBox.createTexturesAt(0, L"bricks.dds", L"defaultAlpha.jpg", L"spec.dds", L"womanHairN.jpg"); mObjBox.setCubeMap(Object::createCubeTex(md3dDevice, L"grassenvmap1024.dds")); mObjBox.rotate(0.0f,0.0f,3.14f); mObjBox.translate(-3.0f,0.0f,2.5f); mObjBox.scale(2.0f,2.0f,2.0f); buildFX(); buildVertexLayouts(); //mLights[0].dir = D3DXVECTOR3(0.57735f, -0.57735f, 0.57735f); mLights[0].dir = D3DXVECTOR3(0.576f, -0.576f, -0.576f); mLights[0].ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f); mLights[0].diffuse = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f); mLights[0].specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); // Pointlight--position is changed every frame to animate. mLights[1].pos = D3DXVECTOR3(2.0f,2.0f,2.0f); mLights[1].ambient = D3DXCOLOR(0.8f, 0.8f, 0.0f, 1.0f); mLights[1].diffuse = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); mLights[1].specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); mLights[1].att.x = 0.0f; mLights[1].att.y = 1.0f; mLights[1].att.z = 0.0f; mLights[1].range = 120.0f; animationTimeElapsed = 0.0f; animationTimePrev = mTimer.getGameTime(); for(int i = 0; i < fireFrameCount; i++) { std::wostringstream fileName; fileName << L"FireAnim\\Fire"; if(i+1 < 10) fileName << L"00"; else if(i+1 < 100) fileName << L"0"; fileName << i+1 << L".bmp"; std::wstring wbuffer = fileName.str(); LPCWSTR usableName = wbuffer.c_str(); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, usableName, 0, 0, &mFireAnimationMapRVs[i], 0)); } HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"bricks.dds", 0, 0, &mCrateMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"bricks_normal.dds", 0, 0, &mDefaultNormalMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"stone_diffuse.dds", 0, 0, &mGrassMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"stone_normal.dds", 0, 0, &mBrickNormalMapRV, 0 )); HR(D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"spec.dds", 0, 0, &mSpecularMapRV, 0 )); // If not, create it. D3DX10_IMAGE_LOAD_INFO loadInfo; loadInfo.MiscFlags = D3D10_RESOURCE_MISC_TEXTURECUBE; ID3D10Texture2D* tex = 0; HR(D3DX10CreateTextureFromFile(md3dDevice, L"grassenvmap1024.dds", &loadInfo, 0, (ID3D10Resource**)&tex, 0) ); D3D10_TEXTURE2D_DESC texDesc; tex->GetDesc(&texDesc); D3D10_SHADER_RESOURCE_VIEW_DESC viewDesc; viewDesc.Format = texDesc.Format; viewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURECUBE; viewDesc.TextureCube.MipLevels = texDesc.MipLevels; viewDesc.TextureCube.MostDetailedMip = 0; HR(md3dDevice->CreateShaderResourceView(tex, &viewDesc, &mCubeMapRV)); ReleaseCOM(tex); mSky.init(md3dDevice, mCubeMapRV, 5000.0f); }
Texture* D3D10Texture::CreateFromFile(CTSTR lpFile, BOOL bBuildMipMaps) { HRESULT err; D3DX10_IMAGE_INFO ii; if(FAILED(D3DX10GetImageInfoFromFile(lpFile, NULL, &ii, NULL))) { AppWarning(TEXT("D3D10Texture::CreateFromFile: Could not get information about texture file '%s'"), lpFile); return NULL; } //------------------------------------------ if(bBuildMipMaps && (!IsPow2(ii.Width) || !IsPow2(ii.Height))) bBuildMipMaps = FALSE; D3DX10_IMAGE_LOAD_INFO ili; ili.Width = D3DX10_DEFAULT; ili.Height = D3DX10_DEFAULT; ili.Depth = D3DX10_DEFAULT; ili.FirstMipLevel = D3DX10_DEFAULT; ili.MipLevels = bBuildMipMaps ? 0 : 1; ili.Usage = (D3D10_USAGE)D3DX10_DEFAULT; ili.BindFlags = D3DX10_DEFAULT; ili.CpuAccessFlags = D3DX10_DEFAULT; ili.MiscFlags = D3DX10_DEFAULT; ili.Format = (DXGI_FORMAT)D3DX10_DEFAULT; ili.Filter = D3DX10_DEFAULT; ili.MipFilter = D3DX10_DEFAULT; ili.pSrcInfo = NULL; ID3D10Resource *texResource; if(FAILED(err = D3DX10CreateTextureFromFile(GetD3D(), lpFile, &ili, NULL, &texResource, NULL))) { AppWarning(TEXT("D3D10Texture::CreateFromFile: failed to load '%s'"), lpFile); return NULL; } //------------------------------------------ D3D10_SHADER_RESOURCE_VIEW_DESC resourceDesc; zero(&resourceDesc, sizeof(resourceDesc)); resourceDesc.Format = ii.Format; resourceDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; resourceDesc.Texture2D.MipLevels = bBuildMipMaps ? -1 : 1; ID3D10ShaderResourceView *resource; if(FAILED(err = GetD3D()->CreateShaderResourceView(texResource, &resourceDesc, &resource))) { SafeRelease(texResource); AppWarning(TEXT("D3D10Texture::CreateFromFile: CreateShaderResourceView failed, result = 0x%08lX"), err); return NULL; } //------------------------------------------ ID3D10Texture2D *tex2D; err = texResource->QueryInterface(__uuidof(ID3D10Texture2D), (void**)&tex2D); if(FAILED(err)) { SafeRelease(texResource); SafeRelease(resource); AppWarning(TEXT("D3D10Texture::CreateFromFile: could not query texture interface")); return NULL; } texResource->Release(); //------------------------------------------ D3D10Texture *newTex = new D3D10Texture; newTex->resource = resource; newTex->texture = tex2D; newTex->width = ii.Width; newTex->height = ii.Height; switch(ii.Format) { case DXGI_FORMAT_R8_UNORM: newTex->format = GS_ALPHA; break; case DXGI_FORMAT_A8_UNORM: newTex->format = GS_GRAYSCALE; break; case DXGI_FORMAT_B8G8R8X8_UNORM: newTex->format = GS_BGR; break; case DXGI_FORMAT_B8G8R8A8_UNORM: newTex->format = GS_BGRA; break; case DXGI_FORMAT_R8G8B8A8_UNORM: newTex->format = GS_RGBA; break; case DXGI_FORMAT_R16G16B16A16_FLOAT: newTex->format = GS_RGBA16F; break; case DXGI_FORMAT_R32G32B32A32_FLOAT: newTex->format = GS_RGBA32F; break; case DXGI_FORMAT_BC1_UNORM: newTex->format = GS_DXT1; break; case DXGI_FORMAT_BC2_UNORM: newTex->format = GS_DXT3; break; case DXGI_FORMAT_BC3_UNORM: newTex->format = GS_DXT5; break; default: newTex->format = GS_UNKNOWNFORMAT; } return newTex; }