OpenGL entry program (1)

Goal: draw a white rectangle

#include <GL/glut.h>

void MyDisplay(void);

int main(int argc, char **argv)
{
    // Set the size of the window 
    glutInitWindowSize( 400 , 400 );

    // Set the position of the window on the screen 
    glutInitWindowPosition( 500 , 200 );

    // Set the display format: GLUT_RGB<use RGB color> GLUT_DOUBLE<use double buffer>    
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    // Initialize GLUT, the format is rigid, remember to 
    glutInit(& argc, argv);      

    // Create a window based on the currently set information, the parameter is the title of the window. Note: glutMainLoop(), you can see the created window 
    glutCreateWindow( " OpenGL first test! " );

    // Set a function, this function will be called when you need to draw a picture, this is inaccurate, the exact word is not easy for beginners to understand,
     // Let's put it this way 
    glutDisplayFunc(MyDisplay);

    // Do a message loop 
    glutMainLoop();
     return  0 ;
}

void MyDisplay(void)
{
    // Clear. GL_COLOR_BUFFER_BIT means to clear the color. Of course, other things can also be cleared. I won't introduce too much 
    glClear(GL_COLOR_BUFFER_BIT);

    // Draw a rectangle, the four parameters represent the horizontal and vertical coordinates of the two vertices on the diagonal respectively 
    glRectf(- 0.5f , - 0.5f , 0.5f , 0.5f );

    // Indicates that the previous OpenGL commands are executed immediately (rather than letting them wait in the buffer), 
    glFlush();
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325312464&siteId=291194637