繁体   English   中英

ImGui + stb_image 不会渲染图像,渲染测试图像(?)

[英]ImGui + stb_image will not render image, renders a test image (?)

我正在尝试使用 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。

这是窗口的屏幕截图。

我有同样的问题。

错误是创建一个新设备。 相反,您不应在 Draw 循环中实现该功能,而应在创建用于呈现 ImGui 的设备的同一位置实现该功能,并使用该功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM