載入圖片到D3D11Texture2D
阿新 • • 發佈:2018-10-31
#include "stdafx.h" #include "LoadPicture.h" #include <d3d11_1.h> #include <D3dx9tex.h> #include <D3DX11.h> #pragma comment(lib,"D3DX11.lib") #pragma comment(lib,"D3D11.lib") bool LoadTexture() { ID3D11Texture2D * pTexture2D = NULL; D3D11_TEXTURE2D_DESC dec; UINT createDeviceFlags = 0; #if defined(DEBUG) || defined(_DEBUG) createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; #endif D3D_FEATURE_LEVEL featureLevel; ID3D11Device * md3dDevice; ID3D11DeviceContext* md3dImmediateContext; HRESULT hr = D3D11CreateDevice(0, // 預設顯示介面卡 D3D_DRIVER_TYPE_HARDWARE , 0, // 不使用軟體裝置 createDeviceFlags , 0, 0, // 預設的特徵等級陣列 D3D11_SDK_VERSION, & md3dDevice , & featureLevel, & md3dImmediateContext); HRESULT result; D3DX11_IMAGE_LOAD_INFO loadInfo; ZeroMemory(&loadInfo, sizeof(D3DX11_IMAGE_LOAD_INFO)); loadInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE; loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM; loadInfo.MipLevels = D3DX11_DEFAULT; //這時會產生最大的mipmaps層。 loadInfo.MipFilter = D3DX11_FILTER_LINEAR; result=D3DX11CreateTextureFromFile(md3dDevice, "C:\\Users\\Administrator\\Desktop\\logo.png", &loadInfo, NULL, (ID3D11Resource**)(&pTexture2D), NULL); pTexture2D->GetDesc(&dec); if (result) return true; else return false; }