Commits

Cliff Biffle committed 8148390

Switched anaglyph rendering from a hackish "toe-in" projection to a proper frustrum-skewing projection. Looks much better now.

Comments (0)

Files changed (1)

 @interface KVDepthView ()
 - (void) resetCamera;
 - (void) resizeGL;
-- (void) updateProjection;
-- (void) updateModelView;
+- (void) updateProjectionWithOffset: (float) offset;
+- (void) updateModelViewWithOffset: (float) offset;
 - (void) updateColorTransform;
 - (GLuint) loadShaderOfType: (GLenum) type fromFiles: (NSArray *) names;
 
     camera.viewWidth = (float) bounds.size.width;
     
     glViewport(0, 0, camera.viewWidth, camera.viewHeight);
-    [self updateProjection];
   }
 }
 
-#define MAXIMUM_DEPTH (8.0F)
-
-- (void) updateProjection {
+- (void) updateProjectionWithOffset: (float) offset {
   [[self openGLContext] makeCurrentContext];
   
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   
-  GLdouble near = -camera.pos.z - MAXIMUM_DEPTH;  
-  GLdouble far = -camera.pos.z + MAXIMUM_DEPTH;
+  GLdouble near = -camera.pos.z + 0.5;  
+  GLdouble far = -camera.pos.z + 8.0;
   
   // I do not fully understand this.
   if (near < 0.0001) near = 0.0001;
     top = halfWidth / aspectRatio;
   }
   
-  glFrustum(left, -left, -top, top, near, far);
+  double zeroParallax = 1;
+  glFrustum(left - offset * near / zeroParallax,
+            -left - offset * near / zeroParallax,
+            -top,
+            top,
+            near,
+            far);
 }
 
-- (void) updateModelView {
+- (void) updateModelViewWithOffset: (float) offset {
   [[self openGLContext] makeCurrentContext];
   
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   
-  gluLookAt(camera.pos.x, camera.pos.y, camera.pos.z,
-            camera.target.x, camera.target.y, camera.target.z,
+  gluLookAt(camera.pos.x - offset, camera.pos.y, camera.pos.z,
+            camera.target.x - offset, camera.target.y, camera.target.z,
             camera.up.x, camera.up.y, camera.up.z);
   
   glRotatef(trackballRotation[0], trackballRotation[1], trackballRotation[2], trackballRotation[3]);
                            nil];
   
   if (self.anaglyph) {
-    camera.target.x += INTEROCULAR_DISTANCE / 2;
-
+    [self updateProjectionWithOffset: INTEROCULAR_DISTANCE / 2];
+    [self updateModelViewWithOffset: INTEROCULAR_DISTANCE / 2];
     glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
+  } else {
+    [self updateProjectionWithOffset: 0];
+    [self updateModelViewWithOffset: 0];
   }
   
   // Draw right eye or center.
-  [self updateModelView];
   [spaceModel drawInCurrentOpenGLContextWithOptions: options];
   
   if (self.anaglyph) {
     // Draw left eye.
     glClear(GL_DEPTH_BUFFER_BIT);
     
-    camera.target.x -= INTEROCULAR_DISTANCE;
-    
+    [self updateProjectionWithOffset: -INTEROCULAR_DISTANCE / 2];
+    [self updateModelViewWithOffset: -INTEROCULAR_DISTANCE / 2];
     glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
-    [self updateModelView];
     [spaceModel drawInCurrentOpenGLContextWithOptions: options];
     
     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-    
-    camera.target.x += INTEROCULAR_DISTANCE / 2;
   }
   
   [[self openGLContext] flushBuffer];
   }
   
   if (changes) {
-    [self updateProjection];
     [self setNeedsDisplay: YES];
   }
 }
   float mag = (float) [theEvent magnification];
   camera.fov -= mag * 10.f;
   if (camera.fov < 5.f) camera.fov = 5.f;
-  [self updateProjection];
   [self setNeedsDisplay: YES];
 }
 
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.