Solved How to create a QOffscreenSurface correctly?
-
When I try to do some offscreen rendering jobs , I create a QOffscreenSurface in my program but it encounters error.The source code about this part says:
qoffscreensurface.cpp 129(Qt5.3):
//if your applications aborts here, then chances are your creating a QOffscreenSurface before //the screen list is populated. Q_ASSERT(d->screen);
So, what should I do when I want to use a QOffscreenSurface correctly ?
-
@MartinChan-0 said in How to create a QOffscreenSurface correctly?:
So, what should I do when I want to use a QOffscreenSurface correctly ?
Can you show what you're doing now?
-
Here is my code,I want to use a QGLFramebufferObject to bind the offscreen surface to do some opengl painting ,then use QGLFramebufferObject to output it .
qtestoffscreen.h
#ifndef QTESTOFFSCREEN_H #define QTESTOFFSCREEN_H #include <QOffscreenSurface> #include <QtGui/QOpenGLFunctions_3_3_Core> #include <QImage> #include <QGLFramebufferObject> #include <QOpenGLPaintDevice> class QTestOffScreen : public QOffscreenSurface, protected QOpenGLFunctions_3_3_Core { public: explicit QTestOffScreen(QWindow *parent = 0); ~QTestOffScreen(); virtual void render(); virtual void initialize(); void renderNow(); void setAnimating(bool animating); private: QGLFramebufferObject *fbo; bool m_animating; QOpenGLContext *m_context; QOpenGLPaintDevice *m_device; }; #endif // QTESTOFFSCREEN_H
qtestoffscreen.cpp
#include "qtestoffscreen.h" #include <QTime> QTestOffScreen::QTestOffScreen(QWindow *parent): fbo(Q_NULLPTR), m_context(Q_NULLPTR), m_device(Q_NULLPTR) { Q_UNUSED(parent); } QTestOffScreen::~QTestOffScreen() { delete m_context; delete m_device; } void QTestOffScreen::render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glViewport(0,0,100,100); glOrtho(0,100,0,100,0,1); glColor3f(1.0,0.0,1.0); glBegin(GL_LINE_STRIP); glVertex2f(10.0f,10.0f); glVertex2f(90.0f,10.0f); glVertex2f(90.0f,90.0f); glVertex2f(90.0f,10.0f); glEnd(); } void QTestOffScreen::initialize() { if (!fbo) { fbo = new QGLFramebufferObject(1920,1080,0); } fbo->bind(); } void QTestOffScreen::renderNow() { bool needsInitialize = false; if (!m_context) { m_context = new QOpenGLContext(this); m_context->setFormat(requestedFormat()); m_context->create(); } m_context->makeCurrent(this); if (needsInitialize) { initializeOpenGLFunctions(); initialize(); } render(); fbo->toImage().save(QString("buffer") + QString(".png")); } void QTestOffScreen::setAnimating(bool animating) { m_animating = animating; }
main.cpp
#include <QCoreApplication> #include "qtestoffscreen.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTestOffScreen screen; ///It is wrong here. screen.renderNow(); return a.exec(); }
-
When I use the most simplified situation -- creating a QOffscreenSurface in an original main() function,it still fails.
-
@MartinChan-0 I'm not an expert on this topic, but my guess is that you need to initialize OpenGL before you create offscreenscreen surface.
-
@jsulm Thank u anyway and if I have any solutions I will update this topic.
-
I have solved it (althought it still has some problems) like the link in this topic