Introduction to OpenGL + a simple program

OpenGL(全写Open Graphics Library)


OpenGL refers to a professional graphics programming interface that defines a cross-programming language, cross-platform programming interface specification. It is used for three-dimensional images (two-dimensional ones are also available), and it is a powerful and convenient underlying graphics library.

OpenGL is the most widely accepted 2D/3D graphics API in the industry. Since its inception, it has spawned thousands of excellent applications on various computer platforms and devices. OpenGL is independent of Windows or other operating systems, and is network transparent. In industries including CAD, content creation, energy, entertainment, game development, manufacturing, pharmaceutical industry, and virtual reality, OpenGL helps programmers achieve high-performance, high-impact performance on hardware devices such as PCs, workstations, and supercomputers. Powerful high visual expressive graphics software development.

A simple OpenGL program.

Environment: CentOS7

g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)

OpenGL Implementation Vendor Name: VMware, Inc.
Renderer Identifier: Gallium 0.4 on llvmpipe (LLVM 3.9, 256 bits)
OpenGL Implementation Version Number: 2.1 Mesa 17.0.1
OGLU Tool Library Version: 1.3

/**
 * Draw a lighted ball.
 *
 *           test.c 2018.05
 * Since the header files gl.h and glu.h are already included in the header file glut.h, you only need to include this file
 */
# include <GL/glut.h>
# include <stdlib.h>

/* Initialize material properties, light properties, lighting model, open depth buffer */
void init ( void )
{
    const GLubyte* name = glGetString(GL_VENDOR);
    const GLubyte* biaoshifu = glGetString(GL_RENDERER);
    const GLubyte* OpenGLVersion =glGetString(GL_VERSION);
    const GLubyte* gluVersion= gluGetString(GLU_VERSION);

    printf("OpenGL implementation manufacturer's name: %s\n", name);
    printf("Renderer identifier: %s\n", biaoshifu);
    printf("The version number of OpenGL implementation: %s\n",OpenGLVersion );
    printf("OGLU tool library version: %s\n", gluVersion);

    GLfloat mat_specular [ ] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat mat_shininess [ ] = { 50.0 };
    GLfloat light_position [ ] = { 1.0, 1.0, 1.0, 0.0 };

    glClearColor ( 0.0, 0.0, 0.0, 0.0 );
    glShadeModel ( GL_SMOOTH );

    glMaterialfv ( GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv ( GL_FRONT, GL_SHININESS, mat_shininess);
    glLightfv ( GL_LIGHT0, GL_POSITION, light_position);
    
    glEnable (GL_LIGHTING);
    glEnable (GL_LIGHT0);
    glEnable (GL_DEPTH_TEST);
}

/* Call the GLUT function to draw a ball */
void display ( void )
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidSphere (1.0, 40, 50);
    glFlush ();
}

/* Define the reshape function of GLUT, w and h are the width and height of the current window respectively*/
void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ( );
    if (w <= h)
        glOrtho (-1.5, 1.5, -1.5 * ( GLfloat ) h / ( GLfloat ) w,
                    1.5 * ( GLfloat ) h / ( GLfloat ) w, -10.0, 10.0 );
    else
        glOrtho (-1.5 * ( GLfloat ) w / ( GLfloat ) h,
                    1.5 * ( GLfloat ) w / ( GLfloat ) h,
                    -1.5, 1.5, -10.0, 10.0);
    glMatrixMode ( GL_MODELVIEW );
    glLoadIdentity ( ) ;
}

/* Define the response function to the keyboard */
void keyboard ( unsigned char key, int x, int y)
{
    /* Press Esc to exit */
    switch (key)
    {
    case 27:
        exit ( 0 );
        break;
    }
}

int main(int argc, char** argv)
{
    /* GLUT environment initialization */
    glutInit (&argc, argv);
    /* Display mode initialization */
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    /* define the window size */
    glutInitWindowSize (300, 300);
    /* define the window position */
    glutInitWindowPosition (100, 100);
    /* Display the window, the window title is the name of the execution function */
    glutCreateWindow ( argv [ 0 ] );
    /* Call OpenGL initialization function */
    init ( );
    /* Register OpenGL drawing functions */
    glutDisplayFunc ( display );
    /* Register the response function when the window size changes */
    glutReshapeFunc ( reshape );
    /* Register the keyboard response function */
    glutKeyboardFunc ( keyboard );
    /* Enter the GLUT message loop and start executing the program */
    glutMainLoop( );
    return 0;
}

Makefile:

ALL:
	gcc test.c -lGL -lglut -lGLU -lXmu -Bstatic -Bdyanmic
	./a.out
clean:
	rm *~

Effect picture:



Guess you like

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