OpenGL 是一套由SGI公司发展出来的绘图函式库,它是一组 C 语言的函式,用于 2D 与 3D 图形应用程式的开发上。OpenGL 让程式开发人员不需要考虑到各种显示卡底层运作是否相同的问题,硬体由 OpenGL 核心去沟通,因此只要显示卡支援 OpenGL,那么程式就不需要重新再移植,而程式开发人员也不需要重新学习一组函式库来移植程式。


安装


首先不可或缺的就是编译器与基本的函式库,如果系统没有安装的话,依照下面的方式安装:

  • $ sudo apt-get install build-essential

  • 安装OpenGL Library

  • $ sudo apt-get install libgl1-mesa-dev

  • 安装OpenGL Utilities

  • $ sudo apt-get install libglu1-mesa-dev
  •        OpenGL Utilities 是一组建构于 OpenGL Library 之上的工具组,提供许多很方便的函式,使 OpenGL 更强大且更容易使用。

    安装OpenGL Utility Toolkit

  • $ sudo apt-get install libglut-dev
  •        OpenGL Utility Toolkit 是建立在 OpenGL Utilities 上面的工具箱,除了强化了 OpenGL Utilities 的不足之外,也增加了 OpenGL 对于视窗介面支援。
           注意:在这一步的时候,可能会出现以下情况,shell提示:

  • Reading package lists... Done
  • Building dependency tree
  • Reading state information... Done
  • E: Unable to locate package libglut-dev
  • 将上述$ sudo apt-get install libglut-dev命令改成$ sudo apt-get install freeglut3-dev即可。

     

    测试


    示例test.c源码:

  • #include <GL/glut.h>

  • void init(void)
  • {
  •     glClearColor(0.0, 0.0, 0.0, 0.0);
  •     glMatrixMode(GL_PROJECTION);
  •     glOrtho(-5, 5, -5, 5, 5, 15);
  •     glMatrixMode(GL_MODELVIEW);
  •     gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

  •     return;
  • }

  • void display(void)
  • {
  •     glClear(GL_COLOR_BUFFER_BIT);
  •     glColor3f(1.0, 0, 0);
  •     glutWireTeapot(3);
  •     glFlush();

  •     return;
  • }

  • int main(int argc, char *argv[])
  • {
  •     glutInit(&argc, argv);
  •     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  •     glutInitWindowPosition(0, 0);
  •     glutInitWindowSize(300, 300);
  •     glutCreateWindow("OpenGL 3D View");
  •     init();
  •     glutDisplayFunc(display);
  •     glutMainLoop();

  •     return 0;
  • }
  • 编译程式时,执行以下指令:

  • $ gcc -o test test.c -lGL -lGLU -lglut
  • 执行:

  • $ ./test
  • Ubuntu 安装OpenGL

    Ubuntu 13.04下安装 OpenGL过程记录。

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

    测试程序

    #include <GL/glut.h>

    void init(void){
        glClearColor(0.0,0.0,0.0,0.0);
        glMatrixMode(GL_PROJECTION);
        glOrtho(-5,5,-5,5,5,15);
        glMatrixMode(GL_MODELVIEW);
        gluLookAt(0,0,10,0,0,0,0,1,0);
        return;
    }

    void display(void){
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0,0,0);
        glutWireTeapot(3);
        glFlush();
        return;
    }

    int main(int argc,char *argv[]){
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
        glutInitWindowPosition(0,0);
        glutInitWindowSize(300,300);
        glutCreateWindow("OpenGL #D View");
        init();
        glutDisplayFunc(display);
        glutMainLoop();

        return 0;
    }

    编译运行

     

    gcc test.c -o test -lGL -lGLU -lglut

    ./test.c

    ubuntu 14.04对于openGL的支持还是比较充分的,使用freeglut和openGL的基本库即可实现快速安装,具体方法如下:

    我们使用终端,并输入以下语句

    注:大多数系统均需要执行所有语句,以安装基本库,少数系统仅需要最后一句即可

    [plain] view plain copy
    1. sudo apt-get install build-essential   
    2.   
    3. sudo apt-get install libgl1-mesa-dev  
    4.   
    5. sudo apt-get install libglu1-mesa-dev   
    [plain] view plain copy
    1. sudo apt-get install freeglut3-dev  


    测试样例(网上很多,在此给出一个)

    编译参数(可写成makefile,在此仅给出终端写法)

    [html] view plain copy
    1. gcc -I/usr/include -L/usr/local/lib -L/usr/X11R6/lib gltest.c -o gltest -lglut -lGLU -lGL -lX11 -lXext  -lXi -lm   

    gltest.c 为源文件名 gltest 为可执行文件文件名 根据需要修改

    注意:请不要随意更变参数书写顺序,否则可能不能编译

    源代码:

    1. #include <GL/glut.h>  
    2.   
    3. void init();  
    4.   
    5. void display();  
    6.   
    7. int main(int argc, char* argv[])  
    8.   
    9. {  
    10.   
    11.     glutInit(&argc, argv);  
    12.   
    13.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  
    14.   
    15.     glutInitWindowPosition(0, 0);  
    16.   
    17.     glutInitWindowSize(300, 300);  
    18.   
    19.     glutCreateWindow("OpenGL 3D View");  
    20.   
    21.     init();  
    22.     glutDisplayFunc(display);  
    23.   
    24.     glutMainLoop();  
    25.   
    26.     return 0;  
    27.   
    28. }  
    29.   
    30. void init()  
    31.   
    32. {  
    33.   
    34.     glClearColor(0.0, 0.0, 0.0, 0.0);  
    35.   
    36.     glMatrixMode(GL_PROJECTION);  
    37.   
    38.     glOrtho(-5, 5, -5, 5, 5, 15);  
    39.   
    40.     glMatrixMode(GL_MODELVIEW);  
    41.   
    42.     gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);  
    43.   
    44. }  
    45.   
    46. void display()  
    47.   
    48. {  
    49.   
    50.     glClear(GL_COLOR_BUFFER_BIT);  
    51.   
    52.     glColor3f(1.0, 0, 0);  
    53.   
    54.     glutWireTeapot(3);  
    55.   
    56.     glFlush();  
    57.   
    58. }  


    样例输出: Ubuntu 安装OpenGL

     

    相关文章:

    • 2021-11-20
    • 2021-11-20
    • 2021-11-21
    • 2022-12-23
    • 2022-12-23
    • 2022-12-23
    • 2021-11-20
    • 2021-09-26
    猜你喜欢
    • 2022-01-23
    • 2022-12-23
    • 2021-09-10
    • 2021-10-26
    • 2022-12-23
    • 2022-12-23
    • 2021-11-20
    相关资源
    相似解决方案