Mac 安装GLFW和简单测试
1 安装Cmake,下载GLFW 源代码
2 通过命令行(不要用带界面的工具)--https://blog.csdn.net/pythonandjava/article/details/122777144
# 切换至下载解压后的GLFW源码目录 cd glfw-3.3.6 # 创建 build 文件夹,用于存放配置和编译后的文件 mkdir build # 切换至 build 目录 cd build # 用 cmake 生成相关构建文件(请确保你的 Mac 已经安装了 CommandLineTool 和 CMake) # 如果安装 CMake 后执行下方命令后依旧出现 “zsh: command not found: cmake” ,请先执行下面这条命令后重试 # sudo "/Applications/CMake.app/Contents/bin/cmake-gui" --install cmake .. # 构建并安装GLFW make && sudo make install
3 安装完成
[100%] Built target docs Install the project... -- Install configuration: "" -- Installing: /usr/local/lib/libglfw3.a -- Installing: /usr/local/include/GLFW -- Installing: /usr/local/include/GLFW/glfw3.h -- Installing: /usr/local/include/GLFW/glfw3native.h -- Installing: /usr/local/lib/cmake/glfw3/glfw3Config.cmake -- Installing: /usr/local/lib/cmake/glfw3/glfw3ConfigVersion.cmake -- Installing: /usr/local/lib/cmake/glfw3/glfw3Targets.cmake -- Installing: /usr/local/lib/cmake/glfw3/glfw3Targets-noconfig.cmake -- Installing: /usr/local/lib/pkgconfig/glfw3.pc
4 导入GLad;到官网下载对应的配置的头文件源文件
5demo:(创建Mac OS 控制台程序,缺少的frame work记得添加)记得吧GLFW复制到项目文件夹下,并添加
build setting里边记得导入glad ,flow的头文件路径和库查找路径
main函数:
// // main.cpp // // // System Headers #include <glad/glad.h> #include <GLFW/glfw3.h> // Standard Headers #include <cstdio> #include <cstdlib> #include <iostream> #include <chrono> void framebuffer_size_callback(GLFWwindow* window, int width, int height);//回调函数原型声明 void processInput(GLFWwindow *window); // settings const unsigned int SCR_WIDTH = 800; const unsigned int SCR_HEIGHT = 600; int main(int argc, char * argv[]) { //初始化GLFW glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #ifdef __APPLE__ glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X #endif //创建一个窗口对象 GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "FirstGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } //通知GLFW将我们窗口的上下文设置为当前线程的主上下文 glfwMakeContextCurrent(window); //对窗口注册一个回调函数,每当窗口改变大小,GLFW会调用这个函数并填充相应的参数供你处理 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); //初始化GLAD用来管理OpenGL的函数指针 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } glfwSwapInterval(10); static auto lastpaint = std::chrono::high_resolution_clock::now(); //渲染循环 while(!glfwWindowShouldClose(window)) { // 输入 processInput(window); // 渲染指令 glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); std::cout<<"flush"<<std::endl; // 重点语句 auto now = std::chrono::high_resolution_clock::now(); auto duration = static_cast<std::chrono::duration<double, std::micro>>(now - lastpaint); lastpaint = now; std::cout<< __FUNCTION__ << "micromillion:interval=" << static_cast<long long>(duration.count()); glfwSwapBuffers(window);//检查触发事件 glfwPollEvents(); //交换颜色缓冲 } //释放/删除之前的分配的所有资源 glfwTerminate(); return EXIT_SUCCESS; } //输入控制,检查用户是否按下了返回键(Esc) void processInput(GLFWwindow *window) { if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); } // 当用户改变窗口的大小的时候,视口也应该被调整 void framebuffer_size_callback(GLFWwindow* window, int width, int height) { // 注意:对于视网膜(Retina)显示屏,width和height都会明显比原输入值更高一点。 glViewport(0, 0, width, height); }
参考:https://blog.csdn.net/lm409/article/details/78420044