opengl学习笔记(2)

来源:互联网 发布:无人机淘宝可靠吗 编辑:程序博客网 时间:2024/05/01 14:22

setup a Window with GLUT

1. The first call we are going to make will initialize GLUT and is done like so: 
    glutInit (&argc, argv)//initialize the program.
2. Keep in mind for this, that argc and argv are passed as parameters to your main method.
 glutInitDisplayMode (GLUT_SINGLE);//set up a basic display buffer (only singular for now)
3. 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 the width and height of the window
    glutInitWindowPosition (100, 100)//set the position of the window
4. And then we give our window a caption/title, and create it.
    glutCreateWindow ("A basic OpenGL Window")//set the caption for the window
5. 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
6. 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
}

1. 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.

2. 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.

3. 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.

4. 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.


/*
 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;
}



0 0