View Full Version : OpenGL windows api
08-02-2011, 11:53 PM
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: http://msdn.microsoft.com/en-us/library/dd374278(v=VS.85).aspx 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 http://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/
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.
08-03-2011, 12:58 AM
You could try the getting started (http://www.opengl.org/wiki/Getting_started) section on OpenGL's Wiki. The page on Creating OpenGL contexts (http://www.opengl.org/wiki/Creating_an_OpenGL_Context) 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?"
08-03-2011, 01:30 AM
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").
08-03-2011, 06:42 AM
There is also the platform specific section on the Wiki
click on Platform specifics
then click on
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.
08-03-2011, 09:02 AM
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.
08-03-2011, 02:52 PM
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.
08-03-2011, 03:13 PM
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 (http://www.opengl.org/wiki/Creating_an_OpenGL_Context) I linked to before? Or perhaps, you could just read my signature.
08-03-2011, 04:20 PM
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 (http://web.agelid.com/protect/utile/documentation/OpenGL/OpenGL%201%20-%20Quick%20Start.htm).
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 (http://www.codesampler.com/oglsrc/oglsrc_1.htm).
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 (http://msdn.microsoft.com/en-us/library/ff381409%28v=VS.85%29.aspx).
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.
08-03-2011, 06:01 PM
That's what I needed to hear I guess.
08-04-2011, 07:03 AM
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.
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
08-04-2011, 10:11 AM
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.
// 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);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int iCmdShow)
HWND hWnd; // Handle to window
HDC hDC; // Handle to device context
HGLRC hRC; //Handle to opengl rendering context
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;
TranslateMessage( &msg );
DispatchMessage( &msg );
I WOULD THROW ALL THE OPENGL STUFF IN
// shutdown OpenGL
DisableOpenGL( hWnd, hDC, hRC );
// destroy the window explicitly
DestroyWindow( hWnd );
// Window Procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
PostQuitMessage( 0 );
switch ( wParam )
return DefWindowProc( hWnd, message, wParam, lParam );
// Enable OpenGL
void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
// get the device context (DC) of the window
*hDC = GetDC( hWnd );
// 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 );
08-04-2011, 11:54 AM
Looks pretty good. Some small changes I'd make: 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.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.