Opengl 使用从wglChoosePixelFormatARB获取的像素格式时,SetPixelFormat返回false
当我使用SetPixelFormat为hDC设置像素格式时,它返回false。我使用了GetLastError(),得到了一个错误代码2000(表示像素格式无效) 代码如下:Opengl 使用从wglChoosePixelFormatARB获取的像素格式时,SetPixelFormat返回false,opengl,wgl,Opengl,Wgl,当我使用SetPixelFormat为hDC设置像素格式时,它返回false。我使用了GetLastError(),得到了一个错误代码2000(表示像素格式无效) 代码如下: // Set pixel format attributes int pixelAttributes[] = { WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, WGL_DOUBLE_BUFFER_ARB, GL_T
// Set pixel format attributes
int pixelAttributes[] = {
WGL_DRAW_TO_WINDOW_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, bits,
WGL_DEPTH_BITS_ARB, 24,
WGL_STENCIL_BITS_ARB, 8,
WGL_SAMPLE_BUFFERS_ARB, 1,
WGL_SAMPLES_ARB, 4,
0
};
int pixelFormat;
UINT numFormat;
// using wgl ext get valid pixel format number.
if (!wglChoosePixelFormatARB(hDC, pixelAttributes, NULL, 1, &pixelFormat, &numFormat)) {
KillGLWindow();
MessageBox(NULL, L"Get pixel format failed", L"Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
if (!SetPixelFormat(hDC, pixelFormat, &pfd)) // try to set pixel format
{
auto x = GetLastError();
KillGLWindow(); // close
MessageBox(NULL, L"Unable to set pixel format.", L"Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
这些代码是在创建并激活一个虚假的openGL上下文之后编写的。SetPixelFormat在PIXELFORMATDESCRIPTOR结构中想要看到的内容有点挑剔。要满足此要求,最好的方法是使用
DescribePixelFormat
初始化pfd
:
INT iPF;
UINT num_formats_choosen;
if( !ChoosePixelFormatARB(
hDC,
pPFAttribs,
NULL,
1,
&iPF,
&num_formats_choosen)
|| !num_formats_choosen ) {
goto fail_choose_pf;
}
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
/* now this is a kludge; we need to pass something in the
* PIXELFORMATDESCRIPTOR to SetPixelFormat; it will be ignored,
* mostly. OTOH we want to send something sane, we're nice
* people after all - it doesn't hurt if this fails. */
DescribePixelFormat(hDC, iPF, sizeof(pfd), &pfd);
if( !SetPixelFormat(hDC, iPF, &pfd) ) {
goto fail_set_pf;
}
SetPixelFormat对于它希望在PIXELFORMATDESCRIPTOR结构中看到的内容有点挑剔。要满足此要求,最好的方法是使用
DescribePixelFormat
初始化pfd
:
INT iPF;
UINT num_formats_choosen;
if( !ChoosePixelFormatARB(
hDC,
pPFAttribs,
NULL,
1,
&iPF,
&num_formats_choosen)
|| !num_formats_choosen ) {
goto fail_choose_pf;
}
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
/* now this is a kludge; we need to pass something in the
* PIXELFORMATDESCRIPTOR to SetPixelFormat; it will be ignored,
* mostly. OTOH we want to send something sane, we're nice
* people after all - it doesn't hurt if this fails. */
DescribePixelFormat(hDC, iPF, sizeof(pfd), &pfd);
if( !SetPixelFormat(hDC, iPF, &pfd) ) {
goto fail_set_pf;
}
SetPixelFormat对于它希望在PIXELFORMATDESCRIPTOR结构中看到的内容有点挑剔。要满足此要求,最好的方法是使用
DescribePixelFormat
初始化pfd
:
INT iPF;
UINT num_formats_choosen;
if( !ChoosePixelFormatARB(
hDC,
pPFAttribs,
NULL,
1,
&iPF,
&num_formats_choosen)
|| !num_formats_choosen ) {
goto fail_choose_pf;
}
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
/* now this is a kludge; we need to pass something in the
* PIXELFORMATDESCRIPTOR to SetPixelFormat; it will be ignored,
* mostly. OTOH we want to send something sane, we're nice
* people after all - it doesn't hurt if this fails. */
DescribePixelFormat(hDC, iPF, sizeof(pfd), &pfd);
if( !SetPixelFormat(hDC, iPF, &pfd) ) {
goto fail_set_pf;
}
SetPixelFormat对于它希望在PIXELFORMATDESCRIPTOR结构中看到的内容有点挑剔。要满足此要求,最好的方法是使用
DescribePixelFormat
初始化pfd
:
INT iPF;
UINT num_formats_choosen;
if( !ChoosePixelFormatARB(
hDC,
pPFAttribs,
NULL,
1,
&iPF,
&num_formats_choosen)
|| !num_formats_choosen ) {
goto fail_choose_pf;
}
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
/* now this is a kludge; we need to pass something in the
* PIXELFORMATDESCRIPTOR to SetPixelFormat; it will be ignored,
* mostly. OTOH we want to send something sane, we're nice
* people after all - it doesn't hurt if this fails. */
DescribePixelFormat(hDC, iPF, sizeof(pfd), &pfd);
if( !SetPixelFormat(hDC, iPF, &pfd) ) {
goto fail_set_pf;
}
你确定你的GPU支持这种格式吗?可能只是驱动程序被它绊倒了。在初始化PIXELFORMATDESCRIPTOR结构(pfd)的地方张贴代码。您确定您的GPU支持该格式吗?可能只是驱动程序被它绊倒了。在初始化PIXELFORMATDESCRIPTOR结构(pfd)的地方张贴代码。您确定您的GPU支持该格式吗?可能只是驱动程序被它绊倒了。在初始化PIXELFORMATDESCRIPTOR结构(pfd)的地方张贴代码。您确定您的GPU支持该格式吗?可能只是驱动程序被它绊倒了。在初始化PIXELFORMATDESCRIPTOR结构(pfd)的地方发布代码,谢谢。现在,我已经解决了这个问题。我忽略了,我只是为同一个hDC设置了两次像素格式。我已经纠正了这个问题,当我创建错误的openGL上下文时,我创建了一个临时的hDC。现在,我已经解决了这个问题。我忽略了,我只是为同一个hDC设置了两次像素格式。我已经纠正了这个问题,当我创建错误的openGL上下文时,我创建了一个临时的hDC。现在,我已经解决了这个问题。我忽略了,我只是为同一个hDC设置了两次像素格式。我已经纠正了这个问题,当我创建错误的openGL上下文时,我创建了一个临时的hDC。现在,我已经解决了这个问题。我忽略了,我只是为同一个hDC设置了两次像素格式。我已经纠正了这个问题,当我创建错误的openGL上下文时,我创建了一个临时的hDC。