This is a straight C++ program that runs on the Mac. It is not an iPhone app.
Go to the OpenGL SuperBible
Home Page.
Download the files
SB5.zip
and
Xcode.zip
into the
Downloads
subdirectory of your home directory on your Mac
and unzip them.
SB5.zip
contains the
include
directory for the GLTools library;
you can see it at
/Users/myname/Downloads/SB5/Src/GLTools/include
Xcode.zip
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 pwd ls -l
nm libGLTools.a | more (mangled; hard to read)
libGLTools.a(GLBatch.o): 00000000000048b8 s EH_frame1 00000000000001a2 T __ZN7GLBatch15CopyColorData4fEPA4_f 0000000000004978 S __ZN7GLBatch15CopyColorData4fEPA4_f.eh 0000000000000108 T __ZN7GLBatch15CopyNormalDatafEPA3_f 0000000000004940 S __ZN7GLBatch15CopyNormalDatafEPA3_f.eh 000000000000081e T __ZN7GLBatch15MultiTexCoord2fEjff 0000000000004ae0 S __ZN7GLBatch15MultiTexCoord2fEjff.eh 000000000000006e T __ZN7GLBatch16CopyVertexData3fEPA3_f etc.
nm libGLTools.a | c++filt | more (demangled; the T's are easier to read)
libGLTools.a(GLBatch.o): 00000000000048b8 s EH_frame1 00000000000001a2 T GLBatch::CopyColorData4f(float (*) [4]) 0000000000004978 S __ZN7GLBatch15CopyColorData4fEPA4_f.eh 0000000000000108 T GLBatch::CopyNormalDataf(float (*) [3]) 0000000000004940 S __ZN7GLBatch15CopyNormalDatafEPA3_f.eh 000000000000081e T GLBatch::MultiTexCoord2f(unsigned int, float, float) 0000000000004ae0 S __ZN7GLBatch15MultiTexCoord2fEjff.eh 000000000000006e T GLBatch::CopyVertexData3f(float (*) [3]) etc.
Do not select iPhone OS Application. Select Mac OS X Application.
File → New Project… Mac OS X Application Cocoa Application Choose… Save As: Triangle Where: Desktop Save
Copy the file
/Users/myname/Downloads/XCode/GLTools/libGLTools.a
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
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…
Add
Add
Tell Xcode where to find the header files you just copied:
Project →
Edit Active Target "Triangle" →
Build →
Search Paths
Header Search Paths: include
Delete four files from the project,
and also move them to the Trash.
Select a file and
Edit → Delete
main.m
TriangleAppDelegate.h
TriangleAppDelegate.m
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 Next File name: Triangle.cpp Finish
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
Triangle
folder.
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:
~/Desktop/Triangle/build/Debug/Triangle.app/Contents/MacOS/Triangle 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.
Comment out lines 75–82 of the file
Triangle/include/GLTools.h
Triangle/include/GL/glew.h
.
#ifdef __APPLE__ #define glGenVertexArrays glGenVertexArraysAPPLE #define glDeleteVertexArrays glDeleteVertexArraysAPPLE #define glBindVertexArray glBindVertexArrayAPPLE #ifndef OPENGL_ES #define glGenerateMipmap glGenerateMipmapEXT #endif #endif
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.
gltSetWorkingDirectory
belongs to the GLTools library in
libGLTools.a
.
It changes the program’s current directory to
Triangle/build/Debug/Triangle.app/Contents/Resources
,glutInit
initializes the GLUT library,
which open up the Macintosh window that displays the picture.
The rest of OpenGL knows nothing about Mac windows.glutInitDisplayMode
.
The depth buffer stores a depth value (distance from viewer)
for very pixel.
glewInit
:
OpenGL Extension Wrangler Library.
Not all drivers offere every extension to OpenGL.reshape
is called once when the program starts,
as well as whenever the window is reshaped.GLUT_SHADER_IDENTITY
requests a shader that applies the same color to every fragment.