Swiftless Game Programming Site
Home Tutorials Contact
Home
News
OpenGL
GLSL
OpenCL
Maths
Misc
Work in Progress
Resume
 
 

How to create an OpenGL window using GLUT

If you would like to see this site updated, please help out and

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 swiftless@gmail.com

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
  // Here is your first OpenGL tutorial. How to make a Window.
// First off you need to call the OpenGL header files. The basics are
// gl.h and glut.h, with these two your program will run on whatever OS
// it is compiled in as it does not currently rely on Windows.

// So that you know, the gl.h supplies the openGL commands while glut.h
// allows us to call the glut commands. If you are not using glut, then
// do not bother to call this.

// After this next little bit, the rest of the code is explained below in the actual code.

// To reshape a window in GLUT you need one command in the 'main' function
// this is:
// glutInitWindowSize (500, 500);
// this will set it to 500 pixels wide and 500 high.
// The other line you may notice I have added is:
// glutInitWindowPosition (100, 100);
// This sets where the window is located on the screen, in this case
// 100 pixels down and 100 to the right.

#include <GL/gl.h> //include the gl header file
#include <GL/glut.h> //include the glut header file

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
}

int main (int argc, char **argv) {
    glutInit (&argc, argv); //initialize the program.
    glutInitDisplayMode (GLUT_SINGLE); //set up a basic display buffer (only singular for now)
    glutInitWindowSize (500, 500); //set whe width and height of the window
    glutInitWindowPosition (100, 100); //set the position of the window
    glutCreateWindow ("A basic OpenGL Window") //set the caption for the window
    glutDisplayFunc (display); //call the display function to draw our world
    glutMainLoop (); //initialize the OpenGL loop cycle
    return 0;
}


Download C++ Source Code for this Tutorial

Download Visual Basic Source Code for this Tutorial


Comments:

Name: Navin
Date: 2010-01-29 22:50:56
Comment:
Thanks for these tutorials. They are simple and very comprehensive. I am new to OpenGL and your tutorials have helped me start.

Name: moss
Date: 2010-02-03 01:15:06
Comment:
man this is awesome!!! i m new to OpenGL and those tutorials are simply sweet, thanks a lot!!!

Name: Helloo!the ;!
Date: 2010-02-09 23:29:25
Comment:
glutCreateWindow ("A basic OpenGL Window")
You have forgot the [color=blue];[/color]
please edit
:D

Name: Donald Urquhart
Date: 2010-02-09 23:40:58
Comment:
Hey,

Thanks for that, I didn't even notice the missing ;

Will edit when I can, cheers,
Donald

Name: flopsy
Date: 2010-03-05 17:50:04
Comment:
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.....
-------------------------------------------------------------
I'm new to this and don't understand what & where to setup?
Help me please

Name: msn
Date: 2010-03-08 18:01:47
Comment:
Thanks for your tutorial
Name   Email


Reload Image

 
     

 

Copyright 2009, Donald Urquhart AKA Swiftless
Check out: http://www.cdadc.com