All you need to do is call [super initWithFrame:frame] with the frame you want from your EAGLView class when you initiate it. This example will create a transparent OpenGL view in the upper left corner and add it to you controllers view.
I've got the following code in my EAGLView class:
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = NO;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO],
kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8,
kEAGLDrawablePropertyColorFormat,
nil];
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context]) {
[self release];
return nil;
}
[EAGLContext setCurrentContext:context];
[self destroyFramebuffer];
[self createFramebuffer];
}
return self;
}
And then I create the EAGLView from my controller like this:
theEAGLView = [[EAGLView alloc] initWithFrame:CGRectMake(30.0f, 30.0f,
90.0f, 70.0f)];
theEAGLView.opaque = NO;
[self.view addSubview:theEAGLView];
The code that does the actual drawing in my EAGLView class is called from the controller and goes something like this:
- (void)render {
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0.0f, backingWidth, 0.0f, backingHeight, 1000.0f, -1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
// this is where we draw our stuff
// ...
// ...
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}