ubuntu16下openGL测试环境搭建

前端之家收集整理的这篇文章主要介绍了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,PointC[] = { -0.5f,PointD[] = { -0.5f,PointE[] = { 0.5f,0.5f },PointF[] = { 0.5f,PointG[] = { -0.5f,PointH[] = { -0.5f,0.5f };
    GLfloat
      ColorA[] = { 1,0 },ColorB[] = { 0,1,ColorC[] = { 0,1 },ColorD[] = { 1,ColorE[] = { 1,ColorF[] = { 0,ColorG[] = { 1,ColorH[] = { 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,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,PointC);
    glEnd();
    glEndList();

    glEnable(GL_DEPTH_TEST);
  }
  
  // 已经创建了显示列表,在每次绘制正四面体时将调用它
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  glRotatef(angle / 10,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

猜你在找的Ubuntu相关文章