SphereWorld

This program makes its first appearance on p. 161 of the OpenGL SuperBible. It aquires a camera on p. 171, textures on p. 226, and a logo on p. 291. It is ported to Windows on p. 559, to OS X on p. 580, and to iPhone on pp. 639–652.

Source code in GL.zip

  1. SphereWorld.cpp creates the torus, spheres, and floor.
  2. Files created by the OpenGL ES template in Xcode. They require only slight modification by hand.
    1. main.mm
    2. Class GLAppDelegate
    3. Class GLViewController
    4. Class EAGLView
  3. Texture files
    1. Marble.tga
    2. Marslike.tga
    3. MoonLike.tga

Create the project

  1. Create the project using the OpenGL ES Application template. Let the fourth argument of UIApplicationMain in the main function remain nil. Do not remove MainWindow.xib from the application or from the Info.plist file.

  2. Create a new group. In the Groups & Files pane of Xcode, right-click on the name of the project (the first line) and select
    Add → New Group. Name the new group GLTools. It isn’t a folder, but it looks like one.

  3. Download SB5.zip and Xcode.zip from the OpenGL SuperBible home page into the Downloads folder on your Mac. In SB5.zip go to the folder Src/GLTools/src and copy all the files except glew.c into the root folder of your project. (There should be five files.) Then go to the folder Src/GLTools/include and copy all the .h files into the root directory of your project. (There should be 11 files.) In the Groups & Files pane of Xcode, select the GLTools group you created and add these 16 files to the project.

  4. In SB5.zip, go to the folder Src/Chapter05/SphereWorld. Copy the three texture files into the root directory of your project. In the Groups & Files pane of Xcode, select the Resources group and add these three files to the project.

  5. In SB5.zip, go to the folder Src/Chapter05/SphereWorld. Copy the file SphereWorld.cpp into the root directory of your project. In the Groups & Files pane of Xcode, select the Other Sources group and add this file to the project.

  6. Project → Edit Active Target "GL" → Build → Search Paths
    Set the Header Search Paths to dot. Dot means the current directory.

  7. In SB5.zip, go to the folder GLTools/GLTools. Copy the file libGLTools.a into the root directory of your project.

  8. Project → Edit Active Target "GL" → General → Linked Libraries
    Press the plus sign and add the OpenGLES.framework to the project. Press the plus sign again and Add Other… to add to the project the libGLTools.a you copied into the root folder of your project.

  9. The three .m files that call functions written in C++ have to be renamed to .mm: main.mm, GLViewController.mm, and EAGLView.mm. In the Groups & Files pane of Xcode, right-click on the filename and select Rename. Update the filename at the top of each file.

  10. main.mm includes GLTools.h and the main function calls gltSetWorkingDirectory.

  11. The function RenderScene is declared at the top of GLViewController.mm and called in drawFrame. Almost all of drawFrame is commented out.

  12. The functions SetupRC, ChangeSize, and ShutdownRC are declared at the top of EAGLView.mm. SetupRC and ChangeSize are called in createFramebuffer; ShutdownRC is called in deleteFrameffer.

  13. SphereWorld.cpp does not use the GLUT library. Comment out the two lines that include glut.h. Comment out the calls to the functions glewInit, glutSwapBuffers, and glutPostRedisplay. Comment out the entire functions SpecialKeys and main.

  14. In the LoadTGATexture function in SphereWorld.cpp, change the macro GL_COMPRESSED_RGB to nComponents.

  15. The framebuffer in EAGLView already contains a renderbuffer. Let’s also give it a depth buffer. Add the following instance variable to class EAGLView in EAGLView.h.
    	GLuint depthRenderbuffer;
    
    Insert the following code into the createFramebuffer method in EAGLView.mm.
    	//Create the depth buffer.
    
    	glGenRenderbuffers(1, &depthRenderbuffer);
    	glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
    	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16,
    		framebufferWidth, framebufferHeight);
    	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
    		depthRenderbuffer);
    
    Insert the following code into the deleteFramebuffer method in EAGLView.mm. (I’ll get around to doing this.)
    	if (depthRenderbuffer)
            {
                glDeleteRenderbuffers(1, &depthRenderbuffer);
                depthRenderbuffer = 0;
            }
    

  16. [Optional.] To make the app touch-sensitive, add the touchesMoved:withEvent: method to class EAGLView in EAGLView.mm.

  17. [Optional.] To make the app landscape, increase the vertical field of view from 35° to 60° in the ChangeSize function in SphereWorld.cpp, and add the 90° rotation.

Run the app

Swipe left and right, up and down—slowly.