OpenGL windows api

Is it a worth while endeavor to begin learning openGL by using the windows api to create a window? I’m not entirely sure if “windows api” is the right word. It’s more like a windows api extension built to support openGL. Anyway, if you check out: OpenGL - Win32 apps | Microsoft Learn you see what I mean.

What I’m wondering is… is there any documentation on how to use these functions and etc besides tutorials written by random people?

For example: check out this page NeHe Productions: Creating an OpenGL Window (Win32)

It seems like a very well written tutorial. But I got to wondering how the author figured some of the stuff out. Is there a more official resource for using the WGL functions? The MSDN site explains some of the functions but it’s more a reference for someone who already knows what they are doing.

I have looked at other things such as the redbook but I have found this rather useless. 1. It’s old. 2. The examples are useless because the auxiliary library is non-existent.

I’m looking for a way to get started in opengl that has well documented resources. I’m open to trying freeglut but I havent found anything explaining how to use it.

I’m programming with visual c++ 2010 express.

You could try the getting started section on OpenGL’s Wiki. The page on Creating OpenGL contexts is still missing Linux and Mac OSX context creation (note to Linux and Mac OSX programmers: feel free to fill in the gaps), but the Windows section is pretty comprehensive.

Personally, I don’t see the point in playing around with that stuff when you’re trying to learn how to use OpenGL. It’s much easier to just use FreeGLUT or GLFW or something; that gets you past all the platform-specific issues.

The MSDN site explains some of the functions but it’s more a reference for someone who already knows what they are doing.

It explains what the functions do; that’s the purpose of reference documentation. It’s meant when you need to answer the question, “What does this function do exactly?” It doesn’t answer the question, “How exactly do I use this in my application to do X?”

You have already mentioned the two important sources: MSDN (ultimate source because it is a MS Windows stuff) and the Red book.

I don’t think it is so complicated. You have to know what some of WGL functions do. Only the following functions are important:

  • ChoosePixelFormat - tests if chosen pixel format of the window is appropriate for the GL, and retrieves the most similar one if not;

  • SetPixelFormat - sets the chosen pixel format;

  • wglCreateContext - creates GL 2.1 (or earlier) context;

  • wglDeleteContext - deletes previously created context (usually on application exit)

  • wglMakeCurrent - makes GL context current for the calling thread (a context can be current only in one thread at the time);

  • SwapBuffers - swaps front and back buffer (in double-buffer mode, it enables you to see what you have drawn; it should be the last command in your draw function);

  • wglGetProcAddress - enables to grab the function pointer from the driver (important only if you want to do extension handling “on your own”).

There is also the platform specific section on the Wiki
http://www.opengl.org/wiki

click on Platform specifics
http://www.opengl.org/wiki/Platform_specifics

then click on
Platform specifics: Windows

http://www.opengl.org/wiki/Platform_specifics:_Windows
and you have information about what is what and what not to do and what not to use and what should you use.

V-man, that’s more what I was looking for only perhaps something a little more organized and detailed.

Alfonse Reinheart, I know the MSDN site is a reference that explains what the functions do. I wasn’t complaining about. It’s just not what I needed.

Are there any books available that explain opengl programming with windows?

I don’t want to hear the redbook unless you can provide a way to go through the examples meaningfully. The books assumes you have the auxiliary library. From what I have read, that library is way outdated. People suggest using freeglut. The problem is a beginner does not know translate the auxiliary functions in the redbook to equivalent functions in freeglut. If you are tempted to say this is an easy task it’s probably because YOU ALREADY KNOW HOW TO DO IT! Obviously anything is easy once you know how to do it.

The trouble with simply reading the MSDN site for the WGL stuff is that there is probably a lot of convention that is understood by experienced programmers of opengl that a newcomer just would not have. The sites that V-man suggested were good indications of this fact. I’m looking for the kind of thing v-man suggested but with a little more order and less randomness.

I don’t want to hear the redbook unless you can provide a way to go through the examples meaningfully.

OpenGL is not about how you create the context. OpenGL is OpenGL, whether you directly created the context with Win32, X11, FreeGLUT, etc. The only non-GL thing those Redbook examples do is use GLUT to create the context and manage the window; after that, it’s all OpenGL.

The books assumes you have the auxiliary library. From what I have read, that library is way outdated. People suggest using freeglut. The problem is a beginner does not know translate the auxiliary functions in the redbook to equivalent functions in freeglut.

No it doesn’t. It assumes you have GLUT, which is superceeded by FreeGLUT. FreeGLUT is 100% compatible with GLUT (hence the name). There’s no translation necessary; they simply work, because FreeGLUT is just another implementation of GLUT’s functions.

If you are tempted to say this is an easy task it’s probably because YOU ALREADY KNOW HOW TO DO IT!

Here’s an idea: yelling at people who are trying to help you ISN’T PRODUCTIVE!

I’m looking for the kind of thing v-man suggested but with a little more order and less randomness.

Was there something wrong with the step-by-step instructions I linked to before? Or perhaps, you could just read my signature.

The free version of the Red Book you can find online uses AUX; more recent versions (dead tree, pay money) upgraded to GLUT.

Third (I think) edition Superbible has a fairly comprehensive chapter on the WGL stuff. It’s a little outdated these days as it includes some extensions that depend on deprecated functionality (display lists for fonts) and won’t help you with a core context, but it might be a reasonably gentle introduction to how everything fits together in a working program.

Both of these options may be available on the second hand market.

MSDN used to include an “OpenGL Quick Start” tutorial; not sure if it’s still on the site, but an archived copy is available (including a charming story about “my uncle Ulysses” - once upon a time MS documentation had a quirky humour to it).

