将 QWidget 渲染到 FBO

3

我正在用Qt编写一个VR应用程序,需要在桌面和VR内部显示QWidget。

目前我的渲染方式如下:

QOpenGLPaintDevice device(QSize(w12,h12));
QPainter painter(&device);


gl_functions->glBindFramebuffer(GL_FRAMEBUFFER, fbo);

//painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
gl_functions->glClearColor(0.0, 0.0, 0.0, 0.0);
gl_functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

//QPixmap pixmap(widget2->size());
//widget2->render(&pixmap);
//pixmap.save("pic.jpg");


widget2->render(&painter);

这个函数被调用于主要的QOpenGLWidget的paintGL()函数中。

在VR中,该纹理被用作一个四边形。目前只有文本可以呈现。如果使用QPixmap作为设备,那么小部件就会呈现得很好。

我的小部件非常复杂,包含了连接所有旋钮、标签等的4000行代码...

小部件外观


你确定在使用渲染结果之前(无论是直接调用还是通过QPainter的析构函数),已经调用了QPainter::end()吗?如果没有,那么渲染可能是不完整的。 - G.M.
widget2->render(&painter); 后面加上 painter.end(); 仍然存在同样的问题。 - pazduha
1个回答

1
我发现了一个解决方案,可能不是最好的。叠加层需要自己的上下文,所以在初始化时:
overlayGLcontext = new QOpenGLContext();
overlayGLcontext->setFormat( format );
overlayGLcontext->setShareContext(widgetGLcontext);
overlayGLcontext->create();

offscreen_surface = new QOffscreenSurface();
offscreen_surface->create();

...

overlayGLcontext->makeCurrent(offscreen_surface);
fbo = new QOpenGLFramebufferObject( w12, h12, GL_TEXTURE_2D);

...

overlayGLcontext->makeCurrent(offscreen_surface);
fbo->bind();

QOpenGLPaintDevice device(QSize(w12,h12));
QPainter painter(&device);

painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

painter.beginNativePainting();

gl_functions->glClearColor(0.0, 0.0, 0.0, 0.0);
gl_functions->glClear(GL_COLOR_BUFFER_BIT);

painter.endNativePainting();

widget1->render(&painter);
widget2->render(&painter,QPoint(widget1->width(),0),QRegion());

painter.end();

fbo->release();

texture = fbo->texture();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接