OpenGL on macOS
OpenGL was deprecated in macOS 10.14 which means that Apple will no longer develop newer versions of OpenGL for macOS. However, you can still run OpenGL 4 natively on higher versions such as macOS 10.15.
Note: I am running macOS 10.15.6 on a 2017 13-inch MacBook Pro, which has an Intel Iris Integrated Graphics Card.
Setup
- Install Xcode. I’m not sure if this step is absolutely essential but Xcode is one of those apps which every developer should have since it also installs many essential coding libraries and frameworks.
- Install GLEW and GLFW using Homebrew. If you already have Homebrew installed (it’s the best package manager for Mac),
just run
brew install glfw3
andbrew install glew
The official GLEW website states: “GLEW provides efficient run-time mechanisms for determining which OpenGL extensions are supported on the target platform”. In other words, it’s a useful extension for developing OpenGL programs.
On the other hand, GLFW is an extension for managing windows. OpenGL is a bare-bones library which does not concern itself with the creation and management of windows. That job is made easy by GLFW.
Sample Code
Let’s consider a simple program which prints the OpenGL version and the Integrated Graphics Card which comes with my Mac. The following code can be found here.
//File: main.cpp
#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library for window management
#include <iostream> //for cout
int main (int argc, char** argv)
{
// start GL context and O/S window using the GLFW helper library
if (!glfwInit ())
{
std::cerr<<"ERROR: could not start GLFW3"<<std::endl;
return 1;
}
//Setting window properties
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);
//Actually create the window
GLFWwindow* window = glfwCreateWindow (640, 480, "OpenGL Initialization Example", NULL, NULL);
if (!window)
{
std::cerr<<"ERROR: could not open window with GLFW3"<<std::endl;
glfwTerminate();
return 1;
}
glfwMakeContextCurrent (window);
// start GLEW extension handler
glewExperimental = GL_TRUE;
glewInit ();
// get version info
const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
const GLubyte* version = glGetString (GL_VERSION); // version as a string
std::cout<<"Renderer: "<<renderer<<std::endl;
std::cout<<"OpenGL version supported "<<version<<std::endl;
// close GL context and any other GLFW resources
glfwTerminate();
return 0;
}
Compilation
It’s time to compile the program! We can use g++:
g++ main.cpp -o test.o -lglfw -lGLEW -framework OpenGL
Let us break this down:
- g++ is the compiler. No surprises here.
- main.cpp is the file which contains the code (e.g. the above code) and is to be compiled
- -o test.o specifies that the output executable should be named test.o
- -lglfw and -lGLEW links the libraries GLFW and GLEW (which we installed via brew)
- -framework OpenGL is the important flag which links the actual OpenGL framework to our code
Output
On running test.o, the following gets printed in the Terminal:
Renderer: Renderer: Intel(R) Iris(TM) Plus Graphics 650
OpenGL version supported 4.1 INTEL-14.7.8