Commits

Cliff Biffle committed b10ad2c

Vertex shader now computes normals, which makes things much prettier.

Comments (0)

Files changed (1)

  */
 vec4 kinect_unproject(sampler2D texture, vec4 point) {
   vec4 sample = texture2D(texture, point.xy);
-  vec4 kinectPoint = vec4(gl_Vertex.xy, sample.g, 1.);
+  vec4 kinectPoint = vec4(point.xy, sample.g, 1.);
   return gl_TextureMatrix[0] * kinectPoint;
 }
 
+const float pixelDistanceX = 1. / 640.;
+const float pixelDistanceY = 1. / 480.;
+
 /*
  * Vertex Shader entry point.
  */
   
   // Project the point using GL's perspective settings.
   gl_Position = gl_ModelViewProjectionMatrix * vertex;
+  
+  // Calculate the vertex normal in Kinect-camera space.
+  vec4 rightVertex = kinect_unproject(depthTex, gl_Vertex + vec4(pixelDistanceX, 0, 0, 0));
+  vec4 downVertex = kinect_unproject(depthTex, gl_Vertex + vec4(0, pixelDistanceY, 0, 0));
+  
+  // Convert out of homogeneous space for the cross product.
+  vec3 right_world = rightVertex.xyz / rightVertex.w;
+  vec3 down_world = downVertex.xyz / downVertex.w;
+  vec3 vertex_world = vertex.xyz / vertex.w;
+  
+  vec3 tangent = down_world - vertex_world;
+  vec3 bitangent = right_world - vertex_world;
+  
+  vec3 normal = cross(tangent, bitangent);
 
   if (vertex.z <= 0.) {
     gl_FrontColor = vec4(0,0,0,0);
   } else {
-    gl_FrontColor = vec4(1,1,1,1);
+    gl_FrontColor = light_vertex(normal);
   }
 }