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.
IndexProgramming Questions & HelpPrograms › picking with projection
Page Index Toggle Pages: 1
picking with projection (Read 414 times)
picking with projection
Oct 16th, 2009, 12:34pm
 
Hello,

I used the picking with projection code from Tom Carden for creating buttons in 3D. Unfortunately, I discover now, after some changes, that for each button I create there is a "ghost-button".

I asked myself if it could be a wrong use of pushMatrix();/ popMatrix();, but I don't have enought experience with processing to find the fault myself..

I would be very grateful for some help.
Thank you very much,
David  



code:

import processing.opengl.*;
import javax.media.opengl.GL;


PVector camPosition;
PVector camLookAt;
float camHeight = -6;

Vector triangles_01 = new Vector();

float currentAngle = -1.5;
int picked_01;


class Vector3d {

float x,y,z;

Vector3d(float x, float y, float z) {
  this.x = x;
  this.y = y;
  this.z = z;
}



Vector3d project() {
  return new Vector3d(screenX(x,y,z),screenY(x,y,z),screenZ(x,y,z));
}

}



////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////



class Triangle_01 implements Comparable {

Vector3d a,b,c;
Vector3d pa,pb,pc;
boolean clicked = false;



Triangle_01(Vector3d v1, Vector3d v2, Vector3d v3)
{
  this.a = v1;
  this.b = v2;
  this.c = v3;
}

void project() {
  pa = a.project();
  pb = b.project();
  pc = c.project();
}
 
boolean mouseOver() {
  Polygon p = new Polygon( new int [] { (int)pa.x, (int)pb.x, (int)pc.x }, new int [] { (int)pa.y, (int)pb.y, (int)pc.y }, 3);
  return p.inside(mouseX,mouseY);
}

int compareTo(Object other) {
  // compare average depth
  float d1 = pa.z+pb.z+pc.z;
  Triangle_01 t2 = (Triangle_01)other;
  float d2 = t2.pa.z+t2.pb.z+t2.pc.z;
  if (d1>d2) {
    return 1;
  }
  else if (d1==d2) {
    return 0;
  }
  else {
    return -1;
  }
}


}




////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////


int getPicked_01() {
int picked_01 = -1;
if (mouseX >= 0 && mouseX < width && mouseY >= 0 && mouseY < height) {
  for (int i = 0; i < triangles_01.size(); i++) {
    Triangle_01 t = (Triangle_01)triangles_01.get(i);
    t.project();
  }
  Collections.sort(triangles_01);
  for (int i = 0; i < triangles_01.size(); i++) {
    Triangle_01 t = (Triangle_01)triangles_01.get(i);
    if(t.mouseOver()) {
      picked_01 = i;
      break;
    }
  }
}
return picked_01;
}


////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////

void setup() {
size(1000, 562,OPENGL);


camPosition = new PVector( -26.0, camHeight, 12.71);
camLookAt = new PVector(camPosition.x + 100*cos(currentAngle), camPosition.y, camPosition.z + 100*sin(currentAngle));


{
  Vector3d v1 = new Vector3d(-25.5,-5.0,10.3); //unten
  Vector3d v2 = new Vector3d(-26.3,-5.75,12.05); //Seite rechts
  Vector3d v3 = new Vector3d(-26.0,-5.0,12.85); //Spitze
  Vector3d v4 = new Vector3d(-25.3,-4.2,12.2); //Seite links
 
 triangles_01.add(new Triangle_01(v1, v2, v3));
 triangles_01.add(new Triangle_01(v1, v3, v4));
}


noStroke();
}









void draw() {
background(255);


beginCamera();
  camera(camPosition.x, camPosition.y, camPosition.z,
         camLookAt.x, camLookAt.y, camLookAt.z,
         0, 1, 0);
 
  //for perspective
  float aspect = float(width)/float(height);
  perspective(1.235, aspect, .1, 500);
endCamera();





 pushMatrix();

picked_01 = getPicked_01();



{
beginShape(TRIANGLES);
noStroke();
for (int i = 0; i < triangles_01.size(); i++) {
  Triangle_01 t = (Triangle_01)triangles_01.get(i);
  if (i == picked_01) {
    fill(#ff0000);
  }
  else {
    fill(#050505);
  }
 
  vertex(t.a.x,t.a.y,t.a.z);
  vertex(t.b.x,t.b.y,t.b.z);
  vertex(t.c.x,t.c.y,t.c.z);
 
   if (t.clicked) {
     link("
     
     
     //your link
     
     
     ");
     t.clicked = false;
     }
   
}
endShape();  
}

popMatrix();

}


void mouseReleased() {
if (picked_01 >= 0 && picked_01 < triangles_01.size()) {
  Triangle_01 t = (Triangle_01)triangles_01.get(picked_01);
  t.clicked = !t.clicked;
}
}
Page Index Toggle Pages: 1