我试图用 WLG_ARB pbufer 进行屏幕外渲染,但我遇到了 wglCreatePbufferARB() 的问题,它总是返回 NULL。这是代码的一部分。
bool COpenGLWnd::OffscreenRender
(/* IN parameters */ int transitionID, int counts, int directionID,
/* OUT parameters */ BYTE* pPixelArray)
{
HDC tmpDC = ::GetDC(NULL);
SetDCPixelFormat(tmpDC);
HGLRC tmpRC = wglCreateContext(tmpDC);
VERIFY(wglMakeCurrent(tmpDC, tmpRC));
HDC hDC = ::GetDC(NULL);
const int attribList[] =
{
WGL_DRAW_TO_PBUFFER_ARB, GL_TRUE,
WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
WGL_COLOR_BITS_ARB, 32,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
0, //End
};
int pixelFormat;
UINT numFormats = 1;
if(!wglGetCurrentContext())
{
return false;
}
wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC) wglGetProcAddress("wglGetExtensionsStringARB");
if(!wglGetExtensionsStringARB) return false;
const GLubyte* extensions = (const GLubyte*) wglGetExtensionsStringARB(wglGetCurrentDC());
if(!extensions)
{
return false;
}
wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC) wglGetProcAddress("wglChoosePixelFormatARB");
wglCreatePbufferARB = (PFNWGLCREATEPBUFFERARBPROC) wglGetProcAddress("wglCreatePbufferARB");
wglGetPbufferDCARB = (PFNWGLGETPBUFFERDCARBPROC) wglGetProcAddress("wglGetPbufferDCARB");
VERIFY(wglMakeCurrent(tmpDC, NULL));
wglDeleteContext(tmpRC);
::ReleaseDC(NULL, tmpDC);
if(!wglChoosePixelFormatARB) return false;
if(!wglCreatePbufferARB) return false;
if(!wglGetPbufferDCARB) return false;
wglChoosePixelFormatARB(hDC, attribList, NULL, 1, &pixelFormat, &numFormats);
const int attribTmp[] = {WGL_PBUFFER_LARGEST_ARB, 0, 0};
m_hbuffer = wglCreatePbufferARB(hDC, pixelFormat, m_BmpWidth, m_BmpHeight, attribTmp);
HDC hpbufferDC = wglGetPbufferDCARB(hbuffer);
if(!hpbufferDC) return false;
HGLRC hpbufferRC = wglCreateContext(hpbufferDC);
VERIFY(wglMakeCurrent(hpbufferDC, hpbufferRC));
...
}
正如我所说,wglCreatePbufferARB 返回 NULL,所以我不能使用 wglGetBufferDCARB。其他 WGL_ARB 扩展函数运行良好,例如 wglGetExtensionsStringARB 和 wglChoosePixelformetARB。并且扩展得到了非常正常的字符串,它以“WGL_ARB_extensions_string ...”开头 wglCreatePbufferARB 使用的每个参数都不是NULL,包括hDC。那么为什么 wglCreatePbufferARB 仍然返回 NULL 并且不能按我的意图工作?
++ps 我调用了 GetLastError,它返回了 ERROR_INVALID_DATA。这是因为attribList,但它是怎么回事?感谢您建议致电 GetLastError。
++ps 我解决了这个问题。现在这工作得很好。谢谢!