Ubuntu 安装OpenGL

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


安装


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

  1. $ sudo apt-get install build-essential


安装OpenGL Library

  1. $ sudo apt-get install libgl1-mesa-dev


安装OpenGL Utilities

  1. $ sudo apt-get install libglu1-mesa-dev

       OpenGL Utilities 是一组建构于 OpenGL Library 之上的工具组,提供许多很方便的函式,使 OpenGL 更强大且更容易使用。

安装OpenGL Utility Toolkit

  1. $ sudo apt-get install libglut-dev

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

  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. E: Unable to locate package libglut-dev

将上述$ sudo apt-get install libglut-dev命令改成$ sudo apt-get install freeglut3-dev即可。

 

测试


示例test.c源码:

  1. #include <GL/glut.h>

  2. void init(void)
  3. {
  4.     glClearColor(0.0, 0.0, 0.0, 0.0);
  5.     glMatrixMode(GL_PROJECTION);
  6.     glOrtho(-5, 5, -5, 5, 5, 15);
  7.     glMatrixMode(GL_MODELVIEW);
  8.     gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);

  9.     return;
  10. }

  11. void display(void)
  12. {
  13.     glClear(GL_COLOR_BUFFER_BIT);
  14.     glColor3f(1.0, 0, 0);
  15.     glutWireTeapot(3);
  16.     glFlush();

  17.     return;
  18. }

  19. int main(int argc, char *argv[])
  20. {
  21.     glutInit(&argc, argv);
  22.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  23.     glutInitWindowPosition(0, 0);
  24.     glutInitWindowSize(300, 300);
  25.     glutCreateWindow("OpenGL 3D View");
  26.     init();
  27.     glutDisplayFunc(display);
  28.     glutMainLoop();

  29.     return 0;
  30. }

编译程式时,执行以下指令:

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

执行:

  1. $ ./test

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. }  


样例输出:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值