Cliff Biffle avatar Cliff Biffle committed 2892936

Moved lighting into the vertex shader. As of my previous commit lighting was simply broken -- now it is closer to functional, though the normals are not transformed with the vertices and thus tend to point toward the camera more than they should.

Comments (0)

Files changed (5)

English.lproj/MainMenu.xib

 		</object>
 		<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
 			<bool key="EncodedWithXMLCoder">YES</bool>
+			<integer value="296"/>
 			<integer value="533"/>
-			<integer value="296"/>
 		</object>
 		<object class="NSArray" key="IBDocument.PluginDependencies">
 			<bool key="EncodedWithXMLCoder">YES</bool>
 									<reference key="NSOnImage" ref="35465992"/>
 									<reference key="NSMixedImage" ref="502551668"/>
 								</object>
-								<object class="NSMenuItem" id="433488773">
-									<reference key="NSMenu" ref="466310130"/>
-									<string key="NSTitle">Use Lighting</string>
-									<string key="NSKeyEquiv">l</string>
-									<int key="NSKeyEquivModMask">1048576</int>
-									<int key="NSMnemonicLoc">2147483647</int>
-									<reference key="NSOnImage" ref="35465992"/>
-									<reference key="NSMixedImage" ref="502551668"/>
-								</object>
 								<object class="NSMenuItem" id="26014547">
 									<reference key="NSMenu" ref="466310130"/>
 									<string key="NSTitle">Anaglyph</string>
 				</object>
 				<object class="IBConnectionRecord">
 					<object class="IBActionConnection" key="connection">
-						<string key="label">toggleLighting:</string>
-						<reference key="source" ref="1014"/>
-						<reference key="destination" ref="433488773"/>
-					</object>
-					<int key="connectionID">553</int>
-				</object>
-				<object class="IBConnectionRecord">
-					<object class="IBActionConnection" key="connection">
 						<string key="label">toggleRecording:</string>
 						<reference key="source" ref="1014"/>
 						<reference key="destination" ref="1064970212"/>
 							<reference ref="477715379"/>
 							<reference ref="578250298"/>
 							<reference ref="406940919"/>
-							<reference ref="433488773"/>
 							<reference ref="26014547"/>
 							<reference ref="264846174"/>
 							<reference ref="1032511002"/>
 						<reference key="parent" ref="466310130"/>
 					</object>
 					<object class="IBObjectRecord">
-						<int key="objectID">552</int>
-						<reference key="object" ref="433488773"/>
-						<reference key="parent" ref="466310130"/>
-					</object>
-					<object class="IBObjectRecord">
 						<int key="objectID">556</int>
 						<reference key="object" ref="1064970212"/>
 						<reference key="parent" ref="720053764"/>
 					<string>539.IBPluginDependency</string>
 					<string>541.IBPluginDependency</string>
 					<string>550.IBPluginDependency</string>
-					<string>552.IBPluginDependency</string>
 					<string>556.IBPluginDependency</string>
 					<string>56.IBPluginDependency</string>
 					<string>56.ImportedFromIB2</string>
 					<string>{74, 862}</string>
 					<string>{{6, 978}, {478, 20}}</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
-					<string>{{547, 643}, {238, 193}}</string>
+					<string>{{547, 663}, {238, 173}}</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 					<string>{{475, 832}, {234, 43}}</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
-					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 					<integer value="1"/>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 					<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
 						<bool key="EncodedWithXMLCoder">YES</bool>
 						<object class="NSArray" key="dict.sortedKeys">
 							<bool key="EncodedWithXMLCoder">YES</bool>
-							<string>selectAveragingDepthMap:</string>
 							<string>selectMeshRenderer:</string>
-							<string>selectMultiDepthMap:</string>
 							<string>selectPointCloudRenderer:</string>
 							<string>selectSolidRenderer:</string>
-							<string>selectStaticDepthMap:</string>
 							<string>toggleAnaglyph:</string>
 							<string>toggleDepthFieldUpdates:</string>
-							<string>toggleLighting:</string>
-							<string>toggleRecording:</string>
 							<string>toggleTextures:</string>
-							<string>toggleWireframe:</string>
 						</object>
 						<object class="NSMutableArray" key="dict.values">
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<string>id</string>
 							<string>id</string>
 							<string>id</string>
-							<string>id</string>
-							<string>id</string>
-							<string>id</string>
-							<string>id</string>
-							<string>id</string>
-							<string>id</string>
 						</object>
 					</object>
 					<object class="NSMutableDictionary" key="actionInfosByName">
 						<bool key="EncodedWithXMLCoder">YES</bool>
 						<object class="NSArray" key="dict.sortedKeys">
 							<bool key="EncodedWithXMLCoder">YES</bool>
-							<string>selectAveragingDepthMap:</string>
 							<string>selectMeshRenderer:</string>
-							<string>selectMultiDepthMap:</string>
 							<string>selectPointCloudRenderer:</string>
 							<string>selectSolidRenderer:</string>
-							<string>selectStaticDepthMap:</string>
 							<string>toggleAnaglyph:</string>
 							<string>toggleDepthFieldUpdates:</string>
-							<string>toggleLighting:</string>
-							<string>toggleRecording:</string>
 							<string>toggleTextures:</string>
-							<string>toggleWireframe:</string>
 						</object>
 						<object class="NSMutableArray" key="dict.values">
 							<bool key="EncodedWithXMLCoder">YES</bool>
 							<object class="IBActionInfo">
-								<string key="name">selectAveragingDepthMap:</string>
-								<string key="candidateClassName">id</string>
-							</object>
-							<object class="IBActionInfo">
 								<string key="name">selectMeshRenderer:</string>
 								<string key="candidateClassName">id</string>
 							</object>
 							<object class="IBActionInfo">
-								<string key="name">selectMultiDepthMap:</string>
-								<string key="candidateClassName">id</string>
-							</object>
-							<object class="IBActionInfo">
 								<string key="name">selectPointCloudRenderer:</string>
 								<string key="candidateClassName">id</string>
 							</object>
 								<string key="candidateClassName">id</string>
 							</object>
 							<object class="IBActionInfo">
-								<string key="name">selectStaticDepthMap:</string>
-								<string key="candidateClassName">id</string>
-							</object>
-							<object class="IBActionInfo">
 								<string key="name">toggleAnaglyph:</string>
 								<string key="candidateClassName">id</string>
 							</object>
 								<string key="candidateClassName">id</string>
 							</object>
 							<object class="IBActionInfo">
-								<string key="name">toggleLighting:</string>
-								<string key="candidateClassName">id</string>
-							</object>
-							<object class="IBActionInfo">
-								<string key="name">toggleRecording:</string>
-								<string key="candidateClassName">id</string>
-							</object>
-							<object class="IBActionInfo">
 								<string key="name">toggleTextures:</string>
 								<string key="candidateClassName">id</string>
 							</object>
-							<object class="IBActionInfo">
-								<string key="name">toggleWireframe:</string>
-								<string key="candidateClassName">id</string>
-							</object>
 						</object>
 					</object>
 					<object class="IBClassDescriptionSource" key="sourceIdentifier">
 				<object class="IBPartialClassDescription">
 					<string key="className">KinectViewerAppDelegate</string>
 					<string key="superclassName">NSObject</string>
+					<object class="NSMutableDictionary" key="actions">
+						<string key="NS.key.0">toggleRecording:</string>
+						<string key="NS.object.0">id</string>
+					</object>
+					<object class="NSMutableDictionary" key="actionInfosByName">
+						<string key="NS.key.0">toggleRecording:</string>
+						<object class="IBActionInfo" key="NS.object.0">
+							<string key="name">toggleRecording:</string>
+							<string key="candidateClassName">id</string>
+						</object>
+					</object>
 					<object class="NSMutableDictionary" key="outlets">
 						<bool key="EncodedWithXMLCoder">YES</bool>
 						<object class="NSArray" key="dict.sortedKeys">
 
 @property(assign) BOOL frozen;
 @property(assign, nonatomic) BOOL anaglyph;
-@property(assign, nonatomic) BOOL lightsOn;
 @property(assign, nonatomic) BOOL textured;
 @property(nonatomic) BOOL recording;
 @property(assign, nonatomic) projective_transform_t colorTransform;
 - (IBAction) selectSolidRenderer: sender;
 
 - (IBAction) toggleDepthFieldUpdates: sender;
-- (IBAction) toggleLighting: sender;
 - (IBAction) toggleAnaglyph: sender;
 - (IBAction) toggleTextures: sender;
 @end
 
 @synthesize vertexShader, program;
 @synthesize updateQueued;
-@synthesize lightsOn;
-
-- (void) setLightsOn: (BOOL)flag {
-  lightsOn = flag;
-  
-  [[self openGLContext] makeCurrentContext];
-  if (flag) {
-    glEnable(GL_LIGHTING);
-  } else {
-    glDisable(GL_LIGHTING);
-  }
-  [self setNeedsDisplay: YES];  
-}
 
 @synthesize textured;
 @synthesize spaceModel;
 static const char *vertex_shader_source[] = {
   "const float halfFov = (57.0 / 180.0 * 3.14159265358) / 2.0;",
   "void main() {",
-  "  gl_FrontColor = gl_Color;",
+  // Compute the angle between the normal and the light
+  "  vec3 normal = normalize(gl_NormalMatrix * gl_Normal);",
+  "  vec3 lightDir = normalize(vec3(gl_LightSource[0].position));",
+  "  float NdotL = max(dot(normal, lightDir), 0.0);",
+  
+  // Compute the diffuse term
+  "  vec4 diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;",
+  
+  // Compute the ambient terms
+  "  vec4 ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;",
+  "  vec4 globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;",
+  
+  // Compute the specular term using Blinn-Phong
+  "  vec4 specular = vec4(0,0,0,0);",
+  "  if (NdotL > 0.0) {",
+  "    float NdotHV = max(dot(normal, gl_LightSource[0].halfVector.xyz), 0.0);",
+  "    specular = gl_FrontMaterial.specular * gl_LightSource[0].specular",
+  "        * pow(NdotHV, gl_FrontMaterial.shininess);",
+  "  }",
+  
+  // Apply the color
+  "  gl_FrontColor = NdotL * diffuse + globalAmbient + ambient + specular;",
+  
+  // Undo the Kinect's perspective projection
   "  vec2 angles = gl_Vertex.xy * halfFov;",
   "  vec2 xyPrime = gl_Vertex.z * sin(angles);",
   "  vec4 vertex = vec4(xyPrime, gl_Vertex.zw);",
   glFrontFace(GL_CCW);
   glShadeModel(GL_SMOOTH);
   
-  glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.01F);
-  
+  GLfloat diffuseColor[4] = { 1.f, 1.0f, 1.0f, 1.0f };
+  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
+
   GLfloat ambientColor[4] = { 0, 0, 0, 1 };
   glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor);
   
-  GLfloat diffuseColor[4] = { 1.f, 1.0f, 1.0f, 1.0f };
-  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
+  GLfloat specularColor[4] = { 1, 1, 1, 1 };
+  glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor);
   
-  GLfloat globalAmbient[4] = { 0.1f, 0.1f, 0.1f, 1.0f };
+  GLfloat globalAmbient[4] = { 0.01f, 0.01f, 0.01f, 1.0f };
   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);
   
   glEnable(GL_NORMALIZE);
-  glEnable(GL_LIGHT0);
   
   glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
+  
+  GLfloat specular[4] = { 0.05f, 0.05f, 0.05f, 1 };
+  glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
   glEnable(GL_COLOR_MATERIAL);
   
   glClearColor(0, 0, 0, 0);
   glRotatef(spinY, 0, 1, 0);
   glRotatef(spinX, 1, 0, 0);
   
-  GLfloat lightPos[4] = { 0, 0, -1, 1 };
+  GLfloat lightPos[4] = { 0, 0, -5, 1 };
   glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
 }
 
   
   NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                            self.polygonMode, KVPolygonMode, 
-                           self.lightsOn? KVDiffuseLighting : KVNoLighting, KVLighting,
                            nil];
   
   if (self.anaglyph) {
   [sender setState: self.frozen? NSOnState : NSOffState];
 }
 
-- (IBAction) toggleLighting: sender {
-  self.lightsOn = !lightsOn;
-  [sender setState: lightsOn? NSOnState : NSOffState];
-}
-
 - (IBAction) toggleAnaglyph: sender {
   self.anaglyph = !self.anaglyph;
   [self setNeedsDisplay: YES];

KVRayFieldSpaceModel.h

 extern NSString * const KVPolygonMode;
 extern NSString * const KVFilledPolygonMode;
 extern NSString * const KVWireframePolygonMode;
-extern NSString * const KVDottedPolygonMode;
-
-extern NSString * const KVLighting;
-extern NSString * const KVNoLighting;
-extern NSString * const KVDiffuseLighting;
+extern NSString * const KVDottedPolygonMode;

KVRayFieldSpaceModel.m

 NSString * const KVWireframePolygonMode = @"KVWireframePolygonMode";
 NSString * const KVDottedPolygonMode = @"KVDottedPolygonMode";
 
-NSString * const KVLighting = @"KVLighting";
-NSString * const KVNoLighting = @"KVNoLighting";
-NSString * const KVDiffuseLighting = @"KVDiffuseLighting";
-
 @interface KVRayFieldSpaceModel ()
 - (void) buildLinearizationTable;
 - (void) fillStaticComponents;
 }
 
 - (void) renderAsSolidWithOptions: (NSDictionary *) options {
-  BOOL lightsOn = ([options valueForKey: KVLighting] == KVDiffuseLighting);
-  
   float jump = 0.05f;
   
   glColor3f(1, 1, 1);
             glBegin(GL_TRIANGLE_STRIP);
             inStrip = YES;
           }
-          if (lightsOn) glNormal3fv(&normals[vidx + step].x);
+          glNormal3fv(&normals[vidx + step].x);
           glVertex3fv(&rays[vidx + step].x);
         }
         
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.