Created
April 20, 2011 16:20
-
-
Save kizzx2/931819 to your computer and use it in GitHub Desktop.
Barebone OpenGL ES initialization with iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import <QuartzCore/QuartzCore.h> | |
#import <OpenGLES/ES1/gl.h> | |
#import <OpenGLES/ES1/glext.h> | |
@interface OGLESView : UIView { | |
EAGLContext * glContext; | |
GLuint framebuffer; | |
GLuint renderbuffer; | |
} | |
@end | |
@implementation OGLESView | |
// This will make ourself a CAEAGLLayer | |
+ (Class)layerClass | |
{ | |
return [CAEAGLLayer class]; | |
} | |
- (id)initWithCoder:(NSCoder *)aDecoder | |
{ | |
if(!(self = [super initWithCoder:aDecoder])) | |
return self; | |
CAEAGLLayer * eaglLayer = (CAEAGLLayer *)self.layer; | |
eaglLayer.opaque = YES; | |
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; | |
glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; | |
[EAGLContext setCurrentContext:glContext]; | |
// A framebuffer is the "rendering destination" | |
glGenFramebuffersOES(1, &framebuffer); | |
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer); | |
// A rendering buffer is something that OpenGL needs | |
glGenRenderbuffersOES(1, &renderbuffer); | |
glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer); | |
// Assign our layer to OpenGL | |
[glContext renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer *)self.layer]; | |
// Bind the framebuffer and the rendering layer | |
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, renderbuffer); | |
glEnableClientState(GL_VERTEX_ARRAY); | |
glEnableClientState(GL_COLOR_ARRAY); | |
static const GLfloat vertices[] = { | |
-0.5f, -0.33f, | |
0.5f, -0.33f, | |
-0.5f, 0.33f, | |
0.5f, 0.33f | |
}; | |
static const GLubyte colors[] = { | |
255, 255, 0, 255, | |
0, 255, 255, 255, | |
0, 0, 0, 0, | |
255, 0, 255, 255, | |
}; | |
// Standard OpenGL goes on | |
glClearColor(0.9f, 0.9f, 0.9f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glViewport(0, 0, 320, 480); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glVertexPointer(2, GL_FLOAT, 0, vertices); | |
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors); | |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
[glContext presentRenderbuffer:GL_RENDERBUFFER_OES]; | |
return self; | |
} | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
// Initialization code | |
} | |
return self; | |
} | |
/* | |
// Only override drawRect: if you perform custom drawing. | |
// An empty implementation adversely affects performance during animation. | |
- (void)drawRect:(CGRect)rect | |
{ | |
// Drawing code | |
} | |
*/ | |
- (void)dealloc | |
{ | |
glDeleteFramebufferOES(1, &framebuffer); | |
glDeleteRenderbufferOES(1, &renderbuffer); | |
[glContext release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment