Cross-eyed stereoscopy

Here is some code for rendering cross-eyed stereo images of 3-D scene with some simple camera() and frustum() calls. It creates images such as this one:

To view the image, gently cross your eyes until you see three images. Focus on the center one and let it come into focus. Sadly, not all people are able to focus the images this way.

For other examples, see https://gfycat.com/giftedrichharpyeagle and https://gfycat.com/alertgrizzledcrab.

Here’s the code. Press ENTER to cycle through different terrains.

float seed;
PGraphics img;
boolean bSaveFrame = false;

void setup() {
  size( 1200, 600, P3D );
  img = createGraphics( width/2, height, P3D );
  seed = random(1000000);
}

void draw() {
  float d = 0.1;
  drawScene( img, d, seed );
  image( img, 0, 0, width/2, height );
  drawScene( img, -d, seed );
  image( img, width/2, 0, width/2, height );
  if( bSaveFrame ) {
    save("stereo.png");
    bSaveFrame = false;
  }
}

void drawScene( PGraphics pg, float offset, float seed ) {
  pg.beginDraw();
  pg.colorMode( HSB, 1, 1, 1, 1 );
  pg.background( 0 );

  float s = 1.0/sqrt(3);  // scaling for a 60 degree camera angle
  pg.camera( offset, 0.0, 2., offset, 0.0, 0.0, 0, 1, 0 );
  pg.frustum( -s-offset*0.666, s-offset*0.666, -s, s, 1, 4 );

  // Create your scene here within 1 unit of the origin
  randomSeed( (long)seed );
  noiseSeed( (long)seed );
  pg.rotateX( TAU/6 );
  pg.rotateZ( TAU*frameCount/600. );
  pg.scale( 0.98 );
  pg.strokeWeight( 5 );
  pg.beginShape(LINES);
  for( float x = -1; x<=1; x+= 0.02 )
    for( float y = -1; y<=1; y += 0.02 ) {
      float z = noise( x+17, y-5 )-0.5+random(0.01);
      pg.stroke( .5-2.*z, 0.7, 1 );
      pg.vertex( x, y, -0.25 );
      pg.vertex( x+random(0.01), y+random(0.01), z );
    }
  pg.endShape(CLOSE );

  pg.endDraw();
}

void keyPressed() {
  if( keyCode == ENTER )
    seed = random(1000000);
  else if( key == '+') bSaveFrame = true;
}

If you want more, there is also a reddit group with mostly still images: https://reddit.com/r/CrossView/

6 Likes