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.