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 › pushmatrix() and popMatrix() ??
Page Index Toggle Pages: 1
pushmatrix() and popMatrix() ???? (Read 2443 times)
pushmatrix() and popMatrix() ????
Mar 9th, 2009, 10:10pm
 
pushmatrix() and popMatrix() ????

Hi, how can I get to understand what's going on with this methods?,

I know they control the coordinates when using P3D rendering or openGL but can't understand very well the concept behind.

do you?

thanks.
Re: pushmatrix() and popMatrix() ????
Reply #1 - Mar 9th, 2009, 10:28pm
 
from the reference "The pushMatrix() function saves the current coordinate system to the stack and popMatrix() restores the prior coordinate system."

look at the examples on the reference pages

http://processing.org/reference/pushMatrix_.html
http://processing.org/reference/popMatrix_.html

basically it means, someone correct me if i'm wrong.

you reset the coordinates of back to whatever the previous transformation was, and if there weren't any, it would revert back to (0,0)

examples.
-----

if you want to move a shape over 10 pixels you would do it like this:

Code:

translate(10,0);
rect(0,0, 20,20);


now your shape has moved 10 pixels to the right.

----
however let's say you have two shapes and you want the first one to move 10 pxiels to the right of the origin point and the second one to move 10 pixels to the left of the origin point

you could do it like this

Code:

translate(10,0);
rect(0,0, 20,20);

translate(-20,0);
rect(0,20, 20,20);



however, if you were to do this you would just undo the first translate, (as opposed to moving the second shape to the left 10 pixels), back to the origin point.

Code:

translate(10,0);  //set the X translate to 10 pixels right
rect(0,0, 20,20);

translate(-10,0); //just reset the X translate to 0
rect(0,20, 20,20);


but if you were to the same thing using pushMatrix() and popMatrix() you'll see that our second shape is now left of the origin by 10 pixels

Code:

pushMatrix();
translate(10,0);   //move shpae right 10 pixels
rect(0,0, 20,20);
popMatrix();      //reset coordinates

translate(-10,0);  //move shape left 10 pixels
rect(0,20, 20,20);


make sense?

Ken
Page Index Toggle Pages: 1