週一到週五,天天一篇,北京時間早上7點準時更新~express
OpenGL represents a 4 × 4 matrix not as a two-dimensional array of floating values, but rather as a single array of 16 floating-point values(OpenGL中用16個浮點數來表示矩陣,而不是用二維浮點數數組). By default, OpenGL uses a columnmajor or column-primary layout for matrices(默認狀況下,OpenGL使用的是列序的矩陣). That is, for a 4 × 4 matrix, the first four elements represent the first column of the matrix, the next four elements represent the second column, and so on(意思就是說,對於4x4個元素的矩陣來講,前四個浮點數表示的是矩陣的第一列,接着的第二個四個浮點數是矩陣第二列,以此類推). This approach is different from many math libraries, which do take the two-dimensional array approach(不少數學庫跟這個不同,他們使用二維數組的方式去實現). For example, OpenGL prefers the first of these two examples:(好比OpenGL選擇使用下面例子中的前者:)數組
GLfloat matrix[16]; // Nice OpenGL-friendly matrix GLfloat matrix[4][4]; // Not as convenient for OpenGL programmers
OpenGL can use the second variation, but the first is a more efficient representation(OpenGL可使用第二種,可是第一種表達方式更高效). The reason for this will become clear in a moment(爲什麼如此,一會就清楚了). These 16 elements represent the 4 × 4 matrix, as shown below(16個元素達標的4x4矩陣以下所示). When the array elements traverse down the matrix columns one by one, we call this column-major matrix ordering(當咱們遍歷數組元素的時候,咱們管這叫列序矩陣). In memory, the 4 × 4 approach of the two-dimensional array (the second option in the preceding code) is laid out in a row-major order(在內存裏,二維數組是以行序存儲的). In math terms, the two orientations are the transpose of each other(數學裏,這兩種方式互爲轉置)
Representing the above matrix in colum-major order in memory produces an array as follows:(列序矩陣以下所示)app
static const float A[] = { A00, A01, A02, A03, A10, A11, A12, A13, A20, A21, A22, A23, A30, A31, A32, A33 };
In contrast, representing it in row-major order would require a layout such as this:(而行序矩陣的表達則以下:)less
static const float A[] = { A00, A10, A20, A30, A01, A11, A21, A31, A20, A21, A22, A23, A30, A31, A32, A33, };
The real magic lies in the fact that these 16 values can represent a particular position in space and an orientation of the three axes with respect to the viewer(這裏神奇的地方就是,矩陣的這16個數字能夠表達一個空間中的相對於觀察者來講的特定位置以及在這個位置的朝向). Interpreting these numbers is not hard at all(解釋這些數字並不難). The four columns each represent a four-element vector(四列元素分別是四個向量). To keep things simple for this book, we focus our attention on just the first three elements of the vectors in the first three columns(爲了使的內容更簡單,咱們主要集中精力在前三個向量的前三個元素上). The fourth column vector contains the x, y, and z values of the transformed coordinate system’s origin(第四列元素的xyz表示的是位移).ide
The first three elements of the first three columns are just directional vectors that represent the orientation (vectors here are used to represent a direction) of the x, y, and z axes in space(前三列的前三個元素表達的是旋轉). For most purposes, these three vectors are always at 90° angles from each other and are usually each of unit length (unless you are also applying a scale or shear)(大多數狀況下,這三個向量都是兩兩垂直的,而且是一個單位向量). The mathematical term for this (in case you want to impress your friends) is orthonormal when the vectors are unit length, and orthogonal when they are not(數學意義上,若是這仨不是單位向量則是正交,若是是則是標準正交). Figure 4.5 shows the 4 × 4 transformation matrix with its components highlighted(圖4.5標註出了4x4矩陣的這些元素). Notice that the last row of the matrix is all 0s with the exception of the very last element, which is 1.(注意到全部列向量的最後一個元素都是0,最後一個列向量的最後一個元素是1)
The upper-left 3 × 3 sub-matrix of the matrix shown in Figure 4.5 represents a rotation or orientation(左上的3x3矩陣表達了旋轉). The last column of the matrix represents a translation or position.(最後一列向量表達的是位置)The most amazing thing is that if you have a 4 × 4 matrix that contains the position and orientation of a coordinate system, and you multiply a vertex expressed in the identity coordinate system (written as a column matrix or vector) by this matrix, the result is a new vertex that has been transformed to the new coordinate system(最神奇的事情就是,你使用4x4矩陣去乘以一個點的座標以後,這個點會被轉換到另外一個座標系裏去). As a result, any position in space and any desired orientation can be uniquely defined by a 4 × 4 matrix, and if you multiply all of an object’s vertices by this matrix, you transform the entire object to the given location and orientation in space!(因此,任何一個空間中的位置以及朝向,均可以用惟一的用一個4x4矩陣表達出來,若是用這個矩陣乘以一個物體的全部點, 那麼這個物體的全部點都會被轉換到那個位置上的姿式上去)ui
In addition, if you transform an object’s vertices from one space to another using one matrix, you can then transform those vertices by yet another matrix, transforming them again into another coordinate space(補充一下,你能夠把一個物體從一個座標系轉換到另外一個座標系,同時還能夠繼續把這些點再轉換到下一個座標系). Given matrices A and B and vector v, we know that(給出A、B倆矩陣和向量v,咱們知道:)
is equivalent to(等價於)
This relationship arises because matrix multiplication is associative(由於矩陣乘法能夠累加,因此他們有這樣的關係). Here in lies the magic: It is possible to stack a whole bunch of transforms together by multiplying the matrices that represent those transforms and using the resulting matrix as a single term in the final product(也就是說:你能夠把全部變換矩陣事先所有乘上,而後拿到結果矩陣再去對你的物體進行相關操做)this
The final appearance of your scene or object can depend greatly on the order in which the modeling transformations are applied(你渲染物體的最終結果取決於這些變換矩陣的相乘的順序). This is particularly true of translation and rotation. We can see this as a consequence of the associativity and commutativity rules for matrix multiplication(這是因爲矩陣的性質決定滴). We can group together sequences of transformations in any way we like because matrix multiplication is associative, but the order in which the matrices appear in the multiplication matters because matrix multiplication is not commutative(咱們能夠用任意的方式去讓一堆矩陣相乘,但不一樣順序會獲得不一樣的結果)spa
Figure 4.6(a) illustrates a progression of a square rotated first about the z axis and then translated down the newly transformed x axis, and Figure 4.6(b) illustrates first translating the same square along the x axis and then rotating it around around the z axis(圖4.6a展現了先旋轉後移動的效果,圖4.6b展現了先移動後旋轉的效果). The difference in the final dispositions of the square occurs because each transformation is performed with respect to the last transformation performed(這種差別產生的緣由是由於,每一次變換是相對於上一次變換的結果來進行的). In Figure 4.6(a), the square is rotated with respect to the origin first(圖4.6a中,旋轉是相對於原點來作的). In Figure 4.6(b), after the square is translated, the rotation is performed around the newly translated origin(圖4.6b中,旋轉是相對於移動後的位置來作的)翻譯
本日的翻譯就到這裏,明天見,拜拜~~code
第一時間獲取最新橋段,請關注東漢書院以及圖形之心公衆號
東漢書院,等你來玩哦