求助 SetPixelFormat(_hDC, formt, &pfd) 一直是false lasterror code =2000 也就时说 formt 这个值有问题 ;
而 formt 来自于 wglChoosePixelFormatARB 的执行结果
请问 wglChoosePixelFormatARB(_hDC, iAttributes, fAttributes, 1, &formt, &numFormats);
请问 formt的取值范围 能在哪里看到或者找到????
我的显卡是 7950 1030 p530三张显卡
很好,我的显卡是 7950 1030 p530三张显卡
把pixAttribs里面的属性选项WGL_ACCELERATION_ARB, 1,取消掉,wglChoosePixelFormatARB能够找到合适的像素格式。
以下是本问题的补充,”出问题的代码“,详情请看注释
#pragma once
#include "glew21/glew.h"
#include "glew21/wglew.h"
class GLContext
{
protected:
int _format;
//! 窗口句柄
HWND _hWnd;
//! 绘制设备上下文
HDC _hDC;
//! OpenGL上下文
HGLRC _hRC;
public:
GLContext()
{
_format = 0;
_hWnd = 0;
_hDC = 0;
_hRC = 0;
}
~GLContext()
{
shutdown();
}
/**
* 初始化GL
*/
bool setup(HWND hWnd, HDC hDC)
{
_hWnd = hWnd;
_hDC = hDC;
unsigned PixelFormat;
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,PFD_MAIN_PLANE,0,0,0,0
};
if (_format == 0)
{
PixelFormat = ChoosePixelFormat(_hDC, &pfd);
}
else
{
PixelFormat = _format;
}
if (!SetPixelFormat(_hDC, PixelFormat, &pfd))
{
return false;
}
_hRC = wglCreateContext(_hDC);
if (!wglMakeCurrent(_hDC, _hRC))
{
return false;
}
////////////////
glewInit();
int formt = setupMultisample();
if (_hRC != NULL)
{
wglMakeCurrent(_hDC, NULL);
wglDeleteContext(_hRC);
_hRC = NULL;
}
//SetPixelFormat(_hDC, formt, &pfd);// 测试用代码
//以下是正式代码 出错地点就是这个位置
if (!SetPixelFormat(_hDC, formt, &pfd))//TODO 无效的像素格式 &pfd
{
// DWORD ds= GetLastError(); //当启用这行 就会得到 错误代码 2000 无效的像素格式
return false; //本程序因无效像素格式问题由此处退出
}
_hRC = wglCreateContext(_hDC);
if (!wglMakeCurrent(_hDC, _hRC))
{
return false;
}
return true;
///////////////
return true;
}
int setupMultisample()
{
if (wglChoosePixelFormatARB == 0)
{
return 0;
}
int pixelFormat;
int valid;
UINT numFormats;
float fAttributes[] = { 0,0 };
int iAttributes[] =
{
WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
WGL_SUPPORT_OPENGL_ARB,GL_TRUE,
/* WGL_ACCELERATION_ARB,
WGL_FULL_ACCELERATION_ARB,*/ //即使注销掉这两行或者其中之一 依然会导致无效像素格式问题
WGL_COLOR_BITS_ARB,24,
WGL_ALPHA_BITS_ARB,8,
WGL_DEPTH_BITS_ARB,8,
WGL_STENCIL_BITS_ARB,0,
WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
WGL_SAMPLES_ARB,8,
0,0
};
valid = wglChoosePixelFormatARB(_hDC, iAttributes, fAttributes, 1, &pixelFormat, &numFormats);
if (valid && numFormats >= 1)
{
return pixelFormat;
}
iAttributes[19] = 2;
valid = wglChoosePixelFormatARB(_hDC, iAttributes, fAttributes, 1, &pixelFormat, &numFormats);
if (valid && numFormats >= 1)
{
return pixelFormat;
}
return 0;
}
/**
* 销毁EGL
*/
void shutdown()
{
if (_hRC != NULL)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(_hRC);
_hRC = NULL;
}
if (_hDC != NULL)
{
ReleaseDC(_hWnd, _hDC);
_hDC = NULL;
}
}
/**
* 交换缓冲区
*/
void swapBuffer()
{
SwapBuffers(_hDC);
}
};