import SGCamera.*;
import Raytracing.*;
import Tuple.*;
 
SGCamera cam;
Raytracer tracer;
 
float dx = 0,dy = 0, dz = 0;
 
PImage [] pix = new PImage[11];
PImage [] maps = new PImage[5];
int level = 0;
boolean jetpack = false;
int nextImg = 0;
int nextPort = 0;
int numImgs = 10;
void setup(){
 
size(550,450,P3D);
cam = new SGCamera(this,-50,-80,-100,0,0,0, SGCamera.MOUSELOOK, SGCamera.MOVE);
tracer = new Raytracer(this);
frameRate(30);
tracer.openFile("thing.mra",cam);
 
 
cam.fov = 1.5;
 
 
 
}
 
public void draw(){
background(220,220,255);
lights();
cam.feed();
tracer.draw();
 
Ray down = new Ray(cam.pos,new Tuple3f(0,1,0));
tracer.accelerator.findNearest(down);
if (down.occluder != null){
float eyeheight = 100;
if (jetpack){
dz -= 1.2f;
cam.translate(random(-1.5,1.5),dz + random(0,2),random(-1.5,1.5));
}
if (down.distance > eyeheight){
dz += .98;
cam.translate(0,dz,0);
}
else if (down.distance < eyeheight){
cam.translate(0,-(eyeheight-down.distance)/2.f,0);
dz = 0;
}
 
}
Tuple3f direction = null;
if (dx > 0) direction = cam.forward.getCopy();
else if (dx < 0) direction = cam.forward.times(-1.f);
if (dy !=0 && direction == null) direction = new Tuple3f();
if (dy > 0) direction.plusEquals(cam.right);
else if (dy < 0) direction.plusEquals(cam.right.times(-1.f));
if (direction != null){
direction.timesEquals(20);
Tuple3f bodypoint = cam.pos.getCopy();
bodypoint.y += 30.f;
direction.y = 0;
down = new Ray(bodypoint,direction);
down.maxDist = 100.f;
tracer.accelerator.findNearest(down);
if (down.occluder == null){
cam.translate(direction.x, direction.y,direction.z);
}
}
 
 
 
}
public void keyPressed(){
if (key == 'w' ){
dx = 20;
}
else if (key == 's' ){
dx = -20;
}
if (key == 'd' ){
dy = 20;
}
else if (key == 'a' ){
dy = -20;
}
else if (key == 'q'){
if (dz == 0){
dz -= 10.f;
cam.translate(0,dz,0);
}
}
 
else if (key == 'e'){
cam.fov -= .2;
}
else if (key == 'c'){
cam.fov += .2;
}
cam.fov = constrain(cam.fov,.6,2.8);
}
 
public void keyReleased(){
if (key == 'w' || key == 's') dx = 0;
if (key == 'a' || key == 'd') dy = 0;
 
}
 
 
 
 
void mousePressed(){
if (mouseButton == RIGHT) jetpack = true;
}
void mouseReleased(){
if (mouseButton == RIGHT) jetpack = false;
}