Skip to main content
gamedev.net
🔒 Locked

OpenGL Init Problems

Started by fyhuang May 2, 2004 at 11:54 AM 2 replies 1.4k views
Original Post
fyhuang
fyhuang
I created a graphics engine that uses OpenGL, and however much I peruse NeHe''s tutorials, I cannot seem to find the problem with it. After I have ''set the graphics mode'' (in D3D talk), I try to use the context to render stuff, but nothing happens. GetLastError() gives me a few error codes, along the lines of ''handle invalid'', and even one, ''printer name invalid''. This is my window class:
	//wcex.cbSize = sizeof( WNDCLASSEX );

	wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wcex.lpfnWndProc = WindowProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInst;
	wcex.hIcon = LoadIcon( NULL, IDI_WINLOGO );
	wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
	wcex.hbrBackground = NULL;
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = ClassName;

// AND MY WINDOW CREATION CODE :

	if ( windowed == true )
	{
		WindowStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
		g_hWnd = hWnd = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, ClassName, WindowCaption, WindowStyle,
			WindowX, WindowY, WindowWidth, WindowHeight, NULL, NULL, hInst, NULL );
	}
	else
	{
		WindowStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
		g_hWnd = hWnd = CreateWindowEx( WS_EX_APPWINDOW, ClassName, WindowCaption, WindowStyle,
			WindowX, WindowY, WindowWidth, WindowHeight, NULL, NULL, hInst, NULL );
	}

// AND MY SET MODE FUNCTION :

