3D Camera Test – Processing


float camY = 200;
float camZ = (200) / tan(PI*30.0 / 180.0);
float centerX = 200;
float centerY = 200;
boolean left = false;
boolean right = false;
boolean up = false;
boolean down = false;
boolean left2 = false;
boolean right2 = false;
boolean up2 = false;
boolean down2 = false;
boolean back = false;
boolean forward = false;
 
 
 
PImage bg;
PImage texmap;

int sDetail = 35;  // Sphere detail setting
float rotationX = 0;
float rotationY = 0;
float velocityX = 0;
float velocityY = 0;
float globeRadius = 400;
float pushBack = 0;

float[] cx, cz, sphereX, sphereY, sphereZ;
float sinLUT[];
float cosLUT[];
float SINCOS_PRECISION = 0.5;
int SINCOS_LENGTH = int(360.0 / SINCOS_PRECISION);

 
void setup() {
  size(400, 400, P3D);
    texmap = loadImage("world32k.jpg");    
  initializeSphere(sDetail);
}
 
void draw() {
  
  camera(camX, camY, camZ, centerX, centerY, 0, 0, 1, 0);
 
  lights();
  background(255);
 
 /*
  pushMatrix();
  translate(200, 350, -80);
  box(400, 20, 200);
  popMatrix();
 
 
  pushMatrix();
  translate(200, 275, 35);
  box(240, 100, 30);
  popMatrix();
 
  pushMatrix();
  translate(30, 250, 15);
  box(20, 175, 20);
  popMatrix();
 
 
  pushMatrix();
  translate(370, 250, 15);
  box(20, 175, 20);
  popMatrix();
 
  pushMatrix();
  translate(30, 250, -100);
  box(20, 175, 20);
  popMatrix();
 
 
  pushMatrix();
  translate(370, 250, -100);
  box(20, 175, 20);
  popMatrix();
 
  pushMatrix();
  translate(30, 150, 15);
  noStroke();
  sphere(12);
  stroke(0);
  popMatrix();
 
 
  pushMatrix();
  translate(370, 150, 15);
  noStroke();
  sphere(12);
  stroke(0);
  popMatrix();
 
  pushMatrix();
  translate(30, 150, -100);
  noStroke();
  sphere(12);
  stroke(0);
  popMatrix();
 
 
  pushMatrix();
  translate(370, 150, -100);
  noStroke();
  sphere(12);
  stroke(0);
  popMatrix();
 
  pushMatrix();
  translate(130, 215, 35);
  box(20);
  popMatrix();
 
  pushMatrix();
  translate(130, 195, 35);
  noStroke();
  sphere(20);
  stroke(0);
  popMatrix();
 
  pushMatrix();
  translate(260, 215, 35);
  box(20);
  popMatrix();
 
  pushMatrix();
  translate(260, 195, 35);
  noStroke();
  sphere(20);
  stroke(0);
  popMatrix();
 
  pushMatrix();
  translate(200, 215, 15);
  box(40);
  popMatrix();
 
  pushMatrix();
  translate(200, 175, 25);
  noStroke();
  sphere(40);
  stroke(0);
  popMatrix();
*/

  movement();


  renderGlobe();
}
 
void keyPressed() {
  switch(key) {
  case'a':
    left = true;
    break;
  case 'd':
    right = true;
    break;
  case 'w':
    up = true;
    break;
  case 's':
    down = true;
    break;
  case'j':
    left2 = true;
    break;
  case 'l':
    right2 = true;
    break;
  case 'i':
    up2 = true;
    break;
  case 'k':
    down2 = true;
    break;
  case 't':
    back = true;
    break;
  case 'g':
    forward = true;
    break;
  }
}
 
void keyReleased() {
  switch(key) {
  case'a':
    left = false;
    break;
  case 'd':
    right = false;
    break;
  case 'w':
    up = false;
    break;
  case 's':
    down = false;
    break;
  case'j':
    left2 = false;
    break;
  case 'l':
    right2 = false;
    break;
  case 'i':
    up2 = false;
    break;
  case 'k':
    down2 = false;
    break;
  case 't':
    back = false;
    break;
  case 'g':
    forward = false;
    break;
  }
}
 
 
void movement() {
  if (up)camY -= 3;
  if (down)camY += 3;
  if (left)camX -= 3;
  if (right)camX += 3;
  if (up2)centerY -= 3;
  if (down2)centerY += 3;
  if (left2)centerX -= 3;
  if (right2)centerX += 3;
  if (back)camZ -= 3;
  if (forward) camZ += 3;
}



