【问题标题】:ImGui + stb_image will not render image, renders a test image (?)ImGui + stb_image 不会渲染图像,渲染测试图像(?)
【发布时间】:2022-12-21 21:26:22
【问题描述】:

我正在尝试使用 stb_image.h 和 DirectX11 在 ImGui 中渲染 JPEG 或 PNG 图像。

它正确获取图像信息,将其加载到内存中(指针有效)并知道图像大小。但是它不会渲染图像,我已经尝试了很多 .jpegs 和 .pngs 以防它是一个损坏的文件但无济于事。

我正在使用 ImGui 开发人员提供的 DX11 示例代码,但我似乎运气不佳。 https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples#Example-for-DirectX11-users

我的 LoadTextureFromFile 函数,我的编辑在评论栏之间:

bool LoadTextureFromFile(const char* filename, ID3D11ShaderResourceView** out_srv, int* out_width, int* out_height)
{
    // Load from disk into a raw RGBA buffer
    int image_width = 0;
    int image_height = 0;
    unsigned char* image_data = stbi_load(filename, &image_width, &image_height, NULL, 4);
    if (image_data == NULL)
        return false;
    // Create texture
    D3D11_TEXTURE2D_DESC desc;
    ZeroMemory(&desc, sizeof(desc));
    desc.Width = image_width;
    desc.Height = image_height;
    desc.MipLevels = 1;
    desc.ArraySize = 1;
    desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    desc.SampleDesc.Count = 1;
    desc.Usage = D3D11_USAGE_DEFAULT;
    desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    desc.CPUAccessFlags = 0;

    ID3D11Texture2D* pTexture = NULL;
    D3D11_SUBRESOURCE_DATA subResource;
    subResource.pSysMem = image_data;
    subResource.SysMemPitch = desc.Width * 4;
    subResource.SysMemSlicePitch = 0;

////////////
    DWORD createDeviceFlags = 0;
    #ifdef _DEBUG
        createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
    #endif

    Microsoft::WRL::ComPtr<ID3D11Device> device;
    Microsoft::WRL::ComPtr<ID3D11DeviceContext> context;
    D3D_FEATURE_LEVEL fl;
    HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE,
        nullptr, createDeviceFlags, nullptr,
        0, D3D11_SDK_VERSION, &device, &fl, &context);
////////////
    device->CreateTexture2D(&desc, &subResource, &pTexture);
    // Create texture view
    D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
    ZeroMemory(&srvDesc, sizeof(srvDesc));
    srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    srvDesc.Texture2D.MipLevels = desc.MipLevels;
    srvDesc.Texture2D.MostDetailedMip = 0;
    device->CreateShaderResourceView(pTexture, &srvDesc, out_srv);
    pTexture->Release();

    *out_width = image_width;
    *out_height = image_height;
    stbi_image_free(image_data);

    return true;
}

像这样初始化一次:

bool ret = LoadTextureFromFile("C:\\Users\\User\\Desktop\\MyImage01.png", &my_texture, &my_image_width, &my_image_height);

在 GUI 代码体中显示如下:

        ImGui::Begin("DirectX11 Texture Test");
        ImGui::Text("pointer = %p", my_texture);
        ImGui::Text("size = %d x %d", my_image_width, my_image_height);
        ImGui::Image((void*)my_texture, ImVec2(my_image_width, my_image_height));
        ImGui::End();

最终结果是一个显示指针和正确大小的窗口,但呈现一个看起来像占位符的奇怪图像。我不知道我的问题是严格意义上的 ImGui 还是 stbi_image,也不知道这个占位符是来自 ImGui 还是 stbi。

Here is a screenshot of the window.

【问题讨论】:

    标签: c++ imgui stb-image


    【解决方案1】:

    我有同样的问题。

    加载的图像是字体 Atlas btw。

    我唯一的线索是这篇文章: https://github.com/ocornut/imgui/issues/3591

    如果您找到解决方案,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多