From 977c6446d5ae7ad2ca7907b0d21f88a911c2772e Mon Sep 17 00:00:00 2001 From: chsieh Date: Fri, 17 Dec 2010 15:24:30 -0800 Subject: [PATCH] Moving more files around. --- Classes/EAGLView.h | 37 -- Classes/EAGLView.m | 163 -------- Classes/Foundation/OSInterface/EAGLView.h | 37 ++ Classes/Foundation/OSInterface/EAGLView.m | 163 ++++++++ .../Foundation/OSInterface/LittlestAppDelegate.h | 22 + .../Foundation/OSInterface/LittlestAppDelegate.m | 56 +++ .../OSInterface/LittlestViewController.h | 34 ++ .../OSInterface/LittlestViewController.m | 412 ++++++++++++++++++++ Classes/LittlestAppDelegate.h | 22 - Classes/LittlestAppDelegate.m | 56 --- Classes/LittlestViewController.h | 34 -- Classes/LittlestViewController.m | 412 -------------------- 12 files changed, 724 insertions(+), 724 deletions(-) delete mode 100644 Classes/EAGLView.h delete mode 100644 Classes/EAGLView.m create mode 100644 Classes/Foundation/OSInterface/EAGLView.h create mode 100644 Classes/Foundation/OSInterface/EAGLView.m create mode 100644 Classes/Foundation/OSInterface/LittlestAppDelegate.h create mode 100644 Classes/Foundation/OSInterface/LittlestAppDelegate.m create mode 100644 Classes/Foundation/OSInterface/LittlestViewController.h create mode 100644 Classes/Foundation/OSInterface/LittlestViewController.m delete mode 100644 Classes/LittlestAppDelegate.h delete mode 100644 Classes/LittlestAppDelegate.m delete mode 100644 Classes/LittlestViewController.h delete mode 100644 Classes/LittlestViewController.m diff --git a/Classes/EAGLView.h b/Classes/EAGLView.h deleted file mode 100644 index 277392d..0000000 --- a/Classes/EAGLView.h +++ /dev/null @@ -1,37 +0,0 @@ -// -// EAGLView.h -// Littlest -// -// Created by Doris Chen on 12/4/10. -// Copyright 2010 __MyCompanyName__. All rights reserved. -// - -#import - -#import -#import -#import -#import - -// This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass. -// The view content is basically an EAGL surface you render your OpenGL scene into. -// Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel. -@interface EAGLView : UIView -{ -@private - EAGLContext *context; - - // The pixel dimensions of the CAEAGLLayer. - GLint framebufferWidth; - GLint framebufferHeight; - - // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view. - GLuint defaultFramebuffer, colorRenderbuffer; -} - -@property (nonatomic, retain) EAGLContext *context; - -- (void)setFramebuffer; -- (BOOL)presentFramebuffer; - -@end diff --git a/Classes/EAGLView.m b/Classes/EAGLView.m deleted file mode 100644 index 4fb0237..0000000 --- a/Classes/EAGLView.m +++ /dev/null @@ -1,163 +0,0 @@ -// -// EAGLView.m -// Littlest -// -// Created by Doris Chen on 12/4/10. -// Copyright 2010 __MyCompanyName__. All rights reserved. -// - -#import - -#import "EAGLView.h" - -//----------------------------------------------------------------------------------------------------------- -@interface EAGLView (PrivateMethods) -- (void)createFrameBuffer; -- (void)deleteFramebuffer; -@end - -//----------------------------------------------------------------------------------------------------------- -@implementation EAGLView - -@dynamic context; - -//----------------------------------------------------------------------------------------------------------- -// You must implement this method -+ (Class)layerClass -{ - return [CAEAGLLayer class]; -} - -//----------------------------------------------------------------------------------------------------------- -//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:. -- (id)initWithCoder:(NSCoder*)coder -{ - self = [super initWithCoder:coder]; - if (self) - { - CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; - - eaglLayer.opaque = TRUE; - eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: - [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, - kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, - nil]; - } - - return self; -} - -//----------------------------------------------------------------------------------------------------------- -- (void)dealloc -{ - [self deleteFramebuffer]; - [context release]; - - [super dealloc]; -} - -//----------------------------------------------------------------------------------------------------------- -- (EAGLContext *)context -{ - return context; -} - -//----------------------------------------------------------------------------------------------------------- -- (void)setContext:(EAGLContext *)newContext -{ - if (context != newContext) - { - [self deleteFramebuffer]; - [context release]; - - context = [newContext retain]; - } -} - -//----------------------------------------------------------------------------------------------------------- -- (void)createFrameBuffer -{ - if (context && !defaultFramebuffer) - { - [EAGLContext setCurrentContext:context]; - - // Create default framebuffer object. - glGenFramebuffers(1, &defaultFramebuffer); - glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer); - - // Create color render buffer and allocate backing store. - glGenRenderbuffers(1, &colorRenderbuffer); - glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); - [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer]; - glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth); - glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight); - - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); - - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); - } -} - -//----------------------------------------------------------------------------------------------------------- -- (void)deleteFramebuffer -{ - if (context) - { - [EAGLContext setCurrentContext:context]; - - if (defaultFramebuffer) - { - glDeleteFramebuffers(1, &defaultFramebuffer); - defaultFramebuffer = 0; - } - - if (colorRenderbuffer) - { - glDeleteRenderbuffers(1, &colorRenderbuffer); - colorRenderbuffer = 0; - } - } -} - -//----------------------------------------------------------------------------------------------------------- -- (void)setFramebuffer -{ - if (context) - { - [EAGLContext setCurrentContext:context]; - - if (!defaultFramebuffer) - [self createFrameBuffer]; - - glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer); - - glViewport(0, 0, framebufferWidth, framebufferHeight); - } -} - -//----------------------------------------------------------------------------------------------------------- -- (BOOL)presentFramebuffer -{ - BOOL success = FALSE; - - if (context) - { - [EAGLContext setCurrentContext:context]; - - glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); - - success = [context presentRenderbuffer:GL_RENDERBUFFER]; - } - - return success; -} - -//----------------------------------------------------------------------------------------------------------- -- (void)layoutSubviews -{ - // The framebuffer will be re-created at the beginning of the next setFramebuffer method call. - [self deleteFramebuffer]; -} - -@end diff --git a/Classes/Foundation/OSInterface/EAGLView.h b/Classes/Foundation/OSInterface/EAGLView.h new file mode 100644 index 0000000..277392d --- /dev/null +++ b/Classes/Foundation/OSInterface/EAGLView.h @@ -0,0 +1,37 @@ +// +// EAGLView.h +// Littlest +// +// Created by Doris Chen on 12/4/10. +// Copyright 2010 __MyCompanyName__. All rights reserved. +// + +#import + +#import +#import +#import +#import + +// This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass. +// The view content is basically an EAGL surface you render your OpenGL scene into. +// Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel. +@interface EAGLView : UIView +{ +@private + EAGLContext *context; + + // The pixel dimensions of the CAEAGLLayer. + GLint framebufferWidth; + GLint framebufferHeight; + + // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view. + GLuint defaultFramebuffer, colorRenderbuffer; +} + +@property (nonatomic, retain) EAGLContext *context; + +- (void)setFramebuffer; +- (BOOL)presentFramebuffer; + +@end diff --git a/Classes/Foundation/OSInterface/EAGLView.m b/Classes/Foundation/OSInterface/EAGLView.m new file mode 100644 index 0000000..4fb0237 --- /dev/null +++ b/Classes/Foundation/OSInterface/EAGLView.m @@ -0,0 +1,163 @@ +// +// EAGLView.m +// Littlest +// +// Created by Doris Chen on 12/4/10. +// Copyright 2010 __MyCompanyName__. All rights reserved. +// + +#import + +#import "EAGLView.h" + +//----------------------------------------------------------------------------------------------------------- +@interface EAGLView (PrivateMethods) +- (void)createFrameBuffer; +- (void)deleteFramebuffer; +@end + +//----------------------------------------------------------------------------------------------------------- +@implementation EAGLView + +@dynamic context; + +//----------------------------------------------------------------------------------------------------------- +// You must implement this method ++ (Class)layerClass +{ + return [CAEAGLLayer class]; +} + +//----------------------------------------------------------------------------------------------------------- +//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:. +- (id)initWithCoder:(NSCoder*)coder +{ + self = [super initWithCoder:coder]; + if (self) + { + CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; + + eaglLayer.opaque = TRUE; + eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, + kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, + nil]; + } + + return self; +} + +//----------------------------------------------------------------------------------------------------------- +- (void)dealloc +{ + [self deleteFramebuffer]; + [context release]; + + [super dealloc]; +} + +//----------------------------------------------------------------------------------------------------------- +- (EAGLContext *)context +{ + return context; +} + +//----------------------------------------------------------------------------------------------------------- +- (void)setContext:(EAGLContext *)newContext +{ + if (context != newContext) + { + [self deleteFramebuffer]; + [context release]; + + context = [newContext retain]; + } +} + +//----------------------------------------------------------------------------------------------------------- +- (void)createFrameBuffer +{ + if (context && !defaultFramebuffer) + { + [EAGLContext setCurrentContext:context]; + + // Create default framebuffer object. + glGenFramebuffers(1, &defaultFramebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer); + + // Create color render buffer and allocate backing store. + glGenRenderbuffers(1, &colorRenderbuffer); + glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); + [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer]; + glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth); + glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight); + + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); + } +} + +//----------------------------------------------------------------------------------------------------------- +- (void)deleteFramebuffer +{ + if (context) + { + [EAGLContext setCurrentContext:context]; + + if (defaultFramebuffer) + { + glDeleteFramebuffers(1, &defaultFramebuffer); + defaultFramebuffer = 0; + } + + if (colorRenderbuffer) + { + glDeleteRenderbuffers(1, &colorRenderbuffer); + colorRenderbuffer = 0; + } + } +} + +//----------------------------------------------------------------------------------------------------------- +- (void)setFramebuffer +{ + if (context) + { + [EAGLContext setCurrentContext:context]; + + if (!defaultFramebuffer) + [self createFrameBuffer]; + + glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer); + + glViewport(0, 0, framebufferWidth, framebufferHeight); + } +} + +//----------------------------------------------------------------------------------------------------------- +- (BOOL)presentFramebuffer +{ + BOOL success = FALSE; + + if (context) + { + [EAGLContext setCurrentContext:context]; + + glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); + + success = [context presentRenderbuffer:GL_RENDERBUFFER]; + } + + return success; +} + +//----------------------------------------------------------------------------------------------------------- +- (void)layoutSubviews +{ + // The framebuffer will be re-created at the beginning of the next setFramebuffer method call. + [self deleteFramebuffer]; +} + +@end diff --git a/Classes/Foundation/OSInterface/LittlestAppDelegate.h b/Classes/Foundation/OSInterface/LittlestAppDelegate.h new file mode 100644 index 0000000..8303682 --- /dev/null +++ b/Classes/Foundation/OSInterface/LittlestAppDelegate.h @@ -0,0 +1,22 @@ +// +// LittlestAppDelegate.h +// Littlest +// +// Created by Doris Chen on 12/4/10. +// Copyright 2010 __MyCompanyName__. All rights reserved. +// + +#import + +@class LittlestViewController; + +@interface LittlestAppDelegate : NSObject { + UIWindow *window; + LittlestViewController *viewController; +} + +@property (nonatomic, retain) IBOutlet UIWindow *window; +@property (nonatomic, retain) IBOutlet LittlestViewController *viewController; + +@end + diff --git a/Classes/Foundation/OSInterface/LittlestAppDelegate.m b/Classes/Foundation/OSInterface/LittlestAppDelegate.m new file mode 100644 index 0000000..d38cd14 --- /dev/null +++ b/Classes/Foundation/OSInterface/LittlestAppDelegate.m @@ -0,0 +1,56 @@ +// +// LittlestAppDelegate.m +// Littlest +// +// Created by Doris Chen on 12/4/10. +// Copyright 2010 __MyCompanyName__. All rights reserved. +// + +#import "LittlestAppDelegate.h" +#import "LittlestViewController.h" + +@implementation LittlestAppDelegate + +@synthesize window; +@synthesize viewController; + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions +{ + [self.window addSubview:self.viewController.view]; + return YES; +} + +- (void)applicationWillResignActive:(UIApplication *)application +{ + [self.viewController stopAnimation]; +} + +- (void)applicationDidBecomeActive:(UIApplication *)application +{ + [self.viewController startAnimation]; +} + +- (void)applicationWillTerminate:(UIApplication *)application +{ + [self.viewController stopAnimation]; +} + +- (void)applicationDidEnterBackground:(UIApplication *)application +{ + // Handle any background procedures not related to animation here. +} + +- (void)applicationWillEnterForeground:(UIApplication *)application +{ + // Handle any foreground procedures not related to animation here. +} + +- (void)dealloc +{ + [viewController release]; + [window release]; + + [super dealloc]; +} + +@end diff --git a/Classes/Foundation/OSInterface/LittlestViewController.h b/Classes/Foundation/OSInterface/LittlestViewController.h new file mode 100644 index 0000000..1d29ba7 --- /dev/null +++ b/Classes/Foundation/OSInterface/LittlestViewController.h @@ -0,0 +1,34 @@ +// +// LittlestViewController.h +// Littlest +// +// Created by Doris Chen on 12/4/10. +// Copyright 2010 __MyCompanyName__. All rights reserved. +// + +#import + +#import + +#import +#import +#import +#import + +@interface LittlestViewController : UIViewController +{ + EAGLContext *context; + GLuint program; + + BOOL animating; + NSInteger animationFrameInterval; + CADisplayLink *displayLink; +} + +@property (readonly, nonatomic, getter=isAnimating) BOOL animating; +@property (nonatomic) NSInteger animationFrameInterval; + +- (void)startAnimation; +- (void)stopAnimation; + +@end diff --git a/Classes/Foundation/OSInterface/LittlestViewController.m b/Classes/Foundation/OSInterface/LittlestViewController.m new file mode 100644 index 0000000..66fef57 --- /dev/null +++ b/Classes/Foundation/OSInterface/LittlestViewController.m @@ -0,0 +1,412 @@ +// +// LittlestViewController.m +// Littlest +// +// Created by Doris Chen on 12/4/10. +// Copyright 2010 __MyCompanyName__. All rights reserved. +// + +#import + +#import "LittlestViewController.h" +#import "EAGLView.h" + +//----------------------------------------------------------------------------------------------------------- +// Uniform index. +enum { + UNIFORM_TRANSLATE, + NUM_UNIFORMS +}; +GLint uniforms[NUM_UNIFORMS]; + +// Attribute index. +enum { + ATTRIB_VERTEX, + ATTRIB_COLOR, + NUM_ATTRIBUTES +}; + +//----------------------------------------------------------------------------------------------------------- +@interface LittlestViewController () +@property (nonatomic, retain) EAGLContext *context; +@property (nonatomic, assign) CADisplayLink *displayLink; +- (BOOL)loadShaders; +- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file; +- (BOOL)linkProgram:(GLuint)prog; +- (BOOL)validateProgram:(GLuint)prog; +@end + +//----------------------------------------------------------------------------------------------------------- +@implementation LittlestViewController + +@synthesize animating, context, displayLink; + +//----------------------------------------------------------------------------------------------------------- +- (void)awakeFromNib +{ + EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; + + if (!aContext) + { + aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; + } + + if (!aContext) + NSLog(@"Failed to create ES context"); + else if (![EAGLContext setCurrentContext:aContext]) + NSLog(@"Failed to set ES context current"); + + self.context = aContext; + [aContext release]; + + [(EAGLView *)self.view setContext:context]; + [(EAGLView *)self.view setFramebuffer]; + + if ([context API] == kEAGLRenderingAPIOpenGLES2) + [self loadShaders]; + + animating = FALSE; + animationFrameInterval = 1; + self.displayLink = nil; +} + +//----------------------------------------------------------------------------------------------------------- +- (void)dealloc +{ + if (program) + { + glDeleteProgram(program); + program = 0; + } + + // Tear down context. + if ([EAGLContext currentContext] == context) + [EAGLContext setCurrentContext:nil]; + + [context release]; + + [super dealloc]; +} + +//----------------------------------------------------------------------------------------------------------- +- (void)viewWillAppear:(BOOL)animated +{ + [self startAnimation]; + + [super viewWillAppear:animated]; +} + +//----------------------------------------------------------------------------------------------------------- +- (void)viewWillDisappear:(BOOL)animated +{ + [self stopAnimation]; + + [super viewWillDisappear:animated]; +} + +//----------------------------------------------------------------------------------------------------------- +- (void)viewDidUnload +{ + [super viewDidUnload]; + + if (program) + { + glDeleteProgram(program); + program = 0; + } + + // Tear down context. + if ([EAGLContext currentContext] == context) + [EAGLContext setCurrentContext:nil]; + self.context = nil; +} + +//----------------------------------------------------------------------------------------------------------- +- (NSInteger)animationFrameInterval +{ + return animationFrameInterval; +} + +//----------------------------------------------------------------------------------------------------------- +- (void)setAnimationFrameInterval:(NSInteger)frameInterval +{ + /* + Frame interval defines how many display frames must pass between each time the display link fires. + The display link will only fire 30 times a second when the frame internal is two on a display that refreshes 60 times a second. The default frame interval setting of one will fire 60 times a second when the display refreshes at 60 times a second. A frame interval setting of less than one results in undefined behavior. + */ + if (frameInterval >= 1) + { + animationFrameInterval = frameInterval; + + if (animating) + { + [self stopAnimation]; + [self startAnimation]; + } + } +} + +//----------------------------------------------------------------------------------------------------------- +- (void)startAnimation +{ + if (!animating) + { + CADisplayLink *aDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)]; + [aDisplayLink setFrameInterval:animationFrameInterval]; + [aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; + self.displayLink = aDisplayLink; + + animating = TRUE; + } +} + +//----------------------------------------------------------------------------------------------------------- +- (void)stopAnimation +{ + if (animating) + { + [self.displayLink invalidate]; + self.displayLink = nil; + animating = FALSE; + } +} + +//----------------------------------------------------------------------------------------------------------- +- (void)drawFrame +{ + [(EAGLView *)self.view setFramebuffer]; + + // Replace the implementation of this method to do your own custom drawing. + static const GLfloat squareVertices[] = { + -0.5f, -0.33f, + 0.5f, -0.33f, + -0.5f, 0.33f, + 0.5f, 0.33f, + }; + + static const GLubyte squareColors[] = { + 255, 255, 0, 255, + 0, 255, 255, 255, + 0, 0, 0, 0, + 255, 0, 255, 255, + }; + + static float transY = 0.0f; + + glClearColor(0.5f, 0.5f, 0.5f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + if ([context API] == kEAGLRenderingAPIOpenGLES2) + { + // Use shader program. + glUseProgram(program); + + // Update uniform value. + glUniform1f(uniforms[UNIFORM_TRANSLATE], (GLfloat)transY); + transY += 0.075f; + + // Update attribute values. + glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices); + glEnableVertexAttribArray(ATTRIB_VERTEX); + glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors); + glEnableVertexAttribArray(ATTRIB_COLOR); + + // Validate program before drawing. This is a good check, but only really necessary in a debug build. + // DEBUG macro must be defined in your debug configurations if that's not already the case. +#if defined(DEBUG) + if (![self validateProgram:program]) + { + NSLog(@"Failed to validate program: %d", program); + return; + } +#endif + } + else + { + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f); + transY += 0.075f; + + glVertexPointer(2, GL_FLOAT, 0, squareVertices); + glEnableClientState(GL_VERTEX_ARRAY); + glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors); + glEnableClientState(GL_COLOR_ARRAY); + } + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + [(EAGLView *)self.view presentFramebuffer]; +} + +//----------------------------------------------------------------------------------------------------------- +- (void)didReceiveMemoryWarning +{ + // Releases the view if it doesn't have a superview. + [super didReceiveMemoryWarning]; + + // Release any cached data, images, etc. that aren't in use. +} + +//----------------------------------------------------------------------------------------------------------- +- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file +{ + GLint status; + const GLchar *source; + + source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String]; + if (!source) + { + NSLog(@"Failed to load vertex shader"); + return FALSE; + } + + *shader = glCreateShader(type); + glShaderSource(*shader, 1, &source, NULL); + glCompileShader(*shader); + +#if defined(DEBUG) + GLint logLength; + glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetShaderInfoLog(*shader, logLength, &logLength, log); + NSLog(@"Shader compile log:\n%s", log); + free(log); + } +#endif + + glGetShaderiv(*shader, GL_COMPILE_STATUS, &status); + if (status == 0) + { + glDeleteShader(*shader); + return FALSE; + } + + return TRUE; +} + +//----------------------------------------------------------------------------------------------------------- +- (BOOL)linkProgram:(GLuint)prog +{ + GLint status; + + glLinkProgram(prog); + +#if defined(DEBUG) + GLint logLength; + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetProgramInfoLog(prog, logLength, &logLength, log); + NSLog(@"Program link log:\n%s", log); + free(log); + } +#endif + + glGetProgramiv(prog, GL_LINK_STATUS, &status); + if (status == 0) + return FALSE; + + return TRUE; +} + +//----------------------------------------------------------------------------------------------------------- +- (BOOL)validateProgram:(GLuint)prog +{ + GLint logLength, status; + + glValidateProgram(prog); + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); + if (logLength > 0) + { + GLchar *log = (GLchar *)malloc(logLength); + glGetProgramInfoLog(prog, logLength, &logLength, log); + NSLog(@"Program validate log:\n%s", log); + free(log); + } + + glGetProgramiv(prog, GL_VALIDATE_STATUS, &status); + if (status == 0) + return FALSE; + + return TRUE; +} + +//----------------------------------------------------------------------------------------------------------- +- (BOOL)loadShaders +{ + GLuint vertShader, fragShader; + NSString *vertShaderPathname, *fragShaderPathname; + + // Create shader program. + program = glCreateProgram(); + + // Create and compile vertex shader. + vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"vsh"]; + if (![self compileShader:&vertShader type:GL_VERTEX_SHADER file:vertShaderPathname]) + { + NSLog(@"Failed to compile vertex shader"); + return FALSE; + } + + // Create and compile fragment shader. + fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"fsh"]; + if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER file:fragShaderPathname]) + { + NSLog(@"Failed to compile fragment shader"); + return FALSE; + } + + // Attach vertex shader to program. + glAttachShader(program, vertShader); + + // Attach fragment shader to program. + glAttachShader(program, fragShader); + + // Bind attribute locations. + // This needs to be done prior to linking. + glBindAttribLocation(program, ATTRIB_VERTEX, "position"); + glBindAttribLocation(program, ATTRIB_COLOR, "color"); + + // Link program. + if (![self linkProgram:program]) + { + NSLog(@"Failed to link program: %d", program); + + if (vertShader) + { + glDeleteShader(vertShader); + vertShader = 0; + } + if (fragShader) + { + glDeleteShader(fragShader); + fragShader = 0; + } + if (program) + { + glDeleteProgram(program); + program = 0; + } + + return FALSE; + } + + // Get uniform locations. + uniforms[UNIFORM_TRANSLATE] = glGetUniformLocation(program, "translate"); + + // Release vertex and fragment shaders. + if (vertShader) + glDeleteShader(vertShader); + if (fragShader) + glDeleteShader(fragShader); + + return TRUE; +} + +@end diff --git a/Classes/LittlestAppDelegate.h b/Classes/LittlestAppDelegate.h deleted file mode 100644 index 8303682..0000000 --- a/Classes/LittlestAppDelegate.h +++ /dev/null @@ -1,22 +0,0 @@ -// -// LittlestAppDelegate.h -// Littlest -// -// Created by Doris Chen on 12/4/10. -// Copyright 2010 __MyCompanyName__. All rights reserved. -// - -#import - -@class LittlestViewController; - -@interface LittlestAppDelegate : NSObject { - UIWindow *window; - LittlestViewController *viewController; -} - -@property (nonatomic, retain) IBOutlet UIWindow *window; -@property (nonatomic, retain) IBOutlet LittlestViewController *viewController; - -@end - diff --git a/Classes/LittlestAppDelegate.m b/Classes/LittlestAppDelegate.m deleted file mode 100644 index d38cd14..0000000 --- a/Classes/LittlestAppDelegate.m +++ /dev/null @@ -1,56 +0,0 @@ -// -// LittlestAppDelegate.m -// Littlest -// -// Created by Doris Chen on 12/4/10. -// Copyright 2010 __MyCompanyName__. All rights reserved. -// - -#import "LittlestAppDelegate.h" -#import "LittlestViewController.h" - -@implementation LittlestAppDelegate - -@synthesize window; -@synthesize viewController; - -- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions -{ - [self.window addSubview:self.viewController.view]; - return YES; -} - -- (void)applicationWillResignActive:(UIApplication *)application -{ - [self.viewController stopAnimation]; -} - -- (void)applicationDidBecomeActive:(UIApplication *)application -{ - [self.viewController startAnimation]; -} - -- (void)applicationWillTerminate:(UIApplication *)application -{ - [self.viewController stopAnimation]; -} - -- (void)applicationDidEnterBackground:(UIApplication *)application -{ - // Handle any background procedures not related to animation here. -} - -- (void)applicationWillEnterForeground:(UIApplication *)application -{ - // Handle any foreground procedures not related to animation here. -} - -- (void)dealloc -{ - [viewController release]; - [window release]; - - [super dealloc]; -} - -@end diff --git a/Classes/LittlestViewController.h b/Classes/LittlestViewController.h deleted file mode 100644 index 1d29ba7..0000000 --- a/Classes/LittlestViewController.h +++ /dev/null @@ -1,34 +0,0 @@ -// -// LittlestViewController.h -// Littlest -// -// Created by Doris Chen on 12/4/10. -// Copyright 2010 __MyCompanyName__. All rights reserved. -// - -#import - -#import - -#import -#import -#import -#import - -@interface LittlestViewController : UIViewController -{ - EAGLContext *context; - GLuint program; - - BOOL animating; - NSInteger animationFrameInterval; - CADisplayLink *displayLink; -} - -@property (readonly, nonatomic, getter=isAnimating) BOOL animating; -@property (nonatomic) NSInteger animationFrameInterval; - -- (void)startAnimation; -- (void)stopAnimation; - -@end diff --git a/Classes/LittlestViewController.m b/Classes/LittlestViewController.m deleted file mode 100644 index 66fef57..0000000 --- a/Classes/LittlestViewController.m +++ /dev/null @@ -1,412 +0,0 @@ -// -// LittlestViewController.m -// Littlest -// -// Created by Doris Chen on 12/4/10. -// Copyright 2010 __MyCompanyName__. All rights reserved. -// - -#import - -#import "LittlestViewController.h" -#import "EAGLView.h" - -//----------------------------------------------------------------------------------------------------------- -// Uniform index. -enum { - UNIFORM_TRANSLATE, - NUM_UNIFORMS -}; -GLint uniforms[NUM_UNIFORMS]; - -// Attribute index. -enum { - ATTRIB_VERTEX, - ATTRIB_COLOR, - NUM_ATTRIBUTES -}; - -//----------------------------------------------------------------------------------------------------------- -@interface LittlestViewController () -@property (nonatomic, retain) EAGLContext *context; -@property (nonatomic, assign) CADisplayLink *displayLink; -- (BOOL)loadShaders; -- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file; -- (BOOL)linkProgram:(GLuint)prog; -- (BOOL)validateProgram:(GLuint)prog; -@end - -//----------------------------------------------------------------------------------------------------------- -@implementation LittlestViewController - -@synthesize animating, context, displayLink; - -//----------------------------------------------------------------------------------------------------------- -- (void)awakeFromNib -{ - EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; - - if (!aContext) - { - aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; - } - - if (!aContext) - NSLog(@"Failed to create ES context"); - else if (![EAGLContext setCurrentContext:aContext]) - NSLog(@"Failed to set ES context current"); - - self.context = aContext; - [aContext release]; - - [(EAGLView *)self.view setContext:context]; - [(EAGLView *)self.view setFramebuffer]; - - if ([context API] == kEAGLRenderingAPIOpenGLES2) - [self loadShaders]; - - animating = FALSE; - animationFrameInterval = 1; - self.displayLink = nil; -} - -//----------------------------------------------------------------------------------------------------------- -- (void)dealloc -{ - if (program) - { - glDeleteProgram(program); - program = 0; - } - - // Tear down context. - if ([EAGLContext currentContext] == context) - [EAGLContext setCurrentContext:nil]; - - [context release]; - - [super dealloc]; -} - -//----------------------------------------------------------------------------------------------------------- -- (void)viewWillAppear:(BOOL)animated -{ - [self startAnimation]; - - [super viewWillAppear:animated]; -} - -//----------------------------------------------------------------------------------------------------------- -- (void)viewWillDisappear:(BOOL)animated -{ - [self stopAnimation]; - - [super viewWillDisappear:animated]; -} - -//----------------------------------------------------------------------------------------------------------- -- (void)viewDidUnload -{ - [super viewDidUnload]; - - if (program) - { - glDeleteProgram(program); - program = 0; - } - - // Tear down context. - if ([EAGLContext currentContext] == context) - [EAGLContext setCurrentContext:nil]; - self.context = nil; -} - -//----------------------------------------------------------------------------------------------------------- -- (NSInteger)animationFrameInterval -{ - return animationFrameInterval; -} - -//----------------------------------------------------------------------------------------------------------- -- (void)setAnimationFrameInterval:(NSInteger)frameInterval -{ - /* - Frame interval defines how many display frames must pass between each time the display link fires. - The display link will only fire 30 times a second when the frame internal is two on a display that refreshes 60 times a second. The default frame interval setting of one will fire 60 times a second when the display refreshes at 60 times a second. A frame interval setting of less than one results in undefined behavior. - */ - if (frameInterval >= 1) - { - animationFrameInterval = frameInterval; - - if (animating) - { - [self stopAnimation]; - [self startAnimation]; - } - } -} - -//----------------------------------------------------------------------------------------------------------- -- (void)startAnimation -{ - if (!animating) - { - CADisplayLink *aDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame)]; - [aDisplayLink setFrameInterval:animationFrameInterval]; - [aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; - self.displayLink = aDisplayLink; - - animating = TRUE; - } -} - -//----------------------------------------------------------------------------------------------------------- -- (void)stopAnimation -{ - if (animating) - { - [self.displayLink invalidate]; - self.displayLink = nil; - animating = FALSE; - } -} - -//----------------------------------------------------------------------------------------------------------- -- (void)drawFrame -{ - [(EAGLView *)self.view setFramebuffer]; - - // Replace the implementation of this method to do your own custom drawing. - static const GLfloat squareVertices[] = { - -0.5f, -0.33f, - 0.5f, -0.33f, - -0.5f, 0.33f, - 0.5f, 0.33f, - }; - - static const GLubyte squareColors[] = { - 255, 255, 0, 255, - 0, 255, 255, 255, - 0, 0, 0, 0, - 255, 0, 255, 255, - }; - - static float transY = 0.0f; - - glClearColor(0.5f, 0.5f, 0.5f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - if ([context API] == kEAGLRenderingAPIOpenGLES2) - { - // Use shader program. - glUseProgram(program); - - // Update uniform value. - glUniform1f(uniforms[UNIFORM_TRANSLATE], (GLfloat)transY); - transY += 0.075f; - - // Update attribute values. - glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices); - glEnableVertexAttribArray(ATTRIB_VERTEX); - glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors); - glEnableVertexAttribArray(ATTRIB_COLOR); - - // Validate program before drawing. This is a good check, but only really necessary in a debug build. - // DEBUG macro must be defined in your debug configurations if that's not already the case. -#if defined(DEBUG) - if (![self validateProgram:program]) - { - NSLog(@"Failed to validate program: %d", program); - return; - } -#endif - } - else - { - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f); - transY += 0.075f; - - glVertexPointer(2, GL_FLOAT, 0, squareVertices); - glEnableClientState(GL_VERTEX_ARRAY); - glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors); - glEnableClientState(GL_COLOR_ARRAY); - } - - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - - [(EAGLView *)self.view presentFramebuffer]; -} - -//----------------------------------------------------------------------------------------------------------- -- (void)didReceiveMemoryWarning -{ - // Releases the view if it doesn't have a superview. - [super didReceiveMemoryWarning]; - - // Release any cached data, images, etc. that aren't in use. -} - -//----------------------------------------------------------------------------------------------------------- -- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file -{ - GLint status; - const GLchar *source; - - source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String]; - if (!source) - { - NSLog(@"Failed to load vertex shader"); - return FALSE; - } - - *shader = glCreateShader(type); - glShaderSource(*shader, 1, &source, NULL); - glCompileShader(*shader); - -#if defined(DEBUG) - GLint logLength; - glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength); - if (logLength > 0) - { - GLchar *log = (GLchar *)malloc(logLength); - glGetShaderInfoLog(*shader, logLength, &logLength, log); - NSLog(@"Shader compile log:\n%s", log); - free(log); - } -#endif - - glGetShaderiv(*shader, GL_COMPILE_STATUS, &status); - if (status == 0) - { - glDeleteShader(*shader); - return FALSE; - } - - return TRUE; -} - -//----------------------------------------------------------------------------------------------------------- -- (BOOL)linkProgram:(GLuint)prog -{ - GLint status; - - glLinkProgram(prog); - -#if defined(DEBUG) - GLint logLength; - glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); - if (logLength > 0) - { - GLchar *log = (GLchar *)malloc(logLength); - glGetProgramInfoLog(prog, logLength, &logLength, log); - NSLog(@"Program link log:\n%s", log); - free(log); - } -#endif - - glGetProgramiv(prog, GL_LINK_STATUS, &status); - if (status == 0) - return FALSE; - - return TRUE; -} - -//----------------------------------------------------------------------------------------------------------- -- (BOOL)validateProgram:(GLuint)prog -{ - GLint logLength, status; - - glValidateProgram(prog); - glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); - if (logLength > 0) - { - GLchar *log = (GLchar *)malloc(logLength); - glGetProgramInfoLog(prog, logLength, &logLength, log); - NSLog(@"Program validate log:\n%s", log); - free(log); - } - - glGetProgramiv(prog, GL_VALIDATE_STATUS, &status); - if (status == 0) - return FALSE; - - return TRUE; -} - -//----------------------------------------------------------------------------------------------------------- -- (BOOL)loadShaders -{ - GLuint vertShader, fragShader; - NSString *vertShaderPathname, *fragShaderPathname; - - // Create shader program. - program = glCreateProgram(); - - // Create and compile vertex shader. - vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"vsh"]; - if (![self compileShader:&vertShader type:GL_VERTEX_SHADER file:vertShaderPathname]) - { - NSLog(@"Failed to compile vertex shader"); - return FALSE; - } - - // Create and compile fragment shader. - fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"fsh"]; - if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER file:fragShaderPathname]) - { - NSLog(@"Failed to compile fragment shader"); - return FALSE; - } - - // Attach vertex shader to program. - glAttachShader(program, vertShader); - - // Attach fragment shader to program. - glAttachShader(program, fragShader); - - // Bind attribute locations. - // This needs to be done prior to linking. - glBindAttribLocation(program, ATTRIB_VERTEX, "position"); - glBindAttribLocation(program, ATTRIB_COLOR, "color"); - - // Link program. - if (![self linkProgram:program]) - { - NSLog(@"Failed to link program: %d", program); - - if (vertShader) - { - glDeleteShader(vertShader); - vertShader = 0; - } - if (fragShader) - { - glDeleteShader(fragShader); - fragShader = 0; - } - if (program) - { - glDeleteProgram(program); - program = 0; - } - - return FALSE; - } - - // Get uniform locations. - uniforms[UNIFORM_TRANSLATE] = glGetUniformLocation(program, "translate"); - - // Release vertex and fragment shaders. - if (vertShader) - glDeleteShader(vertShader); - if (fragShader) - glDeleteShader(fragShader); - - return TRUE; -} - -@end -- 1.7.0.4