Cliff Biffle avatar Cliff Biffle committed f60d65c

Moved lighting into the fragment shaders...not that it makes a whole lot of difference at the moment, since I don't believe normals are actually being interpolated.

Comments (0)

Files changed (6)

 #pragma mark --- GL Stuff ---
 
 - (void) prepareOpenGL {
-  NSArray *vertexShaderFiles = [NSArray arrayWithObjects: @"lighting", @"main", nil];
   GLint vertexShader = [self loadShaderOfType: GL_VERTEX_SHADER
-                                    fromFiles: vertexShaderFiles];
+                                    fromFiles: [NSArray arrayWithObject: @"main"]];
   
   GLint fragmentShader = [self loadShaderOfType: GL_FRAGMENT_SHADER
-                                      fromFiles: [NSArray arrayWithObject: @"main"]];
+                                      fromFiles: [NSArray arrayWithObjects: @"lighting", @"main", nil]];
   
   program = glCreateProgram();
   glAttachShader(program, vertexShader);

KinectViewer.xcodeproj/project.pbxproj

 		C7759C1012A97A3B003479EF /* KVDepthPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = C7759C0F12A97A3B003479EF /* KVDepthPlayer.m */; };
 		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 */; };
+		C775ADF012B99B60003479EF /* lighting.fs in Resources */ = {isa = PBXBuildFile; fileRef = C775ADEF12B99B60003479EF /* lighting.fs */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
 		C7759C0F12A97A3B003479EF /* KVDepthPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KVDepthPlayer.m; 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>"; };
+		C775ADEF12B99B60003479EF /* lighting.fs */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = lighting.fs; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
 		C775A0D312AC08D4003479EF /* Shaders */ = {
 			isa = PBXGroup;
 			children = (
-				C775A1CC12AC2022003479EF /* lighting.vs */,
+				C775ADEF12B99B60003479EF /* lighting.fs */,
 				C775A1AD12AC1DF4003479EF /* main.vs */,
 				C775A11F12AC1749003479EF /* main.fs */,
 			);
 			files = (
 				C775A1AE12AC1DF4003479EF /* main.vs in Resources */,
 				C775A13312AC18EB003479EF /* main.fs in Resources */,
-				C775A1CD12AC2022003479EF /* lighting.vs in Resources */,
+				C775ADF012B99B60003479EF /* lighting.fs in Resources */,
 				8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */,
 				1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */,
 			);
+/*
+ * 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;
+}

lighting.vs

-/*
- * 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.
+ */
+
+varying vec3 normal;
+
 void main() {
   if (gl_Color.a == 0.) {
     discard;
   }
-  gl_FragColor = gl_Color;
+  gl_FragColor = light_vertex(normal);
 }
   return gl_TextureMatrix[0] * kinectPoint;
 }
 
+varying vec3 normal;
+
 const float pixelDistanceX = 1. / 640.;
 const float pixelDistanceY = 1. / 480.;
 
   vec3 tangent = down_world - vertex_world;
   vec3 bitangent = right_world - vertex_world;
   
-  vec3 normal = cross(tangent, bitangent);
-
+  normal = cross(tangent, bitangent);
+  
+  // Abuse the color channel to communicate data validity.
   if (vertex.z <= 0.) {
-    gl_FrontColor = vec4(0,0,0,0);
+    gl_FrontColor = vec4(gl_Color.xyz, 0);
   } else {
-    gl_FrontColor = light_vertex(normal);
+    gl_FrontColor = gl_Color;
   }
 }
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.