【问题标题】:Render QWidget to FBO将 QWidget 渲染到 FBO
【发布时间】:2017-12-12 10:40:48
【问题描述】:

我正在 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 用作设备,则小部件呈现良好。

我的小部件非常复杂,有 4k 行代码用于连接所有旋转框、标签……

【问题讨论】:

  • 您确定QPainter::end() 已被调用(直接或通过QPainter dtor)您使用渲染结果之前?如果不是,则渲染可能不完整。
  • painter.end();在 widget2->render(&painter);同样的问题

标签: qt opengl widget fbo


【解决方案1】:

我找到了解决方案,也许不是最好的。

Overlay 需要自己的上下文,所以在 init 中:

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多