Porting a Qt application that uses QOffscreenSurface and QOpenGLContext

148 views
Skip to first unread message

Gonzalo Garramuno

unread,
Sep 14, 2022, 9:55:28 PM9/14/22
to fltkg...@googlegroups.com
I find myself porting a Qt application that uses QOffscreenSurface and QOpenGLContext to draw opengl 4.1 graphics onto a separate thread that has no window.
The QOpenGLContext class seems like it provides the gl context, with the following code at the constructor:

Constructor::
{
glContext = new QOpenGLContext;
QSurfaceFormat surfaceFormat;
surfaceFormat.setMajorVersion(4);
surfaceFormat.setMinorVersion(1);
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
glContext->setFormat(surfaceFormat);
glContext->create();

offscreenSurface = new QOffscreenSurface;
offscreenSurface->setFormat(glContext->format());
offscreenSurface->create();
}

The thread when running (no draw() function, just a normal function) calls:

void class::run()
{
glContext->makeCurrent( offscreenSurface.get() );
// other GL drawing code
}

The QOffscreenSurface I gather is the equivalent to Fl_Image_Surface in FLTK, but I don’t have a clue what would be equivalent to QOpenGLContext. Does FLTK offer a GL context class?




Gonzalo Garramuno
ggar...@gmail.com




Manolo

unread,
Sep 15, 2022, 2:25:56 AM9/15/22
to fltk.general


Le jeudi 15 septembre 2022 à 03:55:28 UTC+2, ggar...@gmail.com a écrit :
……


The QOffscreenSurface I gather is the equivalent to Fl_Image_Surface in FLTK, but I don’t have a clue what would be equivalent to QOpenGLContext. Does FLTK offer a GL context class?

You can access  the GL context with public member Fl_Gl_Window->context()

Gonzalo Garramuno

unread,
Sep 15, 2022, 9:14:30 AM9/15/22
to fltkg...@googlegroups.com
My problem is not accessing the context from a Fl_Gl_Window. I am trying to set the active GL context *without* opening any windows, as Qt does. It allows the GL context to run on a separate thread instead of the main loop thread.


Gonzalo Garramuno
ggar...@gmail.com




Ian MacArthur

unread,
Sep 15, 2022, 9:27:35 AM9/15/22
to fltk.general
On Thursday, 15 September 2022 at 14:14:30 UTC+1 Gonzalo wrote:

My problem is not accessing the context from a Fl_Gl_Window. I am trying to set the active GL context *without* opening any windows, as Qt does. It allows the GL context to run on a separate thread instead of the main loop thread.

So... like creating an offscreen GL surface, and then getting the context for that, perhaps?
I think you can (at least with fltk-1.4) create offscreen GL surfaces, so that sounds entirely feasible.

That said, the example I thought I had of that, I can not now find; I was pretty sure it was on this laptop, but apparently not...

 

Gonzalo Garramuno

unread,
Sep 15, 2022, 6:17:03 PM9/15/22
to fltkg...@googlegroups.com


> El 15 sep. 2022, a las 10:27, Ian MacArthur <imaca...@gmail.com> escribió:
>
> So... like creating an offscreen GL surface, and then getting the context for that, perhaps?

Yes. The issue is that the offscreen GL surface is not drawn inside the GL window’s draw() method, so there’s no GL context.

> I think you can (at least with fltk-1.4) create offscreen GL surfaces, so that sounds entirely feasible.
>
> That said, the example I thought I had of that, I can not now find; I was pretty sure it was on this laptop, but apparently not...

I found WIN32 code to do what I want, but I cannot find X11/Wayland or macOS code to do it. Here’s what I mean to be doing:

void Program::someFunction()
{
HDC hdc = wglGetCurrentDC();
HGLRC hglrc = wglGetCurrentContext(); // this we can get from another GL window with the context() member function
HGLRC hglrc_new = wglCreateContext(hdc);

wglShareLists(hglrc, hglrc_new);
loadThread = boost::thread(&Program::loadFunc, this, hdc, hglrc_new);
}


/**
* Imports all our assets. Supposed to run in its own thread with its own OpenGL context.
* @param hdc The current device context.
* @param hglrc A OpenGL context thats shares its display list with the main rendering context.
*/

void Program::loadFunc(HDC hdc, HGLRC hglrc)
{
wglMakeCurrent(hdc, hglrc);

//Do stuff...

wglMakeCurrent(NULL, NULL);

wglDeleteContext(hglrc);
}


Gonzalo Garramuno
ggar...@gmail.com




imm

unread,
Sep 16, 2022, 5:21:26 AM9/16/22
to General FLTK
On Thu, 15 Sept 2022 at 23:17, Gonzalo wrote:

> > So... like creating an offscreen GL surface, and then getting the context for that, perhaps?
>
> Yes. The issue is that the offscreen GL surface is not drawn inside the GL window’s draw() method, so there’s no GL context.
>

Yes, but GL contexts under FLTK are a bit weird in that you can
(pretty much, anyway) draw to them outside of the regular draw()
method - if you can get a suitable reference to the context, then
using the usual GL techniques you can then draw stuff into that
context "as you go along"; so that might be sufficient for your needs?
I guess that is what Manolo was meaning when he said "You can access
the GL context with public member Fl_Gl_Window->context()" earlier?
Not sure...

Gonzalo Garramuno

unread,
Sep 16, 2022, 8:35:27 AM9/16/22
to fltkg...@googlegroups.com


> El 16 sep. 2022, a las 06:26, imm <imaca...@gmail.com> escribió:
>
> On Thu, 15 Sept 2022 at 23:17, Gonzalo wrote:
>
>>> So... like creating an offscreen GL surface, and then getting the context for that, perhaps?
>>
>> Yes. The issue is that the offscreen GL surface is not drawn inside the GL window’s draw() method, so there’s no GL context.
>>
>

Okay. Here’s how I solved it. I created a class subclassing an Fl_Gl_Window of size 0, 0; showed it so it created the context. Then, in the draw() function I set the:

if (! valid() )
{
// init gl, in my case with;
initializeGL();
valid(1);
}

The constructor of the class would create a thread calling the run() function of the class, which would process some movie thumbnails by calling make_current() just before the offscreen texture FBOs were created and drawn into.


Gonzalo Garramuno
ggar...@gmail.com




Reply all
Reply to author
Forward
0 new messages