GLUT Tutorial : Initialization
In this section we're going to build the main function of our application. The main function will perform the required initializations and start the event processing loop. All the functions in GLUT have the prefix
glut, and those which perform some kind of initialization have the prefix
glutInit. The first thing you must do is call the function
glutInit.
void glutInit(int *argc, char **argv);
Parameters:
argc - A pointer to the
unmodified argc variable from the main function.
argv - A pointer to the
unmodified argv variable from the main function.
After initializing GLUT itself, we're going to define our window. First we establish the window's position, i.e. its top left corner. In order to do this we use the function
glutInitWindowPosition.
void glutInitWindowPosition(int x, int y);
Parameters:
x - the number of pixels from the left of the screen. -1 is the default value, meaning it is up to the window manager to decide where the window will appear. If not using the default values then you should pick a positive value, preferably one that will fit in your screen.
y - the number of pixels from the top of the screen. The comments mentioned for the
x parameter also apply in here.
Note that these parameters are only a suggestion to the window manager. The window returned may be in a different position, although if you choose them wisely you'll usually get what you want. Next we'll choose the window size. In order to do this we use the function
glutInitWindowSize.
void glutInitWindowSize(int width, int height);
Parameters:
width - The width of the window
height - the height of the window
Again the values for
width and
height are only a suggestion, so avoid choosing negative values.
Then you should define the display mode using the function
glutInitDisplayMode.
void glutInitDisplayMode(unsigned int mode)
Parameters:
mode - specifies the display mode
The mode parameter is a Boolean combination (OR bit wise) of the possible predefined values in the GLUT library. You use
mode to specify the color mode, and the number and type of buffers.
The predefined constants to specify the color model are: GLUT_RGBA or GLUT_RGB - selects a RGBA window. This is the default color mode. GLUT_INDEX - selects a color index mode.
The display mode also allows you to select either a single or double buffer window. The predefined constants for this are: GLUT_SINGLE - single buffer window GLUT_DOUBLE - double buffer window, required to have smooth animation.
There is more, you can specify if you want a window with a particular set of buffers. The most common are: GLUT_ACCUM - The accumulation buffer GLUT_STENCIL - The stencil buffer GLUT_DEPTH - The depth buffer
So, suppose you want a RGB window, with single buffering, and a depth buffer. All you have to do is to OR all the respective constants in order to create the required display mode.
After all the above steps, the window can be created with
glutCreateWindow.
int glutCreateWindow(char *title);
Parameters:
title - sets the window title
The return value of
glutCreateWindow is the window identifier. You can use this identifier later within GLUT but this is outside of the scope of this section.
So now here is a little bit of code to perform all the initializations:
Note the include statement at the beginning of the code. The required include file comes with the GLUT distribution.
If you run this code, you'll obtain an empty black console window but no OpenGL window. Furthermore after a few seconds the window disappears. There are two things left to do before we are ready to render something. The first is to tell GLUT what is the function responsible for the rendering.
Lets create an example function for the rendering. The function presented bellow will clear the color buffer and draw a triangle.
The name of this function is up to you. However, now you must tell GLUT that it should use that function we just wrote for the rendering. This is called registering a callback. GLUT will call the function you supply whenever rendering is in order. For now lets tell GLUT that the function
renderScene should be used whenever the window is reported by the window system to be damaged. GLUT has a function that takes as a parameter the name of the function to use when redrawing is needed. Note: the function you supply as a parameter is also called the first time the window is created. The syntax is as follows:
void glutDisplayFunc(void (*func)(void));
Parameters:
func - the name of the function to be called when the window needs to be redrawn. Note: it is illegal to pass NULL as the argument to this function.
One last thing missing, that is telling GLUT that we're ready to get in the application event processing loop. GLUT provides a function that gets the application in a never ending loop, always waiting for the next event to process. The GLUT function is
glutMainLoop, and the syntax is as follows:
void glutMainLoop(void)
The code so far is presented bellow. Note that we've added an include statement in order to start using standard OpenGL functions, like
glClear,
glBegin,
glVertex3f, and
glEnd.
If you try to run this code you'll get a console window, and a few instants after the OpenGL window with a white triangle, hopefully at the desired position and with the desired size.
glut, and those which perform some kind of initialization have the prefix
glutInit. The first thing you must do is call the function
glutInit.
void glutInit(int *argc, char **argv);
Parameters:
argc - A pointer to the
unmodified argc variable from the main function.
argv - A pointer to the
unmodified argv variable from the main function.
After initializing GLUT itself, we're going to define our window. First we establish the window's position, i.e. its top left corner. In order to do this we use the function
glutInitWindowPosition.
void glutInitWindowPosition(int x, int y);
Parameters:
x - the number of pixels from the left of the screen. -1 is the default value, meaning it is up to the window manager to decide where the window will appear. If not using the default values then you should pick a positive value, preferably one that will fit in your screen.
y - the number of pixels from the top of the screen. The comments mentioned for the
x parameter also apply in here.
Note that these parameters are only a suggestion to the window manager. The window returned may be in a different position, although if you choose them wisely you'll usually get what you want. Next we'll choose the window size. In order to do this we use the function
glutInitWindowSize.
void glutInitWindowSize(int width, int height);
Parameters:
width - The width of the window
height - the height of the window
Again the values for
width and
height are only a suggestion, so avoid choosing negative values.
Then you should define the display mode using the function
glutInitDisplayMode.
void glutInitDisplayMode(unsigned int mode)
Parameters:
mode - specifies the display mode
The mode parameter is a Boolean combination (OR bit wise) of the possible predefined values in the GLUT library. You use
mode to specify the color mode, and the number and type of buffers.
The predefined constants to specify the color model are:
The display mode also allows you to select either a single or double buffer window. The predefined constants for this are:
There is more, you can specify if you want a window with a particular set of buffers. The most common are:
So, suppose you want a RGB window, with single buffering, and a depth buffer. All you have to do is to OR all the respective constants in order to create the required display mode.
...
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT DEPTH);
...
After all the above steps, the window can be created with
glutCreateWindow.
int glutCreateWindow(char *title);
Parameters:
title - sets the window title
The return value of
glutCreateWindow is the window identifier. You can use this identifier later within GLUT but this is outside of the scope of this section.
So now here is a little bit of code to perform all the initializations:
#include <GL/glut.h>
void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("3D Tech- GLUT Tutorial");
}
Note the include statement at the beginning of the code. The required include file comes with the GLUT distribution.
If you run this code, you'll obtain an empty black console window but no OpenGL window. Furthermore after a few seconds the window disappears. There are two things left to do before we are ready to render something. The first is to tell GLUT what is the function responsible for the rendering.
Lets create an example function for the rendering. The function presented bellow will clear the color buffer and draw a triangle.
...
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0.0);
glEnd();
glFlush();
}
The name of this function is up to you. However, now you must tell GLUT that it should use that function we just wrote for the rendering. This is called registering a callback. GLUT will call the function you supply whenever rendering is in order. For now lets tell GLUT that the function
renderScene should be used whenever the window is reported by the window system to be damaged. GLUT has a function that takes as a parameter the name of the function to use when redrawing is needed. Note: the function you supply as a parameter is also called the first time the window is created. The syntax is as follows:
void glutDisplayFunc(void (*func)(void));
Parameters:
func - the name of the function to be called when the window needs to be redrawn. Note: it is illegal to pass NULL as the argument to this function.
One last thing missing, that is telling GLUT that we're ready to get in the application event processing loop. GLUT provides a function that gets the application in a never ending loop, always waiting for the next event to process. The GLUT function is
glutMainLoop, and the syntax is as follows:
void glutMainLoop(void)
The code so far is presented bellow. Note that we've added an include statement in order to start using standard OpenGL functions, like
glClear,
glBegin,
glVertex3f, and
glEnd.
If you try to run this code you'll get a console window, and a few instants after the OpenGL window with a white triangle, hopefully at the desired position and with the desired size.
#include <GL/glut.h>
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0.0);
glEnd();
glFlush();
}
void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("3D Tech- GLUT Tutorial");
glutDisplayFunc(renderScene);
glutMainLoop();
}