openFrameworks | ofxImGui fbo + image + texture + clickable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//.h | |
ofTexture tex; | |
ofFbo fbo; | |
void quantizerRefreshImage(); | |
//.cpp | |
//setup | |
bool b = ofGetUsingArbTex(); | |
ofDisableArbTex(); | |
ofLoadImage(tex, getImagePath()); | |
fbo.allocate(tex.getWidth(), tex.getHeight()); | |
fbo.createAndAttachTexture(GL_RGB, 0); //Position | |
fbo.createAndAttachTexture(GL_RGBA32F, 1); //velocity | |
fbo.createAndAttachRenderbuffer(GL_DEPTH_COMPONENT, GL_DEPTH_ATTACHMENT); | |
fbo.checkStatus(); | |
fbo.begin(); | |
ofClear(0); | |
fbo.end(); | |
if (b) ofEnableArbTex(); | |
fbo.begin();//draw once only | |
ofClear(0, 0, 0, 0); | |
tex.draw(0, 0); | |
fbo.end(); | |
//- | |
//draw image preview | |
if (tex.isAllocated()) | |
{ | |
float w = tex.getWidth(); | |
float h = tex.getHeight(); | |
float ratio = h / w; | |
if (ImGui::ImageButton((ImTextureID)(uintptr_t)fbo.getTexture(0).getTextureData().textureID, ImVec2(_w, _w * ratio))) | |
{ | |
ofLogNotice(__FUNCTION__) << "Image Pressed"; | |
} | |
} | |
ImGui::Text(currentImage_name.get().c_str()); | |
//---- | |
//other examples | |
// https://forum.openframeworks.cc/t/ofximgui-cant-display-more-than-one-image-in-a-window/36677/4 | |
// https://forum.openframeworks.cc/t/how-to-draw-an-offbo-in-ofximgui-window/33174 | |
//I’ve found it works with this kind of FBO: | |
ofFbo::Settings fboSettings ; | |
fboSettings.width = width ; | |
fboSettings.height = height ; | |
fboSettings.internalformat = GL_RGBA ; | |
fboSettings.textureTarget = GL_TEXTURE_2D ; | |
previewFbo.allocate( fboSettings ) ; | |
//then | |
previewFbo.begin() ; | |
cam->draw( 0.f, 0.f ) ; | |
previewFbo.end() ; | |
ImTextureID textureID = ( ImTextureID )( uintptr_t )previewFbo.getTexture().getTextureData().textureID ; | |
auto size = ImGui::GetContentRegionAvail() ; // for example | |
ImGui::Image( textureID, size ) ; | |
-- | |
ofFbo::Settings fboSettings ; | |
fboSettings.width = image1.getWidth() ; | |
fboSettings.height = image1.getHeight() ; | |
fboSettings.internalformat = GL_RGBA ; | |
fboSettings.textureTarget = GL_TEXTURE_2D ; | |
fbo1.allocate( fboSettings ) ; | |
ofFbo::Settings fboSettings2; | |
fboSettings.width = image2.getWidth() ; //Note that I wrote fboSettings instead of fboSettings2, my mistake | |
fboSettings.height = image2.getHeight() ; | |
fboSettings.internalformat = GL_RGBA ; | |
fboSettings.textureTarget = GL_TEXTURE_2D ; | |
fbo2.allocate(fboSettings2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment