Firstly, to check which version of OpenGL your Mac supports, visit this link: https://developer.apple.com/opengl/capabilities/
In this tutorial, we will be using an OpenGL library called GLFW. For this, we will be requiring Homebrew.
To install Homebrew, simply run the command on your terminal.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Next, we need to download GLFW3. To do this, run the command on your terminal.
brew install glfw3
Now, GLFW3 has been successfully installed on our machine. An example code that runs OpenGL 3 context is shown below. Let this be called main.cpp
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
GLFWwindow* window;
if(!glfwInit())
return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if(!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
cout<<glGetString(GL_VERSION)<<endl;
while(!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
To compile the code, run
g++ main.cpp -framework OpenGL -lglfw3 -I/usr/local/include/
And, to run the program, simply run
./a.out
We will get an output similar to
4.1 INTEL-10.0.86
The main magic in the code is done by calling the glfwWindowHint function with the specific arguments.