Following code works fine
const char *title = "glReadOutput";
Mat out1, out2;
out1.create(screenHeight,screenWidth, CV_8UC3);
out2.create(screenHeight,screenWidth, CV_8UC3);
RenderObject();
glReadPixels(0, 0, screenWidth, screenHeight, GL_BGR, GL_UNSIGNED_BYTE, (uchar*)out1.data);
//flip(out1, out1, 0);
imshow(title, out1);
waitKey(5000);
RenderObject();
glReadPixels(0, 0, screenWidth, screenHeight, GL_BGR, GL_开发者_Go百科UNSIGNED_BYTE, (uchar*)out2.data);
//flip(image, out2, 0);
imshow(title, out2);
waitKey(5000);
However, when I transfer glReadPixels to function it works fine on first call but fails/read nothing on second call :(
RenderObject();
displayImage(out1);
RenderObject();
displayImage(out2);
.
.
void displayImage(Mat& image) {
//glReadBuffer(GL_FRONT);
//glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glReadPixels(0, 0, screenWidth, screenHeight, GL_BGR, GL_UNSIGNED_BYTE, (uchar*)image.data);
//flip(image, image, 0);
//glPopClientAttrib();
const char *title = "glReadPixels";
imshow(title, image);
waitKey(5000);
destroyWindow(title);
//image.release();
}
Few points: The thread is also same. There is only single buffer. Same behavior with framebuffer object (FBO) as well as glut window. I also tried glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS), you could see code is commented. I also called glClear(GL_COLOR_BUFFER_BIT); on RenderObject().
Can you please help me, where might be possible mistake?
EDIT: Christian .. thanks! Your are right. But Why that is happening. There is no link on image.data with buffer right? after finishing the read? Or is it ... the display window taking over ownership of gl buffer and something wrong going on while we destroying that?
I guess your first example works fine, because you both times display out1
in imshow
. Otherwise, in the function you destroy the window and then use it again in the next function call, problem?
EDIT: The display window doesn't take ownership of the image (and surely not of your GL frame buffer, why and how should it), but destroying a CV window (with destroyWindow
) and then using this window again (in the imshow
of the next function call) is surely not a good idea. I think imshow
doesn't create a new window each time it is called, it uses a window you created with namedWindow
and in the second function call this window doesn't exist anymore, as you destroyed it with destroyWindow
.
When OpenCV was built using 'WITH_QT_OPENGL', above problem occures.
Solutions:
- Build OpenCV without 'WITH_QT_OPENGL' option. It will completely removes all errors.
- 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);' )
精彩评论