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 › 3d -> 2d transformation
Page Index Toggle Pages: 1
3d -> 2d transformation? (Read 923 times)
3d -> 2d transformation?
Sep 16th, 2007, 1:07am
 
if i call line(x0,y0,z0,x1,y1,z1); to draw a line in 3d, a transformation from 3d to the 2 dimentional space of my screen occures. Can i access this transformation?

Is there a function that takes in a 3d coordinate and returns the 2d coordinate it would be projected to?
Re: 3d -> 2d transformation?
Reply #1 - Sep 16th, 2007, 1:58am
 
See screenX/screenY/screenZ, http://processing.org/reference/screenX_.html et al.
Re: 3d -> 2d transformation?
Reply #2 - Sep 16th, 2007, 3:23am
 
omfg. i wish i'd noticed that the language section came in abridged & extended versions sooner Smiley

thanks!
Re: 3d -> 2d transformation?
Reply #3 - Sep 16th, 2007, 4:17am
 
ok, why isn't this working? i'd like to perform a rotation about Y, compute the screen space coordinates of my 3d points, then draw that line in 2d using line() and screenX()/screenY(). so this is what i have:


float angle = 0;
void draw() {
 background(255);
 
 translate(width/2, height/2);
 rotateY(radians(angle));
 angle += 0.5;
 if(angle>360) {
   angle -= 360;
 }
 translate(-width/2, -height/2);
 
 
 float x0 = 0, y0 = height, z0 = 0,
       x1 = width, y1 = 0, z1 = 0;
 
 float s0X = screenX(x0,y0,z0), s1X = screenX(x1,y1,z1);
 float s0Y = screenY(x0,y0,z0), s1Y = screenY(x1,y1,z1);
 
 line(x0,y0,z0,x1,y1,z1);
 
 pushMatrix();
 line(s0X,s0Y,s1X,s1Y);
 popMatrix();
}


but the line i draw itself seems to get rotated itself. why do these lines diverge?
Re: 3d -> 2d transformation?
Reply #4 - Sep 16th, 2007, 1:44pm
 
The 2D line is affected by the rotation, as well as the 3D one. The screenX/Y values you get are the position on screen, given *no* transformations.

You need to undo the rotations/transaltions before you draw the 2D one.

so you need to do something like:
Code:
void draw()
{
pushMatrix(); //so we can go back to no transformations.
translate(...);
rotate(...);
translate(..);
x0=...;
//etc
sx0=...;
//etc
line(...); //3D line

popMatrix();
// now we have the default settings.
line(...); //2D line
}
Re: 3d -> 2d transformation?
Reply #5 - Sep 17th, 2007, 7:37am
 
aaahh. ok, i had that backwards.
Page Index Toggle Pages: 1