Objective:
To make some onscreen and offscreen rendering via Qt5 OpenGL framework, such that the resources can be easily shared between both rendering parts. Specifically,
- the rendering work is done through the offscreen part (the framebuffer might be larger than the display screen);
- the results of the offscreen rendering can be displayed in multiple onscreen parts (say,
QOpenGLWidget
s) under different settings, e.g. different sizes, for simplicity; - the results of the offscreen rendering can also be extracted from GPU and saved into a
QImage
orcv::Mat
object; - the above tasks can be executed asynchronously (doing the second offscreen rendering, while displaying or extracting the first offscreen result).
Current solution:
Since I don't know how to share resources between both parts, the actual rendering work are done redundantly in both parts in my current solution:
The onscreen part:- A
QMainWindow
containing multipleQOpenGLWidget
(subclass ofQOpenGLWidget
) objects;
- A custom class involving members of
QOffscreenSurface
,QOpenGLContext
, andQOpenGLFramebufferObject
pointers, as well as aQOpenGLFunctions
pointer to invoke OpenGL functions do the actual rendering work, much similar to this link.
- As the reason above, the actual rendering work is extracted into a seperated class and both parts (onscreen and offscreen) have its handle.
Questions:
There are two QOpenGLContext
s:
- When doing the offscreen work in a background thread (for asynchronously rendering), it says the
QWindow
-basedQOffscreenSurface
are not allowed to exist outside the gui thread; - When doing this in the main (GUI) thread, it says the
QOpenGLContext
is invalid.
So my questions are:
- Should I do the offscreen and onscreen work in the same GUI thread or not?
- What is the best way of communicating and sharing resources between the offscreen and onscreen parts?
A brief actual code example doing a simple rendering work (say, draw a triangle via shading language) will be much appreciated.