ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
61.46k stars 10.34k forks source link

`ImGui::Image()` doesn't load the given Image. #8171

Closed MubinMuhammad closed 1 week ago

MubinMuhammad commented 1 week ago

Version/Branch of Dear ImGui:

Version 1.91.5, Branch: master

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp

Compiler, OS:

archlinux 6.6.56-1-lts + g++ 14.2.1

Full config/build information:

Used a python script to build the project.

Details:

Before the details, I did read Image Loading and Displaying Examples from ImGui Wiki. As I'm using OpenGL, the general steps are:

For this, I created this function:

unsigned int engine::textureInit(const img_pixmap &img) {
    GLuint texture_id;
    glGenTextures(1, &texture_id);
    glBindTexture(GL_TEXTURE_2D, texture_id);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0,
                 img.pixelsz == 3 ? GL_RGB : GL_RGBA,
                 img.width, img.height, 0,
                 img.pixelsz == 3 ? GL_RGB : GL_RGBA,
                 GL_UNSIGNED_BYTE, (unsigned char *)img.pixels);

    glBindTexture(GL_TEXTURE_2D, 0);

    return texture_id;
}

The next step is to just use ImGui::Image() to render the image to a window. Here is the function I wrote for this:

void app::Image::show() {
    glBindTexture(GL_TEXTURE_2D, m_id); // m_id is the texture id
    ImGui::Begin(m_name.c_str()); // m_name is the image path
    ImGui::Text("pointer: %x\nsize: %dx%d", m_id,
                m_pix_map.width, m_pix_map.height); // m_pix_map is img_pixmap by `libimago`
    ImGui::Image((intptr_t)m_id, (ImVec2){256, 256});
    ImGui::End();
    glBindTexture(GL_TEXTURE_2D, 0);
}

After all of this I don't get the Image I select. Instead, I get a Font Texture Atlas. This happens no matter which image I choose.

Screenshots/Video:

No matter which Image I choose, I always find this texture atlas: TestImage

Minimal, Complete and Verifiable Example code:

Example code is given in the details.

ocornut commented 1 week ago

Well have you checked what’s your value of m_id ? It is valid ? Use a debugger and confirm all the values and state from both functions.

There’s no need to call glBindTexture() while submitting imgui calls.

MubinMuhammad commented 1 week ago

How do I check the validity of m_id? The Image init function is:

bool app::Image::init(const char *path) {
    img_init(&m_pix_map);
    img_load(&m_pix_map, path);

    if (m_pix_map.fmt == NUM_IMG_FMT) {
        return false;
    }

    m_name = path;
    m_id = engine::textureInit(m_pix_map);
    return true;
}

Here, if the image is invalid the function returns false. But as it doesn't return false, that means the image is valid. And yet, it shows the texture atlas.

Also, the provided image shows pointer: 2 does it confirm validity? Thanks.

MubinMuhammad commented 1 week ago

The actual issue was on my part, and doesn't relate with this. Closing.