Skip to content

Binding Images to Texture2D and then render using ImGui::Image() #108

@TrevorCash

Description

@TrevorCash

Hello,

I am trying to bind an image to a texture and then render using ImGui::Image() call.

The Code I am using is below (which loads a png image and then binds to texture when the button is pushed)

if (ImGui::Button("Load Test Zip Resource")) {
    auto manager = ControlSingleton::GetInstance()->zipResourceManager;
    size_t size;
    auto data = manager->AccessResource("product-icon.png", size);

    Magnum::PluginManager::Manager<Magnum::Trade::AbstractImporter> impManager;
    impManager.setPluginDirectory(getRuntimeDependancyPath());
    Magnum::Containers::Pointer<Magnum::Trade::AbstractImporter> importer =
            impManager.loadAndInstantiate("PngImporter");

    Corrade::Containers::ArrayView<char> dataView{data, size};
    
    bool s = importer->openData(dataView);
    
    auto image = importer->image2D(0);
    CORRADE_INTERNAL_ASSERT(image);
    logoTexture.setWrapping(Magnum::GL::SamplerWrapping::ClampToEdge)
            .setMagnificationFilter(Magnum::GL::SamplerFilter::Linear)
            .setMinificationFilter(Magnum::GL::SamplerFilter::Linear)
            .setStorage(1, Magnum::GL::textureFormat(image->format()), image->size())
            .setSubImage(0, {}, *image);

    
    
}
if(logoTexture.imageSize(0).x() > 0)
    ImGui::Image(ImTextureID(logoTexture.id()), ImVec2(100, 100));

The above code works fine.

later when imgui is drawn. in MagnumImGuiIntegration / Context.cpp / drawFrame()

I get a crash at this call (line 366)
_shader
.bindTexture(*static_castGL::Texture2D*(pcmd->TextureId))
.draw(_mesh);

I think it may not be correctly converting to GL::Texture2D from pcmd->TextureId (which is 0x2)

Call stack

MagnumGL-d.dll!Magnum::GL::AbstractTexture::bind(int textureUnit) Line 344 C++
MagnumShaders-d.dll!Magnum::Shaders::FlatGL<2>::bindTexture(Magnum::GL::Texture<2> & texture) Line 772 C++
MagnumImGuiIntegration-d.dll!Magnum::ImGuiIntegration::Context::drawFrame() Line 366 C++
HC_API_Library_NATIVE.dll!Magnum::Examples::ControlInterfaceApp::drawEvent() Line 452 C++
HC_API_Library_NATIVE.dll!Magnum::Platform::GlfwApplication::mainLoopIteration() Line 784 C++
HC_API_Library_NATIVE.dll!ControlAPI::controlGuiInterface::update() Line 564 C++
HC_API_ControlStudio.exe!main() Line 30 C++
[External Code]

Is there some other step I should be doing to get a image to render with ImGui? I keep 1 Texture2D object around at all times.

Thanks for any help/tips in advance.

-Trevor

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions