一、安装GLEW,GLFW,FreeGLUT(兼容GLUT)
在terminal中输入:
(还没安装Homebrew的先输入:ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)")
brew install glfw
brew install glew
brew install freeglut
安装完毕可以看到路径(配置Xcode的时候要用):
? /usr/local/Cellar/glfw/3.3: 14 files, 497.9KB
? /usr/local/Cellar/glew/2.1.0: 38 files, 3MB
? /usr/local/Cellar/freeglut/3.0.0: 15 files, 921KB
二、Xcode配置
1.在Build Setting中设置Header Search Paths和Library Search Paths
2.在Build Phases里添加库文件
点加号可以直接找到OpenGL.framework,另外两个分别去glew和glfw安装目录下拖过来就可以了。
三、测试
#include <stdio.h>
#include <glew.h>
#include <glfw3.h>
int main(int argc, const char * argv[]) {
GLFWwindow *window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello OpenGL", 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)) {
/* Render here */
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;
}
运行效果:
个人总结:
折腾了两小时终于配好了,一开始找到的教程不是链接失效就是跟着做完最后报一堆错,最有用的三篇链接附在参考文章里。多找教程,关注不同点。比如Header Search Paths和Library Search Paths是否都写好。
附注:
若还要用到glad库,则去https://glad.dav1d.de生成库文件,具体操作如下:
打开GLAD的在线服务,将语言(Language)设置为C/C++,在API选项中,选择3.3以上的OpenGL(gl)版本。之后将模式(Profile)设置为Core,并且保证生成加载器(Generate a loader)的选项是选中的。都选择完之后,点击生成(Generate)按钮来生成库文件,下载zip文件。
原文链接:https://blog.csdn.net/qq_38130710/article/details/80898543
配置Xcode时需要在Header Search Paths中添加glad库include文件夹的位置,且将glad.c拖入左侧工程中和main.cpp放在一起。
参考文章
https://www.cnblogs.com/leojason/p/9619193.html