OpenGL on a Mac w/SuperBible

This is a straight C++ program that runs on the Mac. It is not an iPhone app.

Source code in

  1. Triangle.cpp
  2. GLTools.h
  3. GLShaderManager.h

Download the GLTools library in two .zip files

Go to the OpenGL SuperBible Home Page. Download the files and into the Downloads subdirectory of your home directory on your Mac and unzip them. contains the include directory for the GLTools library; you can see it at /Users/myname/Downloads/SB5/Src/GLTools/include. contains the library file libGLTools.a itself; the .a stands for “archive”. To see the contents of this file, mangled and demangled, open a Terminal window and say

cd ~/Downloads/XCode/GLTools
ls -l
nm libGLTools.a | more				(mangled; hard to read)
00000000000048b8 s EH_frame1
00000000000001a2 T __ZN7GLBatch15CopyColorData4fEPA4_f
0000000000004978 S
0000000000000108 T __ZN7GLBatch15CopyNormalDatafEPA3_f
0000000000004940 S
000000000000081e T __ZN7GLBatch15MultiTexCoord2fEjff
0000000000004ae0 S
000000000000006e T __ZN7GLBatch16CopyVertexData3fEPA3_f
nm libGLTools.a | c++filt | more		(demangled; the T's are easier to read)
00000000000048b8 s EH_frame1
00000000000001a2 T GLBatch::CopyColorData4f(float (*) [4])
0000000000004978 S
0000000000000108 T GLBatch::CopyNormalDataf(float (*) [3])
0000000000004940 S
000000000000081e T GLBatch::MultiTexCoord2f(unsigned int, float, float)
0000000000004ae0 S
000000000000006e T GLBatch::CopyVertexData3f(float (*) [3])

Create the Xcode project

Do not select iPhone OS Application. Select Mac OS X Application.

File → New Project…
Mac OS X Application
Cocoa Application
Save As: Triangle
Where: Desktop

Copy GLTools into the Triangle folder

Copy the file /Users/myname/Downloads/XCode/GLTools/libGLTools.a into your Triangle folder. You can do this in the Macintosh Finder with copy and paste. Also copy the folder /Users/myname/Downloads/SB5/Src/GLTools/include and all of its contents (including the subdirectory GL) into the Triangle folder.

The include folder in the Triangle folder should be added to the project. Select the include folder and say
Project → Add to Project…

Tell Xcode where to find the header files you just copied:
Project → Edit Active Target "Triangle" → Build → Search Paths
Header Search Paths: include

Add one C++ file to the project

Delete four files from the project, and also move them to the Trash. Select a file and
Edit → Delete

  1. main.m
  2. TriangleAppDelegate.h
  3. TriangleAppDelegate.m
  4. MainMenu.xib

Create the file Triangle.cpp. Do not create a Triangle.h to go with it.

File → New File…
Mac OS X
C and C++
C++ File
File name: Triangle.cpp

Linked libraries

Tell Xcode that libGLTools.a belongs to the project.

Project → Edit Active Target "Triangle" → General → Linked Libraries

You should already have one linked library, Cocoa.framework. Press the plus sign and add OpenGL.framework and GLUT.framework. Press the plus sign a third time, press Add Other…, and add the libGLTools.a you copied into your Triangle folder.

Build and Run

Open the Console in case there are error messages to be seen.

Run → Console
Build and Run

We can also run the executable from the Terminal command line to retrieve the exit status:

echo $?

The triangle is not equilateral. The lower left vertex is halfway between the center of the image and the left edge. The top vertex is halfway between the center of the image and the top edge. Stretch the triangle window and see what happens. Then pull down the Triangle menu and select Quit Triangle.

“Redefined” warnings

Comment out lines 75–82 of the file Triangle/include/GLTools.h to avoid “redefined” warnings. These four macros have already been defined in the file Triangle/include/GL/glew.h.

#ifdef __APPLE__
#define glGenVertexArrays glGenVertexArraysAPPLE
#define glDeleteVertexArrays  glDeleteVertexArraysAPPLE
#define glBindVertexArray       glBindVertexArrayAPPLE
#ifndef OPENGL_ES
#define glGenerateMipmap    glGenerateMipmapEXT


The GLUT functions (OpenGL Utility Toolkit) in the GLUT.framework are needed only on Mac. We’ll get rid of them when we move to iPhone.

  1. gltSetWorkingDirectory belongs to the GLTools library in libGLTools.a. It changes the program’s current directory to Triangle/build/Debug/, in case the program needs resource files.
  2. glutInit initializes the GLUT library, which open up the Macintosh window that displays the picture. The rest of OpenGL knows nothing about Mac windows.
  3. glutInitDisplayMode. The depth buffer stores a depth value (distance from viewer) for very pixel.
  4. glewInit: OpenGL Extension Wrangler Library. Not all drivers offere every extension to OpenGL.
  5. reshape is called once when the program starts, as well as whenever the window is reshaped.
  6. GLUT_SHADER_IDENTITY requests a shader that applies the same color to every fragment.