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 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的基本库即可实现快速安装,具体方法如下:
我们使用终端,并输入以下语句
注:大多数系统均需要执行所有语句,以安装基本库,少数系统仅需要最后一句即可
- 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
测试样例(网上很多,在此给出一个)
编译参数(可写成makefile,在此仅给出终端写法)
- 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 为可执行文件文件名 根据需要修改
注意:请不要随意更变参数书写顺序,否则可能不能编译
源代码:
- #include <GL/glut.h>
- void init();
- void display();
- 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;
- }
- void init()
- {
- 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);
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(1.0, 0, 0);
- glutWireTeapot(3);
- glFlush();
- }
样例输出: