We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Picking in 3d (Read 1259 times)
Picking in 3d
Mar 4th, 2009, 10:48pm
 
I've tried to work with the Hack examples but with no luck.  I'm dealing with a sphere or box and using Peasy for a camera library.  The problem I'm running into is with setting the camera position in the buffer.

You would think we could do a printCamera() and feed that into buffer.camera(), but that doesn't work.  printCamera is a matrix and camera won't accept it.  Any suggestions on how to do this?
Re: Picking in 3d
Reply #1 - Mar 5th, 2009, 3:20pm
 
I got it working thanks to Jonathan Feinberg's direction.

Here is an example based on the Hacks code using the Peasy camera.  I'll post a more specific example using Peasy lookat features in this thread (http://processing.org/discourse/yabb_beta/YaBB.cgi?board=OpenGL;action=display;num=1234988194;start=15)

The primary change for dealing with a non-static camera using the hacks example is to comment out:
--- draw ---
//buffer.camera(-20, -20, 50, 0, 0, 0, 0, 1, 0);
//a += 0.002;
//if (a > TWO_PI) a -= TWO_PI;
--- mouseclicked ---
// buffer.camera(-20, -20, 50, 0, 0, 0, 0, 1, 0);
// buffer.rotateY(a);

and replace it with:
--- mouseclicked ---
 buffer.setMatrix(((PGraphics3D)g).camera);

Quote:
/**
picking code based on http://processinghacks.com/hacks:picking
@author nicolas clavaud
*/

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 changeColor() {
   int r = (int)red(c);
   int g = (int)green(c);
   int b = (int)blue(c);
   c = color(r, 255 - g, b);
 }

 // display the Node on screen
 public void display(PGraphics ecran) {
   ecran.fill(c);
   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);
 cam = new PeasyCam(this, 100);
 cam.setResetOnDoubleClick(false);
 cam.setMinimumDistance(50);
 cam.setMaximumDistance(5000);
 // buffer is created using applet dimensions
 buffer = createGraphics(width, height, P3D);

 // put Nodes randomly in the scene
 Nodes = new Node[10];
 for (int i = 0; i < Nodes.length; i++) {
   Nodes[i] = new Node(
     i,                      // identifiant
     -15 + (int)random(30),  // position x
     -15 + (int)random(30),  // position y
     -15 + (int)random(30),  // position z
     5 + (int)random(15)     // taille
   );
 }

}

void draw() {

 background(0);
 // camera(-20, -20, 50, 0, 0, 0, 0, 1, 0);

 lights();
 for (int i = 0; i < Nodes.length; i++) {
   Nodes[i].display(this.g);
 }

 //a += 0.002;
 //if (a > TWO_PI) a -= TWO_PI;

}

void mouseClicked() {
 
 
 // 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 color
   Nodes[id].changeColor();
 }

}

// 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);
}
Page Index Toggle Pages: 1