OpenGL in Xcode (Apple Silicon)

  1. install homebrew

    1. go to https://brew.sh and download homebrew
    2. do not forget 'Next steps'
  2. brew install glfw

  3. add glfw to the path of xode project

    1. find glfw location via brew info glfw

    2. add glfw include path to Xcode

    1

    1. add binary path to Xcode

      Add OpenGL framework

      2

      3

      Add glfw binary.

      4

      5

      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.

  4. 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
    #include <GLFW/glfw3.h>

    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:

    1. 'glClear' is deprecated: first deprecated in macOS 10.14

      Don't worry. We can silence this warning via #define GL_SILENCE_DEPRECATION.

    2. OpenGL is deprecated. Consider migrating to Metal instead. No, I refuse to use Metal, just ignore it.

  5. You may also want to use glad.

    1. generate files in https://glad.dav1d.de

      You may choose the latest version of gl.

      6

    2. add glad path to Xcode (similar with glfw) and copy glad.c to your project file.

    3. try to import glad via #include <glad/glad.h>.

      1. 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
        #include <glad/glad.h>
        #include <GLFW/glfw3.h>
        #include <iostream>
        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;
        }
      2. You may get a bunch of documentation issue. You can choose to disable it on your own will.

      7