Sen Wang
Sen Wang

Reputation: 13

OPENGL glReadPixels how to get larger window content?

I want to get the window content from OpenGL to OpenCV. The code used below:

    unsigned char* buffer = new unsigned char[ Win_width * Win_height * 4];
    glReadPixels(0, 0, Win_width, Win_height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
    cv::Mat image_flip(Win_height, Win_width, CV_8UC4, buffer);

When the window size is small. everything is fine. But when Win_width and Win_height large than 1080p, the image will be resize to 1080p and other part will pad with grey.

Upvotes: 1

Views: 442

Answers (1)

genpfault
genpfault

Reputation: 52084

Render to and read from a FBO so you don't run afoul of the pixel ownership test:

Because the Default Framebuffer is owned by a resource external to OpenGL, it is possible that particular pixels of the default framebuffer are not owned by OpenGL. And therefore, OpenGL cannot write to those pixels. Fragments aimed at such pixels are therefore discarded at this stage of the pipeline.

Generally speaking, if the window you are rendering to is partially obscured by another window, the pixels covered by the other window are no longer owned by OpenGL and thus fail the ownership test. Any fragments that cover those pixels will be discarded. This also includes framebuffer clearing operations.

Note that this test only affects rendering to the default framebuffer. When rendering to a Framebuffer Object, all fragments pass this test.

Upvotes: 3

Related Questions