Number of columns in first matrix must equal the number of rows in the second matrix. Size of final matrix determined by the number of rows in first matrix and number of columns in the second matrix.
4x4 Matrix * 4x1 Matrix = 4x1 Matrix
First Row of 4x4 Matrix DOT4 First Column of 4x1 Matrix = 1st entry in 4x1 matrix
Second Row of 4x4 Matrix DOT4 First Column of 4x1 Matrix = 2nd entry in 4x1 matrix
Third Row of 4x4 Matrix DOT4 First Column of 4x1 Matrix = 3rd entry in 4x1 matrix
Fourth Row of 4x4 Matrix DOT4 First Column of 4x1 Matrix = 4th entry in 4x1 matrix
The fourth component is part of what is called the homogenous coordinate. Normally, in your vector supplied to OpenGL, this is assumed to be 1.0.
Instead of the columns being your axis, think of the entire rows themselves as your axis. When doing the 4 component dot products, if the last part of of the vector (which by defination is a 4x1 or 1x4 depending on which way you look at) is a 1 (your homogenous coordinate), then what is in the last column of the 4x4 matrix is added (translated) after the other manipulations (rotation and scaling stored in the upper-left 3x3 of the 4x4 matrix). So 1 times the values in the last column of the 4x4 matrix is the last column of the 4x4 matrix, so that is what is simply added in the final part of the dot product.
What you are doing is multiplying your vector by the transpose of the upper-left 3x3 of the 4x4 matrix and adding the translation (you are assuming that the last component of each vector is always 1). Don’t transpose that 3x3 (go sideways instead of up and down) and you’d be doing exactly what OpenGL does (minus the homogenous component).
Oh, and you are using the transpose of your modelview matrix, matrices are normally handled in row-major, OpenGL handles things in column major. Slight technical difficulty.
[This message has been edited by Dan82181 (edited 08-23-2002).]
Thanks for your reply, Dan.
I already understand that, but it’s nice to have it clarified.
No, really what I’m saying is, is this correct (considering the order in which a 1 dimensional array is mapped over the matrix):-
I seem to remember seeing an extension for automatically transposing the matrices in OpenGL, is there any reason why I shouldn’t use this? It would make my own transform code more efficient (and more readable) if I could work this way, but would it impose a heavy performance price on OpenGL?
[This message has been edited by inept (edited 08-23-2002).]
Yes, there was an extension that automatically transposed matricies for you. I don’t remember which version it was, but I think it became part of the GL1.3 spec, but it should still be advertised as an extension.
Concerning whether or not to use it, depends on how good you are at grouping/watching brackets and how crafty you can get with matrix math. Case and point…
EXT_vertex_shader does not have the equivelent of a “vertex state program” like NV_vertex_program, so when I need 4 or 5 matrices multiplied together, they’ve gotta be done on the CPU. And full matrices have to be supplied in column major order as well to EXT_vertex_shader. So what I do is if I need to retrieve a matrix from OpenGL, I leave it in it’s transposed state. What I do is write down the matrix formula I need on paper, with grouping brackets and parenthesis and what not. I see what I have to start with (possibly some transposed matrices), and I see what I have to end up with (a transposed matrix). And I just find the best way to come up with the same result requiring the least number of transposes and inverses and stuff like that.
If you really need to have the matrix in row order (say for writing it out to a log file or something like that), I wouldn’t see how there would really be any performance decrease in simply transposing incoming and outgoing matricies to/from OpenGL using the extension. I could be wrong, but I always just went with what the big guys said and never argued or complained about it.
The problem is not in how the data is actually stored in memory; the problem is in how you write it out in text Your matrix is correct, because it puts position in the 12,13,14 elements. If you want to write the matrix the way you do (column major) then that’s fine. Make sure to multiply your column vectors from the right.
If you want, you can write the same matrix in row-major order, and multiply your row vectors from the left:
Thanks for your reply, jwatte.
But no it isn’t the same, I have to code it differently.
In your example, the translation would have to be in , and  for it to work like a vertex program. As Dan said, the dot product would work like this:
ex = (oxm)+(oym)+(ozm)+(owm)
ey = (oxm)+(oym)+(ozm)+(owm)
ez = (oxm)+(oym)+(ozm)+(owm)
So if we assume ow will always be 1, then the translation in m,m and m will just be added on to the vector, giving us the origin of the new coordinate system.
The question still remains, why can’t I define a 4x4 matrix as 4 rows of vector4 instances? With the translation of each axis in the w component of each vector? In the same way that a vertex program appears to do?