Reputation: 13
To make some onscreen and offscreen rendering via Qt5 OpenGL framework, such that the resources can be easily shared between both rendering parts. Specifically,
QOpenGLWidget
s) under different settings, e.g. different sizes, for simplicity;QImage
or cv::Mat
object;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:QMainWindow
containing multiple QOpenGLWidget
(subclass of QOpenGLWidget
) objects;QOffscreenSurface
, QOpenGLContext
, and QOpenGLFramebufferObject
pointers, as well as a QOpenGLFunctions
pointer to invoke OpenGL functions do the actual rendering work, much similar to this link.There are two QOpenGLContext
s:
QWindow
-based QOffscreenSurface
are not allowed to exist outside the gui thread;QOpenGLContext
is invalid.So my questions are:
A brief actual code example doing a simple rendering work (say, draw a triangle via shading language) will be much appreciated.
Upvotes: 1
Views: 1008
Reputation: 7592
Assuming that QOpenGLContext *main_ctx
is the context that was created by QOpenGLWidget
for actual rendering, you can create another context ctx
in any thread and make it share textures and buffers with the first one:
ctx = std::make_unique<QOpenGLContext>();
ctx->setFormat(main_ctx->format());
ctx->setShareContext(main_ctx);
ctx->create();
I don't think that QOffscreenSurface
must be a QWindow
-based.
offscreen_surface = std::make_unique<QOffscreenSurface>();
offscreen_surface->setFormat(ctx->format());
offscreen_surface->create();
ctx->makeCurrent(offscreen_surface);
Then create a QOpenGLFramebufferObject
and render into it from the second context (second thread).
Then use its texture in the main context: glBindTexture(GL_TEXTURE_2D, fbo->texture());
. Maybe there is a need for some synchronization when doing this.
Upvotes: 1