本次實踐所使用環境為CentOS 7。
參考:http://www.xuebuyuan.com/1472808.html
OpenGL開發環境搭建:
1.opengl庫安裝
opengl庫使用mesa庫,安裝命令:
yum intall mesa*
mesa庫是一個開源的三維計算機圖形庫,以開源的形式實現了opengl應用程序接口。具體介紹:https://www.mesa3d.org/intro.html。
2.glut安裝
下載freeglut,下載地址為: https://github.com/dcnieho/FreeGLUT/releases。glut是opengl的實用工具庫,opengl只是三維圖形接口,並沒有窗口,處理鼠標等設備輸入輸出方面的內容,glut提供了相關方面的內容。freeglut是glut的一個發行版本(還有一個是原始版本的glut)。
因為我使用cmake安裝,根據README.cmake中步驟及注意事項安裝。
命令:
cmake .
make
make install
3.簡單例子
新建文件main.cpp
#include <GL/glut.h> #include <stdlib.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(); }
編譯命令:
gcc -lglut -lGLU -lGL main.cpp