"Can't Create A GL Rendering Context" Error

Started by
5 comments, last by Gixugif 14 years, 2 months ago
So, I just started trying to learn OpenGL, and the tutorial I'm following has me creating a simple window with a solid black background. When I try to run it, I get the error "Can't Create A GL Rendering Context." I did a search and it would seem this has to do with your video driver. Because it's such a simple thing to do though, I'd imagine it has to do with my coding, or maybe because I have integrated graphics? Since the code is basically just copied from the tutorial, it doesn't seem like that'd be the issue. If it helps, i'm using NeHe's tutorial.
Advertisement
What integrated graphics is that exactly?
Make sure to update your drivers to the latest version. Windows Update does not always provide the latest version.
Intel 82945G Express

And yeah, I made sure it was fully updated.
Read the information at the following page, and see if it helps you: http://www.intel.com/support/graphics/intel945g/sb/CS-021392.htm.
It doesn't seem to support any newer versions, but if you get the drivers from Intel it should definitely work. If you continue to have problems, try posting your code using [source] .code. [/source] tags, and perhaps we may spot something.

EDIT: There is also a lot of additional information at the Intel pages, with answers to common problems. What Windows version are you using?
For example, do you have Vista/7 with Aero enabled?
Some OpenGL programs only work if you disable Aero.
#include <windows.h>#include <gl\gl.h>#include <gl\glu.h>#include <gl\glaux.h>HGLRC	hRC = NULL;HDC		hDC = NULL;HWND		hWnd = NULL;HINSTANCE	hInstance;bool keys[25];bool active = TRUE;bool fullscreen = TRUE;LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);GLvoid ReSizeGLScene(GLsizei width, GLsizei height){	if (height == 0)	{		height = 1;	}	glViewport(0, 0, width, height);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 0.1f, 100.0f);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}int InitGL(GLvoid){	glShadeModel(GL_SMOOTH);	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	glClearDepth(1.0f);	glEnable(GL_DEPTH_TEST);	glDepthFunc(GL_LEQUAL);	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	return TRUE;}int DrawGLScene(GLvoid){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();	return TRUE;}GLvoid KillGLWindow(GLvoid){	if (fullscreen)	{		ChangeDisplaySettings(NULL, 0);		ShowCursor(TRUE);	}	if (hRC)	{		if (!wglMakeCurrent(NULL, NULL))		{			MessageBox(NULL, "Release of DC And RC Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);		}		if (!wglDeleteContext(hRC))		{			MessageBox(NULL, "Release Rendering Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);		}hRC = NULL;	}	if (hDC && !ReleaseDC(hWnd, hDC))	{		MessageBox(NULL, "Release Device Context Failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);		hDC = NULL;	}	if (hWnd && !DestroyWindow(hWnd))	{		MessageBox(NULL, "Could Not Release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);		hWnd = NULL;	}	if (!UnregisterClass("OpenG", hInstance))	{		MessageBox(NULL, "Could Not UnregisterClass.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);		hInstance = NULL;	}}BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag){	GLuint	PixelFormat;	WNDCLASS	wc;	DWORD	dwExStyle;	DWORD	dwStyle;		RECT		WindowRect;	WindowRect.left = (long)0;	WindowRect.right = (long)width;	WindowRect.top = (long)0;	WindowRect.bottom = (long)height;	fullscreen = fullscreenflag;	hInstance			= GetModuleHandle(NULL);	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	wc.lpfnWndProc		= (WNDPROC) WndProc;	wc.cbClsExtra		= 0;	wc.cbWndExtra		= 0;	wc.hInstance		= hInstance;	wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);	wc.hCursor		= LoadCursor(NULL, IDC_ARROW);	wc.hbrBackground	= NULL;	wc.lpszMenuName	= NULL;	wc.lpszClassName	= "OpenG";	if (!RegisterClass(&wc))	{		MessageBox(NULL, "Failed to Register The Window Class.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return FALSE;	}	if (fullscreen)	{		DEVMODE dmScreenSettings;		memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));		dmScreenSettings.dmSize=sizeof(dmScreenSettings);		dmScreenSettings.dmPelsWidth	= width;		dmScreenSettings.dmPelsHeight	= height;		dmScreenSettings.dmBitsPerPel	= bits;		dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;		if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)		{			if (MessageBox(NULL, "The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?", "NeHe G", MB_YESNO|MB_ICONEXCLAMATION)==IDYES)			{				fullscreen = FALSE;			}			else			{				MessageBox(NULL, "Program Will Now Close.", "ERROR", MB_OK |MB_ICONSTOP);				return FALSE;			}		}	}	if (fullscreen)	{		dwExStyle = WS_EX_APPWINDOW;		dwStyle = WS_POPUP;		ShowCursor(FALSE);	}	else	{		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;		dwStyle = WS_OVERLAPPEDWINDOW;	}	AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);	if (!(hWnd = CreateWindowEx(	dwExStyle,							"OpenG",							title,							WS_CLIPSIBLINGS |							WS_CLIPCHILDREN |							dwStyle,							0, 0,							WindowRect.right-WindowRect.left,							WindowRect.bottom-WindowRect.top,							NULL,							NULL,							hInstance,							NULL)))	{		KillGLWindow();		MessageBox(NULL, "Window Creation Error.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return FALSE;	}	static PIXELFORMATDESCRIPTOR pfd =	{		sizeof(PIXELFORMATDESCRIPTOR),		1,		PFD_DRAW_TO_WINDOW |		PFD_SUPPORT_OPENGL |		PFD_DOUBLEBUFFER,		PFD_TYPE_RGBA,		bits,		0, 0, 0, 0, 0, 0,		0,		0,		0,		0, 0, 0, 0,		16,		0,		0,		PFD_MAIN_PLANE,		0,		0, 0, 0	};	if (!(hDC = GetDC(hWnd)))	{		KillGLWindow();		MessageBox(NULL, "Can't Create A GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return FALSE;	}	if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))	{		KillGLWindow();		MessageBox(NULL, "Can't Find A Suitable PixelFormat", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return FALSE;	}	if (!(hRC = wglCreateContext(hDC)))	{		KillGLWindow();		MessageBox(NULL, "Can't Create A GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return FALSE;	}	if(!wglMakeCurrent(hDC, hRC))	{		KillGLWindow();		MessageBox(NULL, "Can't Activate The GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return FALSE;	}	ShowWindow(hWnd, SW_SHOW);	SetForegroundWindow(hWnd);	SetFocus(hWnd);	ReSizeGLScene(width, height);	if (!InitGL())	{		KillGLWindow();		MessageBox(NULL, "Initialization Failed.", "ERROR", MB_OK | MB_ICONEXCLAMATION);		return FALSE;	}	return TRUE;}LRESULT CALLBACK WndProc(	HWND		hWnd,						UINT		uMsg,						WPARAM	wParam,						LPARAM	lParam){	switch (uMsg)	{		case WM_ACTIVATE:		{			if (!HIWORD(wParam))			{				active = TRUE;			}			else			{				active = FALSE;			}			return 0;		}		case WM_SYSCOMMAND:		{			switch (wParam)			{				case SC_SCREENSAVE:				case SC_MONITORPOWER:				return 0;			}			break;		}		case WM_CLOSE:		{			PostQuitMessage(0);			return 0;		}		case WM_KEYDOWN:		{			keys[wParam] = TRUE;			return 0;		}		case WM_KEYUP:		{			keys[wParam] = FALSE;			return 0;		}		case WM_SIZE:		{			ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));			return 0;		}	}	return DefWindowProc(hWnd, uMsg, wParam, lParam);}int WINAPI WinMain(	HINSTANCE		hInstance,				HINSTANCE		hPrevInstance,				LPSTR		lpCmdLine,				int			nCmdShow){	MSG msg;	BOOL done = FALSE;	if (MessageBox(NULL, "Would You Like To Run In Fullscreen Mode?", "Start Fullscreen?", MB_YESNO | MB_ICONQUESTION)== IDNO)	{		fullscreen = FALSE;	}	if (!CreateGLWindow("NeHe's OpenGL Framework", 640, 480, 16, fullscreen))	{		return 0;	}	while(!done)	{		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))		{			if (msg.message == WM_QUIT)			{				done = TRUE;			}			else			{				TranslateMessage(&msg);				DispatchMessage(&msg);			}		}		else		{			if (active)			{				if(keys[VK_ESCAPE])				{					done = TRUE;				}				else				{					DrawGLScene();					SwapBuffers(hDC);				}			}			if (keys[VK_F1])			{				keys[VK_F1] = FALSE;				KillGLWindow();				fullscreen = !fullscreen;				if (!CreateGLWindow("NeHe's OpenGL FrameWork", 640, 480, 16, fullscreen))				{					return 0;				}			}		}	}	KillGLWindow();	return (msg.wParam);} 


It should be the same as what's on the site, but I suppose it's easier to see it all together, and maybe someone else will see something.

I already had the latest driver from intel - I checked again to make sure. I have Win 7 so I tried disabling aero; that didn't help.

I wasn't really able to find anything else on the intel pages that would help.

I really appreciate all that help.
You never set the pixel format. After ChoosePixelFormat, before wglCreateContext, you must use SetPixelFormat(hDC, PixelFormat, &pfd).

Whenever a function call fails (in your case wglCreateContext), call GetLastError immediately after and print the value it returns. I ran your code, and GetLastError said "2000", which you can look up in the winerror.h header file, and it means Invalid Pixelformat. It often helps in searching for the origin of an error.
Wow, somehow I missed that. Thanks so much for catching that.

This topic is closed to new replies.

Advertisement