环境搭建
=================================================================================================================
建立基本编译环境
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 Utility Toolkit
OpenGL Utility Toolkit 是建立在 OpenGL Utilities 上面的工具箱,除了强化了 OpenGL Utilities 的不足之外,也增加了 OpenGL 对于视窗界面的支持。
sudo apt-get install freeglut3-dev
=================================================================================================================
编译参数设定
命令行编译:
gcc example.c -o example -lglut -lGL -lGLU
Makefile文件内容如下:
CC = cc
LDLIBS = -lglut -lGL -lGLU
.c:
$(CC) $@.c $(LDLIBS) -o $@
执行 make example将生成二进制文件example,运行./example.
例程源码
#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 display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0, 0);
glutWireTeapot(3);
glFlush();
}
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);
}
效果图