milgra
about
articles
projects
blogroll
bit-101
coding horror
blameit
failblog
beszeljukmac
sghu
navigation
posts
docs
admin
|
2010-01-16 The minimal iPhone openGL ES application
January 15th, 2010
|
Let's use the source from the previous tutorial, "The minimal iPhone application".
We will need an openGL ES framework. Right click on Frameworks folder, Add -> Existing Frameworks, select an OpenGLES framework, click Add. If you don't know how to locate one, create a new openGLES project, right click on OpenGLES.framework under frameworks, and check the full path. We also need the QuartzCore framework for CAEAGLLayer. Do as above.
To show an openGL output, we need a core animation gl layer to bind the renderbuffer to, and UIView has this. So we need an UIView instance. But its not that simple, by default UIView's layer is a CALayer instance, but openGL context needs a CAEAGLLayer instance to work, so we have to mimic it. We need a new class which extends UIView, and we have to override UIView's "layerClass" class method definition.
Right click on Classes, Add -> New File -> Objective-C class -> Next -> name it GLView.m, click Finish. Delete the #import from the header file. Extend it from UIView.
#import
@interface GLView : UIView
{
}
@end
Only one thing is needed in the implementation file, the layerClass override :
#import "GLView.h"
@implementation GLView
+ ( Class )
layerClass
{
return [ CAEAGLLayer class ];
}
@end
So from now on it will tell that its layer is a caeagllayer instance. Great. We made two additional files for almost nothing. Quite a shitty solution from Apple.
Yesokay, lets get back to MinimalAppDelegate. We have to instantiate our layer. We need a new property in the header :
GLView* glview;
don't forget to import
#import "GLView.h"
we also need to import the openGL ES1 extensions
#import <OpenGLES/ES1/glext.h>
The result :
#import "GLView.h"
#import <OpenGLES/ES1/glext.h>
@interface MinimalAppDelegate : NSObject
{
UIWindow* window;
GLView* glview;
}
@end
And in the implementation we instantiate our newly created GLView class:
glview = [ [ GLView alloc ] initWithFrame : [ [ UIScreen mainScreen ] bounds ] ];
Lets add this view as a subview to the main window.
[ window addSubview : glview ];
now we can create our openGL ES1 context.
EAGLContext* context = [ [ EAGLContext alloc ] initWithAPI : kEAGLRenderingAPIOpenGLES1 ];
Set it as the active context :
[ EAGLContext setCurrentContext : context ];
From now on we can work with the context. We need a color and a frame buffer. Let's generate names for them.
GLuint colorBuffer;
GLuint frameBuffer;
glGenFramebuffersOES( 1 , &frameBuffer );
glGenRenderbuffersOES( 1 , &colorBuffer );
Bind framebuffer as framebuffer, colorbuffer as renderbuffer.
glBindFramebufferOES( GL_FRAMEBUFFER_OES , frameBuffer );
glBindRenderbufferOES( GL_RENDERBUFFER_OES , colorBuffer );
We have to attach the colorBuffer to the frameBuffer as color attachment.
glFramebufferRenderbufferOES( GL_FRAMEBUFFER_OES , GL_COLOR_ATTACHMENT0_OES , GL_RENDERBUFFER_OES , colorBuffer );
The only thing left is binding glview's layer to opengl's renderbuffer.
[ context renderbufferStorage : GL_RENDERBUFFER_OES fromDrawable : ( CAEAGLLayer * ) glview.layer ];
Now we can do something spectacular, for example, setting the background color to dark green.
glClearColor( 0 , .3 , 0 , 1.0 );
glClear( GL_COLOR_BUFFER_BIT );
And display the render buffer :
[ context presentRenderbuffer : GL_RENDERBUFFER_OES ];
Yaaaay! If you run/debug the application, you will see a beautiful dark green color on your opengl layer.
Download source.
MilGra
|
|