ubuntu16下openGL测试环境搭建

一、首先安装OpenGl依赖库及工具:

sudo  apt-get  install  libgl1-mesa-dev  libglu1-mesa-dev  freeglut3-dev


二、编写测试实例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 myDisplay(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 myIdle(void)  
{  
    ++angle;  
    if (angle >= 3600.0f)  
    {  
        angle = 0.0f;  
    }  
    myDisplay();  
}  
  
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(&myDisplay);  
    glutIdleFunc(&myIdle);     //空闲调用  
  
    glutMainLoop();  
  
    return 0;  
}  

三、编译及运行程序

    C++编译器编译生成:$g++ Test.cpp -o test  -l GL -l GLU -l glut

    C编译器编译生成     :$gcc Test.cpp -o test  -l GL -l GLU -l glut

    编译成功会输出名为test的执行文件, 接下来可以执行test文件: $ ./test




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值