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
opengl picking (Read 1043 times)
opengl picking
Nov 27th, 2007, 9:28am
 
hi all,

since JOGL allows 3D object picking, and Processing relies on JOGL for OpenGL display, I would like to know if any of you ever succeeded in implementing it in a processing sketch?

(it would save me A LOT of time!)
Re: opengl picking
Reply #1 - Nov 27th, 2007, 10:58am
 
you can measure the distance from your mouseposition to the 2d-screencoordiantes of your 3d-objects.
search the reference for screenX() screenY() screenZ()
Re: opengl picking
Reply #2 - Nov 27th, 2007, 8:12pm
 
Thank you for your reply. I wouldn't have though about this solution...

Anyway, I found something more appropriate for what I need : drawing in a back-buffer with different colors (one for each object), and retrieve the color value of the picked pixel to get the object picked. I'll post an example here as soon as possible.
Re: opengl picking
Reply #3 - Nov 28th, 2007, 9:50am
 
Finally, I used P3D, which allows to draw in a back-buffer (OpenGL doesn't).

Therefore, posting this here may be off-board, since we're in the OpenGL topic, but here is the code anyway :

Press 'b' to see the back-buffer content.

Code:
Cube[] cubes;

PGraphics buffer;
PGraphics pickbuffer;
float a = 0;

void setup() {

size(100, 100);
frameRate(30);

cubes = new Cube[2];
cubes[0] = new Cube(0, 0, 0, 0);
cubes[1] = new Cube(100, 0, -2, -10);

buffer = createGraphics(100, 100, P3D);
buffer.camera(2, -5, 10, 0, 0, 0, 0, 1, 0);
buffer.hint(ENABLE_DEPTH_SORT);

pickbuffer = createGraphics(100, 100, P3D);
pickbuffer.camera(2, -5, 10, 0, 0, 0, 0, 1, 0);
pickbuffer.hint(ENABLE_DEPTH_SORT);
}

void mouseClicked() {
drawOnPickBuffer();
color pick = pickbuffer.get(mouseX, mouseY);
int id = (int)((red(pick)+green(pick)+blue(pick))/3);
for (int i = 0; i < cubes.length; i++)
if (cubes[i].id == id) cubes[i].changeColor();
}

void draw() {
drawOnBuffer();
image(buffer, 0, 0);
if (keyPressed && key == 'b') {
drawOnPickBuffer();
image(pickbuffer, 0, 0);
}
a += PI/80; if (a > TWO_PI) a -= TWO_PI;
}

void drawOnBuffer() {
buffer.beginDraw();
buffer.background(255);
for (int i = 0; i < cubes.length; i++) {
buffer.fill(cubes[i].c);
buffer.stroke(0);
buffer.pushMatrix();
buffer.translate(cubes[i].x, cubes[i].y, cubes[i].z);
buffer.rotateY(a);
buffer.box(5);
buffer.popMatrix();
}
buffer.endDraw();
}

void drawOnPickBuffer() {
pickbuffer.beginDraw();
pickbuffer.background(250, 200, 50);
for (int i = 0; i < cubes.length; i++) {
color idcolor = color(cubes[i].id);
pickbuffer.fill(idcolor);
pickbuffer.stroke(idcolor);
pickbuffer.pushMatrix();
pickbuffer.translate(cubes[i].x, cubes[i].y, cubes[i].z);
pickbuffer.rotateY(a);
pickbuffer.box(5);
pickbuffer.popMatrix();
}
pickbuffer.endDraw();
}

class Cube {

int id;
int x, y, z;
color c;

public Cube(int id, int x, int y, int z) {
this.id = id; this.x = x; this.y = y; this.z = z;
changeColor();
}

public void changeColor() {
c = color((int)random(255), (int)random(255), (int)random(255));
}

}


Edit : actually, you can use this method with OpenGL : just use OpenGL for display and P3D for the back-buffer. :-D
Page Index Toggle Pages: 1