void renderGlobe() {
  pushMatrix();
  translate(width * 0.33, height * 0.5, pushBack);
  pushMatrix();
  noFill();
  stroke(255,200);
  strokeWeight(2);
  smooth();
  popMatrix();
  lights();    
  pushMatrix();
  rotateX( radians(-rotationX) );  
  rotateY( radians(270 - rotationY) );
  fill(200);
  noStroke();
  textureMode(IMAGE);  
  texturedSphere(globeRadius, texmap);
  popMatrix();  
  popMatrix();
  rotationX += velocityX;
  rotationY += velocityY;
  velocityX *= 0.95;
  velocityY *= 0.95;
  
  // Implements mouse control (interaction will be inverse when sphere is  upside down)
  if(mousePressed){
    //velocityX += (mouseY-pmouseY) * 0.01;
    //velocityY -= (mouseX-pmouseX) * 0.01;
   // camera(mouseX, mouseY, (height/2) / tan(PI/6), mouseX, height/2, 0, 0, 1, 0);
  }
}

void initializeSphere(int res)
{
  sinLUT = new float[SINCOS_LENGTH];
  cosLUT = new float[SINCOS_LENGTH];

  for (int i = 0; i < SINCOS_LENGTH; i++) {
    sinLUT[i] = (float) Math.sin(i * DEG_TO_RAD * SINCOS_PRECISION);
    cosLUT[i] = (float) Math.cos(i * DEG_TO_RAD * SINCOS_PRECISION);
  }

  float delta = (float)SINCOS_LENGTH/res;
  float[] cx = new float[res];
  float[] cz = new float[res];
  
  // Calc unit circle in XZ plane
  for (int i = 0; i < res; i++) {
    cx[i] = -cosLUT[(int) (i*delta) % SINCOS_LENGTH];
    cz[i] = sinLUT[(int) (i*delta) % SINCOS_LENGTH];
  }
  
  // Computing vertexlist vertexlist starts at south pole
  int vertCount = res * (res-1) + 2;
  int currVert = 0;
  
  // Re-init arrays to store vertices
  sphereX = new float[vertCount];
  sphereY = new float[vertCount];
  sphereZ = new float[vertCount];
  float angle_step = (SINCOS_LENGTH*0.5f)/res;
  float angle = angle_step;
  
  // Step along Y axis
  for (int i = 1; i < res; i++) {
    float curradius = sinLUT[(int) angle % SINCOS_LENGTH];
    float currY = -cosLUT[(int) angle % SINCOS_LENGTH];
    for (int j = 0; j < res; j++) {
      sphereX[currVert] = cx[j] * curradius;
      sphereY[currVert] = currY;
      sphereZ[currVert++] = cz[j] * curradius;
    }
    angle += angle_step;
  }
  sDetail = res;
}

// Generic routine to draw textured sphere
void texturedSphere(float r, PImage t) {
  int v1,v11,v2;
  r = (r + 240 ) * 0.33;
  beginShape(TRIANGLE_STRIP);
  texture(t);
  float iu=(float)(t.width-1)/(sDetail);
  float iv=(float)(t.height-1)/(sDetail);
  float u=0,v=iv;
  for (int i = 0; i < sDetail; i++) {
    vertex(0, -r, 0,u,0);
    vertex(sphereX[i]*r, sphereY[i]*r, sphereZ[i]*r, u, v);
    u+=iu;
  }
  vertex(0, -r, 0,u,0);
  vertex(sphereX[0]*r, sphereY[0]*r, sphereZ[0]*r, u, v);
  endShape();   
  
  // Middle rings
  int voff = 0;
  for(int i = 2; i < sDetail; i++) {
    v1=v11=voff;
    voff += sDetail;
    v2=voff;
    u=0;
    beginShape(TRIANGLE_STRIP);
    texture(t);
    for (int j = 0; j < sDetail; j++) {
      vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1++]*r, u, v);
      vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2++]*r, u, v+iv);
      u+=iu;
    }
  
    // Close each ring
    v1=v11;
    v2=voff;
    vertex(sphereX[v1]*r, sphereY[v1]*r, sphereZ[v1]*r, u, v);
    vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r, u, v+iv);
    endShape();
    v+=iv;
  }
  u=0;
  
  // Add the northern cap
  beginShape(TRIANGLE_STRIP);
  texture(t);
  for (int i = 0; i < sDetail; i++) {
    v2 = voff + i;
    vertex(sphereX[v2]*r, sphereY[v2]*r, sphereZ[v2]*r, u, v);
    vertex(0, r, 0,u,v+iv);    
    u+=iu;
  }
  vertex(sphereX[voff]*r, sphereY[voff]*r, sphereZ[voff]*r, u, v);
  endShape();
  
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s