Link: here.

Beware that it’s MFC and uses AUX too. Neither of these are really relevant for the purpose of getting a basic rendering context up and running, and you should be able to learn enough from the code there to get started. If however you’re looking for copy/paste code you won’t find it there.

CodeSampler has two basic programs for doing nothing but creating a window and setting up a rendering context. First is for windowed mode, second is for fullscreen, both use native Win32 with no external library dependencies. These might be a better bet for basing your own code on, and at least you’ll get to see how the code fits together.

Link: here.

It might be better to briefly ignore OpenGL and focus on writing a simple Win32 program to just create an empty window. Learn what the message loop is and how it works, learn how to create and destroy that window, learn the structure of a Win32 program. Then move on to OpenGL. That way you’re only learning one new thing at a time and can focus on that without being confused by the other.

MSDN sample.

As has been said, once you get a rendering context (and your message loop) up you can forget about the OS layer. Everything else - so far as graphics are concerned - is just pure OpenGL and there’s no need to worry about the Windows API at all.

Excellent.

That’s what I needed to hear I guess.

Wiki pages are editable by people here on the forum.
I’m not sure what order you want. Perhaps if you are specific, we can change the page.

If you are looking for an example, then that is what search engines is for. Since there should be plenty of people using Win32 code.

Some examples are rare : If someone says they are looking for a GL 4.1 tesselation shader example, then ok, perhaps it is so rare that someone should make a tutorial and add it to
http://www.opengl.org/wiki/Tutorials

You’re right. I should be more careful in my comments. I was looking for something that talked about the big picture in the form of a tutorial. What I found on the “platform specifics” page was a Q&A style help. It was useful but not as connected as I needed.

So after some reading this the base I have for an opengl program. Could any of you glace at it and let me know if this is the best way to structure it. What I’m really wondering is a good way to break up the program so that I have the context creation/init stuff separate from the other more important programming stuff such as graphics and game logic etc.


#include <Windows.h>

#include <gl/GL.h>

// Function Declarations

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC);
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC);

// WinMain

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int iCmdShow)
{
	WNDCLASS wc;
	HWND hWnd; // Handle to window
	HDC hDC; // Handle to device context
	HGLRC hRC; //Handle to opengl rendering context
	MSG msg;
	BOOL quit = FALSE;
	float theta = 0.0f;
	
	// register window class
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
	wc.lpszMenuName = NULL;
	wc.lpszClassName = TEXT("GLSample");
	RegisterClass( &wc );
	
	// create main window
	hWnd = CreateWindow( 
		TEXT("GLSample"), TEXT("OpenGL Sample"), 
		WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
		0, 0, 256, 256,
		NULL, NULL, hInstance, NULL );
	
	// enable OpenGL for the window
	EnableOpenGL( hWnd, &hDC, &hRC );
	
	// program main loop
	while ( !quit )
	{
		
		// check for messages
		if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )  )
		{
			
			// handle or dispatch messages
			if ( msg.message == WM_QUIT ) 
			{
				quit = TRUE;
			} 
			else 
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
			
		} 
		else 
		{
			
			/******************************************
			*******************************************
			I WOULD THROW ALL THE OPENGL STUFF IN 
			HERE. 
			*******************************************
			*******************************************/
					
		}

	}

	// shutdown OpenGL
	DisableOpenGL( hWnd, hDC, hRC );

	// destroy the window explicitly
	DestroyWindow( hWnd );

	return msg.wParam;

}

// Window Procedure

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	
	switch (message)
	{
		
	case WM_CREATE:
		return 0;
		
	case WM_CLOSE:
		PostQuitMessage( 0 );
		return 0;
		
	case WM_DESTROY:
		return 0;
		
	case WM_KEYDOWN:
		switch ( wParam )
		{
			
		case VK_ESCAPE:
			PostQuitMessage(0);
			return 0;
			
		}
		return 0;
	
	default:
		return DefWindowProc( hWnd, message, wParam, lParam );
			
	}
	
}

// Enable OpenGL

void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
{
	//PIXELFORMATDESCRIPTOR pfd;
	int format;
	
	// get the device context (DC) of the window
	*hDC = GetDC( hWnd );

	PIXELFORMATDESCRIPTOR pfd;
	// set the pixel format for the DC
	ZeroMemory( &pfd, sizeof( pfd ) );
	pfd.nSize = sizeof( pfd );
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 24;
	pfd.cDepthBits = 16;
	//pfd.iLayerType = PFD_MAIN_PLANE;  NO LONGER USED
	format = ChoosePixelFormat( *hDC, &pfd );
	SetPixelFormat( *hDC, format, &pfd );
	
	// create and enable the render context (RC)
	*hRC = wglCreateContext( *hDC );
	wglMakeCurrent( *hDC, *hRC );
	
}

// Disable OpenGL

void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
	wglMakeCurrent( NULL, NULL );
	wglDeleteContext( hRC );
	ReleaseDC( hWnd, hDC );
}


Looks pretty good. Some small changes I’d make:[ul][]I’d add error checking to your Set/Choose PixelFormat calls, and also to your wglCreateContext and wglMakeCurrent calls.[]You should probably be adding CS_HREDRAW | CS_VREDRAW to your class style.[]Look at the AdjustWindowRect function as the width and height you specify for your window size includes title bar and border.[]Handle WM_ERASEBKGND by returning non-zero.[]Don’t forget SwapBuffers (hDC); at the end of each frame (you might already know this but just being sure).[]I’d generally request a 24-bit depth buffer (pfd.cDepthBits = 24) for the higher accuracy.[/ul]