标题: memo [打印本页] 作者: folklore 时间: 2013-05-07 15:43 标题: memo Basic 3D Math: Matrices
Matrices are rectangular mathematical objects which have a number of rows and columns – think of it as a two-dimensional array. Take a look at the following:Above is a 3×4 Matrix (because it has 3 rows and 4 columns) – speak: 3 by 4 Matrix. Each element of the Matrix can be indexed by using the following notation: where i is the row and j the column: because the element at the 3rd rows and 2nd column has the value 10.Matrices are used in CG to represent a various number of different things like transformations and orientations.Part 1: Multiplying Matrices (Matrix Product)Matrix multiplication is not commutative, which means that the order of operation is relevant (as opposed to the multiplication of real numbers). In order to be able to multiply two matrices they must satisfy the rule that column of the first must match the rows of the second. Take a look at the following graphic which works as a simple reminder for the rules of matrix multiplication:But what’s the purpose of multiplying matrices? In 3D Programming you do Transforming a vertex with such a operation. Stop – how can i multiply a vector (which stores a vertex position) with a matrix? Vectors can be thought as Vectors with 1 Row and 4 Columns – and because multiplying a 1×4 Matrix with a 4×4 Matrix is a valid operation this works.In the first step, lets examine how to actually do the multiplication – formally this looks like:i = row index
j = column index
p = number of columns of the first matrix (or number of rows of the second one)Because a example often says more than thousand words, take a look at the following:We have two matrices: and . Multiplication of those two is possible because the number of columns of A matches the number of rows of B – the result is a 2×2 Matrix.Let’s perform the Calculation step by step.Step 1: Write down the calculation of every index for the resulting matrix Step 2: Do the calculations
results in:Part 2: Creating Matrices for TransformationsAs mentioned above at the beginning of this document, matrices can be used to transform vectors. Its obvious that you need corresponding matrices which represent the transformation, so they are presented below – please note that they are for OpenGL only (The “why” is discussed at the end)Identity Matrix:The Identity matrix can be seen as the “reset” state matrix. If you multiply a vector with the Identity matrix you get the original vector. You initialise new matrices to the identity.Translation Matrix:X, Y and Z represent the amount you want to translate the vector on the axis.Scaling Matrix:X,Y and Z represent the amount of scaling on the corresponding axis.Rotation Matrix:There are four rotation matrices – one about each Axis (X,Y and Z) and one for rotating about an arbitrary axe. is the roation in radians. The matrix used to rotate about an arbitrary axis is a little bit more complicated.Assumptations: Then the matrix looks like:The Vector (x,y,z) which represents the rotation axis must be normalizedMost of the time you don’t need to create those matrices by ourself because either the math library you are using provides them or you write your own.Part 3: Order of operation and combining transformationsThe order of multiplying multiple transformation matrices is cruical. In OpenGL you get the final transformation by reading your statement from left to right. Example:R is a rotation, T a translation matrix. The Vector V is first Translated and then rotated. Take a look at the pictures below to see the difference visually:In this Picture, the vertices of the Object are first multiplied by a rotation matrix (about Z) – which rotates the local object coordinates. After that a translation matrix (move along X) is applied, so the object moves along the rotated X axis. The order of the multiplications are:As mentioned before, we read from right to left (Take Vector V, then Rotate, then Translate)This time we first multiply with a translation matrix (translate along X) and then apply a rotation matrix:Which means: Take Vector V, then translate, then rotate.Please note that the order only applies to OpenGL. In DirectX it’s from left to right – also the matrices are a little bit different in how they look like.Part 4: Going aheadMost OpenGL based 3D Libraries and Toolkits provide easy ways of creating the matrices mentioned above (i REALLY like Richard Wright’s Math3D Library which he uses in his Book OpenGL Superbible).If you want to dig deeper into the mathematics behind 3D Graphics, i highly recommend you the following two books:
for(size_t iAssign =0;iAssign <params.size();iAssign++){ // the size is not include the last terminal tocken
params.at(iAssign) =pParams[iAssign];
//wprintf(L"%c",params.at(iAssign));
}
//wprintf(L"\n%d",dntwcslen(pParams));
if(0 ==SendUIMessage(UUIDBendCAM,NULL,123,L"Title&b",456,L"Context: <happy new year.>",params.data(),params.data())){
std::wstring msg=MakeMessage(NULL,123,L"Title&b",456,L"Context: <happy new year.>",params.data()/*,params.data()*/);
wprintf(L"%s",msg.c_str());
}
return 0;
}
#endif //!defined(DLL)
复制代码
new version 作者: wait_rabbit 时间: 2013-05-09 14:35
马二进三,这还得去翻线性代数课本。作者: wonghoifung 时间: 2013-05-09 15:13
:wink:作者: folklore 时间: 2013-05-09 21:20 回复 10# linux_c_py_php
以前一直以为程序和数学是不分家的。直到有一天,有个电信公司叫我写无聊的电信业务系统。作者: folklore 时间: 2013-06-28 16:17
The following issue explain you how to apply the metries to an 3d vertex.
In order to apply a general 4x4 transformation matrix to a vertex represented as a 3 dimensional vector, you need to:
1.Expand the vector into the 4th dimension by adding a 1 as the W component:
(x, y, z) => (x, y, z, 1)
2.Multiply the transformation matrix by the 4 dimensional vector above. The result will be another 4 dimensional vector:
( 4x4 matrix ) * (x, y, z, 1) => (tx, ty, tz, w)
The general formula for multiplying a 4x4 matrix by a 4x1 vector is (if I didn't mess up):
[ 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 ] = [ m31 * x + m32 * y + m33 * z + m34 * w ]
[ m41 m42 m43 m44 ][ w ] [ m41 * x + m42 * y + m43 * z + m44 * w ]
Or if you consider the usual configuration of a transformation matrix:
[ m11 m12 m13 px ][ x ] [ m11 * x + m12 * y + m13 * z + px ]
[ m21 m22 m23 py ][ y ] [ m21 * x + m22 * y + m23 * z + py ]
[ m31 m32 m33 pz ][ z ] = [ m31 * x + m32 * y + m33 * z + pz ]
[ 0 0 0 1 ][ 1 ] [ 1 ]
3.Convert it back into the 3rd dimension by homogenizing the vector, i.e. dividing everything by the fourth component w:
(tx, ty, tz, w) => (tx/w, tx/w, tz/w)
The thing is that if your transformation matrix only does a simple translation / rotation / scale, the value of w will be 1 and you can just drop the fourth component since that will be the same as dividing by 1.
But it's good to remember that it does not work for every case, e.g. with projection matrices you must remember to do this third step.
MSXML2::IXMLDOMNamedNodeMapPtr pXMLAttributeMap = pXMLFirstChild->Getattributes(); // A map of the a attributes (vesrsion, encoding) values (1.0, utf-8) pair