Commits

Cliff Biffle committed d6c486b

Made a first crack at correcting the normals after projection.

  • Participants
  • Parent commits 2892936
  • Branches shaders

Comments (0)

Files changed (1)

File KVDepthView.m

 static const char *vertex_shader_source[] = {
   "const float halfFov = (57.0 / 180.0 * 3.14159265358) / 2.0;",
   "void main() {",
+  // 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);",
+  "  gl_Position = gl_ModelViewProjectionMatrix * vertex;",
+
   // Compute the angle between the normal and the light
-  "  vec3 normal = normalize(gl_NormalMatrix * gl_Normal);",
+  "  vec2 xyRatio = xyPrime / gl_Vertex.xy;"
+  "  vec3 correctedNormal = vec3(gl_Normal.xy * xyRatio, gl_Normal.z);",
+  "  vec3 normal = normalize(gl_NormalMatrix * correctedNormal);",
   "  vec3 lightDir = normalize(vec3(gl_LightSource[0].position));",
   "  float NdotL = max(dot(normal, lightDir), 0.0);",
   
   
   // 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);",
-  "  gl_Position = gl_ModelViewProjectionMatrix * vertex;",
   "}",
 };
 
   glFrontFace(GL_CCW);
   glShadeModel(GL_SMOOTH);
   
-  GLfloat diffuseColor[4] = { 1.f, 1.0f, 1.0f, 1.0f };
+  GLfloat diffuseColor[4] = { 0.8f, 0.8f, 0.8f, 1.0f };
   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
 
   GLfloat ambientColor[4] = { 0, 0, 0, 1 };
   GLfloat specularColor[4] = { 1, 1, 1, 1 };
   glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor);
   
-  GLfloat globalAmbient[4] = { 0.01f, 0.01f, 0.01f, 1.0f };
+  GLfloat globalAmbient[4] = { 0.1f, 0.1f, 0.1f, 1.0f };
   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);
   
-  glEnable(GL_NORMALIZE);
-  
   glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
   
-  GLfloat specular[4] = { 0.05f, 0.05f, 0.05f, 1 };
+  GLfloat specular[4] = { 0.0f, 0.0f, 0.0f, 1 };
   glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
   glEnable(GL_COLOR_MATERIAL);
   
-  glClearColor(0, 0, 0, 0);
+  glClearColor(0.3f, 0, 0.5f, 0);
   
   [self resetCamera];
 }
   glRotatef(worldRotation[0], worldRotation[1], worldRotation[2], worldRotation[3]);
   glRotatef(spinY, 0, 1, 0);
   glRotatef(spinX, 1, 0, 0);
-  
-  GLfloat lightPos[4] = { 0, 0, -5, 1 };
-  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
 }
 
 #define INTEROCULAR_DISTANCE (0.0635F/2)