LoginSignup
0
0

DxLibでID3D11ShaderResourceView取得

Posted at

忘備用

DxLib上でimguiのUIに画像を表示したい,
マップチップ選択等用

imguiで扱う画像はID3D11ShaderResourceView*なので
そこをあんまり意識せずに表示できるように。
DxLibのLoadImageで読んだ画像はピクセル単位でのアクセスが出来ないので
それ用のLoadSoftImage使って取得する。
CreateTexture2Dに入れられるように格納する。

ID3D11ShaderResourceView* getImageResource11(int softimageHandle) {

    ID3D11Device* device = static_cast<ID3D11Device*>(const_cast<void*>(GetUseDirect3D11Device()));
    ID3D11DeviceContext* context = static_cast<ID3D11DeviceContext*>(const_cast<void*>(GetUseDirect3D11DeviceContext()));
    // 画像の幅と高さを取得
    int width, height;
    GetSoftImageSize(softimageHandle, &width, &height);


    // 画像データを取得
    unsigned char* imageData = new unsigned char[width * height * 4]; // RGBA

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            // ピクセル単位で色を取る
            int r,g,b,a;
            GetPixelSoftImage(softimageHandle, x, y , &r , &g , &b , &a);
            int index = (y * width + x) * 4;
            imageData[index] = static_cast<unsigned char>(r);
            imageData[index + 1] = static_cast<unsigned char>(g);
            imageData[index + 2] = static_cast<unsigned char>(b);
            imageData[index + 3] = static_cast<unsigned char>(a);
        }
    }
    // テクスチャの作成
    D3D11_TEXTURE2D_DESC texDesc;
    ZeroMemory(&texDesc, sizeof(texDesc));
    texDesc.Width = width;
    texDesc.Height = height;
    texDesc.MipLevels = 1;
    texDesc.ArraySize = 1;
    texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    texDesc.SampleDesc.Count = 1;
    texDesc.Usage = D3D11_USAGE_DEFAULT;
    texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;

    D3D11_SUBRESOURCE_DATA initData;
    ZeroMemory(&initData, sizeof(initData));
    initData.pSysMem = imageData;
    initData.SysMemPitch = width * 4; // Assuming 32-bit RGBA

    ID3D11Texture2D* texture = nullptr;
    device->CreateTexture2D(&texDesc, &initData, &texture);

    // ShaderResourceViewの作成
    ID3D11ShaderResourceView* pSRV = nullptr;
    device->CreateShaderResourceView(texture, nullptr, &pSRV);

    // 解放処理
    delete[] imageData;
    return pSRV;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0