- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 828字
- 2021-06-30 14:45:57
Matrix multiplication
Matrix multiplication combines the transformation of two matrices into one matrix. Two matrices can only be multiplied together if their inner dimensions are the same. The following are some examples:
- A 4 x 4 and a 4 x 4 matrix could be multiplied together, since both inner dimensions are 4.
- A 4 x 4 and a 4 x 1 matrix could be multiplied together, since both inner dimensions are 4.
- A 4 x 4 and a 1 x 4 matrix could not be multiplied together, since the inner dimensions, 4 and 1, don't match.
The resulting matrix from a matrix multiplication will have the outer dimensions of the matrices being multiplied together. The following is an example:
- A 4 x 4 and a 4 x 4 matrix would yield a 4 x 4 matrix.
- A 4 x 4 and a 4 x 1 matrix would yield a 4 x 1 matrix.
- A 1 x 4 and a 4 x 2 matrix would yield a 1 x 2 matrix.
Assume there are two matrices, A and B. Matrix A translates 10 units on the X axis. Matrix B rotates by 30 degrees around the Y axis. If the matrices where to be multiplied as A * B, the resulting matrix would rotate by 30 degrees around the Y axis then translate 10 units on the X axis.
Matrix multiplication is not cumulative. Consider the last example but multiply B * A, instead. When multiplying B * A, the resulting matrix will translate 10 units on the X axis and then rotate 30 degrees around the Y axis. Multiplication order matters; A * B is not the same as B * A.
This brings up a new question—what order should matrices be multiplied together in? If M = A * B * C, in what order do those matrices concatenate? A, B, and then C or C, B, and then A? If it's A, B, and then C, the matrix multiplication is defined as left to right. But if it's C, B, and then A, the matrix multiplication is right to left.
To maintain consistency with OpenGL, in this chapter you will be implementing right-to-left multiplication. But how are two matrices multiplied together? Each element of a matrix has a row and a column. The resulting value for any element is the dot product of that row from the left matrix and that column forms the right matrix.
For example, suppose you want to find the value of the element in row 2 column 3 when multiplying two matrices. This means taking the dot product of row 2 from the left-hand side matrix and column 3 from the right-hand side matrix. Figure 3.4 demonstrates this:

Figure 3.4: Multiplying matrices
You might have noticed, in the previous figure, that even though the matrices are column-major, the subscript of the elements appears as row first, then column. The subscript references the physical topology of the matrix; it has nothing to do with what is stored in the matrix or how the matrix is laid out. Subscript indices remain the same, no matter what major the matrix is. Perform the following steps to implement matrix multiplication:
- To keep the code for multiplying matrices short, you will need to create a helper macro. This macro will assume that there are two matrices, a and b. The macro will take two numbers, the row of a and the column of b, to dot together and the result will bet the dot product of the two. Define the M4D macro in mat4.cpp:
#define M4D(aRow, bCol) \
a.v[0 * 4 + aRow] * b.v[bCol * 4 + 0] + \
a.v[1 * 4 + aRow] * b.v[bCol * 4 + 1] + \
a.v[2 * 4 + aRow] * b.v[bCol * 4 + 2] + \
a.v[3 * 4 + aRow] * b.v[bCol * 4 + 3]
- With the M4D macro in place, implement the matrix multiplication function in mat4.cpp. Don't forget to add the function declaration to mat4.h. Remember that the (2, 1) element, for example, should take the dot product of row 2 from matrix a and column 1 of matrix b:
mat4 operator*(const mat4 &a, const mat4 &b) {
return mat4(
M4D(0,0), M4D(1,0), M4D(2,0), M4D(3,0),//Col 0
M4D(0,1), M4D(1,1), M4D(2,1), M4D(3,1),//Col 1
M4D(0,2), M4D(1,2), M4D(2,2), M4D(3,2),//Col 2
M4D(0,3), M4D(1,3), M4D(2,3), M4D(3,3) //Col 3
);
}
The most important property of matrix multiplication is that it combines the transformation encoded in both matrices into a single matrix. This is useful because you can pre-multiply certain matrices to perform fewer multiplications per frame. Next, you will learn about how matrices can apply their transformation data to vectors and points.
- 深入理解Android(卷I)
- Cocos2D-X權威指南(第2版)
- Mastering Zabbix(Second Edition)
- Modern JavaScript Applications
- Learning Salesforce Einstein
- 零基礎學單片機C語言程序設計
- JavaCAPS基礎、應用與案例
- Developing SSRS Reports for Dynamics AX
- Spring Security Essentials
- C語言程序設計習題與實驗指導
- 從零開始學Python網絡爬蟲
- Illustrator CS6設計與應用任務教程
- 超好玩的Scratch 3.5少兒編程
- C# 7 and .NET Core 2.0 Blueprints
- LabVIEW數據采集(第2版)