I have a QOpenglWidget and a customized QOffscreenSurface,then I set Qt::AA_ShareOpenGLContexts attribute to QCoreApplication,here is my code:
void main(){
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QApplication a(argc, argv);
MainWindow w;
w.show();
}
mainwindow has a subclass of QOpenglWidget TextureWidget
and a subclass of QOffscreenSurface OffscreenRender
, I want to share buffer objects and texture objects between these two class.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
center = new QSplitter();
center->resize(800,400);
left = new TextureWidget;
offscreen = new OffscreenRender(this);
setCentralWidget(TextureWidget);
}
TextureWidget
as below:
class TextureWidget:public QOpenGLWidget,protected QOpenGLFunctions_4_3_Core
{
Q_OBJECT
public:
TextureWidget(QWidget* parent = 0);
~TextureWidget();
protected:
void initializeGL() override{
QOpenGLContext *currentContext = context();
QOpenGLContext *share = currentContext->shareContext();
initializeOpenGLFunctions();
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
...
}
void paintGL() override{
...
}
void resizeGL(int width, int height) override{...}
private:
GLuint vao,vbo;
OffscreenRender
code :
class OffscreenRender:public QOffscreenSurface,protected QOpenGLFunctions_4_3_Core
{
OffscreenRender(QObject *parent = 0){
setParent(parent);
QSurfaceFormat surfaceFormat;
surfaceFormat.setMajorVersion(4);
surfaceFormat.setMinorVersion(3);
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
openGLContext = new QOpenGLContext();
openGLContext->setFormat(surfaceFormat);
//set share context with current context explicitly
QOpenGLContext *share = QOpenGLContext::globalShareContext();
openGLContext->setShareContext(share);
if(openGLContext->create()){
share = openGLContext->shareContext();
setFormat(surfaceFormat);
create();
if(!isValid()) qDebug("Unable to create the Offscreen surface");
openGLContext->makeCurrent(this);
glGenVertexArrays(1, &vaoGen);
glBindVertexArray(vaoGen);
glGenBuffers(1, &vboGen);
glBindBuffer(GL_ARRAY_BUFFER,vboGen);
...
}
private:
GLuint vaoOff,vboOff;
}
I find that share
(QOpenGLContext *) in TextureWidget
and OffscreenRender
are different even after setting Qt::AA_ShareOpenGLContexts attribute,but two vbo
are sequential and can be shared.