Commits

Cliff Biffle committed 46f9829

Split the vertex shader up into multiple files.

Comments (0)

Files changed (5)

 - (void) updateProjection;
 - (void) updateModelView;
 - (void) updateColorTransform;
-- (GLuint) loadShader: (NSString *) name ofType: (GLenum) type;
+- (GLuint) loadShaderOfType: (GLenum) type fromFiles: (NSArray *) names;
 
 @property(assign, nonatomic) BOOL updateQueued;
 - (void) requestUpdateFromBackgroundThread;
 #pragma mark --- GL Stuff ---
 
 - (void) prepareOpenGL {
-  vertexShader = [self loadShader: @"kinect_project" ofType: GL_VERTEX_SHADER];
-  fragmentShader = [self loadShader: @"main" ofType: GL_FRAGMENT_SHADER];
+  NSArray *vertexShaderFiles = [NSArray arrayWithObjects: @"kinect_project", @"lighting", @"main", nil];
+  vertexShader = [self loadShaderOfType: GL_VERTEX_SHADER
+                                              fromFiles: vertexShaderFiles];
+  
+  fragmentShader = [self loadShaderOfType: GL_FRAGMENT_SHADER
+                                fromFiles: [NSArray arrayWithObject: @"main"]];
   
   program = glCreateProgram();
   glAttachShader(program, vertexShader);
   self.updateQueued = NO;
 }
 
-- (GLuint) loadShader: (NSString *) name ofType: (GLenum) type {
+- (GLuint) loadShaderOfType: (GLenum) type fromFiles: (NSArray *) names {
   NSString *extension;
   if (type == GL_VERTEX_SHADER) {
     extension = @"vs";
   } else {
     @throw [NSException exceptionWithName: @"KVContractError" reason: @"Bad value for shader type" userInfo: nil];
   }
-  
-  NSData *source = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: name ofType: extension]];
+
+  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);
-  const char *sourcePointer = [source bytes];
-  GLint sourceLength = (GLint) [source length];
-  glShaderSource(shader, 1, &sourcePointer, &sourceLength);
+  glShaderSource(shader, i, &sources[0], &lengths[0]);
   glCompileShader(shader);
   
   return shader;

KinectViewer.xcodeproj/project.pbxproj

 		C7759C1012A97A3B003479EF /* KVDepthPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = C7759C0F12A97A3B003479EF /* KVDepthPlayer.m */; };
 		C775A0F812AC111E003479EF /* kinect_project.vs in Resources */ = {isa = PBXBuildFile; fileRef = C775A0D512AC0942003479EF /* kinect_project.vs */; };
 		C775A13312AC18EB003479EF /* main.fs in Resources */ = {isa = PBXBuildFile; fileRef = C775A11F12AC1749003479EF /* main.fs */; };
+		C775A1AE12AC1DF4003479EF /* main.vs in Resources */ = {isa = PBXBuildFile; fileRef = C775A1AD12AC1DF4003479EF /* main.vs */; };
+		C775A1CD12AC2022003479EF /* lighting.vs in Resources */ = {isa = PBXBuildFile; fileRef = C775A1CC12AC2022003479EF /* lighting.vs */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
 		C7759C0F12A97A3B003479EF /* KVDepthPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KVDepthPlayer.m; sourceTree = "<group>"; };
 		C775A0D512AC0942003479EF /* kinect_project.vs */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = kinect_project.vs; sourceTree = "<group>"; };
 		C775A11F12AC1749003479EF /* main.fs */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = main.fs; sourceTree = "<group>"; };
+		C775A1AD12AC1DF4003479EF /* main.vs */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = main.vs; sourceTree = "<group>"; };
+		C775A1CC12AC2022003479EF /* lighting.vs */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = lighting.vs; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
 			isa = PBXGroup;
 			children = (
 				C775A0D512AC0942003479EF /* kinect_project.vs */,
+				C775A1CC12AC2022003479EF /* lighting.vs */,
+				C775A1AD12AC1DF4003479EF /* main.vs */,
 				C775A11F12AC1749003479EF /* main.fs */,
 			);
 			name = Shaders;
 			isa = PBXResourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				C775A1AE12AC1DF4003479EF /* main.vs in Resources */,
 				C775A13312AC18EB003479EF /* main.fs in Resources */,
 				C775A0F812AC111E003479EF /* kinect_project.vs in Resources */,
 				8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */,
 				1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */,
