I am pretty new to DirectX, and have trouble understanding some of the texture loading. I basically want to load a texture from a JPG file using D3D. Since the createTextureFromFile
and createTextureFromMemory
are deprecated, I tried the WIC Loader, but can't figure out how to use it.
Earlier, this was part of the code I had:
ID3D11DeviceContext* ctx = NULL;
g_D3D11Device->GetImmediateContext(&ctx);
// update native texture from code
if (unity_TexturePointer)
{
ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)unity_TexturePointer;
D3D11_TEXTURE2D_DESC desc;
d3dtex->GetDesc(&desc);
// filled data is an unsigned char* and has the data
ctx->UpdateSubresource(d3dtex, 0, NULL, filledData, (width * 4), 0);
delete[] filledData;
}
ctx->Release();
Now, coming to WIC loading, I followed this post. I now have:
ID3D11DeviceContext* ctx = NULL;
g_D3D11Device->GetImmediateContext(&ctx);
// update native texture from code
if (unity_TexturePointer)
{
ID3D11Texture2D* d3dtex = (ID3D11Texture2D*)unity_TexturePointer;
D3D11_TEXTURE2D_DESC desc;
d3dtex->GetDesc(&desc);
ComPtr<ID3D11ShaderResourceView> srv;
std::basic_ifstream<unsigned char> file("bridge.jpg", std::ios::binary);
if (file.is_open())
{
file.seekg(0, std::ios::end);
int length = file.tellg();
file.seekg(0, std::ios::beg);
unsigned char* buffer = new unsigned char[length];
file.read(&buffer[0], length);
file.close();
HRESULT hr;
hr = CreateWICTextureFromMemory(g_D3D11Device, ctx, &buffer[0], (size_t)length, nullptr, &srv, NULL);
// What goes here??
}
}
ctx->Release();
I am totally confused what now. I somehow want that texture that is loaded to reach my d3dtex
Texture2D pointer.
I am referring to the header file and I see that we can pass the Texture pointer too, but that doesn't seem to work for me.
hr = CreateWICTextureFromMemory(g_D3D11Device, ctx, &buffer[0], (size_t)length, &d3dtex, nullptr, NULL);
It throws the error, there is no instance of overloaded function. I wanted to do this so directly my Texture pointer would be updated, since I am not quite sure how to work with the ShaderResourceView
.
Similar problems for the CreateWICTextureFromFile
methods.
Basically, I know I am doing something fundamentally wrong, and would appreciate any help or examples for the same, as the example in the Microsoft documentation doesn't really help me.
Thanks!