Book Image

OpenGL ES 3.0 Cookbook

By : Parminder Singh
Book Image

OpenGL ES 3.0 Cookbook

By: Parminder Singh

Overview of this book

<p>"Write once, use anywhere" is truly the power behind OpenGL ES and has made it an embedded industry standard. The library provides cutting-edge, easy-to-use features to build a wide range of applications in the gaming, simulation, augmented-reality, image-processing, and geospatial domains.</p> <p>The book starts by providing you with all the necessary OpenGL ES 3.0 setup guidelines on iOS and Android platforms. You'll go on to master the fundamentals of modern 3D graphics, such as drawing APIs, transformations, buffer objects, the model-view-project analogy, and much more. The book goes on to deal with advanced topics and offers a wide range of recipes on the light shading, real-time rendering techniques with static and procedure textures to create stunning visualizations and runtime effects.</p>
Table of Contents (21 chapters)
OpenGL ES 3.0 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Developing an iOS OpenGL ES 3.0 application


Development of OpenGL ES applications on iOS is much simpler compared to Android. The iOS 7 SDK, Xcode 5.0, and later versions support OpenGL ES 3.0. Using App Wizard in Xcode 5.0, the OpenGL ES 3.0 applications can be developed effortlessly.

Getting ready

Make sure that you should have iOS 7 support in your Xcode IDE. For more information, refer to the Software requirements for OpenGL ES 3.0 – Android ADT recipe in Appendix, Supplementary Information on OpenGL ES 3.0. It's advisable to import the sample recipe iOSHelloWorldTriangle in the Xcode. This will be helpful in understanding the theory quickly.

How to do it...

Here are the step-by-step descriptions of the first iOS OpenGL ES 3.0 application:

Note

The development of an OpenGL ES 3.0 application uses Xcode App Wizard.

  1. Open Xcode, go to File | New | Project, select OpenGL Game, and then click on Next.

  2. Give Product Name, Organization Name, and Company Identifier as per your choice. For example, we are using iOSBlueTriangle, macbook, and Cookbook, respectively. Go to the Next page, select the location, and create project.

  3. Delete ViewController.m from the project navigator. Instead, we will use our own file. Go to File | Add Files to iOSBlueTriangle. Now, locate the source code provided with this book and open the HelloWorldiOS folder. Select ViewController.mm, NativeTemplate.cpp, and NativeTemplate.h, and add these into the project. Feel free to explore these added files. Build (command + B) and execute (command + R) the project.

  4. The development of OpenGL ES in Xcode makes sure that the correct version of OpenGL ES is used. It is automatically resolved by the Xcode build system using Deployment Target. If the deployment target is iOS 7, then OpenGL ES 3.0 libraries are used; otherwise, OpenGL ES 2.0 libraries are used. If the code in source files uses fixed function pipeline programming APIs, then it is understood that OpenGL ES 1.1 is used. For our current recipe, make sure you have set Deployment Target to 7.0:

The program handles reference counting by itself. Therefore, it is advised that you disable the automatic reference count (ARC) to build the program. Otherwise, the compilation may fail. Follow these steps to disable the ARC:

  • Click on you project in the organizer on the left-hand side

  • Select your target in the next column

  • Select the Build Settings tab at the top

  • Scroll down to Objective-C Automatic Reference Counting (it may be listed as CLANG_ENABLE_OBJC_ARC under the User-Defined settings group) and set it to NO

How it works...

The Xcode provides an app wizard to the build the applications for iOS 7.0. The OpenGL ES development uses GLKit, which was introduced in iOS 5.0. The GLKit is an OpenGL ES development framework in objective C/C++. It is used to develop 3D graphics applications for programmable pipeline architecture. Since we are developing a portable application that works across platforms, this kit might not be fully helpful for us (GLKit is in Objective C/C++) in that direction. We will create our custom graphics development framework, which will be helpful for portable applications across Android and iOS. We will use GLKit to build the bridge between our graphics development framework kit and iOS. We will introduce this framework in Chapter 2, OpenGL ES 3.0 Essentials.

