Advertisement

Setting Pixel Format - opengl

Started by July 01, 2024 08:39 PM
2 comments, last by scruthut 5 months ago

I get the error failed to set the pixel format from the following code. The code is very simple, can anyone elaborate.

int _pfAttributes[] = {
	  WGL_DRAW_TO_WINDOW_ARB,  GL_TRUE,
	  WGL_SUPPORT_OPENGL_ARB,  GL_TRUE,
	  WGL_DOUBLE_BUFFER_ARB,   GL_TRUE,
	  WGL_ACCELERATION_ARB,    WGL_FULL_ACCELERATION_ARB,
	  WGL_PIXEL_TYPE_ARB,      WGL_TYPE_RGBA_ARB,
	  WGL_COLOR_BITS_ARB,      32,
	  WGL_DEPTH_BITS_ARB,      24,
	  WGL_STENCIL_BITS_ARB,    8,
	  0
};

int _dpf;
UINT _dFormats;

wglChoosePixelFormatARB(g_hDC, _pfAttributes, 0, 1, &_dpf, &_dFormats);
if (!_dFormats)
	fatal_error("Failed to choose the OpenGL 3.3 pixel format.");

PIXELFORMATDESCRIPTOR _pfd = { NULL };

DescribePixelFormat(g_hDC, _dpf, sizeof(_pfd), &_pfd);
if (!SetPixelFormat(g_hDC, _dpf, &_pfd))
	fatal_error("Failed to set the OpenGL 3.3 pixel format.");

You should check the return value of wglChoosePixelFormatARB(). What is the value of _dpf? You should initialize variables to a known value, you might be testing garbage values of _dFormats if the function fails. Furthermore, you should really avoid using leading underscores on variable names, those are reserved by the compiler.

What does GetLastError() return after calling SetPixelFormat()?

These are all basic debugging skills, which you should try first before asking for help.

Advertisement

I forgot to set the handle to HDC. Once resolved the code works fine. Thanks for the help and the input.

This topic is closed to new replies.

Advertisement