因爲工做項目須要開始接觸 OPENGL ,進行圖像處理與可視化方面軟件的開發。以前徹底沒有學習過相關知識,如今才知道原來有些 3D 圖像是經過 OpenGL/Direct3D 等編程接口來作的。html
市面上最好的兩本 OpenGL 的書應該是《 OpenGL 編程指南》(紅寶書)和《 OpenGL 編程寶典》(藍寶書)。有機會的話再系統地學習研究。linux
首先咱們須要配置環境,安裝 OPENGL 及相關的開發包android
~$ sudo apt-get install build-essential \ libgl1-mesa-dev \ freeglut3-dev \ libglew-dev \ libsdl2-dev \ libsdl2-image-dev \ libglm-dev \ libfreetype6-dev
先找個例子實驗一下c++
1 // @filename: test.cc 2 // 該程序利用GLUT繪製一個OpenGL窗口 3 // 同時顯示一個添加光照的球 4 // 頭文件glut.h中已經包含了gl.h和glu.h 5 // 這裏只須要包含glut.h便可 6 #include <stdlib.h> 7 #include <GL/glut.h> 8 9 // 初始化材料屬性/光源屬性/光照模型,並打開深度緩衝區 10 void init(void) { 11 GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0}; 12 GLfloat mat_shininess[] = {50.0}; 13 GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; 14 15 glClearColor(0.0, 0.0, 0.0, 0.0); 16 glShadeModel(GL_SMOOTH); 17 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); 18 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); 19 glLightfv(GL_LIGHT0, GL_POSITION, light_position); 20 21 glEnable(GL_LIGHTING); 22 glEnable(GL_LIGHT0); 23 glEnable(GL_DEPTH_TEST); 24 } 25 26 // 調用GLUT函數,繪製一個球 27 void display(void) { 28 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 29 glutSolidSphere(1.0, 40, 50); 30 glFlush(); 31 } 32 33 int main(int argc, char **argv) { 34 // GLUT環境初始化 35 glutInit(&argc, argv); 36 37 // 顯示模式初始化 38 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); 39 40 // 定義窗口大小 41 glutInitWindowSize(300, 300); 42 43 // 定義窗口位置 44 glutInitWindowPosition(100, 100); 45 46 // 顯示窗口,窗口標題爲執行函數名 47 glutCreateWindow(argv[0]); 48 49 // 調用OpenGL初始化函數 50 init(); 51 52 // 註冊OpenGL繪圖函數 53 glutDisplayFunc(display); 54 55 // 進入GLUT消息循環,開始執行程序 56 glutMainLoop(); 57 58 return 0; 59 }
文件結構展現以下express
playground ├── build ├── CMakeLists.txt ├── Readme.md └── src └── test.cc
其中 CMakeLists.txt 內容以下所示編程
1 cmake_minimum_required( VERSION 2.8 ) 2 3 project( mytest ) 4 5 find_package( GLUT REQUIRED ) 6 include_directories( ${GLUT_INCLUDE_DIRS} ) 7 link_directories( ${GLUT_LIBRARY_DIRS} ) 8 add_definitions( ${GLUT_DEFINITIONS} ) 9 if ( NOT GLUT_FOUND ) 10 message( ERROR "glut not found!!" ) 11 endif ( NOT GLUT_FOUND) 12 13 find_package( OpenGL REQUIRED ) 14 include_directories( ${OpenGL_INCLUDE_DIRS} ) 15 link_directories( ${OpenGL_LIBRARY_DIRS} ) 16 add_definitions( ${OpenGL_DEFINITIONS} ) 17 if (NOT OPENGL_FOUND) 18 message( ERROR "opengl not found!!" ) 19 endif (NOT OPENGL_FOUND) 20 21 add_executable( mytest src/test.cc ) 22 target_link_libraries( mytest ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
而後命令行進行 cmake 時出現了一點問題ide
~/playgroud/build$ cmake .. -- The C compiler identification is GNU 5.4.0 -- The CXX compiler identification is GNU 5.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found GLUT: /usr/lib/x86_64-linux-gnu/libglut.so -- Found OpenGL: /usr/lib/x86_64-linux-gnu/libGL.so CMake Error: The following variables are used in this project, but they are set to NOTFOUND. Please set them or make sure they are set and tested correctly in the CMake files: GLUT_Xmu_LIBRARY (ADVANCED) linked by target "mytest" in directory /home/cv/playgroud -- Configuring incomplete, errors occurred! See also "/home/cv/playgroud/build/CMakeFiles/CMakeOutput.log".
大概搜了 cmake 中 target_link_libraries 的用法函數
TARGET_LINK_LIBRARIES( 設置要連接的庫文件的名稱 )
好比鏈接libhello.so庫,如下寫法均可以:
TARGET_LINK_LIBRARIES(myProject hello)
TARGET_LINK_LIBRARIES(myProject libhello.a)
TARGET_LINK_LIBRARIES(myProject libhello.so)
甚至下面這樣的寫法也是有效的:
TARGET_LINK_LIBRARIES(myProject TARGET_LINK_LIBRARIES(myProject -leng)
後來找到一個相似的問題,並根據其解決方案最終解決了本身的問題oop
// 問題:找不到xi和xmu庫 // 具體錯誤信息 CMake Error: The following variables are used in this project, but they are set to NOTFOUND. Please set them or make sure they are set and tested correctly in the CMake files: GLUT_Xi_LIBRARY (ADVANCED) linked by target "WindowsTest" in directory /home/me/devl/c++/game/2DGame/src GLUT_Xmu_LIBRARY (ADVANCED) linked by target "WindowsTest" in directory /home/me/devl/c++/game/2DGame/src // 解決方法 sudo apt-get install libxmu-dev libxi-dev
而後再進行 cmake 和 make,順利經過編譯,運行生成的可執行文件獲得了預期結果。學習
另外要注意一下, CMakeLists.txt 中三個 mytest 要保持一致,均應爲最終生成的可執行文件的名稱,不能使用 test 的名稱,由於會出現下面的問題,改一下就沒問題了。
CMake Warning (dev) at CMakeLists.txt:21 (add_executable): Policy CMP0037 is not set: Target names should not be reserved and should match a validity pattern. Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy command to set the policy and suppress this warning. The target name "test" is reserved or not valid for certain CMake features, such as generator expressions, and may result in undefined behavior. This warning is for project developers. Use -Wno-dev to suppress it.
Reference
[1] OpenGL學習之路(一)
[3] 使用CMakeLists.txt建立一個簡單的opengl程序
[4] CMake使用總結(一)