The app wizard creates two classes for us, AppDelegate and ViewController. These classes are described here:

  • AppDelegate: This class is inherited from UIResponder<UIApplicationDelegate>, which defines the interfaces for a UIobject that respond to touch and motion events. UIApplication and UIView are also derived from UIResponder. In iOS, the UIApplication class provides a centralized point of control to the underlying OS to coordinate with applications. Each UIApplication must implement some methods for UIApplicationDelegate, which provides the information on the key event happening with in an application. For example, such key events could be application launching, termination, memory status, and state transition.

  • ViewController: GLKit provides a standard View and Controller analogy, through GLKitView and GLKitController. ViewController is derived from GLKitController. Both classes work together to accomplish the rendering job. GLKitView manages the frame buffer object for the application. It takes the responsibility of rendering a draw command into the framebuffer when it is updated. However, GLKitController provides the necessary interfaces to control the pace of frames and their rendering loop:

    //AppDelegate.h
    #import <UIKit/UIKit.h>
    
    @class ViewController;
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    @property (strong, nonatomic) UIWindow *window;
    @property (strong, nonatomic) ViewController *viewController;
    @end

When iOS launches an application, it creates an instance of UIResponder, which basically creates the application object. This application object is a service for the application to provide a physical space in the screen window. This windowing is provided by the object of UIWindow, which will be created during the construction of UIApplication. This window object contains the desired view to display something on screen. In our case, this view should be some OpenGL rendering surface, which is provided by GLKitController to display. When the class object of GLKitController is created, it automatically creates the view associated with it. This helps the application to provide the necessary OpenGL rendering surface:

// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]  bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

The didFinishLaunchingWithOptions interface from UIApplicationDelete informs the event status of the application that it has completed loading. Within this event, we created the window and set the ViewController.

When a subclass from GLKitController is extended, it's very important that we override the viewDidLoad and viewDidUnload methods:

//  ViewController.mm
- (void)viewDidLoad
{
  [super viewDidLoad];
    
  self.context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3] autorelease];
  if (!self.context) {
    NSLog(@"Failed to create ES context");
  }
    
  GLKView *view = (GLKView *)self.view;
  view.context = self.context;
  view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
  
  [self setupGL];
}

The viewDidLoad method helps create the rendering context and set up all its drawable properties for an appropriate configuration. To create an OpenGL ES 3.0 render context, we use initWithAPI. It accepts kEAGLRenderingAPIOpenGLES3 as an argument. This argument makes sure that the rendering context is meant for OpenGL ES 3.0 version.

We can modify the rendering context properties to configure the format of the drawable frame buffer object, such as drawableColorFormat, drawableDepthFormat, drawableStencilFormat and drawableMultisample.

This method is also a good place for initialization and other resource allocations. The last line is calling the setupGL function[self setupGL] in the objective C++ language syntax. Therefore, it is equivalent to this setupGL() in C++:

//  ViewController.mm
- (void)setupGL
{
  [EAGLContext setCurrentContext:self.context];
  GLint defaultFBO, defaultRBO;

  glGetIntegerv(GL_FRAMEBUFFER_BINDING &defaultFBO);
  glGetIntegerv(GL_RENDERBUFFER_BINDING, &defaultRBO);
  glBindFramebuffer( GL_FRAMEBUFFER, defaultFBO );
  glBindRenderbuffer( GL_RENDERBUFFER, defaultRBO );
    
  setupGraphics(self.view.bounds.size.width,
  self.view.bounds.size.height);

}

The setupGL function sets the current context with the one we created in viewDidApplication. This is very important to make the OpenGL ES APIs work. The glBindFramebuffer and glBindRenderbuffer APIs help the other APIs to know which target framebuffer to render on. In OpenGLES, the data is rendered in a rectangular array of information buffer container called a framebuffer. A framebuffer comprises many other helping buffers, such as color, depth, and stencil buffer, to accomplish rendering on the screen window. Sometimes, there could be cases where we may lose framebuffer or the render buffer. In such cases, it is advisable to bind these buffers with these two functions before you call any OpenGL ES3.0 API.

In order to render our application, we must override the drawRect method:

//  ViewController.mm
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    renderFrame();
}

The renderFrame function contains all the necessary code to render blue triangle.

See also

  • Refer to the The fixed function and programmable pipeline architecture recipe in Appendix, Supplementary Information on OpenGL ES 3.0

  • Refer to the Software requirements for OpenGL ES 3.0 – iOS recipe in Appendix, Supplementary Information on OpenGL ES 3.0

  • Refer to the Building prototypes using the GLPI framework recipe, Chapter 2, OpenGL ES 3.0 Essentials