OpenGL ES on iPhone w/Superbible

This iPhone app draws the triangle that we drew on the Mac here.

Source code in Triangle.zip

  1. Triangle.cpp
  2. main.m
  3. Class TriangleAppDelegate
  4. Class TriangleViewController
  5. Class EAGLView
  6. include
    1. GLBatch.h
    2. GLBatchBase.h
    3. GLFrame.h
    4. GLFrustum.h
    5. GLGeometryTransform.h
    6. GLMatrixStack.h
    7. GLShaderManager.h
    8. GLTools.h
    9. GLTriangleBatch.h
    10. math3d.h
    11. StopWatch.h
  7. GLTools
    1. math3d.cpp
    2. GLTriangleBatch.cpp
    3. GLTools.cpp
    4. GLShaderManager.cpp
    5. GLBatch.cpp

Create the project

  1. File → New Project…
    Choose a template for your new project:
    iOS Application
    OpenGL ES Application
    Choose…
    Save As: Triangle
    Where: Desktop
    Save

    Do not delete the MainWindow.xib file from the project. Do not delete the “Main nib file base name” line from the Info.plist file. If you run the app, you should see the bouncing square.

  2. Add a new C++ file to the project. Highlight the Other Sources folder in Xcode’s Groups & Files. Do not create a corresponding .h file.
    File → New File…
    Choose a template for your new file:
    C and C++ (under Mac OS X)
    C++ file
    Next
    File Name: Triangle.cpp
    Do not create Triangle.h
    Finish

  3. The Triangle.cpp file should contain this. It is the same as the Triangle.cpp in the project we ran on the Mac, with two deletions:
    1. Remove the main function from Triangle.cpp. Our app will use the app’s main function in main.m.
    2. The GLUT library (OpenGL Utility Toolkit) is not needed for iPhone, only for Mac. Do not include glut.h in Triangle.cpp and do not call glutSwapBuffers.

  4. The methods of classes TriangleViewController and EAGLView will call the three C++ functions defined in Triangle.cpp. To make it possible to call C++ functions, rename TriangleViewController.m to TriangleViewController.mm, and rename EAGLView.m. to EAGLView.mm. Right-click on TriangleViewController.m in the Classes folder in Groups & Files Rename. Also update the filename comment at the top of TriangleViewController.mm. Do the same for EAGLView.m.

  5. Insert the following function declaration into TriangleViewController.mm before the @interface.
    //C++ function defined in Triangle.cpp
    void display();
    
    In the drawFrame method of the view controller, comment out everything except the calls to setFrameBuffer at the top and presentFramebuffer at the bottom. Then call
    	display();
    
    immediately before the presentFramebuffer.

  6. Insert the following function declarations into EAGLView.mm before the @interface.
    //C++ functions defined in Triangle.cpp
    void reshape(int w, int h);
    void SetupRC();
    
    Insert
    		SetupRC();
    
    after the call to glFramebufferRenderbuffer in the createFrameBuffer method of class EAGLView. In the setFramebuffer method of class EAGLView, replace the call to glViewPort with the following.
    	//glViewport(0, 0, framebufferWidth, framebufferHeight);
    	reshape(framebufferWidth, framebufferHeight);
    

  7. Go to the Superbible home page. Under Da Code, and download the files SB5.zip and XCode.zip into the Downloads folder on your Mac. They should unzip into two folders named SB5 and Xcode in your Downloads folder.

  8. Copy the file Xcode/GLTools/libGLTools.a that you just downloaded into the root directory of your project. Then add it to your project. Highlight the Frameworks folder in Groups & Files.
    Project → Edit Active Target "Triangle" → General → Linked Libraries "Triangle"
    Press the plus sign.
    Add Other…
    Add libGLTools.a.

  9. Copy the five C++ files SB5/Src/GLTools/src/GLBatch.cpp, GLShaderManager.cpp, GLTools.cpp, GLTriangleManager.cpp, and math3.cpp that you just downloaded into the root directory of your project. Let’s create a “Group” to hold them. In the left pane of Xcode, right-click on the name of the project (Triangle) and select Add → New Group. Name the new group GLTools. Although it is not a folder, the group will appear as folder similar to the existing things that look like folders: Other Sources, Resources, etc. Highlight the new group and add the five C++ files to the project.

  10. Create a folder named include in the root folder of your project.
    Project → Edit Active Target "Triangle" → Build → Search Paths
    Set the Header Search Paths to include. Then copy the 11 header files SB5/Src/GLTools/include/GLBatch.h, GLBatchBase.h, GLFrame.h, GLFrustum.h, GLGeometryTransform.h, GLMatrixStack.h, GLShaderManager.h, GLTools.h, GLTriangleBatch.h, math3d.h, DLStopWatch.3dh that you downloaded into the include folder you created. Add the include folder to your project. Then add the 11 files in it to your project too.

  11. Project → Edit Project Settings → Build → Search Paths → Header Search Paths
    include