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 & HelpOpenGL and 3D Libraries › 3d PVector rotation in OPENGL
Page Index Toggle Pages: 1
3d PVector rotation in OPENGL (Read 1239 times)
3d PVector rotation in OPENGL
Jul 4th, 2009, 10:30am
 
I know this has been asked and answered before, but I need some help parsing and applying the solutions out there.

I have a cube class, comprised of 8 PVectors in an array, and 6 QUADs constructed from them. When called, this class draws a cube just fine, but  of course, rotateX/Y/Z only rotate the cube, not the vertices.

I need to be able to address the PVectors directly, no matter the rotations applied or the camera angle. To do this, I know I'll need to construct a rotation scheme using trig, like this http://mkv25.net/applets/_3dDraw/_3dDraw.pde, but I'm quite lost as to how to set this up.

Currently I have ellipses drawn at the vertices so I can keep track of where they end up, and I'm trying to get their screenX & Y so I can match it to the mouse, like so (clay is the instance of the cube class):

Code:

for (int i = 0; i < 8; i++) {
   float vertXd = (screenX(width/2+clay.vertices[i].x,  clay.vertices[i].y, clay.vertices[i].z));
   float vertYd = (screenY(clay.vertices[i].x, clay.vertices[i].y+height/2, clay.vertices[i].z));
   fill(255);
   ellipse(vertXd, vertYd, 10, 10);
   if ((vertXd < (mouseX + 5)) && (vertXd > (mouseX - 5)) && (vertYd < (mouseY + 5)) && (vertYd > (mouseY - 5))) {
     trigger = true;
   }
 }


If I put a basic rotateX() inside the for() loop, it will only move the ellipses, not the actual PVectors, which does me no good. I know 3d picking ain't easy, but I'm trying.

Any suggestions (or at least nudges in the right direction)?
Re: 3d PVector rotation in OPENGL
Reply #1 - Jul 8th, 2009, 9:25pm
 
Hi Alex,

If the cubes displayed are just fine, you could use this:
http://processing.org/reference/modelX_.html. You will be able to map (as it's called here) the coordinates of your original cube to the new place.

In your example, I think that where you have (0,0,0), you could replace the coordinates by the coordinates of your cubes:

Code:

pushMatrix();

//your rotations...
rotateY(1.0); //yrot);
rotateZ(2.0); //zrot);

//Your well displayed quad...
quad(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y);

//Calculations
float q1.x = modelX(p1.x,p1.y,p1.z);
float q1.y = modelY(p1.x,p1.y,p1.z);
float q1.z = modelZ(p1.x,p1.y,p1.z);

float q2.x = modelX(p2.x,p2.y,p2.z);
float q2.y = modelY(p2.x,p2.y,p2.z);
float q2.z = modelZ(p2.x,p2.y,p2.z);

...

float q3,q4,q5...
popMatrix();


Where p1... are the coordinates of your cube without the transformations.
Where q1... are the coordinates of your cube with the transformation.

---------------------------

Otherwise, the way you describe, by implementing your own set of transformations, this is what we did, and it took us a while to do. You might have a look at http://anar.ch. Maybe this example here is more simple: (http://anar.ch/examples/OOG01cSquare01d.htm). I still have to provide better examples, but you might understand the concept from this applet.

We also have function similar to screenX mapping. (http://anar.ch/examples/Test05c3DPickingConstant.htm). Behind the automatic render in the example, we use a simple function:
Code:

   Anar.screenDrawBegin();
   pushMatrix();
   translate(x,y);    
   //draw some vertices...
   popMatrix();
   Anar.screenDrawEnd();  

Even if the stuff you draw within this loop is in 3D, the result is 2D planar on a screen plane. This is an attempt to reduce the way you could go from 2d to 3d with better fluidity.

http://anar.ch/doc/anar/Anar.html#orientedDrawBegin(anar.XYZ)
http://anar.ch/doc/anar/Transform.html .(This class works well for complex transformation according to a different referential coordinate system (UVW).) You provide three points, and your transformation is applied according to this coordinate system. This is a first draft of the documentation, but it gives an idea of it works.
Re: 3d PVector rotation in OPENGL
Reply #2 - Jul 13th, 2009, 3:03am
 
Thanks very much for all this.

I don't think modelX/Y/Z will help much here - since I'm building the cube out of PVectors, or even if I were using a custom 3D point class, the regular rotateX/Y/Z functions will only affect the resulting cube, not the vector positions (which I'll need, since I'd like to be able to move them later).

Sooner or later I'm going to need to do use the trig functions on the vertices themselves, but I'm not sure how to implement that.

I'm really intrigued by your anar+ system. Is there a link somewhere to the raw code of the sliders? I'd like to be able to control some of those transformations by other means (same for zoom in/out, rotate, etc).
Re: 3d PVector rotation in OPENGL
Reply #3 - Jul 14th, 2009, 7:26am
 
Here's how you interract with sliders: http://anar.ch/doc/anar/Param.html

In fact, what you get on a form of a slider is nothing else than a value (a number). As we had to build our own parametric system, it's a little bit more difficult than writing something like:

Code:
float a = 10;
a = 20;
println(a); //output: 20

It will be:

Code:
Param a = new Param(10);
a.set(20);
println(a); //output: 20
println(a.get()); //output: 20 (as a float)


When you are using set(number), in fact, you are triggering all the depencies built on this object. There's many things that you could do with Param objects, but the most basic is what I explain here. You need to use setters and getters. Sliders are nothing else than a list of Param objects, that could be automatically displayed on screen (providing interraction). If you say

Code:
Param b = Anar.sliders.get(0); 


you get the first parameter in the list. You could change the value by this:

Code:
Anar.sliders.get(0).set(10); 


This means I change the value of the first parameter in the default list (Anar.sliders) for the value of 10.

This is the same for coordinates, built on three Param (x,y,z). In this case, you access them by using myPt.getX() or myPt.setX(10); For simplification, they are just written myPt.x(10); [setter] and myPt.x(); [getter]. There's many alternatives, you could also write myPt.set(1,2,3); to define (x,y,z).

http://anar.ch/doc/anar/Pt.html#set%28float%29

The camera is simply a set of points (define with target and camPosition). http://anar.ch/doc/anar/Camera.html

To define your own cam position you could say:

Code:
Scene.setCamera(x,y,z); //for the camera
Scene.setCenter(x,y,z); //for the target


I'm working to provide a direct access to the camera, but it's nothing else than a normal point (coordinate). I hope we could publish the code soon, I realize that it would help a lot of people. I still need to clean up a bit and find a proper place (github?).

I don't have access to my computer right now as it is part of an exhibition in Florence. I can't even work on it now.
Cool
Page Index Toggle Pages: 1