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 › Matrix Stacks -- auugh!
Page Index Toggle Pages: 1
Matrix Stacks -- auugh! (Read 512 times)
Matrix Stacks -- auugh!
Nov 13th, 2008, 1:11pm
 
I feel like a complete idiot here, but I'm trying and trying to understand the whole point of matrix stacks and I just don't get it.

I ran this code:

void setup()
{
 size(640, 480,P3D);
 noStroke();
 smooth();
translate(50,50);
printMatrix();
}

and got this:

001.0000  000.0000  000.0000 -270.0000
000.0000  001.0000  000.0000 -190.0000
000.0000  000.0000  001.0000 -415.6922
000.0000  000.0000  000.0000  001.0000


WTF? Where are those numbers coming from? What's with the random decimal points? "-415.6922"? Huh?

I don't get this at all. And Googling doesn't help; no sites seem to give me a useful or adequate explanation of what matrices are in this context, or why they'd be useful to me.

Please help, as I suspect that a lot of what I want to do in Processing requires these bastard things, and this is driving me mad.

[Update] it just occurred to me that -270 - 50 = -320, which is half my sketch's width, and -190 - 50 = -240, which is the height. But I still don't understand anything beyond that.
Re: Matrix Stacks -- auugh!
Reply #1 - Nov 13th, 2008, 1:33pm
 
The matrix doesn't start at 0.. there's an initial matrix to set 0,0,0 to be the top left of the screen.. with an identity matrix, 0,0,0 would be at the point of the camera.
Re: Matrix Stacks -- auugh!
Reply #2 - Nov 13th, 2008, 6:50pm
 
If you care about how matrices actually work, find a book about 3D graphics programming, and look for topics related to perspective, projection, and the camera.

But it's very rare that you need to worry about the exact numbers found in the matrix, so perhaps you have a different question about something you're trying to do and cannot?
Re: Matrix Stacks -- auugh!
Reply #3 - Nov 13th, 2008, 7:02pm
 
I just don't understand at all what matrix stacks are or what the point of them is. Which makes them impossible for me to use.
Re: Matrix Stacks -- auugh!
Reply #4 - Nov 13th, 2008, 8:17pm
 
The matrix stack allows you to 'stack' transformations of the graphic space, literally adding (well, technically I think the matrices are multiplied) one transformation on top of another. Instead of printing the matrix after translating, rotating, and scaling the drawing context, try drawing something. That should help tremendously with your understanding.

Pushing the matrix is like sticking your hand on top of the stack before adding new transformations. Popping is picking your hand up, and knocking off all the transformations on top of it.

So, instead of printing the matrix, draw something on screen:

Code:

translate( width/4, height/4 );
rect(0,0,50,50);
pushMatrix();
rotate(45);
fill(127);
rect(0,0,50,50);
scale(0.5);
fill(55);
rect(0,0,50,50);
popMatrix();
fill(0);
rect(0,0,10,10);


If you haven't already quit in frustration, I hope this helps.
Re: Matrix Stacks -- auugh!
Reply #5 - Nov 13th, 2008, 9:29pm
 
But here's what I don't get -- I get the exact same result from your code, wicks, if I remove the pushMatrix() and popMatrix() commands. The sketch looks identical. So I'm totally not understanding why you would use this at all.

Forgive me if I sound dense; I'm actually a fairly decent Processing coder for lots of stuff, like Perlin noise flow fields and MIDI and audio. But I'm trying to learn how to do 3D geometry coding, and I'm totally lost with this.

I also don't understand how this can be applied to geometry in one case, and image processing in another.
Re: Matrix Stacks -- auugh!
Reply #6 - Nov 13th, 2008, 9:36pm
 
Okay, when I switch to OpenGL mode, it does something different...but I'm still not understanding what.

I feel very stupid.
Re: Matrix Stacks -- auugh!
Reply #7 - Nov 13th, 2008, 11:11pm
 
OK, try this:
Code:

translate(width/2,height/2);
rect(20);
pushMatrix();
translate(100,0);
rect(20);
popMatrix();
pushMatrix();
translate(0,100);
rect(20);
popMatrix();


This is a bit trivial, and you could to translate(-100,100); the 2nd time, but imagine a situation where you don't know the previous transformations, because it's within a class. push/pop matrix separate out the transformations so that one set of changes don't affect another.
Re: Matrix Stacks -- auugh!
Reply #8 - Nov 13th, 2008, 11:15pm
 
So using push and pop basically resets the transformations? I think I understand now. It keeps the transformation local within a function or class, and then after that function or class does its thing it resets it to the original values.

Okay, that makes more sense now.
Re: Matrix Stacks -- auugh!
Reply #9 - Nov 13th, 2008, 11:47pm
 
was about to reply to this when the last couple posts came in...looks mostly solved.

jzellis wrote on Nov 13th, 2008, 11:15pm:
So using push and pop basically resets the transformations


as i understand it, yes. transformations are cumulative, and pushing and popping "detours" from the linear code to keep the enclosed transformations specific to whatever happens between the push and pop commands. pushMatrix() puts you on the detour, and popMatrix() brings you back to where pushMatrix() occurred.

from looking at the reference, i can't see what printMatrix() would be useful for...other than confusing us.
Re: Matrix Stacks -- auugh!
Reply #10 - Nov 14th, 2008, 2:28pm
 
not sure if it's (still) relevant, but this is a really nice book to browse through:

http://www.opengl.org/documentation/red_book/ ->
http://www.glprogramming.com/red/

Also some 'tutor' programs where you can fiddle with some settings:
http://www.xmission.com/~nate/tutors.html

And the slides we got for the course computer graphics at our university about the Mathstuffs:
http://www.cs.vu.nl/~graphics/slides/lecture4-2up.pdf

HTH
Page Index Toggle Pages: 1