33. OpenGL
Programming 1
Dated: 03-07-2025
Because OpenGL
is an independent platform, we can write code for any operating system using the OpenGL Utility Library
(glut
). This library handles things like making windows, getting keyboard inputs, run an event handler
or message loop in our graphic application.
Functions
with "gl" prefix are from coreOpenGL
Functions
with "glu" prefix are fromglut
int glutCreateWindow(char* name);
This creates a window
which is set to current window
implicitly. Each window
has an OpenGL context
associated with it and changes to its state can be done immediately after the window
is created. The display state
of window
is not acted upon unless we have entered the glutMainLoop
.
X Implementation Notes
The proper X Inter-Client Communication Conventions Manual (ICCCM)
top-level properties are established. The WM_COMMAND
property that lists the command line used to invoke the GLUT
program is only established for the first window
created.
Example Functions
#include <GL/glut.h>
int main () {
// Set up the OpenGL rendering context
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
// Create a window and set its width and height.
glutCreateWindow("Deform");
glutReshapeWindow(640, 480);
// The keyboard function gets called whenever we hit a key.
glutKeyboardFunc(keyboard);
// The display function gets called whenever the window requires an update or when we explicitly call
glutDisplayFunc(display);
// The reshape function gets called whenever the window changes shape
glutReshapeFunc(reshape);
// The idle function gets called when there are no window-events to process.
glutIdleFunc(idle);
// Get the ball rolling!
glutMainLoop();
}
void glutInitDisplayMode(unsigned int mode);
This function
sets the initial display mode.
Mode
Display mode, normally the bitwise OR-ing of GLUT
display mode bit masks.
GLUT_RGBA
Bit mask
to select an RGBA
mode window. This is the default if neither GLUT_RGBA
nor GLUT_INDEX
are specified.
GLUT_RGB
An alias for GLUT_RGBA
.
GLUT_INDEX
Bit mask
to select a color index mode window. This overrides GLUT_RGBA
if it is also specified.
GLUT_SINGLE
Bit mask
to select a single buffered window
. This is the default if neither GLUT_DOUBLE
or GLUT_SINGLE
are specified.
GLUT_DOUBLE
Bit mask
to select a double buffered window
. This overrides GLUT_SINGLE
if it is also specified.
GLUT_ACCUM
Bit mask
to select a window
with an accumulation buffer
.
GLUT_ALPHA
Bit mask
to select a window
with an alpha component to the color buffer
(s).
GLUT_DEPTH
Bit mask
to select a window with a depth buffer
.
GLUT_STENCIL
Bit mask
to select a window
with a stencil buffer
.
GLUT_MULTISAMPLE
Bit mask
to select a window
with multisampling support. If multisampling is not available, a non-multisampling window
will automatically be chosen. Both the OpenGL client-side
and server-side
implementations must support the GLX_SAMPLE_SGIS
extension for multisampling to be available.
GLUT_STEREO
Bit mask
to select a stereo window
.
GLUT_LUMINANCE
Bit mask
to select a window
with a luminance color model
. This model provides the functionality of OpenGL's RGBA color model
, but the green
and blue
components are not maintained in the frame buffer
.1 Instead each pixel
2's red
component is converted to an index between zero and glutGet(GLUT_WINDOW_COLORMAP_SIZE) - 1
and looked up in a per window
color map to determine the color of pixels
2 within the window
. The initial colormap of GLUT_LUMINANCE
windows
is initialized to be a linear gray ramp, but can be modified with GLUT
's colormap routines.
GLUT_LUMINANCE
is not supported on most OpenGL platforms.
void glutReshapeWindow(int width, int height);
Requests a change to the size of the current window
. It disables the full screen status of a window
if previously enabled.
void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y))
Requires a callback function
which takes the ASCII
data alongside the mouse coordinates (x
and y
) when the key is pressed.
glutGetModifiers
can be used to determine the state of modifier keys.
void glutDisplayFunc(void (*func)(void));
GLUT
determines when the display callback should be triggered based on the window
's redisplay state. The redisplay state for a window
can be either set explicitly by calling glutPostRedisplay
or implicitly as the result of window
damage reported by the window system
. Multiple posted redisplays for a window
are coalesced by GLUT
to minimize the number of display callbacks called.
void glutReshapeFunc(void (*func)(int width, int height));
A reshape call back is also triggered alongside the first display call back.
void glutIdleFunc(void (*func)(void));
Sets the global idle callback to be func
so a GLUT
program can perform background processing tasks or continuous animation when window system
events are not being received. If enabled, the idle callback is continuously called when events are not being received.
void glutMainLoop(void);
glutMainLoop
enters the GLUT
event processing loop. This routine should be called at most once in a GLUT
program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered.
void glutSwapBuffers(void);
An implicit glFlush
is done by glutSwapBuffers
before it returns.
References
-
Read more about frame buffer. ↩