├── .gitignore ├── README.markdown ├── Shader.h ├── Shader.m ├── ShaderManager.h ├── ShaderManager.m ├── ShaderManagerDemo ├── ShaderManagerDemo.xcodeproj │ └── project.pbxproj └── ShaderManagerDemo │ ├── EAGLView.h │ ├── EAGLView.m │ ├── ShaderManagerDemo-Info.plist │ ├── ShaderManagerDemo-Prefix.pch │ ├── ShaderManagerDemoAppDelegate.h │ ├── ShaderManagerDemoAppDelegate.m │ ├── ShaderManagerDemoViewController.h │ ├── ShaderManagerDemoViewController.m │ ├── Shaders │ ├── Shader.fsh │ └── Shader.vsh │ ├── en.lproj │ ├── InfoPlist.strings │ ├── MainWindow.xib │ └── ShaderManagerDemoViewController.xib │ └── main.m └── TestShader.plist /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *~ 3 | build 4 | Build 5 | Products 6 | xcuserdata/ 7 | *.pbxuser 8 | *.perspectivev3 9 | *.mode1v3 10 | *.xcworkspace 11 | config 12 | .svn 13 | *.pyc 14 | .DS_STORE 15 | *~ 16 | build/* 17 | *.pbxuser 18 | *.perspectivev3 19 | .svn 20 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | ShaderManager 2 | ============= 3 | 4 | ShaderManager is a simple singleton and class designed to wrap the OpenGL ES shader API. It is intended to provide the simplest API possible to keep track of shader programs, their attributes and uniforms. 5 | 6 | It is not intended to remove the need to interact with OpenGL directly, but as a convenient location to store and access shader data. 7 | 8 | Defining a Shader 9 | ----------------- 10 | Shaders are defined as plist files (in addition to the regular vsh/fsh files). The plist is structured as follows: 11 | 12 | Files 13 | -- array containing the vsh and fsh files to be compiled and linked 14 | Attributes 15 | -- array of strings specifying each attribute in the shader 16 | Uniforms 17 | -- array of strings specifying each uniform in the shader 18 | 19 | Using a Shader 20 | -------------- 21 | ShaderManager consists of a singleton class (ShaderManager) and shader class (Shader) 22 | 23 | ### Loading a shader from a plist 24 | `[[ShaderManager sharedManager] createShader:@"MyShader" withFile:@"MyShader.plist"]` 25 | 26 | Note that the plist file is looked up in the main bundle. You can use the following method to load from an NSDictionary instead. 27 | 28 | `[[ShaderManager sharedManager] createShader:@"MyShader" withSettings:settingsDictionary]` 29 | 30 | ### Using a shader 31 | `[[ShaderManager sharedManager] useShader:@"MyShader"]` 32 | 33 | ### Getting the currently active shader 34 | `Shader *myShader = [[ShaderManager sharedManager] currentShader]` 35 | 36 | ### Setting attributes and uniforms on a shader 37 | 38 | //Uses "MyShader" and returns its shader object 39 | Shader *myShader = [[ShaderManager sharedManager] useShader:@"MyShader"]; 40 | 41 | //Setting a uniform 42 | float translation = 10.0f; 43 | glUniform1f( [myShader uniformLocation:@"translation"], translation ); 44 | 45 | GLuint positionAttribute = [myShader attributeHandle:@"position"]; 46 | glVertexAttribPointer( positionAttribute, 2, GL_FLOAT, 0, 0, vertices ); 47 | glEnableVertexAttribArray( positionAttribute ); 48 | 49 | Sample Project 50 | -------------- 51 | The included sample project is simply Apple's OpenGL template modified to use ShaderManager. -------------------------------------------------------------------------------- /Shader.h: -------------------------------------------------------------------------------- 1 | // 2 | // Shader.h 3 | // TwoLivesLeft.com 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Two Lives Left. All rights reserved. 7 | // 8 | // Permission is given to use this source code file without charge in any 9 | // project, commercial or otherwise, entirely at your risk, with the condition 10 | // that any redistribution (in part or whole) of source code must retain 11 | // this copyright and permission notice. Attribution in compiled projects is 12 | // appreciated but not required. 13 | // 14 | 15 | #import 16 | 17 | #import 18 | #import 19 | #import 20 | #import 21 | 22 | @interface Shader : NSObject 23 | { 24 | GLuint programHandle; 25 | 26 | NSMutableDictionary *attributeHandles; 27 | NSMutableDictionary *uniformHandles; 28 | } 29 | 30 | @property (nonatomic,readonly) GLuint programHandle; 31 | 32 | - (id) initWithShaderSettings:(NSDictionary*)settings; 33 | 34 | - (void) useShader; 35 | - (int) uniformLocation:(NSString*)name; 36 | - (GLuint) attributeHandle:(NSString*)name; 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /Shader.m: -------------------------------------------------------------------------------- 1 | // 2 | // Shader.m 3 | // TwoLivesLeft.com 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Two Lives Left. All rights reserved. 7 | // 8 | // Permission is given to use this source code file without charge in any 9 | // project, commercial or otherwise, entirely at your risk, with the condition 10 | // that any redistribution (in part or whole) of source code must retain 11 | // this copyright and permission notice. Attribution in compiled projects is 12 | // appreciated but not required. 13 | // 14 | 15 | #import "Shader.h" 16 | #import "ShaderManager.h" 17 | 18 | @implementation Shader 19 | 20 | @synthesize programHandle; 21 | 22 | #pragma mark - Shader compiling and linking 23 | 24 | - (BOOL)compileShader:(GLuint *)shader file:(NSString *)file 25 | { 26 | GLint status; 27 | const GLchar *source; 28 | 29 | GLenum type = GL_VERTEX_SHADER; 30 | 31 | NSString *extension = [file pathExtension]; 32 | if( [extension isEqualToString:@"vsh"] ) 33 | { 34 | type = GL_VERTEX_SHADER; 35 | } 36 | else if( [extension isEqualToString:@"fsh"] ) 37 | { 38 | type = GL_FRAGMENT_SHADER; 39 | } 40 | else 41 | { 42 | NSLog(@"Unknown shader file extension"); 43 | return FALSE; 44 | } 45 | 46 | source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String]; 47 | if (!source) 48 | { 49 | NSLog(@"Failed to load vertex shader"); 50 | return FALSE; 51 | } 52 | 53 | *shader = glCreateShader(type); 54 | glShaderSource(*shader, 1, &source, NULL); 55 | glCompileShader(*shader); 56 | 57 | #if defined(DEBUG) 58 | GLint logLength; 59 | glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength); 60 | if (logLength > 0) 61 | { 62 | GLchar *log = (GLchar *)malloc(logLength); 63 | glGetShaderInfoLog(*shader, logLength, &logLength, log); 64 | NSLog(@"Shader compile log:\n%s", log); 65 | free(log); 66 | } 67 | #endif 68 | 69 | glGetShaderiv(*shader, GL_COMPILE_STATUS, &status); 70 | if (status == 0) 71 | { 72 | glDeleteShader(*shader); 73 | return FALSE; 74 | } 75 | 76 | return TRUE; 77 | } 78 | 79 | - (BOOL)linkProgram:(GLuint)prog 80 | { 81 | GLint status; 82 | 83 | glLinkProgram(prog); 84 | 85 | #if defined(DEBUG) 86 | GLint logLength; 87 | glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength); 88 | if (logLength > 0) 89 | { 90 | GLchar *log = (GLchar *)malloc(logLength); 91 | glGetProgramInfoLog(prog, logLength, &logLength, log); 92 | NSLog(@"Program link log:\n%s", log); 93 | free(log); 94 | } 95 | #endif 96 | 97 | glGetProgramiv(prog, GL_LINK_STATUS, &status); 98 | if (status == 0) 99 | return FALSE; 100 | 101 | return TRUE; 102 | } 103 | 104 | #pragma mark - Initialization 105 | 106 | - (id) initWithShaderSettings:(NSDictionary*)settings 107 | { 108 | self = [super init]; 109 | if( self ) 110 | { 111 | NSArray *shaderFiles = [settings objectForKey:@"Files"]; 112 | NSArray *attributes = [settings objectForKey:@"Attributes"]; 113 | NSArray *uniforms = [settings objectForKey:@"Uniforms"]; 114 | 115 | attributeHandles = [[NSMutableDictionary dictionary] retain]; 116 | uniformHandles = [[NSMutableDictionary dictionary] retain]; 117 | 118 | programHandle = glCreateProgram(); 119 | 120 | NSMutableArray *shaderList = [NSMutableArray array]; 121 | 122 | for( NSString *file in shaderFiles ) 123 | { 124 | GLuint shader; 125 | 126 | if( ![self compileShader:&shader file:[[NSBundle mainBundle] pathForResource:file ofType:@""]] ) 127 | { 128 | NSLog(@"Failed to compile shader: %@", file); 129 | } 130 | 131 | [shaderList addObject:[NSNumber numberWithInt:shader]]; 132 | } 133 | 134 | for( NSNumber *shader in shaderList ) 135 | { 136 | //Attach each shader to the program 137 | glAttachShader( programHandle, [shader unsignedIntValue] ); 138 | } 139 | 140 | GLuint attribLoc = 1; 141 | for( NSString *attribute in attributes ) 142 | { 143 | glBindAttribLocation(programHandle, attribLoc, [attribute UTF8String]); 144 | 145 | [attributeHandles setObject:[NSNumber numberWithInt:attribLoc] forKey:attribute]; 146 | 147 | attribLoc += 1; 148 | } 149 | 150 | if( ![self linkProgram:programHandle] ) 151 | { 152 | NSLog(@"Failed to link program: %d", programHandle); 153 | 154 | for( NSNumber *shader in shaderList ) 155 | { 156 | if( [shader unsignedIntValue] ) 157 | { 158 | glDeleteShader([shader unsignedIntValue]); 159 | } 160 | } 161 | 162 | if (programHandle) 163 | { 164 | glDeleteProgram(programHandle); 165 | programHandle = 0; 166 | } 167 | } 168 | 169 | if( programHandle ) 170 | { 171 | for( NSString *uniform in uniforms ) 172 | { 173 | int uniformHandle = glGetUniformLocation(programHandle, [uniform UTF8String]); 174 | 175 | [uniformHandles setObject:[NSNumber numberWithInt:uniformHandle] forKey:uniform]; 176 | } 177 | } 178 | } 179 | return self; 180 | } 181 | 182 | - (void) dealloc 183 | { 184 | if( programHandle ) 185 | { 186 | glDeleteProgram(programHandle); 187 | programHandle = 0; 188 | } 189 | 190 | [attributeHandles release]; 191 | [uniformHandles release]; 192 | 193 | [super dealloc]; 194 | } 195 | 196 | #pragma mark - Using the shader 197 | 198 | - (void) useShader 199 | { 200 | [[ShaderManager sharedManager] useShaderObject:self]; 201 | } 202 | 203 | #pragma mark - Access to uniforms and attributes 204 | 205 | - (int) uniformLocation:(NSString*)name 206 | { 207 | return [[uniformHandles objectForKey:name] intValue]; 208 | } 209 | 210 | - (GLuint) attributeHandle:(NSString*)name 211 | { 212 | return [[attributeHandles objectForKey:name] unsignedIntValue]; 213 | } 214 | 215 | @end 216 | -------------------------------------------------------------------------------- /ShaderManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // ShaderManager.h 3 | // TwoLivesLeft.com 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Two Lives Left. All rights reserved. 7 | // 8 | // Permission is given to use this source code file without charge in any 9 | // project, commercial or otherwise, entirely at your risk, with the condition 10 | // that any redistribution (in part or whole) of source code must retain 11 | // this copyright and permission notice. Attribution in compiled projects is 12 | // appreciated but not required. 13 | // 14 | 15 | #import 16 | 17 | #import "Shader.h" 18 | 19 | #import 20 | #import 21 | #import 22 | #import 23 | 24 | @interface ShaderManager : NSObject 25 | { 26 | NSMutableDictionary *shaderPrograms; 27 | 28 | GLuint activeProgram; 29 | Shader *currentShader; 30 | } 31 | 32 | + (ShaderManager*) sharedManager; 33 | 34 | - (NSArray*) allShaders; 35 | 36 | - (Shader*) currentShader; 37 | - (Shader*) useShader:(NSString*)name; 38 | - (Shader*) shaderForName:(NSString*)name; 39 | 40 | - (Shader*) createShader:(NSString*)name withSettings:(NSDictionary*)settings; 41 | - (Shader*) createShader:(NSString*)name withFile:(NSString*)file; 42 | 43 | - (void) useShaderObject:(Shader*)shader; 44 | 45 | - (void) removeShader:(NSString*)name; 46 | - (void) removeAllShaders; 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /ShaderManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // ShaderManager.m 3 | // TwoLivesLeft.com 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Two Lives Left. All rights reserved. 7 | // 8 | // Permission is given to use this source code file without charge in any 9 | // project, commercial or otherwise, entirely at your risk, with the condition 10 | // that any redistribution (in part or whole) of source code must retain 11 | // this copyright and permission notice. Attribution in compiled projects is 12 | // appreciated but not required. 13 | // 14 | 15 | #import "ShaderManager.h" 16 | 17 | @implementation ShaderManager 18 | 19 | static ShaderManager *sharedShaderManager; 20 | 21 | #pragma mark - Singleton 22 | 23 | + (void)initialize 24 | { 25 | static BOOL initialized = NO; 26 | if(!initialized) 27 | { 28 | initialized = YES; 29 | sharedShaderManager = [[ShaderManager alloc] init]; 30 | } 31 | } 32 | 33 | + (id)allocWithZone:(NSZone *)zone 34 | { 35 | @synchronized(self) 36 | { 37 | if (sharedShaderManager == nil) 38 | { 39 | sharedShaderManager = [super allocWithZone:zone]; 40 | return sharedShaderManager; 41 | } 42 | } 43 | 44 | return nil; 45 | } 46 | 47 | - (id)copyWithZone:(NSZone *)zone 48 | { 49 | return self; 50 | } 51 | 52 | + (ShaderManager*) sharedManager 53 | { 54 | return sharedShaderManager; 55 | } 56 | 57 | - (NSUInteger)retainCount 58 | { 59 | return NSUIntegerMax; 60 | } 61 | 62 | - (oneway void)release 63 | { 64 | } 65 | 66 | - (id)retain 67 | { 68 | return sharedShaderManager; 69 | } 70 | 71 | - (id)autorelease 72 | { 73 | return sharedShaderManager; 74 | } 75 | 76 | #pragma mark - Initialization 77 | 78 | - (id) init 79 | { 80 | self = [super init]; 81 | if(self) 82 | { 83 | shaderPrograms = [[NSMutableDictionary dictionary] retain]; 84 | activeProgram = 0; 85 | } 86 | return self; 87 | } 88 | 89 | #pragma mark - Memory 90 | 91 | - (void) dealloc 92 | { 93 | [shaderPrograms release]; 94 | [super dealloc]; 95 | } 96 | 97 | #pragma mark - Shader management 98 | 99 | - (NSArray*) allShaders 100 | { 101 | return [shaderPrograms allValues]; 102 | } 103 | 104 | - (Shader*) currentShader 105 | { 106 | return currentShader; 107 | } 108 | 109 | - (Shader*) useShader:(NSString*)name 110 | { 111 | currentShader = [self shaderForName:name]; 112 | 113 | if( activeProgram != currentShader.programHandle ) 114 | { 115 | glUseProgram(currentShader.programHandle); 116 | 117 | activeProgram = currentShader.programHandle; 118 | } 119 | 120 | return currentShader; 121 | } 122 | 123 | - (void) useShaderObject:(Shader*)shader 124 | { 125 | currentShader = shader; 126 | 127 | if( activeProgram != currentShader.programHandle ) 128 | { 129 | glUseProgram(currentShader.programHandle); 130 | 131 | activeProgram = currentShader.programHandle; 132 | } 133 | } 134 | 135 | - (Shader*) shaderForName:(NSString *)name 136 | { 137 | return [shaderPrograms objectForKey:name]; 138 | } 139 | 140 | - (Shader*) createShader:(NSString*)name withSettings:(NSDictionary *)settings 141 | { 142 | Shader *shader = [[[Shader alloc] initWithShaderSettings:settings] autorelease]; 143 | 144 | Shader *existingShader = [self shaderForName:name]; 145 | if( existingShader != nil ) 146 | { 147 | [self removeShader:name]; 148 | } 149 | 150 | [shaderPrograms setObject:shader forKey:name]; 151 | 152 | return shader; 153 | } 154 | 155 | - (Shader*) createShader:(NSString*)name withFile:(NSString*)file 156 | { 157 | NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:@""]]; 158 | 159 | return [self createShader:name withSettings:dict]; 160 | } 161 | 162 | - (void) removeAllShaders 163 | { 164 | [shaderPrograms removeAllObjects]; 165 | } 166 | 167 | - (void) removeShader:(NSString*)name 168 | { 169 | Shader *shader = [self shaderForName:name]; 170 | 171 | if( shader ) 172 | { 173 | if( shader.programHandle == activeProgram ) 174 | { 175 | glUseProgram(0); 176 | } 177 | 178 | [shaderPrograms removeObjectForKey:name]; 179 | } 180 | } 181 | 182 | @end 183 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | DBEAAA88139251750064BCE6 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DBEAAA87139251750064BCE6 /* UIKit.framework */; }; 11 | DBEAAA8A139251750064BCE6 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DBEAAA89139251750064BCE6 /* Foundation.framework */; }; 12 | DBEAAA8C139251750064BCE6 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DBEAAA8B139251750064BCE6 /* CoreGraphics.framework */; }; 13 | DBEAAA8E139251750064BCE6 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DBEAAA8D139251750064BCE6 /* QuartzCore.framework */; }; 14 | DBEAAA90139251750064BCE6 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DBEAAA8F139251750064BCE6 /* OpenGLES.framework */; }; 15 | DBEAAA96139251750064BCE6 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = DBEAAA94139251750064BCE6 /* InfoPlist.strings */; }; 16 | DBEAAA99139251750064BCE6 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DBEAAA98139251750064BCE6 /* main.m */; }; 17 | DBEAAA9C139251750064BCE6 /* ShaderManagerDemoAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = DBEAAA9B139251750064BCE6 /* ShaderManagerDemoAppDelegate.m */; }; 18 | DBEAAA9F139251750064BCE6 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = DBEAAA9D139251750064BCE6 /* MainWindow.xib */; }; 19 | DBEAAAA1139251750064BCE6 /* Shader.fsh in Resources */ = {isa = PBXBuildFile; fileRef = DBEAAAA0139251750064BCE6 /* Shader.fsh */; }; 20 | DBEAAAA3139251750064BCE6 /* Shader.vsh in Resources */ = {isa = PBXBuildFile; fileRef = DBEAAAA2139251750064BCE6 /* Shader.vsh */; }; 21 | DBEAAAA6139251750064BCE6 /* EAGLView.m in Sources */ = {isa = PBXBuildFile; fileRef = DBEAAAA5139251750064BCE6 /* EAGLView.m */; }; 22 | DBEAAAA9139251750064BCE6 /* ShaderManagerDemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DBEAAAA8139251750064BCE6 /* ShaderManagerDemoViewController.m */; }; 23 | DBEAAAAC139251750064BCE6 /* ShaderManagerDemoViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = DBEAAAAA139251750064BCE6 /* ShaderManagerDemoViewController.xib */; }; 24 | DBEAAAB7139251D90064BCE6 /* Shader.m in Sources */ = {isa = PBXBuildFile; fileRef = DBEAAAB4139251D90064BCE6 /* Shader.m */; }; 25 | DBEAAAB8139251D90064BCE6 /* ShaderManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DBEAAAB6139251D90064BCE6 /* ShaderManager.m */; }; 26 | DBEAAABA139252850064BCE6 /* TestShader.plist in Resources */ = {isa = PBXBuildFile; fileRef = DBEAAAB9139252850064BCE6 /* TestShader.plist */; }; 27 | DBEAAABC13926DC40064BCE6 /* README.markdown in Resources */ = {isa = PBXBuildFile; fileRef = DBEAAABB13926DC40064BCE6 /* README.markdown */; }; 28 | /* End PBXBuildFile section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | DBEAAA83139251750064BCE6 /* ShaderManagerDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ShaderManagerDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 32 | DBEAAA87139251750064BCE6 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 33 | DBEAAA89139251750064BCE6 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 34 | DBEAAA8B139251750064BCE6 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; 35 | DBEAAA8D139251750064BCE6 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; 36 | DBEAAA8F139251750064BCE6 /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; }; 37 | DBEAAA93139251750064BCE6 /* ShaderManagerDemo-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "ShaderManagerDemo-Info.plist"; sourceTree = ""; }; 38 | DBEAAA95139251750064BCE6 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 39 | DBEAAA97139251750064BCE6 /* ShaderManagerDemo-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ShaderManagerDemo-Prefix.pch"; sourceTree = ""; }; 40 | DBEAAA98139251750064BCE6 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 41 | DBEAAA9A139251750064BCE6 /* ShaderManagerDemoAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ShaderManagerDemoAppDelegate.h; sourceTree = ""; }; 42 | DBEAAA9B139251750064BCE6 /* ShaderManagerDemoAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ShaderManagerDemoAppDelegate.m; sourceTree = ""; }; 43 | DBEAAA9E139251750064BCE6 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainWindow.xib; sourceTree = ""; }; 44 | DBEAAAA0139251750064BCE6 /* Shader.fsh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; name = Shader.fsh; path = Shaders/Shader.fsh; sourceTree = ""; }; 45 | DBEAAAA2139251750064BCE6 /* Shader.vsh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.glsl; name = Shader.vsh; path = Shaders/Shader.vsh; sourceTree = ""; }; 46 | DBEAAAA4139251750064BCE6 /* EAGLView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EAGLView.h; sourceTree = ""; }; 47 | DBEAAAA5139251750064BCE6 /* EAGLView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EAGLView.m; sourceTree = ""; }; 48 | DBEAAAA7139251750064BCE6 /* ShaderManagerDemoViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ShaderManagerDemoViewController.h; sourceTree = ""; }; 49 | DBEAAAA8139251750064BCE6 /* ShaderManagerDemoViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ShaderManagerDemoViewController.m; sourceTree = ""; }; 50 | DBEAAAAB139251750064BCE6 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ShaderManagerDemoViewController.xib; sourceTree = ""; }; 51 | DBEAAAB3139251D90064BCE6 /* Shader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Shader.h; path = ../Shader.h; sourceTree = ""; }; 52 | DBEAAAB4139251D90064BCE6 /* Shader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Shader.m; path = ../Shader.m; sourceTree = ""; }; 53 | DBEAAAB5139251D90064BCE6 /* ShaderManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ShaderManager.h; path = ../ShaderManager.h; sourceTree = ""; }; 54 | DBEAAAB6139251D90064BCE6 /* ShaderManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ShaderManager.m; path = ../ShaderManager.m; sourceTree = ""; }; 55 | DBEAAAB9139252850064BCE6 /* TestShader.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = TestShader.plist; path = ../TestShader.plist; sourceTree = ""; }; 56 | DBEAAABB13926DC40064BCE6 /* README.markdown */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.markdown; path = ../README.markdown; sourceTree = ""; }; 57 | /* End PBXFileReference section */ 58 | 59 | /* Begin PBXFrameworksBuildPhase section */ 60 | DBEAAA80139251750064BCE6 /* Frameworks */ = { 61 | isa = PBXFrameworksBuildPhase; 62 | buildActionMask = 2147483647; 63 | files = ( 64 | DBEAAA88139251750064BCE6 /* UIKit.framework in Frameworks */, 65 | DBEAAA8A139251750064BCE6 /* Foundation.framework in Frameworks */, 66 | DBEAAA8C139251750064BCE6 /* CoreGraphics.framework in Frameworks */, 67 | DBEAAA8E139251750064BCE6 /* QuartzCore.framework in Frameworks */, 68 | DBEAAA90139251750064BCE6 /* OpenGLES.framework in Frameworks */, 69 | ); 70 | runOnlyForDeploymentPostprocessing = 0; 71 | }; 72 | /* End PBXFrameworksBuildPhase section */ 73 | 74 | /* Begin PBXGroup section */ 75 | DBEAAA78139251740064BCE6 = { 76 | isa = PBXGroup; 77 | children = ( 78 | DBEAAABB13926DC40064BCE6 /* README.markdown */, 79 | DBEAAAB2139251CC0064BCE6 /* ShaderManager */, 80 | DBEAAA91139251750064BCE6 /* ShaderManagerDemo */, 81 | DBEAAA86139251750064BCE6 /* Frameworks */, 82 | DBEAAA84139251750064BCE6 /* Products */, 83 | ); 84 | sourceTree = ""; 85 | }; 86 | DBEAAA84139251750064BCE6 /* Products */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | DBEAAA83139251750064BCE6 /* ShaderManagerDemo.app */, 90 | ); 91 | name = Products; 92 | sourceTree = ""; 93 | }; 94 | DBEAAA86139251750064BCE6 /* Frameworks */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | DBEAAA87139251750064BCE6 /* UIKit.framework */, 98 | DBEAAA89139251750064BCE6 /* Foundation.framework */, 99 | DBEAAA8B139251750064BCE6 /* CoreGraphics.framework */, 100 | DBEAAA8D139251750064BCE6 /* QuartzCore.framework */, 101 | DBEAAA8F139251750064BCE6 /* OpenGLES.framework */, 102 | ); 103 | name = Frameworks; 104 | sourceTree = ""; 105 | }; 106 | DBEAAA91139251750064BCE6 /* ShaderManagerDemo */ = { 107 | isa = PBXGroup; 108 | children = ( 109 | DBEAAA9A139251750064BCE6 /* ShaderManagerDemoAppDelegate.h */, 110 | DBEAAA9B139251750064BCE6 /* ShaderManagerDemoAppDelegate.m */, 111 | DBEAAA9D139251750064BCE6 /* MainWindow.xib */, 112 | DBEAAAA0139251750064BCE6 /* Shader.fsh */, 113 | DBEAAAA2139251750064BCE6 /* Shader.vsh */, 114 | DBEAAAA4139251750064BCE6 /* EAGLView.h */, 115 | DBEAAAA5139251750064BCE6 /* EAGLView.m */, 116 | DBEAAAA7139251750064BCE6 /* ShaderManagerDemoViewController.h */, 117 | DBEAAAA8139251750064BCE6 /* ShaderManagerDemoViewController.m */, 118 | DBEAAAAA139251750064BCE6 /* ShaderManagerDemoViewController.xib */, 119 | DBEAAA92139251750064BCE6 /* Supporting Files */, 120 | ); 121 | path = ShaderManagerDemo; 122 | sourceTree = ""; 123 | }; 124 | DBEAAA92139251750064BCE6 /* Supporting Files */ = { 125 | isa = PBXGroup; 126 | children = ( 127 | DBEAAA93139251750064BCE6 /* ShaderManagerDemo-Info.plist */, 128 | DBEAAA94139251750064BCE6 /* InfoPlist.strings */, 129 | DBEAAA97139251750064BCE6 /* ShaderManagerDemo-Prefix.pch */, 130 | DBEAAA98139251750064BCE6 /* main.m */, 131 | ); 132 | name = "Supporting Files"; 133 | sourceTree = ""; 134 | }; 135 | DBEAAAB2139251CC0064BCE6 /* ShaderManager */ = { 136 | isa = PBXGroup; 137 | children = ( 138 | DBEAAAB3139251D90064BCE6 /* Shader.h */, 139 | DBEAAAB4139251D90064BCE6 /* Shader.m */, 140 | DBEAAAB5139251D90064BCE6 /* ShaderManager.h */, 141 | DBEAAAB6139251D90064BCE6 /* ShaderManager.m */, 142 | DBEAAAB9139252850064BCE6 /* TestShader.plist */, 143 | ); 144 | name = ShaderManager; 145 | sourceTree = ""; 146 | }; 147 | /* End PBXGroup section */ 148 | 149 | /* Begin PBXNativeTarget section */ 150 | DBEAAA82139251750064BCE6 /* ShaderManagerDemo */ = { 151 | isa = PBXNativeTarget; 152 | buildConfigurationList = DBEAAAAF139251750064BCE6 /* Build configuration list for PBXNativeTarget "ShaderManagerDemo" */; 153 | buildPhases = ( 154 | DBEAAA7F139251750064BCE6 /* Sources */, 155 | DBEAAA80139251750064BCE6 /* Frameworks */, 156 | DBEAAA81139251750064BCE6 /* Resources */, 157 | ); 158 | buildRules = ( 159 | ); 160 | dependencies = ( 161 | ); 162 | name = ShaderManagerDemo; 163 | productName = ShaderManagerDemo; 164 | productReference = DBEAAA83139251750064BCE6 /* ShaderManagerDemo.app */; 165 | productType = "com.apple.product-type.application"; 166 | }; 167 | /* End PBXNativeTarget section */ 168 | 169 | /* Begin PBXProject section */ 170 | DBEAAA7A139251740064BCE6 /* Project object */ = { 171 | isa = PBXProject; 172 | attributes = { 173 | ORGANIZATIONNAME = Developer; 174 | }; 175 | buildConfigurationList = DBEAAA7D139251740064BCE6 /* Build configuration list for PBXProject "ShaderManagerDemo" */; 176 | compatibilityVersion = "Xcode 3.2"; 177 | developmentRegion = English; 178 | hasScannedForEncodings = 0; 179 | knownRegions = ( 180 | en, 181 | ); 182 | mainGroup = DBEAAA78139251740064BCE6; 183 | productRefGroup = DBEAAA84139251750064BCE6 /* Products */; 184 | projectDirPath = ""; 185 | projectRoot = ""; 186 | targets = ( 187 | DBEAAA82139251750064BCE6 /* ShaderManagerDemo */, 188 | ); 189 | }; 190 | /* End PBXProject section */ 191 | 192 | /* Begin PBXResourcesBuildPhase section */ 193 | DBEAAA81139251750064BCE6 /* Resources */ = { 194 | isa = PBXResourcesBuildPhase; 195 | buildActionMask = 2147483647; 196 | files = ( 197 | DBEAAA96139251750064BCE6 /* InfoPlist.strings in Resources */, 198 | DBEAAA9F139251750064BCE6 /* MainWindow.xib in Resources */, 199 | DBEAAAA1139251750064BCE6 /* Shader.fsh in Resources */, 200 | DBEAAAA3139251750064BCE6 /* Shader.vsh in Resources */, 201 | DBEAAAAC139251750064BCE6 /* ShaderManagerDemoViewController.xib in Resources */, 202 | DBEAAABA139252850064BCE6 /* TestShader.plist in Resources */, 203 | DBEAAABC13926DC40064BCE6 /* README.markdown in Resources */, 204 | ); 205 | runOnlyForDeploymentPostprocessing = 0; 206 | }; 207 | /* End PBXResourcesBuildPhase section */ 208 | 209 | /* Begin PBXSourcesBuildPhase section */ 210 | DBEAAA7F139251750064BCE6 /* Sources */ = { 211 | isa = PBXSourcesBuildPhase; 212 | buildActionMask = 2147483647; 213 | files = ( 214 | DBEAAA99139251750064BCE6 /* main.m in Sources */, 215 | DBEAAA9C139251750064BCE6 /* ShaderManagerDemoAppDelegate.m in Sources */, 216 | DBEAAAA6139251750064BCE6 /* EAGLView.m in Sources */, 217 | DBEAAAA9139251750064BCE6 /* ShaderManagerDemoViewController.m in Sources */, 218 | DBEAAAB7139251D90064BCE6 /* Shader.m in Sources */, 219 | DBEAAAB8139251D90064BCE6 /* ShaderManager.m in Sources */, 220 | ); 221 | runOnlyForDeploymentPostprocessing = 0; 222 | }; 223 | /* End PBXSourcesBuildPhase section */ 224 | 225 | /* Begin PBXVariantGroup section */ 226 | DBEAAA94139251750064BCE6 /* InfoPlist.strings */ = { 227 | isa = PBXVariantGroup; 228 | children = ( 229 | DBEAAA95139251750064BCE6 /* en */, 230 | ); 231 | name = InfoPlist.strings; 232 | sourceTree = ""; 233 | }; 234 | DBEAAA9D139251750064BCE6 /* MainWindow.xib */ = { 235 | isa = PBXVariantGroup; 236 | children = ( 237 | DBEAAA9E139251750064BCE6 /* en */, 238 | ); 239 | name = MainWindow.xib; 240 | sourceTree = ""; 241 | }; 242 | DBEAAAAA139251750064BCE6 /* ShaderManagerDemoViewController.xib */ = { 243 | isa = PBXVariantGroup; 244 | children = ( 245 | DBEAAAAB139251750064BCE6 /* en */, 246 | ); 247 | name = ShaderManagerDemoViewController.xib; 248 | sourceTree = ""; 249 | }; 250 | /* End PBXVariantGroup section */ 251 | 252 | /* Begin XCBuildConfiguration section */ 253 | DBEAAAAD139251750064BCE6 /* Debug */ = { 254 | isa = XCBuildConfiguration; 255 | buildSettings = { 256 | ARCHS = "$(ARCHS_UNIVERSAL_IPHONE_OS)"; 257 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 258 | GCC_C_LANGUAGE_STANDARD = gnu99; 259 | GCC_OPTIMIZATION_LEVEL = 0; 260 | GCC_PREPROCESSOR_DEFINITIONS = DEBUG; 261 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 262 | GCC_VERSION = com.apple.compilers.llvmgcc42; 263 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 264 | GCC_WARN_UNUSED_VARIABLE = YES; 265 | IPHONEOS_DEPLOYMENT_TARGET = 4.3; 266 | SDKROOT = iphoneos; 267 | TARGETED_DEVICE_FAMILY = 2; 268 | }; 269 | name = Debug; 270 | }; 271 | DBEAAAAE139251750064BCE6 /* Release */ = { 272 | isa = XCBuildConfiguration; 273 | buildSettings = { 274 | ARCHS = "$(ARCHS_UNIVERSAL_IPHONE_OS)"; 275 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 276 | GCC_C_LANGUAGE_STANDARD = gnu99; 277 | GCC_VERSION = com.apple.compilers.llvmgcc42; 278 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 279 | GCC_WARN_UNUSED_VARIABLE = YES; 280 | IPHONEOS_DEPLOYMENT_TARGET = 4.3; 281 | OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; 282 | SDKROOT = iphoneos; 283 | TARGETED_DEVICE_FAMILY = 2; 284 | }; 285 | name = Release; 286 | }; 287 | DBEAAAB0139251750064BCE6 /* Debug */ = { 288 | isa = XCBuildConfiguration; 289 | buildSettings = { 290 | ALWAYS_SEARCH_USER_PATHS = NO; 291 | COPY_PHASE_STRIP = NO; 292 | GCC_DYNAMIC_NO_PIC = NO; 293 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 294 | GCC_PREFIX_HEADER = "ShaderManagerDemo/ShaderManagerDemo-Prefix.pch"; 295 | "GCC_THUMB_SUPPORT[arch=armv6]" = ""; 296 | INFOPLIST_FILE = "ShaderManagerDemo/ShaderManagerDemo-Info.plist"; 297 | PRODUCT_NAME = "$(TARGET_NAME)"; 298 | WRAPPER_EXTENSION = app; 299 | }; 300 | name = Debug; 301 | }; 302 | DBEAAAB1139251750064BCE6 /* Release */ = { 303 | isa = XCBuildConfiguration; 304 | buildSettings = { 305 | ALWAYS_SEARCH_USER_PATHS = NO; 306 | COPY_PHASE_STRIP = YES; 307 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 308 | GCC_PREFIX_HEADER = "ShaderManagerDemo/ShaderManagerDemo-Prefix.pch"; 309 | "GCC_THUMB_SUPPORT[arch=armv6]" = ""; 310 | INFOPLIST_FILE = "ShaderManagerDemo/ShaderManagerDemo-Info.plist"; 311 | PRODUCT_NAME = "$(TARGET_NAME)"; 312 | VALIDATE_PRODUCT = YES; 313 | WRAPPER_EXTENSION = app; 314 | }; 315 | name = Release; 316 | }; 317 | /* End XCBuildConfiguration section */ 318 | 319 | /* Begin XCConfigurationList section */ 320 | DBEAAA7D139251740064BCE6 /* Build configuration list for PBXProject "ShaderManagerDemo" */ = { 321 | isa = XCConfigurationList; 322 | buildConfigurations = ( 323 | DBEAAAAD139251750064BCE6 /* Debug */, 324 | DBEAAAAE139251750064BCE6 /* Release */, 325 | ); 326 | defaultConfigurationIsVisible = 0; 327 | defaultConfigurationName = Release; 328 | }; 329 | DBEAAAAF139251750064BCE6 /* Build configuration list for PBXNativeTarget "ShaderManagerDemo" */ = { 330 | isa = XCConfigurationList; 331 | buildConfigurations = ( 332 | DBEAAAB0139251750064BCE6 /* Debug */, 333 | DBEAAAB1139251750064BCE6 /* Release */, 334 | ); 335 | defaultConfigurationIsVisible = 0; 336 | }; 337 | /* End XCConfigurationList section */ 338 | }; 339 | rootObject = DBEAAA7A139251740064BCE6 /* Project object */; 340 | } 341 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/EAGLView.h: -------------------------------------------------------------------------------- 1 | // 2 | // EAGLView.h 3 | // OpenGLES_iPhone 4 | // 5 | // Created by mmalc Crawford on 11/18/10. 6 | // Copyright 2010 Apple Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import 12 | #import 13 | #import 14 | #import 15 | 16 | @class EAGLContext; 17 | 18 | // This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass. 19 | // The view content is basically an EAGL surface you render your OpenGL scene into. 20 | // Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel. 21 | @interface EAGLView : UIView { 22 | @private 23 | // The pixel dimensions of the CAEAGLLayer. 24 | GLint framebufferWidth; 25 | GLint framebufferHeight; 26 | 27 | // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view. 28 | GLuint defaultFramebuffer, colorRenderbuffer; 29 | } 30 | 31 | @property (nonatomic, retain) EAGLContext *context; 32 | 33 | - (void)setFramebuffer; 34 | - (BOOL)presentFramebuffer; 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/EAGLView.m: -------------------------------------------------------------------------------- 1 | // 2 | // EAGLView.m 3 | // OpenGLES_iPhone 4 | // 5 | // Created by mmalc Crawford on 11/18/10. 6 | // Copyright 2010 Apple Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import "EAGLView.h" 12 | 13 | @interface EAGLView (PrivateMethods) 14 | - (void)createFramebuffer; 15 | - (void)deleteFramebuffer; 16 | @end 17 | 18 | @implementation EAGLView 19 | 20 | @synthesize context; 21 | 22 | // You must implement this method 23 | + (Class)layerClass 24 | { 25 | return [CAEAGLLayer class]; 26 | } 27 | 28 | //The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:. 29 | - (id)initWithCoder:(NSCoder*)coder 30 | { 31 | self = [super initWithCoder:coder]; 32 | if (self) { 33 | CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; 34 | 35 | eaglLayer.opaque = TRUE; 36 | eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 37 | [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, 38 | kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, 39 | nil]; 40 | } 41 | 42 | return self; 43 | } 44 | 45 | - (void)dealloc 46 | { 47 | [self deleteFramebuffer]; 48 | [context release]; 49 | 50 | [super dealloc]; 51 | } 52 | 53 | - (void)setContext:(EAGLContext *)newContext 54 | { 55 | if (context != newContext) { 56 | [self deleteFramebuffer]; 57 | 58 | [context release]; 59 | context = [newContext retain]; 60 | 61 | [EAGLContext setCurrentContext:nil]; 62 | } 63 | } 64 | 65 | - (void)createFramebuffer 66 | { 67 | if (context && !defaultFramebuffer) { 68 | [EAGLContext setCurrentContext:context]; 69 | 70 | // Create default framebuffer object. 71 | glGenFramebuffers(1, &defaultFramebuffer); 72 | glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer); 73 | 74 | // Create color render buffer and allocate backing store. 75 | glGenRenderbuffers(1, &colorRenderbuffer); 76 | glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); 77 | [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer]; 78 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth); 79 | glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight); 80 | 81 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer); 82 | 83 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 84 | NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); 85 | } 86 | } 87 | 88 | - (void)deleteFramebuffer 89 | { 90 | if (context) { 91 | [EAGLContext setCurrentContext:context]; 92 | 93 | if (defaultFramebuffer) { 94 | glDeleteFramebuffers(1, &defaultFramebuffer); 95 | defaultFramebuffer = 0; 96 | } 97 | 98 | if (colorRenderbuffer) { 99 | glDeleteRenderbuffers(1, &colorRenderbuffer); 100 | colorRenderbuffer = 0; 101 | } 102 | } 103 | } 104 | 105 | - (void)setFramebuffer 106 | { 107 | if (context) { 108 | [EAGLContext setCurrentContext:context]; 109 | 110 | if (!defaultFramebuffer) 111 | [self createFramebuffer]; 112 | 113 | glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer); 114 | 115 | glViewport(0, 0, framebufferWidth, framebufferHeight); 116 | } 117 | } 118 | 119 | - (BOOL)presentFramebuffer 120 | { 121 | BOOL success = FALSE; 122 | 123 | if (context) { 124 | [EAGLContext setCurrentContext:context]; 125 | 126 | glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer); 127 | 128 | success = [context presentRenderbuffer:GL_RENDERBUFFER]; 129 | } 130 | 131 | return success; 132 | } 133 | 134 | - (void)layoutSubviews 135 | { 136 | // The framebuffer will be re-created at the beginning of the next setFramebuffer method call. 137 | [self deleteFramebuffer]; 138 | } 139 | 140 | @end 141 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/ShaderManagerDemo-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIconFile 12 | 13 | CFBundleIdentifier 14 | com.twolivesleft.${PRODUCT_NAME:rfc1034identifier} 15 | CFBundleInfoDictionaryVersion 16 | 6.0 17 | CFBundleName 18 | ${PRODUCT_NAME} 19 | CFBundlePackageType 20 | APPL 21 | CFBundleShortVersionString 22 | 1.0 23 | CFBundleSignature 24 | ???? 25 | CFBundleVersion 26 | 1.0 27 | LSRequiresIPhoneOS 28 | 29 | NSMainNibFile 30 | MainWindow 31 | UIStatusBarHidden 32 | 33 | UISupportedInterfaceOrientations~ipad 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationPortraitUpsideDown 37 | UIInterfaceOrientationLandscapeLeft 38 | UIInterfaceOrientationLandscapeRight 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/ShaderManagerDemo-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'ShaderManagerDemo' target in the 'ShaderManagerDemo' project 3 | // 4 | 5 | #import 6 | 7 | #ifndef __IPHONE_3_0 8 | #warning "This project uses features only available in iPhone SDK 3.0 and later." 9 | #endif 10 | 11 | #ifdef __OBJC__ 12 | #import 13 | #import 14 | #endif 15 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/ShaderManagerDemoAppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // ShaderManagerDemoAppDelegate.h 3 | // ShaderManagerDemo 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Two Lives Left. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @class ShaderManagerDemoViewController; 12 | 13 | @interface ShaderManagerDemoAppDelegate : NSObject 14 | { 15 | 16 | } 17 | 18 | @property (nonatomic, retain) IBOutlet UIWindow *window; 19 | 20 | @property (nonatomic, retain) IBOutlet ShaderManagerDemoViewController *viewController; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/ShaderManagerDemoAppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // ShaderManagerDemoAppDelegate.m 3 | // ShaderManagerDemo 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Two Lives Left. All rights reserved. 7 | // 8 | 9 | #import "ShaderManagerDemoAppDelegate.h" 10 | 11 | #import "EAGLView.h" 12 | 13 | #import "ShaderManagerDemoViewController.h" 14 | 15 | @implementation ShaderManagerDemoAppDelegate 16 | 17 | 18 | @synthesize window=_window; 19 | 20 | @synthesize viewController=_viewController; 21 | 22 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 23 | { 24 | // Override point for customization after application launch. 25 | self.window.rootViewController = self.viewController; 26 | return YES; 27 | } 28 | 29 | - (void)applicationWillResignActive:(UIApplication *)application 30 | { 31 | /* 32 | Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 33 | Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 34 | */ 35 | [self.viewController stopAnimation]; 36 | } 37 | 38 | - (void)applicationDidEnterBackground:(UIApplication *)application 39 | { 40 | /* 41 | Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 42 | If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 43 | */ 44 | } 45 | 46 | - (void)applicationWillEnterForeground:(UIApplication *)application 47 | { 48 | /* 49 | Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 50 | */ 51 | } 52 | 53 | - (void)applicationDidBecomeActive:(UIApplication *)application 54 | { 55 | /* 56 | Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 57 | */ 58 | [self.viewController startAnimation]; 59 | } 60 | 61 | - (void)applicationWillTerminate:(UIApplication *)application 62 | { 63 | /* 64 | Called when the application is about to terminate. 65 | Save data if appropriate. 66 | See also applicationDidEnterBackground:. 67 | */ 68 | [self.viewController stopAnimation]; 69 | } 70 | 71 | - (void)dealloc 72 | { 73 | [_window release]; 74 | [_viewController release]; 75 | [super dealloc]; 76 | } 77 | 78 | @end 79 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/ShaderManagerDemoViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ShaderManagerDemoViewController.h 3 | // ShaderManagerDemo 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Developer. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import 12 | 13 | #import 14 | #import 15 | #import 16 | #import 17 | 18 | @interface ShaderManagerDemoViewController : UIViewController 19 | { 20 | @private 21 | EAGLContext *context; 22 | 23 | BOOL animating; 24 | NSInteger animationFrameInterval; 25 | CADisplayLink *displayLink; 26 | } 27 | 28 | @property (readonly, nonatomic, getter=isAnimating) BOOL animating; 29 | @property (nonatomic) NSInteger animationFrameInterval; 30 | 31 | - (void)startAnimation; 32 | - (void)stopAnimation; 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/ShaderManagerDemoViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ShaderManagerDemoViewController.m 3 | // ShaderManagerDemo 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Two Lives Left. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import "ShaderManagerDemoViewController.h" 12 | #import "EAGLView.h" 13 | 14 | #import "ShaderManager.h" 15 | 16 | @interface ShaderManagerDemoViewController () 17 | @property (nonatomic, retain) EAGLContext *context; 18 | @property (nonatomic, assign) CADisplayLink *displayLink; 19 | @end 20 | 21 | @implementation ShaderManagerDemoViewController 22 | 23 | @synthesize animating, context, displayLink; 24 | 25 | - (void)awakeFromNib 26 | { 27 | EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 28 | 29 | if (!aContext) { 30 | aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; 31 | } 32 | 33 | if (!aContext) 34 | NSLog(@"Failed to create ES context"); 35 | else if (![EAGLContext setCurrentContext:aContext]) 36 | NSLog(@"Failed to set ES context current"); 37 | 38 | self.context = aContext; 39 | [aContext release]; 40 | 41 | [(EAGLView *)self.view setContext:context]; 42 | [(EAGLView *)self.view setFramebuffer]; 43 | 44 | if ([context API] == kEAGLRenderingAPIOpenGLES2) 45 | { 46 | //Load our simple test shader 47 | [[ShaderManager sharedManager] createShader:@"TestShader" withFile:@"TestShader.plist"]; 48 | } 49 | 50 | animating = FALSE; 51 | animationFrameInterval = 1; 52 | self.displayLink = nil; 53 | } 54 | 55 | - (void)dealloc 56 | { 57 | // Delete all shader programs 58 | [[ShaderManager sharedManager] removeAllShaders]; 59 | 60 | // Tear down context. 61 | if ([EAGLContext currentContext] == context) 62 | [EAGLContext setCurrentContext:nil]; 63 | 64 | [context release]; 65 | 66 | [super dealloc]; 67 | } 68 | 69 | - (void)didReceiveMemoryWarning 70 | { 71 | // Releases the view if it doesn't have a superview. 72 | [super didReceiveMemoryWarning]; 73 | 74 | // Release any cached data, images, etc. that aren't in use. 75 | } 76 | 77 | - (void)viewWillAppear:(BOOL)animated 78 | { 79 | [self startAnimation]; 80 | 81 | [super viewWillAppear:animated]; 82 | } 83 | 84 | - (void)viewWillDisappear:(BOOL)animated 85 | { 86 | [self stopAnimation]; 87 | 88 | [super viewWillDisappear:animated]; 89 | } 90 | 91 | - (void)viewDidUnload 92 | { 93 | [super viewDidUnload]; 94 | 95 | // Delete all shader programs 96 | [[ShaderManager sharedManager] removeAllShaders]; 97 | 98 | // Tear down context. 99 | if ([EAGLContext currentContext] == context) 100 | [EAGLContext setCurrentContext:nil]; 101 | self.context = nil; 102 | } 103 | 104 | - (NSInteger)animationFrameInterval 105 | { 106 | return animationFrameInterval; 107 | } 108 | 109 | - (void)setAnimationFrameInterval:(NSInteger)frameInterval 110 | { 111 | /* 112 | Frame interval defines how many display frames must pass between each time the display link fires. 113 | 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. 114 | */ 115 | if (frameInterval >= 1) { 116 | animationFrameInterval = frameInterval; 117 | 118 | if (animating) { 119 | [self stopAnimation]; 120 | [self startAnimation]; 121 | } 122 | } 123 | } 124 | 125 | - (void)startAnimation 126 | { 127 | if (!animating) { 128 | CADisplayLink *aDisplayLink = [[UIScreen mainScreen] displayLinkWithTarget:self selector:@selector(drawFrame)]; 129 | [aDisplayLink setFrameInterval:animationFrameInterval]; 130 | [aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 131 | self.displayLink = aDisplayLink; 132 | 133 | animating = TRUE; 134 | } 135 | } 136 | 137 | - (void)stopAnimation 138 | { 139 | if (animating) { 140 | [self.displayLink invalidate]; 141 | self.displayLink = nil; 142 | animating = FALSE; 143 | } 144 | } 145 | 146 | - (void)drawFrame 147 | { 148 | [(EAGLView *)self.view setFramebuffer]; 149 | 150 | // Replace the implementation of this method to do your own custom drawing. 151 | static const GLfloat squareVertices[] = { 152 | -0.5f, -0.33f, 153 | 0.5f, -0.33f, 154 | -0.5f, 0.33f, 155 | 0.5f, 0.33f, 156 | }; 157 | 158 | static const GLubyte squareColors[] = { 159 | 255, 255, 0, 255, 160 | 0, 255, 255, 255, 161 | 0, 0, 0, 0, 162 | 255, 0, 255, 255, 163 | }; 164 | 165 | static float transY = 0.0f; 166 | 167 | glClearColor(0.5f, 0.5f, 0.5f, 1.0f); 168 | glClear(GL_COLOR_BUFFER_BIT); 169 | 170 | if ([context API] == kEAGLRenderingAPIOpenGLES2) 171 | { 172 | // This sets the current shader, and returns it so we can configure it 173 | Shader *testShader = [[ShaderManager sharedManager] useShader:@"TestShader"]; 174 | 175 | // Update uniform value. 176 | glUniform1f([testShader uniformLocation:@"translate"], (GLfloat)transY); 177 | transY += 0.075f; 178 | 179 | // Update attribute values. 180 | GLuint vertexAttrib = [testShader attributeHandle:@"position"]; 181 | glVertexAttribPointer(vertexAttrib, 2, GL_FLOAT, 0, 0, squareVertices); 182 | glEnableVertexAttribArray(vertexAttrib); 183 | 184 | GLuint colorAttrib = [testShader attributeHandle:@"color"]; 185 | glVertexAttribPointer(colorAttrib, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors); 186 | glEnableVertexAttribArray(colorAttrib); 187 | } 188 | else 189 | { 190 | glMatrixMode(GL_PROJECTION); 191 | glLoadIdentity(); 192 | glMatrixMode(GL_MODELVIEW); 193 | glLoadIdentity(); 194 | glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f); 195 | transY += 0.075f; 196 | 197 | glVertexPointer(2, GL_FLOAT, 0, squareVertices); 198 | glEnableClientState(GL_VERTEX_ARRAY); 199 | glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors); 200 | glEnableClientState(GL_COLOR_ARRAY); 201 | } 202 | 203 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 204 | 205 | [(EAGLView *)self.view presentFramebuffer]; 206 | } 207 | 208 | 209 | @end 210 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/Shaders/Shader.fsh: -------------------------------------------------------------------------------- 1 | // 2 | // Shader.fsh 3 | // ShaderManagerDemo 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Developer. All rights reserved. 7 | // 8 | 9 | varying lowp vec4 colorVarying; 10 | 11 | void main() 12 | { 13 | gl_FragColor = colorVarying; 14 | } 15 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/Shaders/Shader.vsh: -------------------------------------------------------------------------------- 1 | // 2 | // Shader.vsh 3 | // ShaderManagerDemo 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Developer. All rights reserved. 7 | // 8 | 9 | attribute vec4 position; 10 | attribute vec4 color; 11 | 12 | varying vec4 colorVarying; 13 | 14 | uniform float translate; 15 | 16 | void main() 17 | { 18 | gl_Position = position; 19 | gl_Position.y += sin(translate) / 2.0; 20 | 21 | colorVarying = color; 22 | } 23 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/en.lproj/MainWindow.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 800 5 | 10H574 6 | 1248 7 | 1038.35 8 | 461.00 9 | 10 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 11 | 106 12 | 13 | 14 | YES 15 | IBUIWindow 16 | IBUICustomObject 17 | IBUIViewController 18 | IBUIView 19 | IBProxyObject 20 | 21 | 22 | YES 23 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 24 | 25 | 26 | YES 27 | 28 | YES 29 | 30 | 31 | 32 | 33 | YES 34 | 35 | IBFilesOwner 36 | IBIPadFramework 37 | 38 | 39 | IBFirstResponder 40 | IBIPadFramework 41 | 42 | 43 | 44 | 292 45 | 46 | YES 47 | 48 | 49 | 274 50 | {768, 1004} 51 | 52 | 53 | 54 | 55 | 3 56 | MQA 57 | 58 | 2 59 | 60 | 61 | NO 62 | IBIPadFramework 63 | 64 | 65 | {768, 1004} 66 | 67 | 68 | 69 | 70 | 1 71 | MSAxIDEAA 72 | 73 | NO 74 | NO 75 | IBIPadFramework 76 | YES 77 | YES 78 | 79 | 80 | IBIPadFramework 81 | 82 | 83 | 84 | 2 85 | 86 | 87 | 1 88 | 1 89 | 90 | IBIPadFramework 91 | NO 92 | 93 | 94 | 95 | 96 | YES 97 | 98 | 99 | window 100 | 101 | 102 | 103 | 7 104 | 105 | 106 | 107 | delegate 108 | 109 | 110 | 111 | 8 112 | 113 | 114 | 115 | viewController 116 | 117 | 118 | 119 | 12 120 | 121 | 122 | 123 | 124 | YES 125 | 126 | 0 127 | 128 | 129 | 130 | 131 | 132 | -1 133 | 134 | 135 | File's Owner 136 | 137 | 138 | -2 139 | 140 | 141 | 142 | 143 | 2 144 | 145 | 146 | YES 147 | 148 | 149 | 150 | 151 | 152 | 6 153 | 154 | 155 | 156 | 157 | 9 158 | 159 | 160 | 161 | 162 | 11 163 | 164 | 165 | 166 | 167 | 168 | 169 | YES 170 | 171 | YES 172 | -1.CustomClassName 173 | -2.CustomClassName 174 | 11.CustomClassName 175 | 11.IBPluginDependency 176 | 2.IBEditorWindowLastContentRect 177 | 2.IBPluginDependency 178 | 6.CustomClassName 179 | 6.IBPluginDependency 180 | 9.CustomClassName 181 | 9.IBPluginDependency 182 | 183 | 184 | YES 185 | UIApplication 186 | UIResponder 187 | ShaderManagerDemoViewController 188 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 189 | {{185, 50}, {783, 806}} 190 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 191 | ShaderManagerDemoAppDelegate 192 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 193 | EAGLView 194 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 195 | 196 | 197 | 198 | YES 199 | 200 | 201 | 202 | 203 | 204 | YES 205 | 206 | 207 | 208 | 209 | 12 210 | 211 | 212 | 213 | YES 214 | 215 | EAGLView 216 | UIView 217 | 218 | IBProjectSource 219 | ./classes-xjh84/EAGLView.h 220 | 221 | 222 | 223 | 224 | 0 225 | IBIPadFramework 226 | 227 | com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS 228 | 229 | 230 | 231 | com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3 232 | 233 | 234 | YES 235 | OpenGLES_iPad.xcodeproj 236 | 3 237 | 106 238 | 239 | 240 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/en.lproj/ShaderManagerDemoViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1056 5 | 10H574 6 | 1248 7 | 1038.35 8 | 461.00 9 | 10 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 11 | 106 12 | 13 | 14 | YES 15 | IBProxyObject 16 | IBUIView 17 | 18 | 19 | YES 20 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 21 | 22 | 23 | YES 24 | 25 | YES 26 | 27 | 28 | 29 | 30 | YES 31 | 32 | IBFilesOwner 33 | IBIPadFramework 34 | 35 | 36 | IBFirstResponder 37 | IBIPadFramework 38 | 39 | 40 | 41 | 274 42 | {{0, 20}, {768, 1004}} 43 | 44 | 45 | 46 | 47 | 3 48 | MQA 49 | 50 | 2 51 | 52 | 53 | 54 | 2 55 | 56 | IBIPadFramework 57 | 58 | 59 | 60 | 61 | YES 62 | 63 | 64 | view 65 | 66 | 67 | 68 | 3 69 | 70 | 71 | 72 | 73 | YES 74 | 75 | 0 76 | 77 | 78 | 79 | 80 | 81 | 1 82 | 83 | 84 | 85 | 86 | -1 87 | 88 | 89 | File's Owner 90 | 91 | 92 | -2 93 | 94 | 95 | 96 | 97 | 98 | 99 | YES 100 | 101 | YES 102 | -1.CustomClassName 103 | -2.CustomClassName 104 | 1.CustomClassName 105 | 1.IBEditorWindowLastContentRect 106 | 1.IBPluginDependency 107 | 108 | 109 | YES 110 | ShaderManagerDemoViewController 111 | UIResponder 112 | EAGLView 113 | {{514, 109}, {768, 1024}} 114 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 115 | 116 | 117 | 118 | YES 119 | 120 | 121 | 122 | 123 | 124 | YES 125 | 126 | 127 | 128 | 129 | 3 130 | 131 | 132 | 133 | YES 134 | 135 | EAGLView 136 | UIView 137 | 138 | IBProjectSource 139 | ./classes-xjh84/EAGLView.h 140 | 141 | 142 | 143 | 144 | 0 145 | IBIPadFramework 146 | 147 | com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3 148 | 149 | 150 | YES 151 | 152 | 3 153 | 106 154 | 155 | 156 | -------------------------------------------------------------------------------- /ShaderManagerDemo/ShaderManagerDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // ShaderManagerDemo 4 | // 5 | // Created by Simeon Nasilowski on 29/05/11. 6 | // Copyright 2011 Developer. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 14 | int retVal = UIApplicationMain(argc, argv, nil, nil); 15 | [pool release]; 16 | return retVal; 17 | } 18 | -------------------------------------------------------------------------------- /TestShader.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Attributes 6 | 7 | position 8 | color 9 | 10 | Files 11 | 12 | Shader.vsh 13 | Shader.fsh 14 | 15 | Uniforms 16 | 17 | translate 18 | 19 | 20 | 21 | --------------------------------------------------------------------------------