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.
It is not intended to remove the need to interact with OpenGL directly, but as a convenient location to store and access shader data.
Shaders are defined as plist files (in addition to the regular vsh/fsh files). The plist is structured as follows:
Files
-- array containing the vsh and fsh files to be compiled and linked
Attributes
-- array of strings specifying each attribute in the shader
Uniforms
-- array of strings specifying each uniform in the shader
ShaderManager consists of a singleton class (ShaderManager) and shader class (Shader)
[[ShaderManager sharedManager] createShader:@"MyShader" withFile:@"MyShader.plist"]
Note that the plist file is looked up in the main bundle. You can use the following method to load from an NSDictionary instead.
[[ShaderManager sharedManager] createShader:@"MyShader" withSettings:settingsDictionary]
[[ShaderManager sharedManager] useShader:@"MyShader"]
Shader *myShader = [[ShaderManager sharedManager] currentShader]
//Uses "MyShader" and returns its shader object
Shader *myShader = [[ShaderManager sharedManager] useShader:@"MyShader"];
//Setting a uniform
float translation = 10.0f;
glUniform1f( [myShader uniformLocation:@"translation"], translation );
GLuint positionAttribute = [myShader attributeHandle:@"position"];
glVertexAttribPointer( positionAttribute, 2, GL_FLOAT, 0, 0, vertices );
glEnableVertexAttribArray( positionAttribute );
The included sample project is simply Apple's OpenGL template modified to use ShaderManager.