package heightMap;

import processing.core.PApplet;
import processing.core.PImage;
import java.awt.Color;

/* Heightmap by Gunes Direk, MAS CAAD ETH, 2008
** This code turns a given image into a 3D surface, depending on either the brightness
** value (HSB) or the blue value (RGB). 
** "O" for the original image,
** "B" for blue values; 
** "R" for brightness. 
** Drag mouse to orbit.
** 
** Camera class by Georg Munkel.
*/

public class HeightBitmap extends PApplet {
	

	Camera myCam = new Camera(300);

	PImage img;
	int clr,clr_next,clr_down,clr_next_down;
	float clr_blu,clr_next_blu,clr_down_blu,clr_next_down_blu; 
	int edge;
	boolean a,b,c;

	
public void setup(){
	
	edge = 1;              //....size of one pixel
	  fill(200);
	  background(0);

	  img=loadImage("superman.jpg");         //image to use
	  size (750,750,P3D);
	  myCam.setTarget( img.width/2,img.height/2, -50 );    //target of the camera
	  myCam.myApplet = this;

	

}

public void draw(){
	

	  lights();
	  directionalLight(10, 10, 200, -1, 0, 0);
	  directionalLight(200, 10, 10, 1, 0, 0);
	  background(0);
	  myCam.useCam();

	  if (a) { heightmap(); }
	  if (c) { image(img,0,0); }
	  if (b) { brightnessmap(); }
	  
}

//KEYCODE B > show the blue map
public void keyPressed(){
if ( key == 'b' || key == 'B'){
	if(c){
		c=false;
		}
	if(b){
		b=false;
	}
  if (!a){
    a=true;
  }
  else {
    a=false;
  }
}
// KEYCODE R > show the brightness map
if (key =='r' || key=='R'){
	if(c){
		c=false;
		}
	if(a){
		a=false;
	}
  if (!b){
    b=true;
  }
  else {
    b=false;
  }
}
// KEYCODE o > show the original picture
if (key =='o' || key=='O'){
  if (!c){
    c=true;
  }
  else {
    c=false;
  }
}

}

public void heightmap(){


for (int i=0;i<img.width-5;i=i+edge){
  for(int j=0;j<img.height-5;j=j+edge){

    clr=img.get(i,j);
    clr_next=img.get(i+edge,j);
    clr_down=img.get(i,j+edge);
    clr_next_down=img.get(edge+i,edge+j);

    clr_blu=blue(clr);
    clr_next_blu=blue(clr_next);
    clr_down_blu=blue(clr_down);
    clr_next_down_blu=blue(clr_next_down);

    noStroke();
    beginShape(TRIANGLE_STRIP);
    vertex(i,j,-clr_blu/20);
    vertex(i,edge+j,-clr_down_blu/20);
    vertex(edge+i,j,-clr_next_blu/20);
    vertex(edge+i,edge+j,-clr_next_down_blu/20);
    endShape();
  }
}
}
public void brightnessmap(){
for (int i=0;i<img.width-5;i=i+edge){
  for(int j=0;j<img.height-5;j=j+edge){

    clr=img.get(i,j);
    clr_next=img.get(i+edge,j);
    clr_down=img.get(i,j+edge);
    clr_next_down=img.get(edge+i,edge+j);

    clr_blu=brightness(clr);
    clr_next_blu=brightness(clr_next);
    clr_down_blu=brightness(clr_down);
    clr_next_down_blu=brightness(clr_next_down);

    noStroke();
    beginShape(TRIANGLE_STRIP);
    vertex(i,j,-clr_blu/20);
    vertex(i,edge+j,-clr_down_blu/20);
    vertex(edge+i,j,-clr_next_blu/20);
    vertex(edge+i,edge+j,-clr_next_down_blu/20);
    endShape();
  }
}
}
public void mouseDragged() {
	if (key == '1')
		myCam.mouseOrbit();
	if (key == '2')
		myCam.mouseZoom();
	if (key == '3')
		myCam.mousePan();
}


}
