OpenCV 2.2 window causes problem on OpenGL

2019-08-04 03:54发布

问题:

Here is very simple code .. Only thing is I repeated same code manytimes for detail debug. Detail info: OpenGL version 3.3.0, Window 7 OS and VS2008, OpenCV 2.2.0.

RenderObject();
//glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
Mat image;
image.create(screenHeight,screenWidth, CV_8UC3);
glReadPixels(0, 0, screenWidth, screenHeight, GL_BGR, GL_UNSIGNED_BYTE, (uchar*)image.data);
int error_code1 = glGetError();  // Error Code: 0, NO Error, Also output is good/as expected!
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
int error_code8 = glGetError();  // Error Code: 0, NO Error
//flip(image, image, 0);
//glPopClientAttrib();
const char *title = "glReadPixels Output";
cv::namedWindow(title);  
int error_code2 = glGetError();  // Error Code: 1282, GL_INVALID_OPERATION
imshow(title, image);
int error_code3 = glGetError();  // Error Code: 1282, GL_INVALID_OPERATION
waitKey(5000);
int error_code4 = glGetError();  // Error Code: 0, NO Error
destroyWindow(title);
int error_code5 = glGetError();  // Error Code: 1282, GL_INVALID_OPERATION
image.release();

int error_code6 = glGetError();  // Error Code: 1282, GL_INVALID_OPERATION

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId);
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);

int error_code7 = glGetError();  // Error Code: 1282, GL_INVALID_OPERATION

Something going on here under-the-hood. What is that? ... well at least error_code7 should be 0 (i.e. no error) even if OpenCV window is taking over the default buffer. I tested code with framebuffer object as well as default glut hidden window. Also let me tell you that I must use framebuffer object for my purpose. So, cannt use same default window to draw and show.

回答1:

code4 is 0 because no new OpenGL operation occured after the previous glGetError(). You should fix the problem were it starts, which is at the first occurence of a non-zero return value from glGetError(). Hence, first thing on the todo list is add a glGetError() below glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

That line should not really cause any troubles, but still, check it. If not, then probably the namedWindow call is messing things up. Which version is your OpenGL context?



回答2:

When OpenCV was built using 'WITH_QT_OPENGL', above problem occures.

Solutions:

  1. Build OpenCV without 'WITH_QT_OPENGL' option. It will completely removes all errors.
  2. Or to work around- Re-attach the draw buffer (i.e. glDrawBuffer only with default buffer object or with framebuffer object (FBO) both FBO and texture/render buffer, you could valid this using 'glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);' )