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
3d mouse pointing (Read 2781 times)
3d mouse pointing
Dec 12th, 2009, 2:24am
 
hi, i was writing this program where i want to rotate the 3d cube grid and then pinpoint the exact x,y,z intersections with the mouse.
so now I can rotate a cube, but how do the mouse coordinates for the z axis work?
I was reading other threads, but as I am new I had a bit of a problem understanding everything.

my code here is:

import processing.opengl.*;

float rotx = 0;
float roty = 0;
float rotz = 0;
float rate = 0.01;
float depth = 400;
float expo = 1;

void setup()
{
 size(700,400,OPENGL);
 stroke(150);
 smooth();
}

void draw()
{  
 if (keyPressed && keyCode==UP)
 {
   expo = expo + 0.01;
 }
 if (keyPressed && keyCode==DOWN)
 {
   expo = expo - 0.01;
 }
 translate(width/2,height/2,-100);
 scale(expo);
 background(250);
 rotateX(rotx);
 rotateY(roty);
 for (float x = -height/2; x<=height/2; x+=100)
 {
   for (float y = -height/2; y<=height/2; y+=100)
   {
     for(float z = -height/2; z<=height/2; z+=100)
     {
       pushMatrix();
       translate(x,y,z);
       line(-x,y,z,x,y,z);
       line(x,-y,z,x,y,z);
       line(x,y,-z,x,y,z);  
       popMatrix();
     }
   }
 }
}



void mouseDragged()
{
 roty =roty + (mouseX - pmouseX) * rate;
 rotx =rotx - (mouseY - pmouseY) * rate;
}

Re: 3d mouse pointing
Reply #1 - Dec 12th, 2009, 10:32am
 
still working on this... =[ seriously is there a way to transform your 3d object thats facing the camera into 2D coordinates so the mouse can pick any point if the object is capable of rotating around?
I thought I could do it somehow if I rewrite the code to use the OCD. OCD works real good, but I can't move further from there.
Re: 3d mouse pointing
Reply #2 - Dec 12th, 2009, 11:40am
 
Picking, maybe?

http://www.processing.org/hacks/hacks:picking

Does it help?
Re: 3d mouse pointing
Reply #3 - Dec 12th, 2009, 1:19pm
 
it possible would, but i can't seem to be able to apply it to the 3d Cube Grid  Embarrassed but this looks similar enough to what i need, i will try it and come back with the results.

thanks!!!
Re: 3d mouse pointing
Reply #4 - Dec 12th, 2009, 1:38pm
 
Maybe you could start with adding spheres to the grid intersections. Then, it's easy to know if a sphere has been pointed with the mouse using the picking method. At the end, you can make the spheres invisible, and it will feel like each node in your grid is clickable.
Re: 3d mouse pointing
Reply #5 - Dec 13th, 2009, 5:45am
 
thanks antiplastic!!! Grin it actually works. I used boxes in intersections for now and just to keep it simple tried to change fill() of the boxes once the cursor is over them. it really is almost perfect. almost.... at some certain angles the boxes change fill on their own. Roll Eyes  i just have two more questions. how would i make buffer boxes all in different colours without having to fill them all separately? and I've tried to run it with spheres just now. That made the program really slow. Is there a way to speed it up?
At the moment my grid mesh is quite rare and I understand that one or two spheres won't make it as slow as spheres on every intersection, but later on the program will have a denser mesh and i think that will make the program near to unusable due to all the spheres in buffer. Cab I increase somehow the dedicated buffer size?

any answers or hints are really welcome

at this stage I got this far:

import processing.opengl.*;

PGraphics bf;
float expo = 1;
float rotx, roty;
float rate = 0.01;

void mouseDragged()
{
 rotx = rotx + (pmouseY - mouseY)*rate;
 roty = roty - (pmouseX - mouseX)*rate;
}

void setup()
{
 size(700,400,OPENGL);
 bf = createGraphics(width,height,P3D);
 stroke(150);
}

void draw()
{
 background(255);
 translate(width/2,height/2,0);
 if (keyPressed && keyCode==UP)
 {
   expo = expo + 0.01;
 }
 if (keyPressed && keyCode==DOWN)
 {
   expo = expo - 0.01;
 }
 rotateX(rotx);
 rotateY(roty);
 scale(expo);
 for (int x = -height/2; x<=height/2; x+=100)
 {
   for (int y = -height/2; y<=height/2; y+=100)
   {
     for (int z = -height/2; z<=height/2; z+=100)
     {
       pushMatrix();
       translate(x,y,z);
       line(x,y,z,-x,y,z);
       line(x,y,z,x,-y,z);
       line(x,y,z,x,y,-z);
       pushMatrix();
       translate(x,y,z);
       color inbf = bf.get(mouseX,mouseY);
       fill(inbf);
       box(10);
       popMatrix();
       popMatrix();
     }
   }
 }
 bf.beginDraw();
 bf.background(0);
 bf.translate(width/2,height/2,0);
 bf.rotateX(rotx);
 bf.rotateY(roty);
 bf.scale(expo);
 for (int x = -height/2; x<=height/2; x+=100)
 {
   for (int y = -height/2; y<=height/2; y+=100)
   {
     for (int z = -height/2; z<=height/2; z+=100)
     {
       bf.pushMatrix();
       bf.translate(x,y,z);
       bf.pushMatrix();
       bf.translate(x,y,z);
       bf.noStroke();
       bf.fill(166);
       bf.box(10);
       bf.popMatrix();
       bf.popMatrix();
     }
   }
 }
}
Re: 3d mouse pointing
Reply #6 - Dec 13th, 2009, 10:31am
 
Quote:
how would i make buffer boxes all in different colours without having to fill them all separately?

Make an array of boxes. The thing is, you need to map each box to the color you used to draw it. An HashMap would be a good idea (you can use colors for keys, and boxes as values). I've written a small tuto but it's in french (you can have a look at the code, though) :
http://n.clavaud.free.fr/processing/picking/

Quote:
I've tried to run it with spheres just now. That made the program really slow. Is there a way to speed it up?

Forget about spheres. You can draw 2D ellipses or 2D rectangles in the buffer.
Re: 3d mouse pointing
Reply #7 - Dec 15th, 2009, 6:28am
 
hey, antiplastic! as I am quite new to processing (just about a month) i could not really understand all the arrays and mapping stuff. sorry could not follow your advice there.. =] though i came up with the idea to use red(), green(), blue() values of the color in the buffer and use them as translate(x,y,z). works really great. (I imagine that this is somehow the long way of arrays and hashmap). I understand that this is a very limited method as there are only 255 possible values for each axis, but for now it works for me.  Smiley  thanks for all the help, it was your suggestion initially that set me on this approach. I am sure I'll need more help further down the project somewhere, but for now the code is like this:

import processing.opengl.*;

PGraphics bf;
float expo = 1;
float rotx, roty;
float rate = 0.01;

void mouseDragged()
{
 rotx = rotx + (mouseY - pmouseY) * rate;
 roty = roty + (mouseX - pmouseX) * rate;
}

void setup()
{
 size(700,400,OPENGL);
 bf = createGraphics(width,height,P3D);
 stroke(150);
}

void draw()
{
 background(255);
 if (keyPressed && keyCode==UP)
 {
   expo = expo + 0.01;
 }
 if (keyPressed && keyCode==DOWN)
 {
   expo = expo - 0.01;
 }
 translate(350,200,0);
 rotateX(rotx);
 rotateY(roty);
 scale(expo);
 translate(-350,-350,-350);
 for (int x = 150; x<=550; x+=100)
 {
   for (int y = 150; y<=550; y+=100)
   {
     for (int z = 150; z<=550; z+=100)
     {      
       line(150,y,z,x,y,z);
       line(x,150,z,x,y,z);
       line(x,y,150,x,y,z);
       pushMatrix();
       color c1 = bf.get(mouseX,mouseY);
       color c2 = color(0);
       if (c1 > c2)
       {
       translate(red(c1)*10,green(c1)*10,blue(c1)*10);
       fill(0);
       box(10);
       }
       popMatrix();
     }
   }
 }
 bf.beginDraw();
 bf.background(255);
 bf.translate(350,200,0);
 bf.rotateX(rotx);
 bf.rotateY(roty);
 bf.scale(expo);
 bf.translate(-350,-350,-350);
 for (int x = 150; x<=550; x+=100)
 {
   for (int y = 150; y<=550; y+=100)
   {
     for (int z = 150; z<=550; z+=100)
     {      
       bf.line(150,y,z,x,y,z);
       bf.line(x,150,z,x,y,z);
       bf.line(x,y,150,x,y,z);
       bf.pushMatrix();
       bf.translate(x,y,z);
       bf.noStroke();
       bf.fill(x/10,y/10,z/10);
       bf.box(10);
       bf.popMatrix();
     }
   }
 }
}
 

thanks again, antiplastic!
Re: 3d mouse pointing
Reply #8 - Dec 17th, 2009, 5:26am
 
A small comment and suggestion to this thread.

The picking hack is a general way to know if the mouse is over a shape. The process described in this article is similar to a suggested way  in OpenGL graphics (gluPickMatrix) where you involve an offline render. One good side of this process is that you could pick an object even if you pick inside a face (where there is in fact no vertices). One bad side of this is that you need to render your scene twice, which might slow everything.

An alternative might be to project all points of your scene on an ideal plane (which is the screen). Then, you could test if the mouse coordinates are near or exactly over the points. In fact, you build a flat representation of your scene similar to your 2d screen. In this case, the model is just a set of 2D points and you can't pick a face (triangle of polygon) from inside. I don't know how much faster it is, but I think it might be faster, since you don't have to render the whole scene offline.

These are the two most common ways to do 3D picking.

[http://www.euclideanspace.com/maths/geometry/elements/plane/lineOnPlane/index.htm]
Re: 3d mouse pointing
Reply #9 - Dec 17th, 2009, 5:37am
 
There's a method in processing to handle projections.

http://processing.org/reference/screenX_.html

Note: Personally, I think that this section could be improved if the method would return an array[Floats] or a PVector, otherwise you calculate the projection twice with screenX(x,y,z) and screenY(x,y,z). Here, (x,y,z) are the same.

Suggestion (note, this is not implemented):
PVector 2dPostionOnScreen = screen(x,y,z);

Where PVector is 2D.

And what about screenZ? It was a joke or a prophecy? Maybe Ben and Casey where thinking about the Avatar's 3D movie? Coming soon...  Grin

I think screenZ should be removed from the documentation, isn't it?
http://processing.org/reference/screenZ_.html
Re: 3d mouse pointing
Reply #10 - Dec 30th, 2009, 10:50am
 

Avatar was awesome.

Cameron gave me back the cinema.

Any progress with the 3D-turtle?

Thanks,

Chrisir
Page Index Toggle Pages: 1