Cliff Biffle avatar Cliff Biffle committed d0f32bb

Refactored GL initialization, which has been kind of screwed up. The Renderer is now in charge of all of its vertex-shader mumbo-jumbo.

Comments (0)

Files changed (3)

 - (void) updateProjectionWithOffset: (float) offset;
 - (void) updateModelViewWithOffset: (float) offset;
 - (void) updateTextureMatrix;
-- (GLuint) loadShaderOfType: (GLenum) type fromFiles: (NSArray *) names;
 
 @property(assign, nonatomic) BOOL updateQueued;
 - (void) requestUpdateFromBackgroundThread;
 - (void) markAsDirty;
 
-@property GLuint program;
-@property GLuint textureEnableUniform;
-
 @end
 
 @implementation KVDepthView
 #pragma mark --- Properties ---
 
-@synthesize program;
-@synthesize textureEnableUniform;
 @synthesize updateQueued;
 
 @synthesize textured;
 #pragma mark --- GL Stuff ---
 
 - (void) prepareOpenGL {
-  GLint vertexShader = [self loadShaderOfType: GL_VERTEX_SHADER
-                                    fromFiles: [NSArray arrayWithObject: @"main"]];
-  
-  GLint fragmentShader = [self loadShaderOfType: GL_FRAGMENT_SHADER
-                                      fromFiles: [NSArray arrayWithObjects: @"lighting", @"main", nil]];
-  
-  program = glCreateProgram();
-  glAttachShader(program, vertexShader);
-  glAttachShader(program, fragmentShader);
-  glLinkProgram(program);
-  glUseProgram(program);
-  
   // Sync to vertical blank.
   GLint swapInterval = 1;
   [[self openGLContext] setValues: &swapInterval forParameter: NSOpenGLCPSwapInterval];
   glEnable(GL_COLOR_MATERIAL);
   
   glClearColor(0.3f, 0, 0.5f, 0);
-  
-  GLint depthUniform = glGetUniformLocation(program, "depthTex");
-  glUniform1i(depthUniform, 0);
-
-  GLint colorUniform = glGetUniformLocation(program, "colorTex");
-  glUniform1i(colorUniform, 1);
-  
-  textureEnableUniform = glGetUniformLocation(program, "useTextureFromCamera");
-  glUniform1i(textureEnableUniform, 1);
 
   [self resetCamera];
   [renderer initGL];
   [self resizeGL];
   [self updateTextureMatrix];
   
-  glUniform1i(textureEnableUniform, textured);
-    
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   
   if (self.anaglyph) {
   self.updateQueued = NO;
 }
 
-- (GLuint) loadShaderOfType: (GLenum) type fromFiles: (NSArray *) names {
-  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];
-  }
-
-  const char *sources[[names count]];
-  GLint lengths[[names count]];
-  int i = 0;
-  for (NSString *name in names) {
-    NSData *source = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: name ofType: extension]];
-    sources[i] = [source bytes];
-    lengths[i] = (GLint) [source length];
-    i++;
-  }
-  
-  GLuint shader = glCreateShader(type);
-  glShaderSource(shader, i, &sources[0], &lengths[0]);
-  glCompileShader(shader);
-  
-  return shader;
-}
-
 - (void) requestUpdateFromBackgroundThread {
   @synchronized (self) {
     if (!self.updateQueued) {
 
 @interface KVRenderer ()
 - (void) buildPointField;
+- (GLuint) loadShaderOfType: (GLenum) type fromFiles: (NSArray *) names;
 
 @property(assign, nonatomic) double rayCastingTotal;
 @property(assign, nonatomic) NSUInteger rayCastingCount;
 
 @property GLuint depthTexture;
 @property GLuint colorTexture;
+@property GLuint program;
 
 - (void) renderAsPointsWithOptions: (NSDictionary *) options;
 @end
 
 @synthesize rayCastingCount, rayCastingTotal, vertexPlacementCount, vertexPlacementTotal;
 @synthesize depthTexture, colorTexture;
+@synthesize program;
 
 - init {
   if ((self = [super init])) {
 #pragma mark --- Implementation of KVSpaceModel
 
 - (void) initGL {
+  GLint vertexShader = [self loadShaderOfType: GL_VERTEX_SHADER
+                                    fromFiles: [NSArray arrayWithObject: @"main"]];
+  
+  GLint fragmentShader = [self loadShaderOfType: GL_FRAGMENT_SHADER
+                                      fromFiles: [NSArray arrayWithObjects: @"lighting", @"main", nil]];
+  
+  program = glCreateProgram();
+  glAttachShader(program, vertexShader);
+  glAttachShader(program, fragmentShader);
+  glLinkProgram(program);
+  glUseProgram(program);
+  
+  GLint depthUniform = glGetUniformLocation(program, "depthTex");
+  glUniform1i(depthUniform, 0);
+  
+  GLint colorUniform = glGetUniformLocation(program, "colorTex");
+  glUniform1i(colorUniform, 1);
+  
+  
   glGenTextures(1, &depthTexture);
   glBindTexture(GL_TEXTURE_2D, depthTexture);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   }
 }
 
+- (GLuint) loadShaderOfType: (GLenum) type fromFiles: (NSArray *) names {
+  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];
+  }
+  
+  const char *sources[[names count]];
+  GLint lengths[[names count]];
+  int i = 0;
+  for (NSString *name in names) {
+    NSData *source = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: name ofType: extension]];
+    sources[i] = [source bytes];
+    lengths[i] = (GLint) [source length];
+    i++;
+  }
+  
+  GLuint shader = glCreateShader(type);
+  glShaderSource(shader, i, &sources[0], &lengths[0]);
+  glCompileShader(shader);
+  
+  return shader;
+}
+
 @end
  
 uniform sampler2D depthTex;
 uniform sampler2D colorTex;
-uniform bool useTextureFromCamera;
 
 /*
  * Kinect Projection Correction
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.