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 & HelpSyntax Questions › picking with projection
Page Index Toggle Pages: 1
picking with projection (Read 1078 times)
picking with projection
Oct 16th, 2009, 12:25pm
 
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;
 }
}


Re: picking with projection
Reply #1 - Oct 16th, 2009, 11:36pm
 
Here are my observations, might or might not be helpful:

• the "your link" bit has a comment in the middle of the line of code
• there are some blocks in { curly braces } in both setup() and draw() that have no associated statement, so the braces are going to create problems.
• float(width) has been deprecated in favor of (float)width, etc.
• the "too many left parentheses" error appears to have been caused by the # signs in the fill() commands...check out fill() reference for full details on usage.

After making those changes I end up with a black 3D cube at the bottom of the window, which disappears on mouse-over.  What exactly are you aiming for?
Re: picking with projection
Reply #2 - Oct 17th, 2009, 7:18am
 
ben_hem wrote on Oct 16th, 2009, 11:36pm:
the "your link" bit has a comment in the middle of the line of code
Actually, it won't compile, so I suppose the link have been hidden (or OP couldn't let it because of spam policy of the forum).

Quote:
there are some blocks in { curly braces } in both setup() and draw() that have no associated statement, so the braces are going to create problems.
Not really, they are legal, and can even be useful to limit the scope of some local variables.. Although I see this rarely used (it is better to make small functions instead).

Quote:
float(width) has been deprecated in favor of (float)width, etc.
Ah, where did you saw that Not in float() page at least.

Quote:
the "too many left parentheses" error appears to have been caused by the # signs in the fill() commands...check out fill() reference for full details on usage.
Well, fill(#ff0000); is legal in Processing, that's just fill with some color in hexa/Web form.
Re: picking with projection
Reply #3 - Oct 17th, 2009, 7:47am
 
Ok, lips sealed!  :|  (fingers ...mittened?)  ...Mind expanded
Re: picking with projection
Reply #4 - Oct 17th, 2009, 8:44am
 
ben_hem wrote on Oct 17th, 2009, 7:47am:
Ok, lips sealed!

Sure not! I was terse, so I hope you wasn't offended by my tone. I just wanted to clarify some points, but your input is welcome.
Re: picking with projection
Reply #5 - Oct 17th, 2009, 11:43am
 
PhiLho, no offense taken!  Was trying for tongue in cheek :T , but jokes get lost in text...I didn't know you could have curly braces without statements.  I know I read about the (float) casting somewhere in the new version info -- I only run into issues with it if there's a statement that calls for a left parenthesis next to the float, like this:
println((pow(float(5.7-2.2)),2));;  // the compiler will complain about the parentheses...

The error with (#ff0000) puzzled me; I thought that was legal, but it didn't autoformat properly until I removed the # -- so I jumped to the conclusion that it wasn't the right syntax.  Parser bug?
Re: picking with projection
Reply #6 - Oct 17th, 2009, 1:48pm
 
I think it is autoformat which is buggy, I recall reports against it.
But I never use it...
And indeed, I saw these errors against float pseudo-functions but I had hard time to reproduce them when I wanted... Smiley And actually I rarely use this form too, to be honest.

I perceived the humor, but I preferred to clarify... Sometime, I fear my tone is wrongly translated. Smiley Even in my mother tongue: as you point out, in front of a person, an ironical remark can be tempered by expression, voice tone, a gesture... All things harder to express in writing.
Re: picking with projection
Reply #7 - Oct 19th, 2009, 12:51pm
 
Thank you for your answer.
I had no error-messages when I tried the code.
Yes, I replaced the active link because I had not the right to post it..

I would like to design an 3D-environment with buttons for going to the next environment (or the previous).
The problem I have is when I design a button, I have this one plus an other invisible one. Tom Carden wrote me that I should take attention to the popMatrix and puspMatrix, but I dond't realy find a solution.    

Also, if you have a look at the camera (I found the code on the web) has -Y up. When I import an obj-model and I make a translateY from 0 to -10, the object is higher than before..  Is the camera upside-down?

Do you have maybe an Idee why the picking with projection is not working correct?  

David
Page Index Toggle Pages: 1