Peasy Picking 3d Example
Double click a sphere to set it to the center focus point.
Quote://Double click a sphere to set it to the center focus point
import processing.opengl.*;
import peasy.*;
PeasyCam cam;
class Node {
// variables
int id; // id
int x, y, z, w; // position (x, y, z) and width (w)
color c; // color (in scene, not buffer)
// constructor
public Node(int id, int x, int y, int z, int w) {
this.id = id;
this.x = x;
this.y = y;
this.z = z;
this.w = w;
c = color(random(255), 250, 50);
}
// make the color change
public void focus() {
cam.lookAt(x,y,z,1000); //lookat xyz - speed 1000 ms
}
// display the Node on screen
public void display(PGraphics ecran) {
ecran.fill(c, 100); //color, alpha
ecran.noStroke();
drawNode(ecran);
}
// draw the Node in the buffer
public void drawBuffer(PGraphics buffer) {
color idColor = getColor(id);
buffer.fill(idColor);
drawNode(buffer);
}
private void drawNode(PGraphics g) {
g.pushMatrix();
g.translate(x, y, z);
g.sphere(w);
g.popMatrix();
}
}
PGraphics buffer; // buffer
Node[] Nodes; // Nodes
//float a = 0; // angle to make the scene rotate
void setup() {
// we use OPENGL for display
size(800, 600, OPENGL);
//printCamera();
cam = new PeasyCam(this, 400);
cam.setResetOnDoubleClick(false);
cam.setMinimumDistance(100);
cam.setMaximumDistance(5000);
// buffer is created using applet dimensions
buffer = createGraphics(width, height, P3D);
// put Nodes randomly in the scene
Nodes = new Node[7];
Nodes[0] = new Node(
0, // identifiant
0, // position x
0, // position y
0, // position z
10 // size
);
Nodes[1] = new Node(1,100,0,0,10);
Nodes[2] = new Node(2,0,100,0,10);
Nodes[3] = new Node(3,0,0,100,10);
Nodes[4] = new Node(4,-100,0,0,10);
Nodes[5] = new Node(5,0,0,-100,10);
Nodes[6] = new Node(6,0,-100,0,10);
}
void draw() {
background(0);
lights();
smooth();
strokeWeight(1.5);
stroke(126, 50);
line(0, 0, 0, 100, 0, 0);
line(0, 0, 0, 0, 100, 0);
line(0, 0, 0, 0, 0, 100);
line(0,0,0,-100,0,0);
line(0,0,0,0,-100,0);
line(0,0,0,0,0,-100);
for (int i = 0; i < Nodes.length; i++) {
Nodes[i].display(this.g);
}
}
void mouseClicked() {
if (mouseEvent.getClickCount() == 2) {
// draw the scene in the buffer
buffer.beginDraw();
buffer.background(getColor(-1)); // since background is not an object, its id is -1
buffer.noStroke();
buffer.sphereDetail(10);
buffer.setMatrix(((PGraphics3D)g).camera);
//buffer.camera(-20, -20, 50, 0, 0, 0, 0, 1, 0);
//buffer.rotateY(a);
for (int i = 0; i < Nodes.length; i++) {
Nodes[i].drawBuffer(buffer);
}
buffer.endDraw();
// get the pixel color under the mouse
color pick = buffer.get(mouseX, mouseY);
// get object id
int id = getId(pick);
// if id > 0 (background id = -1)
if (id >= 0) {
// change the Node position
Nodes[id].focus();
}
}
}
// id 0 gives color -2, etc.
color getColor(int id) {
return -(id + 2);
}
// color -2 gives 0, etc.
int getId(color c) {
return -(c + 2);
}