Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created December 14, 2012 22:42
Show Gist options
  • Save roxlu/4289308 to your computer and use it in GitHub Desktop.
Save roxlu/4289308 to your computer and use it in GitHub Desktop.
Bare openGL application template for IOS. This application templates creates a basic GL context with a framebuffer without the need of a nibb. Note: I've based this on the examples from Apple. If something is wrong or there is a better way to create a full screen GL application, please add a comment or fork this.
#import <UIKit/UIKit.h>
#import "AppViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
AppViewController* view_controller;
}
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect whole_window = [[self window] bounds];
view_controller = [[AppViewController alloc] initWithFrame:whole_window];
[[self window] setRootViewController:view_controller];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application { }
- (void)applicationDidEnterBackground:(UIApplication *)application { }
- (void)applicationDidBecomeActive:(UIApplication *)application { }
- (void)applicationWillTerminate:(UIApplication *)application { }
@end
#import "EAGLView.h"
@interface AppViewController : UIViewController {
EAGLView* gl_view;
}
- (id) initWithFrame:(CGRect) rect;
@end
#import "AppViewController.h"
@implementation AppViewController
- (id) initWithFrame:(CGRect) rect {
self = [super init];
if(self) {
gl_view = [[EAGLView alloc] initWithFrame: rect];
self.view = gl_view;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[gl_view startAnimation];
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[gl_view stopAnimation];
[super viewDidDisappear:animated];
}
@end
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/EAGLDrawable.h>
#include "Simulation.h"
#define eglCheckFramebufferStatus( )\
{\
switch ( glCheckFramebufferStatus( GL_FRAMEBUFFER ) )\
{\
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: printf( "\n%s\n\n", "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT" ); assert( 0 ); break;\
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: printf( "\n%s\n\n", "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS" ); assert( 0 ); break;\
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: printf( "\n%s\n\n", "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT" ); assert( 0 ); break;\
case GL_FRAMEBUFFER_UNSUPPORTED: printf( "\n%s\n\n", "GL_FRAMEBUFFER_UNSUPPORTED" ); assert( 0 ); break;\
default: break;\
}\
}
@interface EAGLView : UIView {
BOOL is_animating;
id display_link;
NSInteger frame_interval;
Simulation simulation;
EAGLContext* gl;
GLuint fbo;
GLint fbo_w;
GLint fbo_h;
GLuint color_rbo;
GLuint depth_rbo;
}
@property (readonly, nonatomic, getter=isAnimating) BOOL is_animating;
//@property (nonatomic, getter=getFrameInterval) NSInteger frame_interval;
- (void) createFramebuffer;
- (void) startAnimation;
- (void) stopAnimation;
- (void) drawView:(id)sender;
//- (void) render;
- (BOOL) resizeFromLayer:(CAEAGLLayer*) layer;
@end
#import "EAGLView.h"
@implementation EAGLView
@synthesize is_animating;
+ (Class) layerClass {
return [CAEAGLLayer class];
}
- (id) initWithFrame: (CGRect)frame {
self = [super initWithFrame:frame];
if(self) {
CAEAGLLayer* layer = (CAEAGLLayer*)self.layer;
layer.opaque = TRUE;
layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:FALSE],
kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8,
kEAGLDrawablePropertyColorFormat,
nil];
is_animating = FALSE;
display_link = nil;
frame_interval = 1;
gl = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if(!gl || ![EAGLContext setCurrentContext:gl]) {
return nil;
}
simulation.setup();
}
return self;
}
- (void) drawView:(id) sender {
if(is_animating) {
[EAGLContext setCurrentContext:gl];
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0, 0, fbo_w, fbo_h);
glClearColor(0.5f, 0.4f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
simulation.update();
simulation.draw();
glBindRenderbuffer(GL_RENDERBUFFER, color_rbo);
[gl presentRenderbuffer:GL_RENDERBUFFER];
}
}
- (BOOL) resizeFromLayer:(CAEAGLLayer*) glLayer {
[self createFramebuffer];
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glBindRenderbuffer(GL_RENDERBUFFER, color_rbo);
[gl renderbufferStorage:GL_RENDERBUFFER fromDrawable:glLayer];
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &fbo_w);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &fbo_h);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
NSLog(@"ERROR: failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
return FALSE;
}
return TRUE;
}
- (void) startAnimation {
if(!is_animating) {
display_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView:)];
[display_link setFrameInterval:frame_interval];
[display_link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
is_animating = true;
}
}
- (void) stopAnimation {
if(is_animating) {
[display_link invalidate];
is_animating = false;
}
}
- (void) layoutSubviews {
[self resizeFromLayer: (CAEAGLLayer*) self.layer];
}
- (void) createFramebuffer {
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenRenderbuffers(1, &color_rbo);
glBindRenderbuffer(GL_RENDERBUFFER, color_rbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_rbo);
}
@end
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
#include "Simulation.h"
Simulation::Simulation() {
}
Simulation::~Simulation() {
}
void Simulation::setup() {
printf("Simulation::setup()\n");
// CREATE SHADER
// --------------
GLuint vert_id = glCreateShader(GL_VERTEX_SHADER);
GLuint frag_id = glCreateShader(GL_FRAGMENT_SHADER);
const char* vss = TEST_VS.c_str();
const char* fss = TEST_FS.c_str();
glShaderSource(vert_id, 1, &vss, NULL);
glShaderSource(frag_id, 1, &fss, NULL);
glCompileShader(vert_id);
glCompileShader(frag_id);
prog = glCreateProgram();
glAttachShader(prog, vert_id);
glAttachShader(prog, frag_id);
glLinkProgram(prog);
// CREATE VAO/VBO
glGenVertexArraysOES(1, &vao);
glBindVertexArrayOES(vao);
float s = 0.5f;
float vertices[] = {
-s, -s,
s, -s,
s, s,
-s, -s,
s, s,
-s, s
};
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glUseProgram(prog);
glEnableVertexAttribArray(glGetAttribLocation(prog, "a_pos"));
glVertexAttribPointer(glGetAttribLocation(prog, "a_pos"), 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, (GLvoid*)0);
}
void Simulation::update() {
// printf("Simulation::update()\n");
}
void Simulation::draw() {
//printf("Simulation::draw()\n");
glUseProgram(prog);
glBindVertexArrayOES(vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
#ifndef ROXLU_SIMULATION_H
#define ROXLU_SIMULATION_H
#include <string>
#include <iostream>
#include <stdio.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
const std::string TEST_VS = ""
"attribute vec4 a_pos;"
"void main() {"
" gl_Position = a_pos; "
"}";
const std::string TEST_FS = ""
"void main() { "
" gl_FragColor = vec4(1.0, 0.0, 0.4, 1.0);"
"}";
class Simulation {
public:
Simulation();
~Simulation();
void setup();
void update();
void draw();
private:
GLuint prog;
GLuint vao;
GLuint vbo;
};
#endif
@roxlu
Copy link
Author

roxlu commented Dec 15, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment