View Full Version : Extracting a Vector4 from GL_MODELVIEW_MATRIX

I've figured out how to extract the transformation info from the top matrix in the modelview matrix stack. However, I'm unsure of how to put this into an x, y, z vector so I can take a look at the coordinates (so I can determine a direction, etc. or use it for other info.)

Looking at the matrix after a few translations isn't too difficult, it's listed as such;

1 0 0 0

0 1 0 0

0 0 1 0

x y z 1

After performing rotations (and some translations), all the numbers in the 1 position change to decimal values and I'm unsure of how to calculate the current x, y, z position from there.

I'm not all that great at math, but my gut tells me that I'll have to take a Vector4 with default values all set to 1, and multiply it by the 4x4 matrix. That might (guessing here) give me my vector with x, y, z (plus the w) values. Am I on the right track here?

Thanks in advance.

CrazyButcher

06-02-2004, 10:55 PM

transpose that matrix as OGL is "column major"

ie indices of the 16 floats would be:

0 4 8 12

1 5 9 13

2 6 10 14

3 7 11 15

what exactly do you want to get out of the matrix ?

normally to get the "position" you would throw in a (0 0 0 1)T vector

and for direction well I would use each main axis

e.g

(1 0 0 1)T , (0 1 0 1)T, (0 0 1 1)T

and not translate it (ie set index 12,13,14 to 0) then multiply with matrix.

that will get you the transformed axis.

Ahh sheesh, I'm silly. The column major threw me off, and that's why it didn't look right. After looking again at the Matrix values after some transformation, I noticed that the x, y, z values were in the 4th column, as they should be. I did a rotation and then a translation, and the matrix came out as it should (unless I'm being goofy again);

glRotatef(90, 1.0, 0, 0);

glTranslatef(5, 2, 0);

The printed matrix displays;

1 0 0 5

0 0 -1 0

0 1 0 2

0 0 0 1

Rotating 90 degrees on the x-axis should rotate the 2 on the y-axis counter-clockwise, which would be 2 on the z-axis if I'm not mistaken. That's what the matrix shows in the end position; <5, 0, 2, 1>. So I guess to get the current position I could just extract the matrix indices 12, 13 and 14.

If I'm wrong please let me know. Thanks for your help. :)

CrazyButcher

06-03-2004, 04:23 AM

yes a (0,0,0,1)T vector thrown into that matrix would do exactly that, giving you those last indices.

Ah okay, that's what I thought. :) I'll have to crack open my Algebra book and refresh my memory on Matrix multiplication.

Thanks again for the help!

Hi again. :)

Could you further explain what you mean by throwing in a (0, 0, 0, 1)T vector into the matrix? I thought you ment multiply, but that can't be right, because multiplying (0, 0, 0, 1) by the matrix listed above results in (0, 0, 0, 1).

CrazyButcher

06-05-2004, 08:33 AM

I did mean multiplying

1 0 0 5 0 0*1 + 0*0 + 0*0 + 1*5 5

0 0 -1 0 0 0*0 + 0*0 + 0*-1 + 1*0 0

0 1 0 2 * 0 = 0*0 + 0*1 + 0*0 + 1*2 = 2

0 0 0 1 1 0*0 + 0*0 + 0*0 + 1*1 1basically you put the vector horizontally in the matrix and multiply each pair and sum them up

and you do this for every row of the matrix

Oh, lol. I show my stupidity once again. :) I did VectorT * Matrix.. why I didn't try it the other way before asking, I don't know. Told you I sucked at math. :p

Thanks... again.

harsman

06-08-2004, 02:28 AM

The T signifies transposition that is, Crazybutcher wrote it as a row vector in his post but wanted to signify that it was a column vector. If v is a column vector v*M is not defined, which is why you have to do M*v. Vertices and normals are considered column vectors in OpenGL which is why transformations are left multiplied.

With that said, there is no need to do the multiplication, you can just extract elements 12, 13 and 14 from the pointer you get from glGetFloatv(GL_MODELVIEW_MATRIX, p).

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.