Cross Product woes

Started by
15 comments, last by someusername 18 years, 1 month ago
I have the impression that openGL transforms vectors as columns, hence performs multiplication as:
[ m11  m12  m13  m14 ] (x)   ( m11*x+ m12*y + m13*z + m14*w )[ m21  m22  m23  m24 ] (y)   ( m21*x+ m22*y + m23*z + m24*w )[ m31  m32  m33  m34 ]*(z) = (             ...              )[ m41  m42  m43  m44 ] (w)   (             ...              )

Therefore, the model local X axis, (1,0,0,0), when transformed, has coordinates:
( m11, m21, m31, m41 ). Thus the local X axis becomes the first column...

If my initial assumption is wrong (openGL is not my specialty!), then I am probably wrong too...

edit:
A quick way to remember this, is that, if you use row vectors, these axes are also represented by row vectors in the matrices, and the opposite for column vectors
Advertisement
You are right about OpenGL. However, you mentioned extracting the axes from the 'view' matrix, in which case the basis vectors would be transposed and would therefore be in the rows. Maybe we have different ideas of what 'view matrix' means though.
I didn't know that the transpose is used for the view matrix. I can't be sure about it, but I think that's how I did it a couple of times when I had to make one from scratch... Let me try this in DX and I'll be back...
Well, I didn't even have to try that! You obviously want to apply the inverse of the orientation in the view matrix to simulate it... I mean you even translate things by the negative of the camera position.

In openGL, the camera axes will be the row vectors. You're right.
Hi guys,

The Anonymous Poster was right, tried it and I've nearly got it working. Thanks for that Anonymous Poster!

As for OpenGL matrices; I am using them to represent my transformations. But I do have a bit of difficulty understanding them. I use a 4x4 matrix:

0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10,11,
12,13,14,15

OpenGL uses column vectors, is that correct?

so:

0,4,8,12 is the first vector
1,5,9,13 is the second vector
.. etc

is that correct?

Also, what is the 4rth element of these vectors used for? And what should it be initialised to?

Thanks!
Reject the basic asumption of civialisation especially the importance of material possessions
Quote:Original post by Cacks
Hi guys,

The Anonymous Poster was right, tried it and I've nearly got it working. Thanks for that Anonymous Poster!

As for OpenGL matrices; I am using them to represent my transformations. But I do have a bit of difficulty understanding them. I use a 4x4 matrix:

0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10,11,
12,13,14,15

OpenGL uses column vectors, is that correct?

so:

0,4,8,12 is the first vector
1,5,9,13 is the second vector
.. etc

is that correct?

Also, what is the 4rth element of these vectors used for? And what should it be initialised to?

Thanks!
OpenGL matrices are column major, and so are indexed like this:
0 4 8  121 5 9  132 6 10 143 7 11 15
They also use column vectors, so a simple model matrix looks like this:
xx yx zx txxy yy zy tyxz yz zz tz0  0  0  1
Where x, y and z are the basis vector of the coordinate system, and 't' is the translation. The inverse of this matrix is:
xx xy xz -t.xyx yy yz -t.yzx zy zz -t.z0  0  0  1
Which is what you would use for a camera or view matrix (this is what gluLookAt() generates).

Because of the way the matrix is arranged, you can (to a certain extent) use row-major matrices with row vectors and get similar results. However, IMO for conceptual clarity it's good to match the conventions of whatever API you're using.

I don't know if that answers all your questions, but please ask if you need further clarification.
Quote:Original post by Cacks
Also, what is the 4rth element of these vectors used for? And what should it be initialised to?
Thanks!


The 4th element has been introduced to overcome certain difficulties that arise in 3d, like the fact that the origin is invariant under matrix multiplication.

Without getting in the underlying math, if you want to represent a point initialize its 4-th coordinate "w" to 1. If you want to represent a direction of some magnitude (vector), set it to 0. As you can see, 4d vectors with w==0 are not affected by the translation part of the matrices, sums of vectors are vectors, difference of points is vector etc... (just like they should)

Furthermore, all points *must* have w==1. If you transform a 4d vector by such a matrix, its w will generally change. In order for the result to represent a point in the original space, all its components must be divided by its w, thus it must be normalized to its w. Else, it's not a point, it's a set of projective coordinates.

And a clarification on the matrices mentioned by jyk...

Their upper-left 3x3 part must be strictly unit column vectors, mutually perpendicular. The determinant of that sub-matrix is 1, which means geometrically that it preserves the volume of the transformed set.
It is quite often, that these unit vectors are multiplied by a scaling value for that axis (to apply scaling to the model), so make sure to normalize them before using them in any way.
The rest part, the zeros and translation do not affect this property because they do not affect the determinant.
The inverse matrix has the aforementioned form, only if there is no scaling encoded in the matrix.

This topic is closed to new replies.

Advertisement