From 89f9671b406d1d2ac0377ef90d44ebfa7a92150e Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Sun, 25 Sep 2022 16:39:40 +0200 Subject: Add cross-platform support for adding widgets to an OpenGL3-based Fl_Gl_Window. Under non-macOS platforms, the key is to call glUseProgram(0); after having used OpenGL 3 which allows to then use OpenGL 1 and draw FLTK widgets over the OpenGL3 scene. Under macOS, this is impossible because macOS GL3 contexts are not compatible with GL1. The solution implemented here is to create an additional Fl_Gl_Window placed above and sized as the GL3-based window, to give it a non opaque, GL1-based context, and to put the FLTK widgets in that additional window. --- examples/OpenGL3test.cxx | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'examples/OpenGL3test.cxx') diff --git a/examples/OpenGL3test.cxx b/examples/OpenGL3test.cxx index 9acbaf38b..26ee1b816 100644 --- a/examples/OpenGL3test.cxx +++ b/examples/OpenGL3test.cxx @@ -138,6 +138,13 @@ public: GLfloat p[]={0,0}; glUniform2fv(positionUniform, 1, (const GLfloat *)&p); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); +#ifndef __APPLE__ + // suggested by https://stackoverflow.com/questions/22293870/mixing-fixed-function-pipeline-and-programmable-pipeline-in-opengl + // Switch from GL3-style to GL1-style drawing; + // good under Windows, X11 and Wayland; impossible under macOS. + glUseProgram(0); + Fl_Gl_Window::draw(); // Draw FLTK child widgets. +#endif } virtual int handle(int event) { static int first = 1; @@ -161,6 +168,9 @@ public: redraw(); } + int retval = Fl_Gl_Window::handle(event); + if (retval) return retval; + if (event == FL_PUSH && gl_version_major >= 3) { static float factor = 1.1; GLfloat data[4]; @@ -175,7 +185,7 @@ public: add_output("push Fl_Gl_Window::pixels_per_unit()=%.1f\n", pixels_per_unit()); return 1; } - return Fl_Gl_Window::handle(event); + return retval; } void reset(void) { shaderProgram = 0; } }; @@ -218,6 +228,25 @@ void add_output(const char *format, ...) } +void button_cb(Fl_Widget *, void *) { + add_output("run button callback\n"); +} + +void add_widgets(Fl_Gl_Window *g) { +#ifdef __APPLE__ + g = fl_mac_prepare_add_widgets_to_GL3_win(g); +#endif + Fl::set_color(FL_FREE_COLOR, 255, 0, 0, 140); // partially transparent red + g->begin(); + // Create here widgets to go above the GL3 scene + Fl_Button* b = new Fl_Button( 0, 170, 60, 30, "button"); + b->color(FL_FREE_COLOR); + b->box(FL_BORDER_BOX ); + b->callback(button_cb, NULL); + g->end(); +} + + int main(int argc, char **argv) { Fl::use_high_res_GL(1); @@ -225,6 +254,7 @@ int main(int argc, char **argv) SimpleGL3Window *win = new SimpleGL3Window(0, 0, 300, 300); win->end(); output_win(win); + add_widgets(win); topwin->end(); topwin->resizable(win); topwin->label("Click GL panel to reshape"); -- cgit v1.2.3