HRESULT GraphicsManager::CreateShaderResourceViewFromFile(ID3D11Device *device, ID3D11DeviceContext *dc, const wchar_t *filename, ID3D11ShaderResourceView **resourceView) { HRESULT result = E_FAIL; #ifdef OLD_DX_SDK //If not, then we have to load it! D3DX11_IMAGE_LOAD_INFO imageInfo; result = D3DX11CreateShaderResourceViewFromFile(device, filename, &imageInfo, NULL, resourceView, NULL); #else ID3D11Texture2D *tex; result = CreateWICTextureFromFile(device, dc, filename, (ID3D11Resource **)&tex, resourceView); if (FAILED(result)) { DirectX::TexMetadata md; DirectX::ScratchImage img; result = LoadFromDDSFile(filename, 0, &md, img); result = CreateShaderResourceView(device, img.GetImages(), img.GetImageCount(), md, resourceView); } #endif if (FAILED(result)) { printf("There was a problem loading \"%s\"\n", filename); } return result; }
//=============================================================================================================================== HRESULT TextureManager::LoadDDSTextureFromFile(LPCWSTR tex_filename, ID3D11ShaderResourceView** srv) { HRESULT hr; TexMetadata imageMetadata; ScratchImage* image = new ScratchImage(); hr = LoadFromDDSFile(tex_filename, DDS_FLAGS_NONE, &imageMetadata, *image); hr = CreateShaderResourceView(mD3DSystem->GetDevice11(), image->GetImages(), image->GetImageCount(), imageMetadata, srv); return hr; }
DirectX::ScratchImage *ImageUtils::loadFromFile(std::string const &path) { HRESULT loadResult; DirectX::TexMetadata info; DirectX::ScratchImage *retImage = new DirectX::ScratchImage; std::string extension = path.substr(path.size() - 4, 4); std::wstring widePath = std::wstring(path.begin(), path.end()); std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); if (extension == ".dds") { loadResult = LoadFromDDSFile(widePath.c_str(), DirectX::DDS_FLAGS_NONE, &info, *retImage); if (FAILED(loadResult)) { std::cerr << "Material convertor error : DDS texture loading failed (\"" << path << "\")" << std::endl; return (NULL); } } else if (extension == ".tga") { loadResult = LoadFromTGAFile(widePath.c_str(), &info, *retImage); if (FAILED(loadResult)) { std::cerr << "Material convertor error : TGA texture loading failed (\"" << path << "\")" << std::endl; return (NULL); } } else { loadResult = LoadFromWICFile(widePath.c_str(), DirectX::TEX_FILTER_DEFAULT, &info, *retImage); if (FAILED(loadResult)) { std::cerr << "Material convertor error : WIC texture loading failed (\"" << path << "\")" << std::endl; return (NULL); } } return (retImage); }