我成功读取了OpenGL渲染的DC像素。 这是我的代码的一部分。
void COpenGLWnd::OffscreenRender
(/* IN parameters */ int transitionID, int counts, int directionID,
/* OUT parameters */ BYTE* pPixelArray)
{
HDC hDC = ::GetDC(m_hWnd);
SetDCPixelFormat(hDC);
HGLRC hRC = wglCreateContext(hDC);
VERIFY(wglMakeCurrent(hDC, hRC));
for(int i = 0; i < counts; i++)
{
GLFadeinRender((GLfloat)(i+1) / (GLfloat)counts);
BYTE* data = (BYTE*) malloc(m_BmpSize);
glReadBuffer(GL_BACK);
glReadPixels(0,0,m_BmpWidth,m_BmpHeight,GL_BGR_EXT,GL_UNSIGNED_BYTE, data);
memcpy(&pPixelArray[i * m_BmpSize], data, m_BmpSize);
free(data);
}
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
::ReleaseDC(m_hWnd, hDC);
}
它工作得很好,但我想在没有hWnd的情况下使用glReadPixels。我听说PBO或FBO可能会对这种情况有所帮助,但我无法做到。这是我尝试使用PBO呈现的代码。
void COpenGLWnd::OffscreenRender
(/* IN parameters */ int transitionID, int counts, int directionID,
/* OUT parameters */ BYTE* pPixelArray)
{
GLuint pbo;
glGenBuffersARB(1,&pbo);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pbo);
glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, m_BmpSize, NULL, GL_STREAM_READ_ARB);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
for(int i = 0; i < counts; i++)
{
glReadBuffer(GL_BACK);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pbo);
GLFadeinRender((GLfloat)(i+1) / (GLfloat)counts);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pbo);
//glReadBuffer(GL_BACK);
glReadPixels(0,0,m_BmpWidth,m_BmpHeight,GL_BGR_EXT,GL_UNSIGNED_BYTE, 0);
BYTE* data = (BYTE*) glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB);
memcpy(&pPixelArray[i * m_BmpSize], data, m_BmpSize);
if(data)
{
glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
}
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB,0);
}
glDeleteBuffers(1,&pbo);
}
但glReadPixels
没有发生任何事。没有像素读取。
那么我可以在不使用hWnd
的情况下屏幕渲染和读取像素吗?