Fl_Gl_Window glViewport(0,0,pixel_w(),pixel_h()) and child widget size?

36 views
Skip to first unread message

duncan gibson

unread,
Dec 15, 2025, 7:31:58 PM12/15/25
to fltk.general
Hi everyone, I'm back for my annual foray with an FLTK OpengGL3 project.

I'm on a MacBook Pro running MacOS 14.0 (Sonoma) with a 3024x1964 Retina XDR display

Can I first just say the CMake support for FetchContent(FLTK) makes life so much easier :-)

My project basically has half of the main window dedicated to user interface widgets.
and the other half is derived from Fl_Gl_Window, and a lot of the setup logic is based
on the OpenGL3test example and previous help in this forum with Apple specifics.

In a previous incarnation, I had an Fl_Box child of the main window, declared after the
derived Fl_Gl_Window, and positioned over it, to be used for status messages, etc.
That mostly worked, but introduced a dependency on the main window that I thought
could be encapsulated as a true child of the derived Fl_GL_Window instead, especially
when I noticed the Fl_Gl_Window docs describing the additional explicit call to begin().

But, there's no example code to base this on, and when I implemented it, I found that
the size of the Fl_Gl_Window Fl_Box child is half what I think it should be, and if I double
the width, the box appears to be clipped half-way across the Fl_Gl_Window.
Is this me not understanding something [as usual] or possibly a bug?

I've simplified the code down as much as possible, so there's no real OpenGL3 code
with shaders,  there's basically just the OpenGL3 checks, initialization and glViewport().

Any insights gratefully received.

main.cpp
CMakeLists.txt

Manolo

unread,
Dec 16, 2025, 2:29:15 AM12/16/25
to fltk.general
I didn't look at the attached code yet but can already put here some probably relevant information:

An example of adding child FLTK widgets to an Fl_Gl_Window is available with test/cube.cxx.
Another one uses OpenGL3 and is at examples/OpenGL3test.cxx.
Both create child widgets with the desired size and work equally well under macOS with and
without a retina display.
They also show how it's possible to otain a partially transparent color in such widgets.

duncan gibson

unread,
Dec 22, 2025, 12:45:30 PM12/22/25
to fltk.general
An example of adding child FLTK widgets to an Fl_Gl_Window is available with test/cube.cxx.
Another one uses OpenGL3 and is at examples/OpenGL3test.cxx.
Both create child widgets with the desired size and work equally well under macOS with and
without a retina display.

Thanks for the examples of how to create FLTK widgets as children of an Fl_Gl_Window.
test/cube.cxx and examples/OpenGL3test.cxx add the child widgets using extra code
to save and unset the Fl_Group::current(), add the widget, and reset Fl_Group::current()
which lead me a merry dance around the houses until I finally noticed the magic trick:

int main(int argc, char* argv[]) {
    Fl::use_high_res_GL(1);    // this is the magic trick
    Fl_Double_Window window(1000, 500, "Window v GL_Window");
    Fl_Box w_box(5, 450, 490, 50, "[x=5 y=450 w=490 h=50]");
    w_box.align(FL_ALIGN_INSIDE | FL_ALIGN_CENTER); 
    GlWindow gl_window(500, 0, 500, 500);
    window.end();
    // 1. simple
    gl_window.begin();
    Fl_Box g_box(5, 450, 490, 50, "[x=5 y=450 w=490 h=50]");
    g_box.labelcolor(FL_WHITE);
    gl_window.end();
    // or 2. comprehensive
    // Fl_Group* saved = Fl_Group::current();
    // Fl_Group::current(0);
    // Fl_Box g_box(5, 450, 490, 50, "[x=5 y=450 w=490 h=50]");
    // g_box.labelcolor(FL_WHITE);
    // gl_window.add(&g_box);
    // Fl_Group::current(saved);
    window.show(argc, argv);
    return Fl::run();
}

The "OpenGL and support of HighDPI display" section already describes the use of
    glViewport(0, 0, pixel_w(), pixel_h());  // rather than plain w() and h()
but should maybe mention Fl::use_high_res_GL(1); as well as a link to the details.

Something else to note is that "Using OpenGL Optimizer with FLTK" sections starts with
a link to "OpenGL Optimizer" that now points to a generic HPE Cray Supercomputing page.
Is "OpenGL Optimizer" still sufficiently alive to be "recommended" by the FLTK docs?

The most recent reference given at the end of the manual is from 1996

D.

Manolo

unread,
Dec 23, 2025, 4:16:53 AM12/23/25
to fltk.general
Please notice that section  "OpenGL and support of HighDPI display" of the FLTK
documentation includes a "Note" stating that 
"A further coding rule is necessary to properly support retina displays and OpenGL under macOS"
where the necessity to call Fl::use_high_res_GL(1) appears.

duncan gibson

unread,
Dec 23, 2025, 11:33:33 AM12/23/25
to fltk.general
Please notice that section  "OpenGL and support of HighDPI display" of the FLTK
documentation includes a "Note" stating that 
"A further coding rule is necessary to properly support retina displays and OpenGL under macOS"
where the necessity to call Fl::use_high_res_GL(1) appears.

Yes, I found that link eventually, but I'd argue that it would be better to replace that Note in the
"OpenGL and support of HighDPI Displays" with an explicit reference such as the following:

To use high resolution graphics in OpenGL windows on MacOS set "Fl::use_high_res_GL(1);"
before creating the window: see [OpenGL and 'retina' displays] link

That way it is explicit and appears directly with the given recommendation to use
 glViewport(0, 0, glw->pixel_w(), glw->pixel_h());

Unless there's some subtle distinction between "HighDPI" and "Retina" displays?

Thinking ahead, on a quasi-related issue...
Having skimmed through the whole User Manual, it isn't clear to me whether it is possible
for a cross-platform application to query whether you have a high dpi screen before you
create your window, or whether you just set Fl::use_high_res_GL(1); anyway at the
start of main() and in handle() check whether it is still true, and/or w() == pixel_w() 
For the moment, I only have this Mac with Retina display to test with.

Manolo

unread,
Dec 28, 2025, 5:30:41 AM12/28/25
to fltk.general
Le mardi 23 décembre 2025 à 17:33:33 UTC+1, englishman.in....@gmail.com a écrit :
...
Having skimmed through the whole User Manual, it isn't clear to me whether it is possible
for a cross-platform application to query whether you have a high dpi screen before you
create your window, or whether you just set Fl::use_high_res_GL(1); anyway at the
start of main() and in handle() check whether it is still true, and/or w() == pixel_w() 
For the moment, I only have this Mac with Retina display to test with.

I agree it's not easy for an FLTK app on macOS to query whether a HighDPI display is present.
I believe the intention is that any macOS app may encounter a HighDPI display. It's enough to call
Fl::use_high_res_GL(1) for the app to work correctly under any kind of display.
Reply all
Reply to author
Forward
0 new messages