How to setup a Window with GLUT
Rewritten: 10th August 2009
Welcome, to what is now your first OpenGL tutorial. Today we are going
to learn how to create a Window using GLUT (The OpenGL Utility), which has
an OpenGL context attached and can render to the screen.
While GLUT itself is no longer being worked on, it is still available, along with several
other products such as FreeGLUT.
GLUT gives you the ability to create a window, handle input and render to the screen
without being Operating System dependent.
The first things you will need are the OpenGL and GLUT header files and libraries for
your current Operating System.
Once you have them setup on your system correctly, open your first c++ file and
include them at the start of your file like so:
#include <GL/gl.h> //include the gl header file
#include <GL/glut.h> //include the GLUT header file
Now, just double check that your system is setup correctly, and try compiling your current file.
If you get no errors, you can proceed. If you have any errors, try your best to fix them.
Once you are ready to move onto the next step, create a main() method in your current file.
Inside this is where all of your main GLUT calls will go.
The first call we are going to make will initialize GLUT and is done like so:
glutInit (&argc, argv); //initialize the program.
Keep in mind for this, that argc and argv are passed as parameters to your main method. You can see how to do this below.
Once we have GLUT initialized, we need to tell GLUT how we want to draw. There are several parameters we can pass here, but we are going to stick the with most basic GLUT_SINGLE, which will give use a single buffered window.
glutInitDisplayMode (GLUT_SINGLE);//set up a basic display buffer (only singular for now)
The next two methods we are going to use, simply set the size and position of the GLUT window on our screen:
glutInitWindowSize (500, 500); //set whe width and height of the window
glutInitWindowPosition (100, 100); //set the position of the window
And then we give our window a caption/title, and create it.
glutCreateWindow ("A basic OpenGL Window"); //set the caption for the window
This is good. We now have a window of the size and position that we want. But we need to be able to draw to it. We do this, by telling GLUT which method will be our main drawing method. In this case, it is a void method called display()
glutDisplayFunc (display); //call the display function to draw our world
Finally, we tell GLUT to start our program. It does this by executing a loop that will continue until the program ends.
glutMainLoop (); //initialize the OpenGL loop cycle
So thus far, we have a window. But the display method that I mentioned is needed. Lets take a look at this and dissect it.
void display (void) {
glClearColor (0.0,0.0,0.0,1.0); //clear the color of the window
glClear (GL_COLOR_BUFFER_BIT); //Clear teh Color Buffer (more buffers later on)
glLoadIdentity(); //load the Identity Matrix
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //set the view
glFlush(); //flush it all to the screen
}
The first method, glClearColor will set the background color of our window. In this example, we are setting the background to black (RGB 0, 0, 0). The 1.0 value on the end is an alpha value and makes no difference at this stage as we don't have alpha enabled.
The next thing we want to do, is erase everything currently stored by OpenGL. We need to do this at the start of the method, as the method keeps looping over itself and if we draw something, we need to erase it before we draw our next frame. If we don't do this, we can end up with a big mess inside our buffer where frames have been drawn over each other.
The third method, glLoadIdentity resets our model view matrix to the identity matrix, so that our drawing transformation matrix is reset.
From then, we will set the 'camera' for our scene. I am placing it 5 units back into the user so that anything we draw at 0,0,0 will be seen infront of us.
The final thing we need to do is then flush the current buffer to the screen so we can see it. This can be done with glFlush() as we are only using a single buffer.
And that's it. Everything you need to get your very first OpenGL application up and running.
If you have any questions, please email me at
|