Last active
December 20, 2015 17:39
-
-
Save mousebird/6170096 to your computer and use it in GitHub Desktop.
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
// | |
// ViewController.m | |
// NSMeetupExample | |
// | |
// Created by [email protected] on 8/6/13. | |
// | |
#import "ViewController.h" | |
#import "SimpleGLObject.h" | |
#import "FlexiVertexBuffer.h" | |
@interface ViewController () | |
{ | |
// Rotation applied to geometry. Changes over time. | |
float _rotation; | |
// These are the objects we're drawing | |
NSMutableArray *_glObjects; | |
// EAGLContext used by the other thread; | |
EAGLContext *otherContext; | |
} | |
// The OpenGL ES2 rendering context | |
@property (strong, nonatomic) EAGLContext *context; | |
// A GLKit effect we'll use for lighting and transforms | |
@property (strong, nonatomic) GLKBaseEffect *effect; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; | |
if (!self.context) | |
NSLog(@"Failed to create ES context"); | |
GLKView *view = (GLKView *)self.view; | |
view.context = self.context; | |
view.drawableDepthFormat = GLKViewDrawableDepthFormat24; | |
_glObjects = [NSMutableArray array]; | |
[self setupGL]; | |
} | |
- (void)dealloc | |
{ | |
[self tearDownGL]; | |
if ([EAGLContext currentContext] == self.context) { | |
[EAGLContext setCurrentContext:nil]; | |
} | |
} | |
// Create a single cube, much like the Apple test case | |
- (void)setupSingleCube | |
{ | |
// A single cube, centered at the origin | |
float origin[3] = {0,0,0}; | |
float size[3] = {0.75,0.75,0.75}; | |
FlexiVertexBuffer *flexiBuffer = [FlexiVertexBuffer | |
BufferWithCubeAt:origin | |
sized:size]; | |
SimpleGLObject *glObject = [flexiBuffer makeSimpleGLObject]; | |
[_glObjects addObject:glObject]; | |
} | |
- (void)setupGL | |
{ | |
[EAGLContext setCurrentContext:self.context]; | |
self.effect = [[GLKBaseEffect alloc] init]; | |
self.effect.light0.enabled = GL_TRUE; | |
self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f); | |
glEnable(GL_DEPTH_TEST); | |
[self setupSingleCube]; | |
} | |
- (void)tearDownGL | |
{ | |
[EAGLContext setCurrentContext:self.context]; | |
// Tear down the buffers | |
for (SimpleGLObject *glObject in _glObjects) | |
[glObject tearDownGL]; | |
[_glObjects removeAllObjects]; | |
self.effect = nil; | |
} | |
#pragma mark - GLKView and GLKViewController delegate methods | |
- (void)update | |
{ | |
// Set up the projection matrix | |
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height); | |
GLKMatrix4 projectionMatrix = | |
GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f); | |
self.effect.transform.projectionMatrix = projectionMatrix; | |
// Now the model matrix (for rotating the model) | |
GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -1.5f); | |
baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f); | |
// Compute the model view matrix for the object rendered with GLKit | |
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(-0.5f, -0.5f, -0.5f); | |
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f); | |
modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix); | |
self.effect.transform.modelviewMatrix = modelViewMatrix; | |
_rotation += self.timeSinceLastUpdate * 0.5f; | |
} | |
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect | |
{ | |
glClearColor(0.65f, 0.65f, 0.65f, 1.0f); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
// Work through the vertex arrays (and their triangles) | |
@synchronized(_glObjects) | |
{ | |
for (SimpleGLObject *glObject in _glObjects) | |
{ | |
// See if the vertex array has been built | |
// We only need to do this once, but it has to be here | |
if (glObject.vertexArray == 0) | |
[glObject makeVertexArray]; | |
glBindVertexArrayOES(glObject.vertexArray); | |
// Render the object with GLKit | |
[self.effect prepareToDraw]; | |
glDrawArrays(GL_TRIANGLES, 0, glObject.numVertices); | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment