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
Clickable 3D Objects? (Read 717 times)
Clickable 3D Objects?
Feb 17th, 2009, 5:50pm
 
Hi,

I'm currently writing a project in Processing that requires that users are able to interact with a 3D object, lets say a cube, by clicking on it.  A simple example might be to double the size of a cube when the cube has been clicked on.

I was wondering what people opinions are on the best way to implement this.

Currently the only way I can see is to implement my own version of the z-buffer algorithm on top of processing for the pixel the user has clicked on.  The problem with this is that somewhere between Processing and OpenGL, all those calculations are already being performed, and it seems a waste to perform them again.  It also requires object position tracking, although I have already implemented that on top of processing for another part of the project (as far as I could tell Processing does not offer a way of getting objects absolute positions in 3D space).

Many Thanks,
Stephen
Re: Clickable 3D Objects?
Reply #1 - Feb 17th, 2009, 6:52pm
 
File this under "Old Hacks"...
This'll work with Beta 0148, but not 1.0+ as the stencil buffer has gone away. (probably a good thing for performance reasons, as it was undocumented and likely seldom used)

The P3D renderer has (had) a stencil buffer you can (could) use as shown in sketch below.  If rendering with OPENGL, then you'd want to create an offscreen PGraphics3D to serve as proxy stencil buffer, and render your scene twice (once to OPENGL to see, once to P3D to pick)

I've run across other "picking" code out there too, just don't happen to have any links handy, some searching should turn them up if this approach doesn't fit.


Quote:
void setup() {
  size(400,400,P3D);
}

void draw() {
  background(0);
  // manually handle the stencil buffer
  // it is created but never cleared:
  Arrays.fill(g.stencil, 0);
  // and the index needs to be reset:
  ((PGraphics3D)g).shape_index = 0;
  // now draw some stuff
  translate(width/3f, height/3f);
  rotateX(PI/4f);
  rotateY(PI/4f);
  fill(255,0,0);
  box(100); // id = 1
  translate(50,50,25);
  fill(0,255,0);
  box(100); // id = 2
  translate(50,50,25);
  fill(0,0,255);
  box(100); // id = 3
  // use the stencil to id what mouse points to:  
  if ((g.stencil!=null) && (mouseX>=0) && (mouseY>=0) && (mouseX<width) && (mouseY<height)) {
    int pixidx = mouseY * width + mouseX;
    int shpidx = g.stencil[pixidx];
    println("stencil value at " + mouseX + ", " + mouseY + " = " + shpidx);
  }
}
Re: Clickable 3D Objects?
Reply #2 - Feb 18th, 2009, 6:05pm
 
Hi davbol,

Thanks, that's exactly the kind of thing I was looking for.  I'm going to have a quick google (now I know it is called "picking") to see if I can find any methods for Processing 1.* as that's what I'm currently using, but if not I'll fall back and use the stencil buffer.

Once again, many thanks!
Steve
Page Index Toggle Pages: 1