DEV Community

Giovanni Cortés
Giovanni Cortés

Posted on • Originally published at giovanni.codes on

OpenGL Setup in macOS

We have configured SFML in macOS, now we’re going to configure OpenGL with macOS and Xcode.

First, you need to install GLFW

brew install glfw
Enter fullscreen mode Exit fullscreen mode

Then, you need to download GLAD to get the OpenGL headers: Use gl version 4.1 if you want to be compatible with macOS and in Profile select Core. Click in Generate. Download the glad.zip and unzip in your Download folder.

https://glad.dav1d.de

Once you are downloaded and unzipped the folder, put the glad and KHR folders in the /usr/local/include directory of your mac.

Creating the Xcode Project

After installed GLFW and downloading the glad files, is time to create the XCode project.

Create a new Project and macOS and Command Line Tool, then click on Next

Then, put a name to your project and select C++ as language, next and save the project

In the zip that you downloaded, you will have a glad.c file, add that file to your project

We need to configure Xcode to find the libraries that we added, so in your Xcode, select the Project Target and then Build Settings and in Header Search Paths add /usr/local/include

Now, in Framework and Libraries you need to add the libglfw library, so clic on + symbol and add the library that you will find in /usr/local/Cellar/glfw/3.3/lib

Now, in the main.cpp file, copy the follow code to test OpenGL

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);

int main()
{
    GLFWwindow* window;

    // Initialize the library
    if(!glfwInit())
        return -1;

    // Define version and compatibility settings
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    // Create a windowed mode window and its OpenGL context
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    // Mathe the window's context current
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // Initialize the OpenGL API with GLAD
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    // 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;
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width
    // and height will be significantly larger than specified on retina displays
    glViewport(0, 0, width, height);
}
Enter fullscreen mode Exit fullscreen mode

If you are in Catalina and run the project, your are going to see an error, that is because the library has not the permissions to run in your mac, so you will need to sign it to run.

In the terminal copy and paste the command

codesign -f -s "your@email.com" /usr/local/Cellar/glfw/3.3/lib/libglfw.3.3.dylib
Enter fullscreen mode Exit fullscreen mode

Now, run the project again and you should see the window (https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f642.png)

Now you will be able to play with OpenGL 🙂

The post OpenGL Setup in macOS appeared first on Giovanni Codes.

Top comments (0)