UPDATE : My sample code
MyViewController.h
CubeView *cubeView;
MyViewController.m
cubeView = [[CubeView alloc] initWithFrame:CGRectMake(250, 60, 450, 600)];
cubeView.multipleTouchEnabled = YES;
cubeView.backgroundColor = [UIColor clearColor];
[self.view addSubview:cubeView];
CubeView.m
- (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder]))
{
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
[self setMultipleTouchEnabled:YES];
// self.frame = CGRectMake(20, 30, 500, 500);
renderer = nil;
if (!renderer)
{
renderer = [[CubeRenderer alloc] init];
if (!renderer)
{
[self release];
return nil;
}
}
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Get the layer
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
[self setMultipleTouchEnabled:YES];
renderer = nil;
if (!renderer)
{
renderer = [[CubeRenderer alloc] init];
if (!renderer)
{
[self release];
return nil;
}
}
}
return self;
}
CubeRenderer.m
- (id)init
{
if ((self = [super init]))
{
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext:context])
{
[self release];
return nil;
}
currentCalculatedMatrix = CATransform3DIdentity;
// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
glGenFramebuffersOES(1, &defaultFramebuffer);
glGenRenderbuffersOES(1, &colorRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);
}
return self;
}
static const GLfloat cubeVertices[] = {
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
};
static const GLushort cubeIndicesFaceFront[] = {
0, 1, 2, 3, 0
};
static const GLushort cubeIndicesFaceBack[] = {
4, 5, 6, 7, 4
};
static const GLushort cubeIndicesFaceLeft[] = {
0, 4, 7, 3, 0
};
static const GLushort cubeIndicesFaceRight[] = {
1, 5, 6, 2, 1
};
static const GLushort cubeIndicesFaceTop[] = {
3, 2, 6, 7, 3
};
static const GLushort cubeIndicesFaceBottom[] = {
0, 1, 5, 4, 0
};
[EAGLContext setCurrentContext:context];
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-2.0, 2.0, -2.0 * 480.0 / 320.0, 2.0 * 480.0 / 320.0, -3.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeColorsBlueFace);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceFront);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceFront);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceBack);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBack);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBack);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceLeft);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceLeft);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceLeft);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceRight);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceRight);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceRight);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceTop);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceTop);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceTop);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeFillColorsFaceBottom);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLE_FAN, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBottom);
//glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, cubeIndicesFaceBottom);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];