Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4204868
  • 博文数量: 176
  • 博客积分: 10059
  • 博客等级: 上将
  • 技术积分: 4681
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-24 12:27
文章分类

全部博文(176)

文章存档

2012年(1)

2011年(4)

2010年(14)

2009年(71)

2008年(103)

分类: C/C++

2009-09-30 21:39:02

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

/*
 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/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 the 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 the width and height of the window

    glutInitWindowPosition (100, 100); //set the position of the window

    glutCreateWindow ("A basic OpenGL Window"); //create the window and set the caption for the window

    glutDisplayFunc (display); //call the display function to draw our world

    glutMainLoop (); //initialize the OpenGL loop cycle

    return 0;
}


阅读(3657) | 评论(0) | 转发(0) |
0

上一篇:OpenGL教程

下一篇:OpenGL教程 "Top Ten"

给主人留下些什么吧!~~