GLFW - 很遺憾,沒有找到FW的確切含義,Wiki上沒有,GLFW主頁也沒有。猜想F表示for,W表示Windowhtml
一個輕量級的,開源的,跨平臺的library。支持OpenGL及OpenGL ES,用來管理窗口,讀取輸入,處理事件等。由於OpenGL沒有窗口管理的功能,因此不少熱心的人寫了工具來支持這些功能,好比早期的glut,如今的freeglut等。那麼GLFW有何優點呢?glut太老了,最後一個版本仍是90年代的。freeglut徹底兼容glut,算是glut的代替品,功能齊全,可是bug太多。穩定性也很差(不是我說的啊),GLFW應運而生。編程
1. 到這裏下載編譯好的庫文件框架
2. 解壓後直接使用便可,詳見後面配置。ide
若是想提升B格,自行編譯的話,能夠按以下步驟進行。工具
1. 到這裏下載GLFWoop
2. 到這裏下載CMakespa
3. 參考這個頁面進行編譯3d
1. 打開Visual Studio 2012,新建一個Console程序code
2. 右鍵單擊project選擇properties,打開屬性頁面htm
3. 在VC++Directories-Include Directories中寫入glfw的頭文件目錄,我這裏是 ../glfw-3.0.4.bin.WIN32/include
4. 在VC++ Directories-Library Directory中寫入glfw的庫文件目錄,我這裏是 ../glfw-3.0.4.bin.WIN32/lib-msvc110
5. 在Linker - Input - Additional Dependencies中填入glfw3dll.lib
注意:若是使用靜態連接,那麼上面第五步中glfw3dll.lib應該換成glfw3.lib,而且在工程屬性頁面中C/C++ - Code Generation 將 Runtime Library 設置爲 /Mt 或者 /Mtd
配置好環境以後,開始進入正題,總得畫點什麼吧。 渲染三角形幾乎是圖形程序中的Hello world。代碼以下,框架是從glfw首頁copy過來的,繪製三角形的代碼是我加上去的。
#include <GLFW/glfw3.h> int main(void) { GLFWwindow* window; /* Initialize the library */ if (!glfwInit()) return -1; /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); /* Loop until the user closes the window */ while (!glfwWindowShouldClose(window)) { /* Draw a triangle */ glBegin(GL_TRIANGLES); glColor3f(1.0, 0.0, 0.0); // Red glVertex3f(0.0, 1.0, 0.0); glColor3f(0.0, 1.0, 0.0); // Green glVertex3f(-1.0, -1.0, 0.0); glColor3f(0.0, 0.0, 1.0); // Blue glVertex3f(1.0, -1.0, 0.0); glEnd(); /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } glfwTerminate(); return 0; }
代碼很簡單,看註釋便可,無需多解釋。若是一切正常,屏幕上會出現一個窗口,裏面有一個五光十色的三角形。
使用一個庫無非包含三個步驟:
只要這三個步驟正確了,基本不會出問題。
1 error C1083: Cannot open include file: 'GLFW/glfw3.h': No such file or directory
2 IntelliSense: cannot open source file "GLFW/glfw3.h"
3 IntelliSense: identifier "GLFWwindow" is undefined
4 IntelliSense: identifier "window" is undefined
5 IntelliSense: identifier "glfwInit" is undefined
6 IntelliSense: identifier "glfwCreateWindow" is undefined
7 IntelliSense: identifier "NULL" is undefined
8 IntelliSense: identifier "glfwTerminate" is undefined
9 IntelliSense: identifier "glfwMakeContextCurrent" is undefined
10 IntelliSense: identifier "glfwWindowShouldClose" is undefined
11 IntelliSense: identifier "glfwSwapBuffers" is undefined
12 IntelliSense: identifier "glfwPollEvents" is undefined
13 IntelliSense: identifier "glfwTerminate" is undefine
這顯然是頭文件沒有找到,致使下面全部glfw相關的類型都找不到定義。須要正確設置頭文件的路徑,注意只要包含到include目錄一級便可,好比\glfw-3.0.4.bin.WIN32\include
Error 1 error LNK2019: unresolved external symbol _glfwInit referenced in function _main
Error 2 error LNK2019: unresolved external symbol _glfwTerminate referenced in function _main
Error 3 error LNK2019: unresolved external symbol _glfwCreateWindow referenced in function _main
Error 4 error LNK2019: unresolved external symbol _glfwWindowShouldClose referenced in function _main
Error 5 error LNK2019: unresolved external symbol _glfwPollEvents referenced in function _main
Error 6 error LNK2019: unresolved external symbol _glfwMakeContextCurrent referenced in function _main
Error 7 error LNK2019: unresolved external symbol _glfwSwapBuffers referenced in function _main
Error 8 error LNK1120: 7 unresolved externals
這個錯誤發生在連接階段,說明是庫文件(.lib)文件找不到,須要在項目的屬性頁面中設置lib文件,在Visual Studio中右鍵單擊Project,選擇Properties - Configuration Propterties - Linker - Input - Additional Dependencies,單擊右側的下三角進入編輯頁面,將glfw3dll.lib加入其中便可。
注意:gflw提供了不一樣版本的庫文件,若是你使用的是Visual Studio 2012,請使用lib-msvc110下面的庫文件,若是是Visual Studio 2013,那麼則須要使用lib-msvc120下面的庫文件。lib文件有兩個,一個是glfw3.lib,一個是glfw3dll.lib,前者是靜態連接用的(連接此文件後,運行時無需dll文件,可是程序體積會變大),後者是動態連接用的(配合dll使用),不要搞混。
dll缺失,以下。
編譯連接都沒問題,運行時出現這個錯誤,很簡單,使用的是動態連接,可是程序沒有找到對應的dll文件,將glfw3.dll複製到程序所在目錄便可,通常是Visual Studio的Debug或Release目錄。
Error 1 error LNK2005: _free already defined in LIBCMT.lib(free.obj)
Error 2 error LNK2005: _realloc already defined in LIBCMT.lib(realloc.obj)
Error 4 error LNK1169: one or more multiply defined symbols foun
這是因爲VS默認連接了LIBCMT.lib,只要將它禁止便可,在工程屬性頁面選擇Linker - Input - Ignore Specific Library,點擊右側小箭頭進行編輯,加入libcmt.lib便可。
==