Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My Image is replaced by Font Image #1840

Closed
home3d2001 opened this issue May 27, 2018 · 6 comments
Closed

My Image is replaced by Font Image #1840

home3d2001 opened this issue May 27, 2018 · 6 comments

Comments

@home3d2001
Copy link

home3d2001 commented May 27, 2018

Hi there,

I am using GLFW3 +imgui ( off of master branch ) + imgui_impl_glfw_gl3.h to create GUI in my app.

I load my image in the GPU and pass the textureID to ImGui::Image. After this, I was hoping to see my image but instead I see the font image.

Here is the my code:


#include <WindowApp.h>
#include <imgui.h>
#include <imgui_impl_glfw_gl3.h>

void 
WindowApp::initialize()
{
    // initialize GLFW
    if (glfwInit())
        std::cout << "GLFW initialized successfully!" << '\n';

    // Set all the required options for GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_SAMPLES, m_multiSample);
    glfwWindowHint(GLFW_DEPTH_BITS, 24);
    glfwWindowHint(GLFW_RED_BITS, 8);
    glfwWindowHint(GLFW_GREEN_BITS, 8);
    glfwWindowHint(GLFW_BLUE_BITS, 8);
    glfwWindowHint(GLFW_ALPHA_BITS, 8);

    // create glfw window and its openGL context
    m_window = glfwCreateWindow(m_width, m_height, m_windowTitle.c_str(), nullptr, nullptr);
    if (m_window)
    {
        std::cout << "Window created successfully!" << '\n';
        glfwMakeContextCurrent(m_window);
    }

    // glad
    gladLoadGL();

    glfwSwapInterval(1);

    ImGui::CreateContext();
    ImGui_ImplGlfwGL3_Init(m_window, false);

    // load my load image
    glGenTextures(1, &m_id);
    glBindTexture(GL_TEXTURE_2D, m_id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    m_image = std::make_shared<Image>("someImage.png");
    assert( m_image );
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_image ->width(), m_image ->height(), 0, GL_BGR, GL_UNSIGNED_BYTE, m_image ->data());
}

void 
WindowApp::drawLoop()
{
    while (!glfwWindowShouldClose(m_window))
    {
        glfwPollEvents();
 
        ImGui_ImplGlfwGL3_NewFrame();

        ImGui::Begin("Debug Window", NULL, ImGuiWindowFlags_AlwaysAutoResize);

        float w = static_cast<float>(m_image ->width());
        float h = static_cast<float>(m_image ->height());
        ImGui::Image(reinterpret_cast<ImTextureID*>(&m_id), ImVec2(w, h));

        ImGui::End();

        ImGui::Render();
        ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
        
        glfwSwapBuffers(m_window);
    }

    ImGui_ImplGlfwGL3_Shutdown();
    ImGui::DestroyContext();
    glfwTerminate();
}

And here is what I get:
result

Could you tell me if this is this a bug in "imgui_impl_glfw_gl3.h"? or am I doing something wrong?

Best Regards,

H

@JSandusky
Copy link

ImGui::Image(reinterpret_cast<ImTextureID*>(&m_id), ImVec2(w, h));

Wrong.

ImGui::Image((ImTextureID)m_id, ImVec2(w, h));

@home3d2001
Copy link
Author

I tried this before but strangely visual studio 2015 compiler didn't like it.

        ImGui::Image((ImTextureID)m_id, ImVec2(w, h));  // error C2220: warning treated as error - no 'object' file generated
                                                        // warning C4312: 'type cast': conversion from 'GLuint' to 'ImTextureID' of greater size

I tried following which compiles fine but still doesn't fix my issue:

 ImTextureID vPtr = &m_id;
 ImGui::Image(vPtr, ImVec2(w, h));

@home3d2001
Copy link
Author

Oh!! This is a x64 bit app!!!

std::cout << sizeof(GLuint)  <<  '\n';  // 4 bytes
std::cout << sizeof(ImTextureID) << '\n'; //8 bytes

I did the following which fixed my issue:

ImGui::Image((ImTextureID)static_cast<uintptr_t>(m_id), ImVec2(w, h));

I still don't see why the earlier code didn't work:

ImGui::Image(reinterpret_cast<ImTextureID*>(&m_id), ImVec2(w, h));

@ocornut
Copy link
Owner

ocornut commented May 27, 2018

Your earlier code didn’t work because are passing an address to the GLuint instead of passing the value.

Using:

(ImTextureID)(intptr_t)m_id

Should work without warnings.

@ocornut
Copy link
Owner

ocornut commented May 27, 2018

Closing this now, also tried to clarify the FAQ about this.

@ocornut ocornut closed this as completed May 27, 2018
ocornut added a commit that referenced this issue May 30, 2018
@ocornut
Copy link
Owner

ocornut commented Sep 20, 2019

I have now written a tutorial to cover image file loading for common graphics API:
https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants