Commits

Cliff Biffle committed 13d8e65

The RayFieldSpaceModel can now do normal calculations, so we have lighting again. The factoring improvements have let me move normal calculation to the Kinect thread, so that both threads stay within their 33ms deadlines.

Comments (0)

Files changed (3)

     
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   
-  NSDictionary *options = [NSDictionary dictionaryWithObject: self.polygonMode forKey: KVPolygonMode];
+  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
+                           self.polygonMode, KVPolygonMode, 
+                           self.lightsOn? KVDiffuseLighting : KVNoLighting, KVLighting,
+                           nil];
   
   if (self.anaglyph) {
     camera.pos.x -= INTEROCULAR_DISTANCE / 2;

KVRayFieldSpaceModel.h

   // code if we use [480][640].
   vec3f_t rays[480 * 640];
   
+  // Normal vectors for each vertex.
+  vec3f_t normals[480 * 640];
+  
   // The raw RGB samples of the last color image.  These do not
   // map one-to-one onto rays because the cameras are offset.
   rgb_t colorImage[480][640];
 extern NSString * const KVPolygonMode;
 extern NSString * const KVFilledPolygonMode;
 extern NSString * const KVWireframePolygonMode;
-extern NSString * const KVDottedPolygonMode;
+extern NSString * const KVDottedPolygonMode;
+
+extern NSString * const KVLighting;
+extern NSString * const KVNoLighting;
+extern NSString * const KVDiffuseLighting;

KVRayFieldSpaceModel.m

  */
 
 #import "KVRayFieldSpaceModel.h"
+#import "utility.h"
 
 NSString * const KVPolygonMode = @"KVPolygonMode";
 NSString * const KVFilledPolygonMode = @"KVFilledPolygonMode";
 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) buildUnitRays;
     }
   }
   
+  for (int y = 0; y < 480 - 1; y++) {
+    vidx = 640 * y;
+    for (int x = 0; x < 640 - 1; x++, vidx++) {
+      normals[vidx] = compute_normal(rays[vidx], rays[vidx + 640], rays[vidx + 1]);
+    }
+  }
+  
+  
   NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
   rayCastingCount++;
   rayCastingTotal += end - start;
-  NSLog(@"setDepthSamples: mean %.03fµs", (rayCastingTotal / rayCastingCount) * 1000000.);
+  
+  if ((rayCastingCount & 0xF) == 0) {
+    NSLog(@"Raycasting: latest %.03fµs mean %.03fµs",
+          (end - start) * 1000000.,
+          (rayCastingTotal / rayCastingCount) * 1000000.);
+  }
 }
 
 - (void) setColorSamples: (NSData *)data {
   NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
   vertexPlacementCount++;
   vertexPlacementTotal += end - start;
-  NSLog(@"drawInCurrentOpenGLContextWithOptions: current %.03fµs mean %.03fµs",
-        (end - start) * 1000000.,
-        (vertexPlacementTotal / vertexPlacementCount) * 1000000.);
+  
+  if ((vertexPlacementCount & 0xF) == 0) {
+    NSLog(@"Vertex placement: latest %.03fµs mean %.03fµs",
+          (end - start) * 1000000.,
+          (vertexPlacementTotal / vertexPlacementCount) * 1000000.);
+  }
+  
 }
 
 #pragma mark --- Internals
 - (void) renderAsPointsWithOptions: (NSDictionary *) options {
   glBegin(GL_POINTS);
   glColor3f(1, 1, 1);
+  glNormal3f(0, 0, -1);
   for (int i = 0; i < 480 * 640; i++) {
     glVertex3fv(&rays[i].x);    
   }
 }
 
 - (void) renderAsSolidWithOptions: (NSDictionary *) options {
+  BOOL lightsOn = ([options valueForKey: KVLighting] == KVDiffuseLighting);
+  
   float jump = 0.1f;
   
   glColor3f(1, 1, 1);
+  glNormal3f(0, 0, 1);
+  
   int vidx = 0;
+  
   for (int y = 0; y < 480 - 1; y++) {
     BOOL inStrip = NO;
     float lastDepth = rays[vidx].z;
             glBegin(GL_TRIANGLE_STRIP);
             inStrip = YES;
           }
+          if (lightsOn) glNormal3fv(&normals[vidx + step].x);
           glVertex3fv(&rays[vidx + step].x);
         }