一、構建GLFW
GLFW 是配合 OpenGL 使用的輕量級工具程序庫,縮寫自 Graphics Library Framework(圖形庫框架)。GLFW 的主要功能是創建並管理窗口和 OpenGL 上下文,同時還提供了處理手柄、鍵盤、鼠標輸入的功能。
下載地址:https://www.glfw.org/download.html
我們可以選擇直接下載編譯好的庫,也可以下載源文件然后通過camke來自己編譯,區別是如果用cmake庫的話,在執行成功以后,會直接將生成的庫與頭文件分別添加到系統路徑,也就是/usr/local/include和/usr/local/lib。
這里我選擇了下載編譯好的庫
二、配置GLAD
同時它還提供了一個在線服務: https://glad.dav1d.de/
打開GLAD在線服務,如圖11所示,我設置語言為C/C++,gl版本選擇4.6:
點擊GENERATE按鈕,GLAD現在應該提供給你了一個zip壓縮文件,包含兩個頭文件目錄,和一個glad.c文件
三、創建XCode項目
3.1 新建XCode項目,如圖13所示,選擇”Command Line Tool“:
3.2 導入第三方庫:
將之前下載的編譯好的GLFW庫和GLAD文件導入項目中
3.3 鏈接第三方庫:
設置libs搜索路徑:
設置頭文件的搜索路徑:
3.4 把glad.c加入到工程項目中:
3.5 示例代碼:
1 #include <glad.h> 2 #include <glfw3.h> 3 4 #include <iostream> 5 6 void framebuffer_size_callback(GLFWwindow* window, int width, int height); 7 void processInput(GLFWwindow *window); 8 9 // settings 10 const unsigned int SCR_WIDTH = 800; 11 const unsigned int SCR_HEIGHT = 600; 12 13 int main() 14 { 15 // glfw: initialize and configure 16 // ------------------------------ 17 glfwInit(); 18 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 19 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 20 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 21 22 #ifdef __APPLE__ 23 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 24 #endif 25 26 // glfw window creation 27 // -------------------- 28 GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); 29 if (window == NULL) 30 { 31 std::cout << "Failed to create GLFW window" << std::endl; 32 glfwTerminate(); 33 return -1; 34 } 35 glfwMakeContextCurrent(window); 36 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); 37 38 // glad: load all OpenGL function pointers 39 // --------------------------------------- 40 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) 41 { 42 std::cout << "Failed to initialize GLAD" << std::endl; 43 return -1; 44 } 45 46 // render loop 47 // ----------- 48 while (!glfwWindowShouldClose(window)) 49 { 50 // input 51 // ----- 52 processInput(window); 53 54 // render 55 // ------ 56 glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 57 glClear(GL_COLOR_BUFFER_BIT); 58 59 // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) 60 // ------------------------------------------------------------------------------- 61 glfwSwapBuffers(window); 62 glfwPollEvents(); 63 } 64 65 // glfw: terminate, clearing all previously allocated GLFW resources. 66 // ------------------------------------------------------------------ 67 glfwTerminate(); 68 return 0; 69 } 70 71 // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly 72 // --------------------------------------------------------------------------------------------------------- 73 void processInput(GLFWwindow *window) 74 { 75 if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) 76 glfwSetWindowShouldClose(window, true); 77 } 78 79 // glfw: whenever the window size changed (by OS or user resize) this callback function executes 80 // --------------------------------------------------------------------------------------------- 81 void framebuffer_size_callback(GLFWwindow* window, int width, int height) 82 { 83 // make sure the viewport matches the new window dimensions; note that width and 84 // height will be significantly larger than specified on retina displays. 85 glViewport(0, 0, width, height); 86 }
3.6 示例代碼的運行結果
四、遇到的問題
按照步驟配置,發現並沒有成功運行,出現如下錯誤:
dyld: Library not loaded: @rpath/libglfw.3.dylib
Referenced from: /Users/a11/Library/Developer/Xcode/DerivedData/GlfwTest-fswltakqdftvrjhcuqcxnrfzjoko/Build/Products/Debug/GlfwTest
Reason: image not found
這個意思是找不到dylib庫圖片(鏡像)了,我們再來確認下這個庫是否鏈接了
發現這個庫文件確實存在,是不是重新導入就好了,結果重新導入沒有成功。
解決辦法:
將出現問題的Framework拷貝到項目里去,步驟如下圖所示:
把出現Reason:image not found的Framework添加進去
添加之后再次運行,成功運行!