9
\$\begingroup\$

Is this the proper method for creating an OpenGL 3.3+ forward-compatible context?

// Create window
WNDCLASSEX wc;
ZeroMemory( &wc, sizeof( wc ) );
wc.cbSize = sizeof( wc );
wc.lpfnWndProc = _eventHandler;
wc.hInstance = GetModuleHandle( NULL );
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor  = LoadCursor( NULL, IDC_ARROW );
wc.lpszClassName = "OpenGL 3.3";
if ( !RegisterClassEx( &wc ) ) Error( "Failed to register window class!" );
if ( !( _window = CreateWindowEx( WS_EX_CLIENTEDGE, "OpenGL 3.3", "OpenGL 3.3", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, GetModuleHandle( NULL ), NULL ) ) )
    Error( "Failed to create the window!" );
if ( !( _hdc = GetDC( _window ) ) ) Error( "Failed to retrieve device context!" );

// Choose pixel format
int pixelFormat;
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1; 
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;  
pfd.iPixelType = PFD_TYPE_RGBA; 
pfd.cColorBits = 32;
pfd.cDepthBits = 32; 
pfd.iLayerType = PFD_MAIN_PLANE;
if ( !( pixelFormat = ChoosePixelFormat( _hdc, &pfd ) ) ) Error( "Failed to find suitable pixel format!" );
if ( !SetPixelFormat( _hdc, pixelFormat, &pfd ) ) Error( "Failed to set pixel format!" );

// Create temporary context and make sure we have support
HGLRC tempContext = wglCreateContext( _hdc );
if ( !tempContext ) Error( "Failed to create temporary context!" );
if ( !wglMakeCurrent( _hdc, tempContext ) ) Error( "Failed to activate temporary context!" );

int major, minor; glGetIntegerv( GL_MAJOR_VERSION, &major ); glGetIntegerv( GL_MINOR_VERSION, &minor );
if ( major < 3 || minor < 2 ) Error( "OpenGL 3.2 is not supported!" );

// Create forward compatible context
int attribs[] =
{
    WGL_CONTEXT_MAJOR_VERSION_ARB, major,
    WGL_CONTEXT_MINOR_VERSION_ARB, minor, 
    WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
    WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
    0
};
PFNWGLCREATEBUFFERREGIONARBPROC wglCreateContextAttribsARB = (PFNWGLCREATEBUFFERREGIONARBPROC)wglGetProcAddress( "wglCreateContextAttribsARB" );
if ( !wglCreateContextAttribsARB ) Error( "Failed to find pointer to wglCreateContextAttribsARB function!" );
if ( !( _context = wglCreateContextAttribsARB( _hdc, 0, (UINT)attribs ) ) ) Error( "Failed to create forward compatible context!" );

// Remove temporary context and activate forward compatible context
wglMakeCurrent( NULL, NULL );
wglDeleteContext( tempContext );
if ( !wglMakeCurrent( _hdc, (HGLRC)_context ) ) Error( "Failed to activate forward compatible context!" );

// Show window
ShowWindow( _window, SW_SHOW );
UpdateWindow( _window );

It seems to work (gives GL_INVALID_OPERATION when using glBegin), but I just want to make sure this is the right way to go about it.

\$\endgroup\$
4
\$\begingroup\$

This looks right to me. I am a bit tired right now and usually use GLFW.

You might be interested in this page on the OpenGL wiki describing setting up the context after 3.1 which is essentially the same work.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

You're creating a core profile (WGL_CONTEXT_CORE_PROFILE_BIT_ARB) which means that all deprecated functionality will be unavailable. glBegin/glEnd was deprecated in OpenGL 3. The modern way to go is to use vertex buffer objects (VBOs) instead.

You can create a compability context to get access to the old fixed function API (including glBegin/glEnd).

For more information, see http://www.opengl.org/wiki/Core_And_Compatibility_in_Contexts

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Yes, I tried calling glBegin to test the deprecation mechanism. \$\endgroup\$ – Overv Mar 24 '11 at 14:17
  • \$\begingroup\$ In 3.1 even core profiles contain and allow the deprecated stuff. See the wiki link above. For 3.3, yes, the core flag should do. \$\endgroup\$ – Anton Duzenko Aug 25 '19 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.