I am trying to load my data from a binary file (ppm) and create a texture using this data. It is important that I learn to do it this way as I am eventually going to be packing all of my textures into a single binary file and then index them so creating the texture with pure binary data is something that I need to be able to do.
It seems that the texture is drawing correctly, but the colours are incorrect. I saved my image as .ppm just for this test application.
Here is the code to load my data
ppm ppm;
ppm.read(std::string("textureppm.ppm"));
//just to ensure the data is correct
uint32_t val = ppm.pixels[0];
unsigned char r = (val & 0xFF000000) >> 24;
unsigned char g = (val & 0x00FF0000) >> 16;
unsigned char b = (val & 0x0000FF00) >> 8;
unsigned char a = (val & 0x000000FF);
ID3D11ShaderResourceView* texSRV = nullptr;
D3D11_SUBRESOURCE_DATA initData = { &ppm.pixels, ppm.width * sizeof(uint32_t), 0};
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = ppm.width; desc.Height = ppm.height; desc.MipLevels = 1; desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
ID3D11Texture2D* tex;
HRESULT hr = getDevice()->CreateTexture2D(&desc, &initData, &tex);
if (SUCCEEDED(hr))
{
D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {};
SRVDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
SRVDesc.Texture2D.MipLevels = 1;
hr = getDevice()->CreateShaderResourceView(tex,
&SRVDesc, &texSRV);
}
if (FAILED(hr)) {
throw 0;
}
else {
setTexture(texSRV);
}
I have packed each byte into a uint32_t
as it seems that is the format that is required DXGI_FORMAT_R8G8B8A8_UNORM
Here is the packing
uint32_t ppm::CreateRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
uint32_t value = 0;
int r2 = (r & 0xff) << 24;
int g2 = (g & 0xff) << 16;
int b2 = (b & 0xff) << 8;
int a2 = (a & 0xff);
value += r2 + g2 + b2 + a2;
return value;
}
This code produces the following texture
Does anyone know what I am doing wrong?