Given two vectors, V1 and V2, the dot product
V1 . V2 = (V1x*V2x + V1y*V2y + V1z*V2z)
Note that the dot product returns ONE value. It''s a scalar, not a vector. So
(1, 0, 1) . (2, 0, -1) = 2*1 + 0*0 + 1*-1 = 2-1 = 1.
I''m going to start by looking at 3x3 matrices, for simplicity.
Take a matrix
|A B C||D E F||G H I|
This can be thought of as three vectors:
(A, B, C)(D, E, F)and(G, H, I)
What a 3x3 matrix multiplication is is simply taking three dot products.
So multiplying the matrix above (M) by a vector V you would be doing (this is written as a 3x3 matrix multiplied by a 1x3 matrix):
|A B C| |Vx||D E F| |Vy| = (V . (A,B,C), V . (D,E,F), V . (G,H,I))|G H I| |Vz|
Note that each output component (Say the output vector is called P)
Px = V . (1st row of matrix)
Py = V . (2nd row of matrix)
Pz = V . (3rd row of matrix)
These rows can be though of as axes (like X, Y, and Z). So if you have a matrix that''s
|1 0 0||0 1 0||0 0 1|
then multiplying it and V together would return V.
This matrix is called the Identity Matrix, because vectors (and other 3x3 matrices) multiplied by it are unchanged.
A 3x3 matrix can represent many things, but it''s mostly used to represent rotations.
What can a 3x3 matrix NOT represent? Translation (moving a point in space) and perspective projection (making objects farther from the origin get smaller to simulate distance).
So we use 4x4 matrices.
But because we use 4x4 matrices, we need to have a 4-element coordinate, too, so a fourth coordinate is added (w).
S our vectors are written as (x, y, z, w).
These are homogenous vectors. Generally, you don''t have to deal with them, they''re implied. When you take a vector that''s a 3-part vector (x, y, z) and make it a 4-part vector, w is 1.
(x, y, z, 1). Don''t worry about the reason right now, it''ll come up later.
So now you have a 4x4 matrix. They''re generally arranged like this (for a rigid transform):
|R R R T||R R R T||R R R T||0 0 0 1|
In this, the "R"s are where the rotation information is (note that it''s the 3x3 portion of it), and the "T"s define the translation.
Remember, each component of the vector is dot-producted (dot-productized? I dunno) with it''s corresponding row in the matrix (1st component (x) dot 1st matrix row, etc).
Perspective matrices get a little bit more tricky. I''m just going to touch them for now. The basic idea behind perspective is (simply), making
P = [x/z, y/z, 1/z, 1]
That is, x and y (given the center of the screen is (0,0)) are divided by the distance from the screen. However, in a matrix multiplication you can''t do a divide directly, so you cheat by using the homogenous coordinate (w).
Yes, that little bastard. The basic idea is this:
if you multiply all of the (4) coordinates by any number (except 0, of course), the vector stays the same.
So (1, 2, 3, 1) = (2, 4, 6, 2) = (3, 6, 9, 3), etc. And all of those four-component vectors are the same as (1, 2, 3), the three-component vector.
Confusing? Course it is.
Basically, you can take whatever you have (using the (2, 4, 6, 2) example above), and divide everything by the w component (2) to get the three-component version.
So (x/w, y/w, z/w) is the three-component vector.
You''d get (2/2, 4/2, 6/2) = (1, 2, 3). See?
So the perspective matrix accomplishes the division by making the 4-vector:
(x'', y'', z'', w'') = (x, y, 1, z). Thus, doing (x''/w'', y''/w'', z''/w'', w''/w'') gets you (x/z, y/z, 1/z, z/z) = (x/z, y/z, 1/z, 1).
Which is exactly what you wanted.
---
Whew. That''s the worst of it. Now, why use 4x4 vectors?
Simple. You can represent almost any type of transformation you can think of in ONE 4x4 matrix. If you take two matrices and multiply them together (it''s a similar operation to matrix * vector), you get a combined transform. So Rotation * Translation would make one single matrix that''s (Rotation And Translation).
Etc. All, then, transforming a single point takes would be four 4-component dot products (x1*x2 + y1*y2 + z1*z2 + w1*w2), done four times, one for each row of the matrix.
Then you run that output through the "/w" routine (x/w, y/w, z/w) to get the actual output coordinate.
Complex? Yes.
But once you understand how matrices work, they''re an invaluable tool.
Hope you get some of that, and I hope I didn''t mess up anything too badly in there Don''t forget to ask questions about things you don''t understand, I''ll explain further (unless someone else beats me to the punch)!
Good luck,
Josh