Ubuntu16.04下第一个openGL程序

1.安装Mesa库
sudo apt-get install libgl1-mesa-dev
2.安装glut库
sudo apt-get install freeglut3-dev
3安装mesa-util
sudo apt-get install mesa-util
4编译C/CPP程序
gcc opengl1.cpp -o test -lGL -lGLU -lglut

测试所用程序源码

/*
 *  As a first example of using OpenGL in C, this program draws the
 *  classic red/green/blue triangle.  It uses the default OpenGL
 *  coordinate system, in which x, y, and z are limited to the range
 *  -1 to 1, and the positive z-axis points into the screen.  Note
 *  that this coordinate system is hardly ever used in practice.
 *
 *  When compiling this program, you must link it to the OpenGL library
 *  and to the glut library. For example, in Linux using the gcc compiler,
 *  it can be compiled with the command:
 *
 *          gcc -o first-triangle first-triangle.c -lGL -lglut
 */

#include <GL/gl.h>
#include <GL/glut.h>   // freeglut.h might be a better alternative, if available.

void display() {  // Display function will draw the image.

    glClearColor( 0, 0, 0, 1 );  // (In fact, this is the default.)
    glClear( GL_COLOR_BUFFER_BIT );

    glBegin(GL_TRIANGLES);
    glColor3f( 1, 0, 0 ); // red
    glVertex2f( -0.8, -0.8 );
    glColor3f( 0, 1, 0 ); // green
    glVertex2f( 0.8, -0.8 );
    glColor3f( 0, 0, 1 ); // blue
    glVertex2f( 0, 0.9 );
    glEnd();

    glutSwapBuffers(); // Required to copy color buffer onto the screen.

}


int main( int argc, char** argv ) {  // Initialize GLUT and

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);    // Use single color buffer and no depth buffer.
    glutInitWindowSize(500,500);         // Size of display area, in pixels.
    glutInitWindowPosition(100,100);     // Location of window in screen coordinates.
    glutCreateWindow("GL RGB Triangle Setup Test"); // Parameter is window title.
    glutDisplayFunc(display);            // Called when the window needs to be redrawn.

    glutMainLoop(); // Run the event loop!  This function does not return.
                    // Program ends when user closes the window.
    return 0;

}

测试程序2

//Test.cpp
#include <GL/glut.h>
 #define ColoredVertex(c, v) do{ glColor3fv(c); glVertex3fv(v); }while(0)
static int angle = 0;
static int rotateMode = 0; 
void myDisplay16(void)
{
    static int list = 0;
    if (list == 0)
    {
            GLfloat
            PointA[] = { 0.5f, 0.5f, -0.5f },
            PointB[] = { 0.5f, -0.5f, -0.5f },
            PointC[] = { -0.5f, -0.5f, -0.5f },
            PointD[] = { -0.5f, 0.5f, -0.5f },
            PointE[] = { 0.5f, 0.5f, 0.5f },
            PointF[] = { 0.5f, -0.5f, 0.5f },
            PointG[] = { -0.5f, -0.5f, 0.5f },
            PointH[] = { -0.5f, 0.5f, 0.5f };

        GLfloat
            ColorA[] = { 1, 0, 0 },
            ColorB[] = { 0, 1, 0 },
            ColorC[] = { 0, 0, 1 },
            ColorD[] = { 1, 1, 0 },
            ColorE[] = { 1, 0, 1 },
            ColorF[] = { 0, 1, 1 },
            ColorG[] = { 1, 1, 1 },
            ColorH[] = { 0, 0, 0 }; 

        list = glGenLists(1);
        glNewList(list, GL_COMPILE);

        // 面1
        glBegin(GL_POLYGON);
        ColoredVertex(ColorA, PointA);
        ColoredVertex(ColorE, PointE);
        ColoredVertex(ColorH, PointH);
        ColoredVertex(ColorD, PointD);
        glEnd();    
        // 面2
        glBegin(GL_POLYGON);
        ColoredVertex(ColorD, PointD);
        ColoredVertex(ColorC, PointC);
        ColoredVertex(ColorB, PointB);
        ColoredVertex(ColorA, PointA);
        glEnd();
        // 面3
        glBegin(GL_POLYGON);
        ColoredVertex(ColorA, PointA);
        ColoredVertex(ColorB, PointB);
        ColoredVertex(ColorF, PointF);
        ColoredVertex(ColorE, PointE);
        glEnd();
        // 面4
        glBegin(GL_POLYGON);
        ColoredVertex(ColorE, PointE);
        ColoredVertex(ColorH, PointH);
        ColoredVertex(ColorG, PointG);
        ColoredVertex(ColorF, PointF);
        glEnd();
        // 面5
        glBegin(GL_POLYGON);
        ColoredVertex(ColorF, PointF);
        ColoredVertex(ColorB, PointB);
        ColoredVertex(ColorC, PointC);
        ColoredVertex(ColorG, PointG);
        glEnd();
        // 面6
        glBegin(GL_POLYGON);
        ColoredVertex(ColorG, PointG);
        ColoredVertex(ColorH, PointH);
        ColoredVertex(ColorD, PointD);
        ColoredVertex(ColorC, PointC);
        glEnd();
        glEndList();
        glEnable(GL_DEPTH_TEST);

    }
    // 已经创建了显示列表,在每次绘制正四面体时将调用它

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(angle / 10, 1, 0.5, 0.0);
    glCallList(list);
    glPopMatrix();
    glutSwapBuffers();

} 

void myIdle6(void)
{
    ++angle;
    if (angle >= 3600.0f)
    {
        angle = 0.0f;
    }
    myDisplay16();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(700, 700);
    glutCreateWindow("First OpenGL Program");
    glutDisplayFunc(&myDisplay16);
    glutIdleFunc(&myIdle6);     //空闲调用
    glutMainLoop(); 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ambercctv/article/details/82390477
今日推荐