OpenGL in Xcode (Apple Silicon)
install
homebrew
- go to
https://brew.sh
and downloadhomebrew
- do not forget 'Next steps'
- go to
brew install glfw
add
glfw
to the path of xode projectfind
glfw
location viabrew info glfw
add
glfw
include path to Xcode
add binary path to Xcode
Add OpenGL framework
Add glfw binary.
Find the path to libglfw lib (it is inside the folde of glfw). By default, the path to it is hidden. Show hidden file via
shift+command+.
in finder.
test (code from glfw documentation):
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
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "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))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}Your code will be built and run successfully. But there will be 2 issues:
'glClear' is deprecated: first deprecated in macOS 10.14
Don't worry. We can silence this warning via
#define GL_SILENCE_DEPRECATION
.OpenGL is deprecated. Consider migrating to Metal instead.
No, I refuse to useMetal
, just ignore it.
You may also want to use
glad
.generate files in
https://glad.dav1d.de
You may choose the latest version of
gl
.add
glad
path to Xcode (similar withglfw
) and copyglad.c
to your project file.try to import
glad
via#include <glad/glad.h>
.code from learnopengl
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
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader(GLADloadproc(glfwGetProcAddress)))
{
std::cout<<"Failed to initialize GLAD"<<std::endl;
}
glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
while(!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}You may get a bunch of
documentation issue
. You can choose to disable it on your own will.