Advertisement

Vector and matrix math. Help me fast please!

Started by March 07, 2001 08:36 AM
3 comments, last by Mr Cucumber 23 years, 11 months ago
I desperately need to know how to multiply a vector with a matrix. The matrix is built like this: xx yx zx 0 xy yy zy 0 xz yz zz 0 x y z 1 So the first column is the direction of the x axis, the second column the direction of the y axis and the third column the direction you are looking in. The simple x,y and z are the values I change when I translate with the matrix. I also need to know how to inverse the same matrix. I only use it for translating and rotating objects and the definition of the inverse of the matrix would also be good. Please help me with this as fast as you can. Edited by - Mr Cucumber on March 7, 2001 9:44:24 AM
First you need to pick one standard for your matrices...i like to do row major(first index chooses row, second index chooses column) but you can pick whatever.
So you end up with a matrix like this(using row major):

Column|__0__|__1__|__2__|__3__
Row___|_____|_____|_____|_____
0 | 00 | 01 | 02 | 03
1 | 10 | 11 | 12 | 13
2 | 20 | 21 | 22 | 23
3 | 30 | 31 | 32 | 33

Also consider your vector as a column vector:
basically, like this

Column|__0__|
Row___|_____|
0 | 0
1 | 1
2 | 2
3 | 3

So, to multiply a matrix into a vector, you end up with a vector because 4x4 * 4x1 = 4x1
essentially you create a new vector, with each value equal to the dot product of the old vector with the corresponding row of the matrix:

so

VecNew[0]=Matrix[0][0]*Vec[0] + Matrix[0][1]*Vec[1] + Matrix[0][2]* Vec[2]+Matrix[0][3]*Vec[3];

repeat for the rest of the components.

However, you''re using a column major approach(i personally like row major a lot better, and it''s pretty much the agreed upon academic standard.) So in that case, you want to multiply somewhat differently
VecNew[0]=Matrix[0][0]*Vec[0] + Matrix[1][0]*Vec[1] + Matrix[2][0]*Vec[2] + Matrix[3][0]*Vec[3];

repeat this for the other components of the vector.
Essentially you go down the columns of the matrix, rather than across the rows.

My question now is why do this in software when GL or D3D will do it in the driver, or in hardware, and probably faster than you can?


Advertisement
Thanks for your reply.
The reason I do it in software is that I am making a software engine as a school project.
There is one thing I don''t understand and that is what the fourth component of the vector should be? I guees it is either 0 or 1 but I dont know.

ahh, i see.
I too am working on a software renderer for a school project. The fourth component of the vector is the w value, which is what makes it a homogenous coordinate. Basically it''s used to store the value of z*tan(theta) which is used in the perspective divide. So after your projection matrix, divide all values by w to normalize.

If the component is 0, this means that you have a direction vector, rather than a point in some 3d space. When you specify points in space, use 1.
Thanks.
It seems to work with 1.

This topic is closed to new replies.

Advertisement