官术网_书友最值得收藏!

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

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:

  1. 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]

  2. 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.

主站蜘蛛池模板: 岑溪市| 马公市| 莆田市| 普洱| 华安县| 昌平区| 云安县| 桃江县| 龙陵县| 灵川县| 成武县| 麦盖提县| 海淀区| 尉氏县| 阿克苏市| 陵川县| 从化市| 定兴县| 乐清市| 万山特区| 离岛区| 得荣县| 曲周县| 老河口市| 永川市| 安西县| 南康市| 克什克腾旗| 舟曲县| 从化市| 富阳市| 东乡| 淮阳县| 明水县| 孙吴县| 乌拉特中旗| 隆林| 太原市| 宜都市| 新郑市| 娱乐|