This document discusses programming for the iPhone using OpenGL ES. It provides an overview of Objective-C and the OpenGL ES pipeline, describes a block game project to gain experience with the OpenGL ES API, and discusses some of the challenges encountered with object selection, game structure, and using Foundation collections.
3. Requirements Requirements: An Intel Mac An iPod Touch Development License iPhone Developer University Program Free for degree-granting institutions Allows for a development team of up to 200 student developers.
6. Obj-C Crashcourse Creating a new object: Rectangle *rect = [[Rectangle alloc] init]; id rect = [Rectangle alloc]; Sending a message: [rectinitWithWidth: 30 andHeight: 45 int area = [rect area]; Accessing a field rect.width = 10;
8. Obj-C Crashcourse Implementing a class @implementation Rectangle@synthesize width, height;-(id) init{ self = [super init]; if (self) {self.width = 0;self.height = 0; } return self;}@end
9. Obj-C Crashcourse SelectorsSEL draw = @selector(“draw”);SEL init = @selector(“initWithHeight:andWidth:”);if([rectrespondsToSelector:draw]) {...} Can freely mix in C-style arrays, pointers, calls to malloc() and free(). Obj-C class pointers managed through retain, release, autorelease messages[rect release];
10. OpenGL ES Source: iPhone Game Development Technologies, Part 1 http://developer.apple.com/adconitunes
11. OpenGL ES Continued Geometry glVertexPointer, glNormalPointer, glTexCoordPointer Points, Lines, Triangles, Triangle Fans, Triangle Strips glDrawArrays, glDrawElement Textures 8888, 4444, and more. PVRTC 2D only iPhone hardware optimized for floats, not double’s or fixed.
12. The Game A clone of other “block” exploding games. Select an orb of a particular color, if it neighbors at least one orb of the same color, all orbs of the same color connected to that orb will be removed from the game. The remaining orbs fall into the space remaining. Simple enough concept to give experience in the OpenGL ES API and interacting with touches.
13. The Experience Wrapping a C API in a set of Objective-C classes Lack of glu functions Object selection when receiving a touch event. Writing a framework from scratch
14. Program Structure Vector3D Required for nearly any work in a 3D space. Supports add and copy operations. Sphere Contains the geometry for the Bubble class. Vertices are computed ahead of time, at application load.
15. Program Structure Bubble Main “game piece” Maintains a reference to a Sphere Provides bounding boxes and drawing functions Receives an update message for future animation Scene3D Manages, updates, draws objects Manages lights and camera, which have their own objects.
16. Program Structure BubblesGame Root of game logic and drawing. Receives regular update calls via EAGLView’sdrawView message Receives “taps” via EAGLView’stouchesEnd:Event: message Contains the “game board” and game logic.
17. Selection No rendering in selection mode. Most commonly suggested solution: Generate and bind an entirely new set of buffers (color, depth, etc.) Disable lighting, fog, and any other features that may change the color of a pixel. Render the entire scene into the buffers, making each object a slightly different color. Read the pixel located under the touch and match the color to one of the objects.
18. Another Solution Produce the projection and model/view matrices. Multiply the matrices together. Invert the resulting matrix. Multiply the screen coordinates by the inverted matrix, generating a line. Compute the intersection of the line with an object.
19.
20. Use an orthographic projection that aligns 0,0 with one corner of the screen and matches a distance of 1.0 with the length of a pixel.
21. Simply use screen coordinates as 2D world coordinates with simple adjustments as needed.
22.
23. Foundation Collections NSArray/NSMutableArray Does not accept nil object references. A deletion at n adjusts all elements at valid indices greater than n down by one. The game board needs to accept empty slots. Solution is to use a dictionary with NSNumber objects as keys and the actual Bubble objects as values.
24. Conclusions Even for very active and animated 2D games, Quartz should be seriously considered and fully discounted before selecting OpenGL ES. Consider using one of the available iPhone game frameworks. Cocos2D Sio2Engine Oolong Engine
25. Next Steps Complete logic and animations Control the direction of falling/compacting with accelerometer Scoring Endless mode
Goals of this project was to explore and asses the iPod touch as a development platform for future inclusion in the Games Development track at CSU.We specifically selected OpenGL ES as one of our environments.Foreshadows: Quartz, Apple’s 2D drawing package was probably a better idea.
Apple’s marketing materials for the fall of 2009 specifically spin the iPod touch as a gaming device in addition to it’s pocket computer and music playing capabilities.Over 75,000 games on the Apple App StoreRelevance to students, both in interest and marketable skills
Objective-C is the preferred development language for the iPhone OS.However, you may also use C/C++, though the documentation on using the Core Foundation data structures are much more complex.
Next 4 slidesTalkabout relation between Rectangle * and id types.Interleave arguments and labelsField access is actually a message, as we’ll see.
all fields/members are private.access is granted through @propertyoptions include nonatomic, readonly, and custom names of the methods called upon assignment/read.defaults allow you to define your own get or set messages-/+ methods (class/instance)
synthesize completes the @property directive.discuss initialization methods, self pointer, super pointerinitialization methods are not really special
Give a few minutes to memory function.
no rendering directly to screenTile Based Diferred Rendering (TBDR)rendering is “lazy”, polygons are culled early and pixels are calculated in tiles laid out like a grid on the screenEAGL is a companion API that connects OpenGL ES to core AnimationOpenGL ES talks to 3D hardware, PowerVR MBX/SGX, it does the math and produces a bunch of pixels.pixels go back to main memory, then get pulled through Core Animation to be displayed.
Talking points:C API: extrapolate, no extra slidesGLU functions: perspective, Mesa3DObject Selection: addressed directlyFramework: express what you wanted out of the framework and what sorts of design missteps you found
Problems:Requires an entirely new set of frame buffer objects to be allocated, rendered to, read from, and deallocated.FBO’s are documented sparsely on Khronos’s site, requireing you to reference SGI’s full EXT_framebuffer_object documentation.There is little in the way of example code or discussion to be found past “How do you do it” and “draw into a side framebuffer and read the pixel data.
Way too much computation for the iPhone.
Discussion:This does present a bit of cheating. It breaks the whole re-useable framework idea, making it specific to the world. However, taps are handled in the Game class tree, so perhaps by delegating the tap handling to the custom code, it’s still a framework. Just not as reusable.