ofTexture too small with imGui::ImageButton on gles / iOS

I had an ImGui::ImageButton with the image being too small and I thought I would share how I fixed it in case it help anyone some day.

// setup
// In GLES, this will be upgraded to 512 x 512 without warning (power of 2 textures)
ImVec2 butSize = ImVec2(303, 505);

ofFbo::Settings settings;
settings.width = butSize.x;
settings.height = butSize.y;
settings.textureTarget = GL_TEXTURE_2D;
settings.internalformat = GL_RGBA;
fbo.allocate(settings);

fboTexID = reinterpret_cast<ImTextureID>(fbo.getTexture().texData.textureID);

// (draw to fbo)

// draw too small on gles
if (ImGui::ImageButton(fboTexID, butSize)) {
            std::cout << "clicked!" << std::endl;
}

To fix the issue:

// use this variable to compensate for the texture enlargement
ImVec2 fboUV = fbo.getTexture().getCoordFromPoint(butSize.x, butSize.y);

// draw using the calculated uv
// if you don't specify uvs, they are (0.0, 0.0) and (1.0, 1.0)
if (ImGui::ImageButton(fboTexID, butSize, ImVec2(), fboUV)) {
            std::cout << "clicked!" << std::endl;
}

Based on https://github.com/openframeworks/openFrameworks/issues/5826#issuecomment-380633564

1 Like