Error code 1282 when using opengl functions in QOpenGLWindow
-
I tried to rewrite the example given in the qt-book (for QT4) using modern opengl instead of legacy immediate mode version given in the book. The problem is how to do picking when clicking with the mouse on the screen to determine the triangle being clicked on. I legacy opengl it was easy, most of the work is done by opengl. In modern opengl you have to create a frame buffer to render the scene and basically instead of color you write info about objects and primitives (basically the id) being rendered instead of color. Then you have to use glReadPixel to read the pixel where the clicking of the mouse occurred and retrieve the object and primitive id.
Well I used a render buffer and did that. It worked with freeglut, but when I tried with QOpenglWindow I keep getting error code 1282 for using the simple opengl functions such as glClear and glViewport. When I comment out the picking part concerning allocating the render buffer and the switching between it and defaul framebuffer, everything works fine.
Here are the relevant part of the code.void Window::initializeGL() { makeCurrent(); prepareShaderPrograms(); prepareVertexBuffers(); view = QMatrix4x4(); glClearColor(1.0, 1.0, 1.0, 1.0); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); rotationX = -21.0; rotationY = -57.0; rotationZ = 0.0; for (int i=0; i<4; i++) for (int j=0; j<4; j++) if (i==j) faceColors[i][j] = 1.0; else if (j==3) faceColors[i][j] = 1.0; else if (i==3 && j!=2) faceColors[i][j] = 1.0; else faceColors[i][j] = 0.0; QOpenGLExtraFunctions *glFuncs = context()->currentContext()->extraFunctions(); glFuncs->glGenRenderbuffers(1, &rbo); glFuncs->glBindRenderbuffer(GL_RENDERBUFFER, rbo); glFuncs->glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 450); glFuncs->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); if(glFuncs->glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) qCritical() << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!"; glFuncs->glBindFramebuffer(GL_FRAMEBUFFER, 0); qDebug() << "Done Initializing GL"; }
void Window::resizeGL(int w, int h)
{
makeCurrent();
glAssert( glViewport(0, 0, width(), height()) );
GLfloat x = GLfloat(w) / h;
projection = QMatrix4x4();
projection.frustum(-x, x, -1.0, 1.0, 4.0, 15.0);
}void Window::paintGL() { glAssert( glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ); model = QMatrix4x4(); model.translate(0.0, 0.0, -10.0); model.rotate(rotationX, 1.0, 0.0, 0.0); model.rotate(rotationY, 0.0, 1.0, 0.0); model.rotate(rotationZ, 0.0, 0.0, 1.0); QMatrix4x4 MVP = projection*view*model; mVAO.bind(); mShaderProgram.bind(); mShaderProgram.setUniformValueArray("MVP", &MVP, 1); mShaderProgram.setUniformValue("faceColor", faceColors[3][0], faceColors[3][1], faceColors[3][2], faceColors[3][3]); glAssert( glDrawArrays(GL_TRIANGLES, 9, 3) ); mShaderProgram.setUniformValue("faceColor", faceColors[2][0], faceColors[2][1], faceColors[2][2], faceColors[2][3]); glAssert( glDrawArrays(GL_TRIANGLES, 6, 3) ); mShaderProgram.setUniformValue("faceColor", faceColors[1][0], faceColors[1][1], faceColors[1][2], faceColors[1][3]); glAssert( glDrawArrays(GL_TRIANGLES, 3, 3) ); mShaderProgram.setUniformValue("faceColor", faceColors[0][0], faceColors[0][1], faceColors[0][2], faceColors[0][3]); glAssert( glDrawArrays(GL_TRIANGLES, 0, 3) ); mShaderProgram.release(); mVAO.release(); mShaderProgram.release(); } The error occurs in the resize function, the initialization function is fine.
-
Well, I tried not to allocate an auxiliary framebuffer and do the rendering with object and primitive ids as colors in default framebuffer and do glReadPixel without painting and take the proper action then call update() of QOpenGLWindow to do painting with proper color. Well, it worked. But still the question remains is there a problem when allocating a secondary framebuffer in QT?
-
Well, I thought of another solution that can uses a secondary framebuffer which might be a must in an advanced scenario instead of calling the paint function every time the picking occurs and its required action affects the scene. Instead of using the QOpenGLWindow one can use QWindow with glew, but this is a lower level for working with opengl and I am not sure I know how.
-