最近正在学习OpengGL超级宝典第五版,在第十三章有个示例SphereWorld_redux和block_redux,这两个例子中都用到了wglChoosePixelFormatARB,在我的windows7的操作系统上,wglChoosePixelFormatARB总是返回-1。代码如下:
int nPixCount = 0;
// Specify the important attributes we careabout
int pixAttribs[] = {WGL_SUPPORT_OPENGL_ARB, 1, // Must support OGL rendering
WGL_DRAW_TO_WINDOW_ARB, 1, // pf that can run a window
WGL_ACCELERATION_ARB, 1, // mustbe HW accelerated
WGL_COLOR_BITS_ARB, 24, // 8bits of each R, G and B
WGL_DEPTH_BITS_ARB, 16, // 16bits of depth precision for window
WGL_DOUBLE_BUFFER_ARB, GL_TRUE, // Double buffered context
WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,// MSAA on
WGL_SAMPLES_ARB,8,// MSAA 8x
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, // pf should be RGBA type
0}; // NULLtermination
// Ask OpenGL to find the most relevantformat matching our attribs
// Only get one format back.
wglChoosePixelFormatARB(g_hDC,&pixAttribs[0], NULL, 1, &nPixelFormat, (UINT*)&nPixCount);
经过反复调试,发现WGL_ACCELERATION_ARB,属性导致wglChoosePixelFormatARB查找不到正确的像素格式,解决方法如下:把pixAttribs里面的属性选项WGL_ACCELERATION_ARB, 1,取消掉,wglChoosePixelFormatARB能够找到合适的像素格式。