Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
GLKit starter ViewController
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
EAGLContext* context;
GLuint vertexArray; // our VAO
GLuint vertexBuffer; // our VBO for vertices
GLuint indexBuffer; // our VBO for indices
GLKBaseEffect* effect;
}
- (void)viewDidLoad
{
[super viewDidLoad];
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; // context creation
// View setup
GLKView* view = (GLKView*)self.view;
view.context = context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[EAGLContext setCurrentContext:context];
GLKVector3 vertices[4];
vertices[0] = GLKVector3Make(-0.5f, -0.5f, 0); // create a new GLKVector3 structure
vertices[1] = GLKVector3Make(0.5f, -0.5f, 0);
vertices[2] = GLKVector3Make(0.5f, 0.5f, 0);
vertices[3] = GLKVector3Make(-0.5f, 0.5f, 0);
GLuint indices[6];
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 0;
indices[4] = 2;
indices[5] = 3;
glGenVertexArraysOES(1, &vertexArray); // create the VAO
glBindVertexArrayOES(vertexArray); // tell opengl we're using it
// create the vertices VBO
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); // tell opengl we're using it
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // upload the date to the GPU
// same story for the indices VBO
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// tell opengl to activate position on shaders...
glEnableVertexAttribArray(GLKVertexAttribPosition);
// and use this part of our vertex data as input
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(GLKVector3), 0);
// close our VAO because we're done here
glBindVertexArrayOES(0);
effect = [[GLKBaseEffect alloc] init];
// set up constant color
effect.useConstantColor = GL_TRUE;
effect.constantColor = GLKVector4Make(0, 1, 0, 1);
// set up "camera"
effect.transform.projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(60), fabsf(view.bounds.size.height / view.bounds.size.width), 0.1f, 100.f);
effect.transform.modelviewMatrix = GLKMatrix4MakeTranslation(0, 0, -5);
}
-(void)viewDidUnload
{
[super viewDidUnload];
// delete our buffers GPU side
glDeleteBuffers(1, &vertexBuffer);
glDeleteBuffers(1, &indexBuffer);
glDeleteVertexArraysOES(1, &vertexArray);
// delete our context
[EAGLContext setCurrentContext:context];
if ([EAGLContext currentContext] == context)
[EAGLContext setCurrentContext:nil];
context = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArrayOES(vertexArray); // use our VAO
[effect prepareToDraw]; // do secret mysterious apple stuff
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // draw
glBindVertexArrayOES(0); // don't use our VAO
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment