You need the Shapes3D library (
http://processing.org/reference/libraries/#3d)
and then create the following sketch
Code:/**
* Simple program to demonstrate the shape picking feature
* of this library.
* Click on the slowly revolving cubes to change their colour.
*
* This demo uses the off-screen buffer version of the pick
* shape algorithm.
*
* created by Peter Lager 2010
*/
import shapes3d.utils.*;
import shapes3d.*;
import processing.opengl.*;
Box[] box = new Box[10];
float a;
boolean mouseClicked = false;
boolean orthoOn = false;
PMatrix3D orgMat;
void setup(){
size(200,200,OPENGL);
orgMat = (PMatrix3D)g.getMatrix();
cursor(CROSS);
float size;
for (int i = 0; i < box.length; i++) {
size = 5 + (int)random(15);
box[i] = new Box(this,size,size,size);
box[i].moveTo(random(-18,18), random(-18,18), random(-18,18));
box[i].fill(randomColor());
}
translate(width/2, height/2);
}
void draw(){
background(255);
pushMatrix();
if(orthoOn){
background(255,200,200);
ortho(-width/6,width/6,-height/6,height/6,-100,100);
}
else {
background(200,2550,200);
perspective();
}
camera(70 * sin(a), 10, 70 * cos(a), 0, 0, 0, 0, 1, 0);
ambientLight(200,200,200);
directionalLight(128, 128, 128, -1, 0, -1);
for (int i = 0; i < box.length; i++) {
box[i].draw();
}
if(mouseClicked){
Shape3D picked = Shape3D.pickShapeB(this,mouseX, mouseY);
if(picked != null)
picked.fill(randomColor());
mouseClicked = false;
}
popMatrix();
a += 0.002;
g.setMatrix(orgMat);
perspective();
fill(0);
if(orthoOn)
text("Orthoganal View",10,30);
else
text("Perspective View",10,30);
}
void mouseClicked(){
mouseClicked = true;
orthoOn = !orthoOn;
}
int randomColor(){
return color(random(60,200), random(60,200), random(60,200));
}