问题
The problem: I am using glReadPixles and only occasionally getting black (values of all 0) in my image buffer when reading from GL_BACK.
Tool stack: C++17, GLFW, and OpenGL 4.6
What I have tried I have tried setting my glDrawBuffer and glReadBuffer to read directly from GL_FRONT and GL_BACK. GL_FRONT does work fine as I believe the frame is complete (I have a debug function which checks the framebuffer status complete).
However, I need to read from GL_BACK because I am rendering several passes at once (i.e. I am moving the camera to create a stereo view, and I need to capture the frames in the front buffer so the user does not see the camera move).
Is there any way to force OpenGL to finish rendering the frame in the backbuffer so that I can consistently get glReadPixels to not return black images? How can I otherwise perform blitting properly?
Here is the sample code which I am trying to use to capture frames, and write the captured frame out as an image to debug.
//glFinish(); // Tried to force a glfinish and flush, but it did not work
//glFlush();
glDrawBuffer(GL_BACK);
//glFinish();
//glFlush();
glReadBuffer(GL_BACK);
//glFinish();
//glFlush();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
unsigned char* frameBufferBytesBuffer = new unsigned
char[viewport[2] * viewport[3] * 3];
framebufferStatus();
GLDebugCall(glReadPixels(viewport[0], // x
viewport[1], // y
viewport[2], // width
viewport[3], // height
GL_RGB, // Format
GL_UNSIGNED_BYTE, // data type
frameBufferBytesBuffer);) // Where to store the pixels
// Write the image out as a jpeg to debug.
// This appears black about 10% of the time.
stbi_write_jpg(path.c_str(), viewport[2], viewport[3], 3,frameBufferBytesBuffer, 100);
来源:https://stackoverflow.com/questions/64944670/glreadpixels-rendering-black