Jump to content
  • Advertisement
Sign in to follow this  
Chris_F

Help with using WGL

This topic is 1421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone help me out? I'm looking for a bare minimum example (preferably just one file) for how to set up a core profile context with sRGB framebuffer using WGL.

Share this post


Link to post
Share on other sites
Advertisement

This *should* create a GL3.3 core window. Note I've removed all error checking etc. just to show what needs to be done. Always make sure your hWnds, hDCs and hGLRC are valid. It assumes you're using GLEW.

 

Edit to add: It also assumes you know Win32...

 

Look for wglChoosePixelFormatARB (and the structure above it) near the bottom of the code.

HWND CreateGLWindow( void )
{
	HWND hWnd;
	HDC hDC;
	HGLRC hGLRC;
	WNDCLASSEX wc;
	PIXELFORMATDESCRIPTOR pfd;

	// create dummy GL window and get function pointers from GLEW

	ZeroMemory( &wc, sizeof( WNDCLASSEX ) );

	wc.cbSize = sizeof( WNDCLASSEX );
	wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC) DefWindowProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = GetModuleHandle( NULL );
	wc.hIcon = 0;
	wc.hCursor = NULL;
	wc.hbrBackground = 0;
	wc.lpszMenuName = 0;
	wc.lpszClassName = L"dummy window class";
	wc.hIconSm = 0;

	RegisterClassEx( &wc );
	
	hWnd = CreateWindowEx( WS_EX_APPWINDOW, L"dummy window class", L"", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 100, 100, 800, 600, NULL, NULL, GetModuleHandle( NULL ), NULL );

	hDC = GetDC( hWnd );

	ZeroMemory( &pfd, sizeof( PIXELFORMATDESCRIPTOR ) );

	pfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;
	pfd.cDepthBits = 24;
	pfd.cStencilBits = 8;

	SetPixelFormat( hDC, ChoosePixelFormat( hDC, &pfd ), &pfd );
	hGLRC = wglCreateContext( hDC );

	wglMakeCurrent( hDC, hGLRC );

	glewInit();

	wglMakeCurrent( NULL, NULL );
						
	wglDeleteContext( hGLRC );

	DestroyWindow( hWnd );

	UnregisterClass( L"dummy window class", GetModuleHandle( NULL ) );



	// Create GL window

	ZeroMemory( &wc, sizeof( WNDCLASSEX ) );

	wc.cbSize = sizeof( WNDCLASSEX );
	wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
	wc.lpfnWndProc = (WNDPROC) WindowProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = sizeof( LONG_PTR );
	wc.hInstance = GetModuleHandle( NULL );
	wc.hIcon = 0;
	wc.hCursor = NULL;
	wc.hbrBackground = 0;
	wc.lpszMenuName = 0;
	wc.lpszClassName = L"Window Class";
	wc.hIconSm = 0;

	RegisterClassEx( &wc );

	hWnd = CreateWindowEx( NULL, L"Window Class", L"Window Caption", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 100, 100, 800, 600, NULL, NULL, GetModuleHandle( NULL ), NULL );

	hDC = GetDC( hWnd );

	const unsigned int maxFormats = 20; // max number of returned pixel formats
	int pixelFormats[maxFormats]; // array of returned pixel formats, best at index 0...
	unsigned int numFormatsAvailable; // number of matching formats

	const int pfdAttributes[] =
	{
		WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
		WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
		WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
		WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, GL_FALSE,
		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
	};
				
	wglChoosePixelFormatARB( hDC, pfdAttributes, NULL, maxFormats, pixelFormats, &numFormatsAvailable );

	SetPixelFormat( hDC, pixelFormats[0], &pfd );
				
	const int glContextAttributes[] =
	{
		WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
		WGL_CONTEXT_MINOR_VERSION_ARB, 3,
		//WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
		WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
		0
	};

	hGLRC = wglCreateContextAttribsARB( hDC, NULL, glContextAttributes );

	wglMakeCurrent( hDC, hGLRC );

	ShowWindow( hWnd, SW_SHOW );
	
	return hWnd;
}


Edited by mark ds

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!