# OpenGL How to find the center of a modelview matrix?

This topic is 987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, guys, I have 1 stupid question. But first some introduction. I'm reading that "Beginning OpenGL Game Programming" book because it's written by some smart foos from gamedev.net. The thing that I don't understand in particular is this: (don't read the whole stuff if you don't have time)

This is from the book:

"Using the modelview matrix stack essentially allows you to transform from one coordinate

system to another while being able to revert back to the original coordinate system.
For instance, if we position ourselves at the point (10, 5, 7), and we then push the current
modelview matrix onto the current stack, then our current transformation matrix is reset
to the local coordinate system centered around the point (10, 5, 7). This means that any
transformations we do are now based on the coordinate system at (10, 5, 7). So if we then
translate 10 units down the positive x axis with glTranslate(10.0, 0.0, 0.0), we are at the
position (10, 0, 0) in the current transformation matrix, but in the world we are positioned
at (20, 5, 7).When the matrix stack is popped, we revert back to the original transformation
matrix and therefore the original coordinate system, which means we are again

positioned at (10, 5, 7)."

The Question: If I have 1 object which is made by 3 vectors. E.g. vector1 = [ 2, 2, 1 ], vector2 = [ 3, 3, 1 ], vector3 = [ 4, 4, 1 ]. We are talking world coordinates here. Now the local matrix is this: (it's like the world matrix because I haven't pushed or popped or moved anything)

[                 ]
[  x  y  z  w  ]

[  2, 2, 1, 1  ]
[  3, 3, 1, 1  ]
[  4, 4, 1, 1  ]

[                 ]
( this means "big brackets" ).

( I need "w" for the translation below ).
So the vectors are in a certain position from the origin (center of the world) .Then I decide to multiply that local / world matrix by some translation matrix, for example, glTranslate( z by 10 ) and push the matrix onto the stack, then how the heck the transformation matrix gets reset to the local coordinate matrix. First I thought that "reset" means glLoadIdentity(), but all this does is set 1s to the diagonal numbers, which will set these particular vectors on all the wrong places, so it's not that. The most logical solution is to see where the center of the object was translated to after the glTranslate and to construct the model/local/whatever matrix based on that center, but how exactly this stuff happens, and how this center is calculated, I don't know.

After the translation, the matrix is like this:

[               ]

[  x, y,  z   ]
[  2, 2, 11  ]
[  3, 3, 11  ]
[  4, 4, 11  ]

[               ]

Now what does this "reset to local coord. system" means?

Give me some basic explanation here, because I feel like I'm doing magic when I call these push and pop functions. How this change of coordinate systems happens???

Edited by Heelp

##### Share on other sites

Imagine a stack of matrices, in this case the "model_view" matrix stack activated with a call to glMatrixMode()

<Top>
[matrix1]
[matrix0]
<bottom>
-----------

glPushMatrix() copies the matrix on the top, and then makes it the new top

<Top>
[matrix1copy]
[matrix1]
[matrix0]
<bottom>
-----------

this essentially saves matrix1 (any transforms youve done) and creates a copy, which you might then follow with a call that modifies the top matrix further, such as

glTranslate*() -> builds a translation matrix and multiplies it into the top of the current stack (the matrix 1 copy) further transforming matrix 1
glScale() -> same thing, but a scale matrix is multipled into top
glRotate()  -> same thing, but a rotation matrix it multipled into top
glLoadIdentity() -> clear top to identity
glMultMatrix() -> you provide the matrix already built

<Top>
[matrix1copy] *= Trans|Scale|Rot Matrix
[matrix1]
[matrix0]
<bottom>
-----------

now you draw, its use the top, then when you are done drawing, you call PopMatrix(), which removes the top and goes back to the original

<Top>
[matrix1]
[matrix0]
<bottom>
-----------

simple example, want to draw an object at 10 down +x, at 20 down +x, and 5 up +y

<Top>
[matrix0.identity]
<bottom>
-----------

PushMatrix()

<Top>
[matrix0.identity] <- glPushMatrix()
[matrix0.identity]
<bottom>
-----------

<Top>
[matrix0.(10, 0, 0)] <- PushMatrix(), *= glTranslate(10, 0, 0), now draws at 10 down x
[matrix0.identity]
<bottom>
-----------

<Top>
[matrix0.(20, 0, 0)] <- PushMatrix(), *= glTranslate(10, 0, 0), now draws at 20 down x
[matrix0.(10, 0, 0)]
[matrix0.identity] <- drawing now draws at origin
<bottom>
-----------

PopMatrix(), PopMatrix()

<Top>
[matrix0.identity] <- drawing now draws at origin
<bottom>
-----------

<Top>
[matrix0.(0, 5, 0)] <- PushMatrix(), *= glTranslate(0, 5, 0), now draws at 5 up y
[matrix0.identity]
<bottom>
-----------

thats the general idea anyway, think of pushmatrix as transform save point, a fork in the road you might want to come back to before applying more transforms.
Its the model view matrix because you generally put the view matrix (inverse of the cameras matrix) at the bottom (first) which brings an objects model matrix (world matrix)
into the cameras "view space". These are all old opengl calls, but the idea can still be important to understand.

Edited by NumberXaero

##### Share on other sites

Ok, so translation goes before drawing the vertices, the guys who wrote OpenGL decided that way, ok. But when does the multiplication of matrices take place? For example, first I use glTranslate( x by 10 ), then I draw 70 vertices. How does the program know that I stopped drawing and that it should multiply the vertices matrix by the translation matrix???

And one more thing: You said that the top matrix onto the stack doesn't get accessed directly, but is copied. So when I use pushMatrix(), my main matrix gets copied onto the top of the matrix stack, BUT THE VALUES of my MAIN matrix remain the same, they are not resetted just copied( but the guy in the book says that it gets reset??? ) and when I use popMatrix(), the top matrix on the stack replaces the values of my current/main matrix.

Edited by Heelp

##### Share on other sites

I checked it. glPushMatrix() doesn't do anything to the current matrix, except just copying it onto the stack, so no reset stuff happens, you were right. If you want to reset, you have to do it by yourself with glLoadIdentity. So pushmatrix just saves some numbers onto the stack for you, so you don't have to do it manually, and popmatrix just gets some numbers for you, nothing complicated.... and the guy on the book was sooo wrong...., cuz there is no change/reset/whatever in coordinate systems, and the reason glTranslate() is cumulative is not because you reset the matrix to local coordinates, glTranslate is cumulative because it operates on only 1 matrix (the current one), it just multiplies "w" by the value you specify and adds it to x,y or z, again you need to specify where to add it to. And when you call glTranslate for the 2nd, 3rd or 4th time, it operates on the matrix that was already changed by the previous translations. That's what 'cumulative' means.( right??)
Hey, man, thanks a lot for the answer, really, I had it all messed up in my head. Big thanks for your help. I drawed all my cubes your way and they are all in the right position now.

Edited by Heelp

##### Share on other sites

And 1 last very short question.

The Question:

Is there a local coordinate space in OpenGL???
I think the answer is No, because all the vertices' positions are stored RELATIVE to the WORLD matrix, doesn't matter if you push or pop or clear the matrix, if the vertex says [ 5, 0, 0 ], it means that it is x=5 from the origin, and nothing else matters, right???

Edited by Heelp

##### Share on other sites

To make things simpler, everything is always in world coordinates. Anytime a matrix is present it takes the points in your 3d model as vectors and stretches those vectors to point somewhere else and gives you new vectors. Any matrix operations afterwords are applied to those new vectors.

If you have played Zelda, or any game similar. You might have a light or creature that circles around a main character. It is circling relative to the player. Keyword relative, so you need a vector relative to the player to happen first. So translate some distance from the origin of the world (treating 0,0,0 as the characters position even though he is thousands of units from the origin).  Apply your rotation treating the origin as the player, and then finally translate to the players position.

If you don't think locally you might translate to the player first, and then apply rotation, but every rotation takes vectors from 0,0,0 and rotates them. So if you translate thousands of units away first, you are then rotating vectors around the origin that are thousands of units long (centered around the origin).

If that clears anything up. Local matrix is simply more of a logical thing.

##### Share on other sites

That's exactly what I needed to hear. Thanks for the answer. :cool:

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 9
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013663
×