kinect-viewer / KVRenderer.m

Diff from to

KVRenderer.m

 
 @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
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.