bool CGraphicsGL::SetMode( bool windowed, bool zbuffer, int multisample, int width, int height, int bpp )
{
	// Set graphics mode

	GLuint PixelFormat = 0;
	RECT ClientRect;

	int errornum = 0;
	DWORD result;
	char message[256];
	char msg2[256];

	// Clear structures

	ZeroMemory( &GLDM, sizeof( DEVMODE ) );
	ZeroMemory( &GLPF, sizeof( PIXELFORMATDESCRIPTOR ) );

	// Error checking

	if ( GDLGlobal == NULL )
	{
		return GDL_BOOL_FAILED;
	}

	if ( g_hWnd == NULL && GDLGlobal->GetHWND() == NULL )
	{
		return GDL_BOOL_FAILED;
	}
	else if ( GDLGlobal->GetHWND() != NULL )
	{
		g_hWnd = GDLGlobal->GetHWND();
	}

	// Check width/height

	if ( ( DisplayWidth = width ) <= 0 )
	{
		// Check fullscreen

		if ( windowed == false )
		{
			// Use default width

			DisplayWidth = 640;
		}
		else
		{
			// Use client width

			GetClientRect( g_hWnd, &ClientRect );
			DisplayWidth = ClientRect.right;
		}
	}

	if ( ( DisplayHeight = height ) <= 0 )
	{
		// Check fullscreen

		if ( windowed == false )
		{
			// Use default height

			DisplayHeight = 480;
		}
		else
		{
			// Use client height

			GetClientRect( g_hWnd, &ClientRect );
			DisplayHeight = ClientRect.bottom;
		}
	}

	// Check BPP

	if ( ( DisplayBPP = bpp ) <= 0 || windowed == true )
	{
		// Set default BPP

		if ( !( DisplayBPP = 32 ) )
		{
			return GDL_BOOL_FAILED;
		}
	}

	// Resize client if using windowed mode

	if ( windowed == true )
	{
		GDLGlobal->WindowResize( DisplayWidth, DisplayHeight );
	}

	// Check for fullscreen

	if ( ( DisplayWindowed = windowed ) == false )
	{
		// Init display mode

		GLDM.dmSize = sizeof( DEVMODE );
		GLDM.dmPelsWidth = DisplayWidth;
		GLDM.dmPelsHeight = DisplayHeight;
		GLDM.dmBitsPerPel = DisplayBPP;
		GLDM.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

		// Set selected mode

		if ( ChangeDisplaySettings( &GLDM, CDS_FULLSCREEN ) != DISP_CHANGE_SUCCESSFUL )
		{
			// Check if user wants to switch to windowed

#ifdef _DEBUG
			Global->LogError( "CGraphicsGL::SetMode() : Set fullscreen mode failed." );
#endif
			if ( MessageBox( g_hWnd, "Your video card does not support the selected fullscreen mode. Switch to windowed?", "Fullscreen failed.", MB_YESNO | MB_ICONEXCLAMATION ) == IDYES )
			{
				windowed = true;
			}
			else
			{
				GDLGlobal->ShowMessage( "Windowed mode not requested -- OpenGL set mode failed. Closing now." );
				return GDL_BOOL_FAILED;
			}
		}
	}

	// Setup pixel format (both don''t work)

	GLPF.nSize = sizeof( PIXELFORMATDESCRIPTOR );
	GLPF.nVersion = 1;
	GLPF.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	GLPF.iPixelType = PFD_TYPE_RGBA;
	GLPF.cColorBits = DisplayBPP;
	GLPF.cRedBits = 0;
	GLPF.cRedShift = 0;
	GLPF.cGreenBits = 0;
	GLPF.cGreenShift = 0;
	GLPF.cBlueBits = 0;
	GLPF.cBlueShift = 0;
	GLPF.cAlphaBits = 0;
	GLPF.cAlphaShift = 0;
	GLPF.cAccumBits = 0;
	GLPF.cAccumRedBits = 0;
	GLPF.cAccumGreenBits = 0;
	GLPF.cAccumBlueBits = 0;
	GLPF.cAccumAlphaBits = 0;
	GLPF.cDepthBits = 16;
	GLPF.cStencilBits = 0;
	GLPF.cAuxBuffers = 0;
	GLPF.iLayerType = PFD_MAIN_PLANE;
	GLPF.bReserved = 0;
	GLPF.dwLayerMask = 0;
	GLPF.dwVisibleMask = 0;
	GLPF.dwDamageMask = 0;

	static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be

	{
		sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor

		1,											// Version Number

		PFD_DRAW_TO_WINDOW |						// Format Must Support Window

		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL

		PFD_DOUBLEBUFFER,							// Must Support Double Buffering

		PFD_TYPE_RGBA,								// Request An RGBA Format

		DisplayBPP,										// Select Our Color Depth

		0, 0, 0, 0, 0, 0,							// Color Bits Ignored

		0,											// No Alpha Buffer

		0,											// Shift Bit Ignored

		0,											// No Accumulation Buffer

		0, 0, 0, 0,									// Accumulation Bits Ignored

		16,											// 16Bit Z-Buffer (Depth Buffer)  

		0,											// No Stencil Buffer

		0,											// No Auxiliary Buffer

		PFD_MAIN_PLANE,								// Main Drawing Layer

		0,											// Reserved

		0, 0, 0										// Layer Masks Ignored

	};

	// Get the DC

	GLDC = GetDC( g_hWnd );

	// Check the DC

	if ( GLDC == NULL )
	{
		Global->ShowError( "Couldn''t get DC.", true );
		return GDL_BOOL_FAILED;
	}

	// Choose a pixel format

	if ( !( PixelFormat = ChoosePixelFormat( GLDC, &pfd ) ) )
	{
		Global->ShowError( "Couldn''t find a pixel format.", true );
		return GDL_BOOL_FAILED;
	}
	
	// Set the pixel format

	if ( !SetPixelFormat( GLDC, PixelFormat, &pfd ) )
	{
		Global->ShowError( "Couldn''t set pixel format.", true );
		return GDL_BOOL_FAILED;
	}

	// Activate rendering context

	if ( !wglMakeCurrent( GLDC, GLRC ) )
	{
		Global->ShowError( "Couldn''t activate rendering context.", true );
		return GDL_BOOL_FAILED;
	}

	// Resize the viewport

	glViewport( 0, 0, DisplayWidth, DisplayHeight );
	
	// Set perspective

	SetPerspective( 45.0f, (GLfloat)DisplayWidth / (GLfloat)DisplayHeight, 1.0f, 500.0f );

	// Check for ZBuffer

	if ( ( DisplayZBuffer = zbuffer ) == true )
	{
		glEnable( GL_DEPTH_TEST );
		glDepthFunc( GL_LEQUAL );
	}
	else
	{
		glDisable( GL_DEPTH_TEST );
	}

	// Reset modelview matrix

	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();

	// Set smooth shading

	glShadeModel( GL_SMOOTH );

	return GDL_BOOL_SUCCESS;
}

Looks fine to me, but there''s probably some little obvious line that I forgot to add that''s killing the program. I''ve tested it using both windowed and fullscreen modes, 16-bit, 24-bit, and 32-bit color, nothing works. SetPixelFormat() gives me an error, ''printer name invalid (#1801)''. Can anyone help me???
- fyhuang [ site ]
Hybrid666
Hybrid666
sounds like it cant set the pixel format, which might b causing that printer error..

invalid handle errors suggest that it cant create a handle to the window properly (HWND) ... this wud have the result of messing up the set pixel format command later on

best bet is check out msdn error codes just search the lib stick the error code in there and get a list of D3D errors up.. unfortunatly im an OpenGL guy so cant help with that bit

http://msdn.microsoft.com/library/default.asp

hope it helps

Hybrid
Hybrid666
Hybrid666
oops misread.. u are an OpenGL guy lol..

check out the error codes on www.opengl.org in that case will give more help
fyhuang
fyhuang
Well, I can''t seem to find any kind of error code things on opengl.org...

And you must be psychic, this project is a multi-API engine which already (somewhat) supports D3D, and I''m trying to get it working w/OpenGL now. OpenGL seems much more straightforward.

Also, some parts opengl.org seem to not load on my browsers (I tried both MSIE and Mozilla). Specifically, the search feature.
- fyhuang [ site ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.