Commits

Cliff Biffle committed 7c7c96b

Factored shader loading code into a separate method, for when there are multiple shaders.

  • Participants
  • Parent commits 22fef44
  • Branches shaders

Comments (0)

Files changed (1)

 - (void) updateProjection;
 - (void) updateModelView;
 - (void) updateColorTransform;
+- (GLuint) loadShader: (NSString *) name ofType: (GLenum) type;
 
 @property(assign, nonatomic) BOOL updateQueued;
 - (void) requestUpdateFromBackgroundThread;
 #pragma mark --- GL Stuff ---
 
 - (void) prepareOpenGL {
-  NSData *vertexShaderSource = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"kinect_project" ofType: @"vs"]];
-  
-  vertexShader = glCreateShader(GL_VERTEX_SHADER);
-  const char *sourcePointer = [vertexShaderSource bytes];
-  GLint sourceLength = (GLint) [vertexShaderSource length];
-  glShaderSource(vertexShader, 1, &sourcePointer, &sourceLength);
-  glCompileShader(vertexShader);
+  vertexShader = [self loadShader: @"kinect_project" ofType: GL_VERTEX_SHADER];
   
   program = glCreateProgram();
   glAttachShader(program, vertexShader);
   self.updateQueued = NO;
 }
 
+- (GLuint) loadShader: (NSString *) name ofType: (GLenum) type {
+  NSString *extension;
+  if (type == GL_VERTEX_SHADER) {
+    extension = @"vs";
+  } else if (type == GL_FRAGMENT_SHADER) {
+    extension = @"fs";
+  } else if (type == GL_GEOMETRY_SHADER_EXT) {
+    extension = @"gs";
+  } else {
+    @throw [NSException exceptionWithName: @"KVContractError" reason: @"Bad value for shader type" userInfo: nil];
+  }
+  
+  NSData *source = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: name ofType: extension]];
+  
+  GLuint shader = glCreateShader(type);
+  const char *sourcePointer = [source bytes];
+  GLint sourceLength = (GLint) [source length];
+  glShaderSource(shader, 1, &sourcePointer, &sourceLength);
+  glCompileShader(shader);
+  
+  return shader;
+}
+
 - (void) requestUpdateFromBackgroundThread {
   @synchronized (self) {
     if (!self.updateQueued) {