+				C775A1CD12AC2022003479EF /* lighting.vs in Resources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

kinect_project.vs

  */
  
 /*
- * Kinect Projection Vertex Shader
+ * Kinect Projection Routines
  *
  * Since the Kinect is, fundamentally, a camera, its data has already
  * undergone perspective projection.  Closer objects seem relatively
  */
 vec3 kinect_unproject_normal(vec3 normal, vec3 cameraOrigin, vec3 glOrigin) {
   return kinect_unproject(normal + cameraOrigin) - glOrigin;
-}
-
-/*
- * Computes lighting per-vertex using Blinn-Phong, plus diffuse/ambient.
- */
-vec4 light_vertex(vec3 normal) {
-  vec3 eyeSpaceNormal = normalize(gl_NormalMatrix * normal);
-  // Note: this forces all lights to be directional lights.
-  vec3 lightDir = normalize(vec3(gl_LightSource[0].position));
-  float NdotL = max(dot(eyeSpaceNormal, lightDir), 0.0);
-  
-  // Diffuse
-  vec4 diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
-  vec4 ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient
-               + gl_FrontMaterial.ambient * gl_LightModel.ambient;
-  vec4 specular = vec4(0, 0, 0, 0);
-  if (NdotL > 0.0) {
-    float NdotHV = max(dot(eyeSpaceNormal, gl_LightSource[0].halfVector.xyz), 0.0);
-    specular = gl_FrontMaterial.specular * gl_LightSource[0].specular
-             * pow(NdotHV, gl_FrontMaterial.shininess);
-  }
-  
-  return NdotL * diffuse + ambient + specular;
-}
-
-/*
- * Vertex Shader entry point.
- */
-void main() {
-  // Undo the Kinect's perspective projection, leaving the homogeneous term.
-  vec4 vertex = vec4(kinect_unproject(gl_Vertex.xyz), gl_Vertex.w);
-  
-  // Project the point using GL's perspective settings.
-  gl_Position = gl_ModelViewProjectionMatrix * vertex;
-
-  // The normal was computed in Kinect-space.  Unproject it too.
-  vec3 normal = kinect_unproject_normal(normalize(gl_Normal), gl_Vertex.xyz, vertex.xyz);
-  
-  if (vertex.z <= 0.) {
-    gl_FrontColor = vec4(0,0,0,0);
-  } else {
-    gl_FrontColor = light_vertex(normal);
-  }
 }
+/*
+ * Copyright 2010 Cliff L. Biffle.  All Rights Reserved.
+ * Use of this source code is governed by the Apache License 2.0,
+ * which can be found in the LICENSE file.
+ */
+
+/*
+ * Computes lighting per-vertex using Blinn-Phong, plus diffuse/ambient.
+ */
+vec4 light_vertex(vec3 normal) {
+  vec3 eyeSpaceNormal = normalize(gl_NormalMatrix * normal);
+  // Note: this forces all lights to be directional lights.
+  vec3 lightDir = normalize(vec3(gl_LightSource[0].position));
+  float NdotL = max(dot(eyeSpaceNormal, lightDir), 0.0);
+  
+  // Diffuse
+  vec4 diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
+  vec4 ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient
+               + gl_FrontMaterial.ambient * gl_LightModel.ambient;
+  vec4 specular = vec4(0, 0, 0, 0);
+  if (NdotL > 0.0) {
+    float NdotHV = max(dot(eyeSpaceNormal, gl_LightSource[0].halfVector.xyz), 0.0);
+    specular = gl_FrontMaterial.specular * gl_LightSource[0].specular
+             * pow(NdotHV, gl_FrontMaterial.shininess);
+  }
+  
+  return NdotL * diffuse + ambient + specular;
+}
+/*
+ * Copyright 2010 Cliff L. Biffle.  All Rights Reserved.
+ * Use of this source code is governed by the Apache License 2.0,
+ * which can be found in the LICENSE file.
+ */
+
+/*
+ * Vertex Shader entry point.
+ */
+void main() {
+  // Undo the Kinect's perspective projection, leaving the homogeneous term.
+  vec4 vertex = vec4(kinect_unproject(gl_Vertex.xyz), gl_Vertex.w);
+  
+  // Project the point using GL's perspective settings.
+  gl_Position = gl_ModelViewProjectionMatrix * vertex;
+
+  // The normal was computed in Kinect-space.  Unproject it too.
+  vec3 normal = kinect_unproject_normal(normalize(gl_Normal), gl_Vertex.xyz, vertex.xyz);
+  
+  if (vertex.z <= 0.) {
+    gl_FrontColor = vec4(0,0,0,0);
+  } else {
+    gl_FrontColor = light_vertex(normal);
+  }
+}