November 1st, 2014

Random API of the day

Today’s random API is glutCreateWindow(), provided by the GLUT graphics library.  GLUT was originally written by Mark Kilgard at SGI, and was intended to trade control for simplicity for OpenGL apps that just wanted to draw a window and start doodling on it.  Any OpenGL stack usually includes the following components:

A platform-specific API for manipulating low-level graphics objects,

The “standard” OpenGL API, as ratified by the OpenGL Architecture Review Board, and
The OpenGL utility toolkit, GLU.  Despite the similar name, this has nothing to do with GLUT.
GLUT is not usually considered a “standard” part of the OpenGL stack; for example, Windows does not ship with a copy by default.  Most commercial OpenGL applications prefer to duplicate GLUT’s functionality so that they will have tighter control over the low-level details of the OpenGL environment.

The nice thing about using GLUT is that it makes writing simple applications easier.  GLUT provides a nice abstraction from the platform-specific API for creating the GL context and dealing with mouse and keyboard events.  For example, here’s everything glutCreateWindow() does on Win32:

* The procedure to do this is:
* 1) Define a window class with RegisterClass().
* 2) Change the display settings if necessary with ChangeDisplaySettings().
* 3) Turn off the cursor with ShowCursor(FALSE).
* 4) Determine the window geometry via AdjustWindowRectEx().
* 5) Create a window using the previously defined window class and
*    geometry using CreateWindowEx().
* 6) Get a device context from the window with GetDC().
* 7) Construct a list of requirements for a pixel format and have the
*    operating system suggest some pixel formats using
*    ChoosePixelFormat().
* 8) Set the pixel format to one of the choices returned using
*    SetPixelFormat().
* 9) Use wglCreateContext() to create a rendering context from the
*    device context now that the device context is bound to a suitable
*    OpenGL capable pixel format.
* 10) Use wglMakeCurrent() to bind the device context and rendering
*     context together.
* 11) Use ShowWindow() to make the window visible.
* 12) Use SetForegroundWindow() to make the window the foreground
*     window.
* 13) Use SetFocus() to give the window the focus and have it start
*     receiving keyboard events.
* Multithreaded applications must perform steps 6-10 in each thread
* which will be calling OpenGL APIs, or the calls will no-op
* successfully.

This can all be replaced with a call to glutCreateWindow().  You can read more about GLUT at the OpenGL site.

Leave a Response

Imhotep theme designed by Chris Lin. Proudly powered by Wordpress.
XHTML | CSS | RSS | Comments RSS