20210805Mac安装Opengl库

Aug 5, 2021

Mac 安装Opengl库

之前想着搞下编译原理,于是过了下BNF/AST,但是发现那篇文章对于初学cpp不友好,更重要,由于很多库还得下载,不晓得什么库缺失或者include路径不对,无法运行,遂暂时放弃一捏捏。

跟着下篇文章的也是入门cpp+opengl游戏,研发机器装好opengl环境,就成功运行,这就对了嘛。

昨天给研发的机器和家里的机器装上两遍glfw、glew、glad等,踩的小坑记下。

用brew安装都成功,用cmake安装,依然无法include,故本记录推荐brew 安装。

环境: 10.13.6黑苹果,都有独显。

  1. brew报No formulae found in taps错误,方法是重新安装brew解决该问题:
1
2
3
$ cd /usr/local/Homebrew/Library/Taps/homebrew/
$ rm -rf homebrew-core
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  1. brew 安装glfw/glew 时报: Missing dependencies for SOCKS support错误,解决该问题:

    unset all_proxy && unset ALL_PROXY

安装各库

  1. 安装glew/glfw3
1
2
$ brew install glew
$ brew install glfw3
  1. glad安装:

glad在线安装, Language选择C/C++, Specification选择OpenGL,API选择gl - Version4.5(>Version 3.3),Profile选择Core

左下角Options 勾选Generate a loader,然后右下角按钮[GENERATE]生成,然后下载新加载的页面的glad.zip.

  1. 将解压glad.zip 中include 内的 KHR|glad 两个文件夹复制到 /usr/local/include 中.

  2. 新建Xcode C++的Command Line Tool项目,command + , 进入设置页面,选择Locations - Custom Paths,添加如下图:

  1. 修改Header search Paths 和 Library Search Paths

learnopgl - TARGETS - Build Settings

  1. 修改Xcode项目的Build Phases - Link Binary With Libraries

参考

  1. 添加glad.zip 解压后的src/glad.c 至learnopgl项目下:

测试是否正常执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://github.com/JoeyDeVries/LearnOpenGL
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main() {
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);
#endif
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
while (!glfwWindowShouldClose(window)) {
processInput(window);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
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) {
glViewport(0, 0, width, height);
}

出现大黑框就正确了: