OpenGL on OS X - shawfdong/hyades GitHub Wiki
My desktop computer is a Mid 2010 27-inch iMac, with a quad-core Intel Core i7 processor at 2.93 GHz and 8 GB memory. The graphics card is ATI Radeon HD 5750 with 1 GB VRAM. The card supports OpenGL 4.1 and GLSL 4.1.0 in Mac OS X 10.9 (Mavericks)[1].
GLUT (OpenGL Utility Toolkit) is available on OS X through the GLUT framework[2]. Although it is marked as deprecated, it is still useful for demo codes[3].
OS X uses Legacy Profile as default for all created OpenGL contexts. Therefore by default it supports only OpenGL up to 2.1 and GLSL up to 1.20[4]. Here is a skeleton GLUT code:
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdio.h>
int main (int argc, char** argv)
{
const GLubyte* renderer;
const GLubyte* version;
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 100);
glutCreateWindow ("OpenGL");
/* get version info */
renderer = glGetString (GL_RENDERER);
version = glGetString (GL_VERSION);
printf ("Renderer: %s\n", renderer);
printf ("OpenGL version supported: %s\n", version);
/*
init ();
glutDisplayFunc (display);
glutMainLoop ();
*/
return 0;
}
To compile it on Mac OS X, run:
$ clang -framework GLUT -framework OpenGL -framework Cocoa gl21.c -o gl21.x $ otool -L gl21.x gl21.x: /System/Library/Frameworks/GLUT.framework/Versions/A/GLUT (compatibility version 1.0.0, current version 1.0.0) /System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0) /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 20.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
It only loads OpenGL 2.1!
$ ./gl21.x Renderer: ATI Radeon HD 5750 OpenGL Engine OpenGL version supported: 2.1 ATI-1.24.38
To use OpenGL 3.2+, we must switch to the Core Profile, by passing the GLUT_3_2_CORE_PROFILE option to glutInitDisplayMode. For example:
glutInitDisplayMode (GLUT_3_2_CORE_PROFILE | GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
We can verify that it now indeed loads OpenGL 4.1:
$ clang -framework GLUT -framework OpenGL -framework Cocoa gl41.c -o gl41.x $ ./gl41.x Renderer: ATI Radeon HD 5750 OpenGL Engine OpenGL version supported: 4.1 ATI-1.24.38
GLFW is a newer alternative to GLUT. We can get GLFW from MacPorts (we'll install GLEW (OpenGL Extension Wrangler Library) as well):
$ sudo port install glew glfw
Here is a skeleton GLFW code for OS X[5]:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
int main ()
{
GLFWwindow* window = NULL;
const GLubyte* renderer;
const GLubyte* version;
if (!glfwInit ()) {
fprintf (stderr, "ERROR: could not start GLFW3\n");
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, "OpenGL", NULL, NULL);
if (!window) {
fprintf (stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate ();
return 1;
}
glfwMakeContextCurrent (window);
/* start GLEW extension handler */
glewExperimental = GL_TRUE;
glewInit ();
/* get version info */
renderer = glGetString (GL_RENDERER);
version = glGetString (GL_VERSION);
printf ("Renderer: %s\n", renderer);
printf ("OpenGL version supported: %s\n", version);
/* tell GL to only draw onto a pixel if the shape is closer to the viewer */
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LESS);
/* OTHER STUFF GOES HERE NEXT */
/* close GL context and any other GLFW resources */
glfwTerminate ();
return 0;
}
To compile it, run:
$ clang -framework OpenGL -framework Cocoa \ glfw.c -o glfw.x \ -I/opt/local/include -L/opt/local/lib -lGLEW -lglfw
In this article, we only cover using cross-platform toolkits to create and manage OpenGL windows on OS X. The native toolkit for OpenGL window creation on OS X is Cocoa-based NSOpenGL. Comprehensive documentation on NSOpenGL can be found in Apple's OpenGL Programming Guide for Mac.