Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Air-Craft/6120736 to your computer and use it in GitHub Desktop.
Save Air-Craft/6120736 to your computer and use it in GitHub Desktop.
A GLKVC with frag and vertex shaders as files and with the CeedGL lib. Coord space mapped to 0,0...1,1. #glsl #opengl
/*******
*** vertex.glsl ***********
uniform mat4 u_projection_matrix;
attribute vec4 a_position;
//attribute vec4 a_source_color;
varying vec4 v_position;
void main(void)
{
// We'll map position (0..1) to colour in the frag shader
v_position = a_position;
// Multiply by our scale correction matrix
gl_Position = u_projection_matrix * a_position;
}
*** fragment.glsl ***********
varying highp vec4 v_position;
void main(void)
{
gl_FragColor = vec4(v_position.y,1,1,0);
}
*******/
#import "SPTemperatureGLBackgroundVC.h"
#import "CeedGL.h"
@implementation SPTemperatureGLBackgroundVC
{
EAGLContext *_context;
GLProgram *_glProgram;
GLShader *_glShader;
GLDrawCommand *_glRenderer;
}
/////////////////////////////////////////////////////////////////////////
#pragma mark - Life Cycle
/////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
// Setup the GL
GLKView *view = (GLKView *)self.view;
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
#warning TODO: Check for success....
view.context = _context;
view.drawableMultisample = GLKViewDrawableMultisample4X;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[self _setupGL];
}
//---------------------------------------------------------------------
- (void)viewWillAppear:(BOOL)animated
{
// Be sure to update the current context when the screen shows
[EAGLContext setCurrentContext:_context];
}
//---------------------------------------------------------------------
- (void)viewWillDisappear:(BOOL)animated
{
// Clean up context to prevent conflict with others.
// Only unset if someone else hasn't already...
if ([EAGLContext currentContext] == _context) {
[EAGLContext setCurrentContext:nil];
}
}
//---------------------------------------------------------------------
- (void)viewDidUnload
{
[super viewDidUnload];
// Break all the GL down
[self _tearDownGL];
if ([EAGLContext currentContext] == _context) {
[EAGLContext setCurrentContext:nil];
}
_context = nil;
}
//---------------------------------------------------------------------
- (void)_setupGL
{
/////////////////////////////////////////
// SHADERS, LOAD, COMPILE, ATTACH
/////////////////////////////////////////
NSString *filePath;
[EAGLContext setCurrentContext:_context];
_glProgram = [GLProgram program];
filePath = [[NSBundle mainBundle] pathForResource:@"vertex" ofType:@"glsl"];
NSString *vertexSrc = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
filePath = [[NSBundle mainBundle] pathForResource:@"fragment" ofType:@"glsl"];
NSString *fragmentSrc = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSError *error = nil;
GLShader *vshader = [GLShader vertexShader], *fshader = [GLShader fragmentShader];
[vshader setSource:vertexSrc];
if(![vshader compile:&error]) { DDLogError(@"Vertex shader compilation error: %@", error); }
[fshader setSource:fragmentSrc];
if(![fshader compile:&error]) { DDLogError(@"Fragment shader compilation error: %@", error); }
[_glProgram attachShader:vshader];
[_glProgram attachShader:fshader];
[_glProgram bindAttributeLocation:0 forName:@"a_position"];
if(![_glProgram link:&error]) { NSLog(@"Could not link program error: %@", error); }
/////////////////////////////////////////
// DRAWING DATA & BINDINGS
/////////////////////////////////////////
// Create the drawing renderer
_glRenderer = [GLDrawCommand drawCommand];
_glRenderer.program = _glProgram;
_glRenderer.mode = GL_TRIANGLES;
_glRenderer.firstElement = 0;
_glRenderer.elementCount = 6;
GLBuffer *attr = [GLBuffer buffer];
GLfloat pos[] = {0,0, 1,0, 0,1, 1,1, 1,0, 0,1};
[attr loadData:pos size:sizeof(pos) usage:GL_STATIC_DRAW target:GL_ARRAY_BUFFER];
[_glRenderer setAttributeBuffer:attr size:2 type:GL_FLOAT normalized:GL_FALSE stride:0 offset:0 forName:@"a_position"];
// [_glRenderer setAttribute:[GLValue vectorWithFloats:1 :1 :0 :1] forName:@"a_source_color"];
// Correct our view coords to be 0,0 -> 1,1 for easier shader color mapping
GLKMatrix4 projection = GLKMatrix4MakeScale(2, 2, 2);
projection = GLKMatrix4Translate(projection, -0.5, -0.5, 0);
[_glRenderer setUniform:[GLValue matrixWithFloats:projection.m size:4] forName:@"u_projection_matrix"];
[EAGLContext setCurrentContext:nil];
}
//---------------------------------------------------------------------
- (void)_tearDownGL
{
}
/////////////////////////////////////////////////////////////////////////
#pragma mark - Delegates
/////////////////////////////////////////////////////////////////////////
- (void)glkViewControllerUpdate:(GLKViewController *)controller
{
}
//---------------------------------------------------------------------
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0, 0, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT); // | GL_DEPTH_BUFFER_BIT);
[_glRenderer draw];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment