Commits

Cliff Biffle  committed f167c68

The Kinect's depth field is now passed to the GPU as a texture, used by the vertex shader to reconstruct geometry. It's also being passed as vertices. One step at a time....

  • Participants
  • Parent commits 158ddcb
  • Branches shaders

Comments (0)

Files changed (3)

File KVRayFieldSpaceModel.h

   // 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];
+  
+  // The raw samples of the last depth image.
+  uint16_t depthImage[480][640];
 }
 
 @end

File KVRayFieldSpaceModel.m

 @property(assign, nonatomic) double vertexPlacementTotal;
 @property(assign, nonatomic) NSUInteger vertexPlacementCount;
 
+@property GLuint depthTexture;
+
 - (void) renderAsPointsWithOptions: (NSDictionary *) options;
 - (void) renderAsMeshWithOptions: (NSDictionary *) options;
 - (void) renderAsSolidWithOptions: (NSDictionary *) options;
 @implementation KVRayFieldSpaceModel
 
 @synthesize rayCastingCount, rayCastingTotal, vertexPlacementCount, vertexPlacementTotal;
+@synthesize depthTexture;
 
 - init {
   if ((self = [super init])) {
 #pragma mark --- Implementation of KVSpaceModel
 
 - (void) initGL {
-  // Nothing required yet.
+  glGenTextures(1, &depthTexture);
+  glBindTexture(GL_TEXTURE_2D, depthTexture);
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 }
 
 - (void) setDepthSamples: (NSData *)data {
   NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
   
   const uint16_t *sample = [data bytes];
+  memcpy(&depthImage, sample, sizeof depthImage);
   
   // gcc 4.2.1 is not clever enough to turn raster two-dimensional
   // subscripting (e.g. ray[y][x]) into a single incremented induction
 - (void) drawInCurrentOpenGLContextWithOptions: (NSDictionary *) options {
   NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
 
+  glBindTexture(GL_TEXTURE_2D, depthTexture);
+  glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16, 640, 480, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, &depthImage);
+  
   id modeOption = [options valueForKey: KVPolygonMode];
   if (modeOption == KVWireframePolygonMode) {
     [self renderAsMeshWithOptions: options];
  * Use of this source code is governed by the Apache License 2.0,
  * which can be found in the LICENSE file.
  */
+ 
+uniform sampler2D tex;
 
 /*
  * Vertex Shader entry point.
  */
 void main() {
+  // Load our depth from the camera's depth texture.
+  vec2 imageCoord = gl_Vertex.xy * vec2(1., 4./3.) * -0.5 + 0.5;
+  vec4 sample = texture2D(tex, imageCoord);
+  vec3 kinectPoint = vec3(gl_Vertex.xy, sample.g * 65536.);
+  
   // Undo the Kinect's perspective projection, leaving the homogeneous term.
-  vec4 vertex = vec4(kinect_unproject(gl_Vertex.xyz), gl_Vertex.w);
+  vec4 vertex = vec4(kinect_unproject(kinectPoint), gl_Vertex.w);
   
   // Project the point using GL's perspective settings.
   gl_Position = gl_ModelViewProjectionMatrix * vertex;