Concatenating Transformations(變換的累加)

週一到週五,天天一篇,北京時間早上7點準時更新~app

As you have learned, coordinate transforms can be represented by matrices, and transformation of a vector from one space to another involves a simple matrix–vector multiplication operation(座標的轉換能夠用矩陣來表示,把一個向量從一個座標系轉換到另一個座標系涉及到一個矩陣乘以向量的操做). Multiplying by a sequence of matrices can apply a sequence of transformations. It is not necessary to store the intermediate vectors after each matrix– vector multiplication(若是你使用一堆矩陣對向量進行轉換的話,就牽涉到一堆的轉換,你沒必要邀存儲向量與這一堆矩陣相乘時每一個階段的向量狀態). Rather, it is possible and generally preferable to first multiply together all of the matrices making up a single set of related transformations to produce a single matrix representing the entire transformation sequence(你能夠先把全部的矩陣相乘,獲得最終矩陣,而後再拿這個最後的結果去與向量相乘). This matrix can then be used to transform vectors directly from the source to the destination coordinate spaces. Remember, order is important(你要記住的是,矩陣相乘的順序不能隨意改變). When writing code with vmath or in GLSL, you should always multiply a matrix by a vector and read the sequence of transformations in reverse order. For example, consider the following code sequence:(在GLSL中,你一般會使用矩陣乘以向量的操做,而且矩陣的相乘順序與正確的數學邏輯意義上的順序是反着來的,好比下面的)ide

vmath::mat4 translation_matrix = vmath::translate(4.0f, 10.0f, -20.0f);vmath::mat4 rotation_matrix = vmath::rotate(45.0f,
vmath::vec3(0.0f, 1.0f,
0.0f));
vmath::vec4 input_vertex = vmath::vec4(...);
vmath::vec4 transformed_vertex = translation_matrix
rotation_matrix

input_vertex;
This code first rotates a model 45° around the y axis (due to rotation_matrix) and then translates it by 4 units in the x axis, 10 units in the y axis, and negative 20 units in the z axis (due to translation_matrix)(上面的代碼先繞y軸旋轉45度,而後再沿x、y、z方向分別平移了4,10,-20). This places the model in a particular orientation and then moves it into position. Reading the sequence of transformations backward gives the order of operations (rotation, then translation). We could rewrite this code as follows:(請注意矩陣書寫時候的順序,與咱們邏輯上的順序是反的,咱們也能夠把代碼寫成下面這樣)this

vmath::mat4 translation_matrix = vmath::translate(4.0f, 10.0f, -20.0f);
vmath::mat4 rotation_matrix = vmath::rotate(45.0f,
vmath::vec3(0.0f, 1.0f,
0.0f));
vmath::mat4 composite_matrix = translation_matrix rotation_matrix;
vmath::vec4 input_vertex = vmath::vec4(...);
vmath::vec4 transformed_vertex = composite_matrix

input_vertex;
Here, composite_matrix is formed by multiplying the translation matrix by the rotation matrix, forming a composite that represents the rotation followed by the translation(這裏,咱們先獲得了合成後的矩陣,而後咱們拿這個合成後的矩陣去乘以向量). This matrix can then be used to transform any number of vertices or other vectors. If you have a lot of vertices to transform, this can greatly speed up your calculation. Each vertex now takes only one matrix–vector multiplication operation rather than two.(若是你的點比較多的時候,這種方式會提升效率,每一個向量只須要乘以最後的合成矩陣,而不用依次去乘以兩個矩陣) Care must be taken here. It’s too easy to read (or write) the sequence of transformations left-to-right as you would code. If we were to multiply our translation and rotation matrices together in that order, then in the first transform we would move the origin of the model; the rotation operation would then take place around that new origin, potentially sending our model flying off into space!(這一大坨英文的意思就是,這裏你須要注意矩陣的相乘順序,若是你順序寫錯了,那就得不到你想要的結果,好比你總想着先旋轉再移動, 因而乎代碼很容易寫成rotation_matrix*translate_matrix)spa

Quaternions(四元數)scala

A quaternion is a four-dimensional quantity that is similar in some ways to a complex number(四元數跟複數有點相似,它有一個實部和三個虛部,這個跟腎虛無關,有人就說腎虛也會引起失眠和頭疼,因此小夥子們應該保持良好的生活習慣). It has a real part and three imaginary parts (as compared to a complex number’s one imaginary part). Just as a complex number has an imaginary part i, a quaternion has three imaginary parts, i, j, and k. Mathematically, a quaternion q is represented as(如同複數有一個虛部i同樣,四元數有三個虛部i、j、k,數學上,四元數q能夠寫成下面的樣子)
Concatenating Transformations(變換的累加)
The imaginary parts of the quaternion have properties similar to the imaginary part of a complex number. In particular:(四元數的虛部跟複數的虛部有着類似的性質,特別是:)翻譯

Concatenating Transformations(變換的累加)
Also, the product of any two of i, j, and k gives whichever one was not part of that product. Thus:(而且,i、j、k的兩兩乘積等於除開這倆個參與乘法的第三者,以下:)
Concatenating Transformations(變換的累加)
Given this, we can see that it is possible to multiply two quaternions together as follows:(有了這些理論以後,四元數的乘法公式以下:)
Concatenating Transformations(變換的累加)
As with complex numbers, multiplication of quaternions is non-commutative(如同複數同樣,四元數的乘法也是不可交換的). Addition and subtraction for quaternions is defined as simple vector addition and subtraction, with the terms being added or subtracted on a component-by-component basis(四元數的加減法就直接用對應元素進行加減操做就能夠了). Other functions such as unary negation and magnitude also behave as expected for a four component vector(四元數求反和求長度就跟四維向量同樣操做就能夠). Although a quaternion is a four-component entity, it is common practice to represent a quaternion as a real scalar part and a three-component imaginary vector part. Such representation is often written(雖然四元數是個四維的數據,可是一般咱們使用一個標量和三維虛部的向量表示它,這樣的表述一般能夠這麼來寫:)
Concatenating Transformations(變換的累加)
Okay, great—but this isn’t the dreaded math chapter, right?(OK,屌爆了,能夠看到這並非一個很狗帶的數學章節) This is about computer graphics, OpenGL, and all that fun stuff(這是關於圖形學、OpenGL和全部那些有趣的玩意,有趣個卵啊,頭都快禿了,搞很差寫書的就是個禿友亮). Well, here’s where quaternions get really useful. Recall that our rotation functions take an angle and an axis to rotate around(Well,咱們來瞅瞅四元數爲什麼有用吧,讓咱們回想起旋轉的那些操做). Well, we can represent those two quantities as a quaternion by stuffing the angle in the real part and the axis in the vector part, yielding a quaternion that represents a rotation around any axis(咱們可使用實部去記錄旋轉,虛部去記錄旋轉軸,這樣一來一個四元數就能夠表達繞任意軸旋轉這個概念了). A sequence of rotations can be represented by a series of quaternions multiplied together, producing a single resulting quaternion that encodes the whole lot in one go(四元數的乘法也是能夠累加的,你能夠把屢次旋轉讓多個四元數相乘以後存到一個結果四元數裏). While it’s possible to make a bunch of matrices that represent rotation around the various Cartesian axes and then multiply them all together, that method is susceptible to gimbal lock(使用四元數就很好的解決了在使用旋轉矩陣去表達旋轉時,會產生萬向鎖的問題). If you do the same thing with a sequence of quaternions, gimbal lock cannot occur. For your coding pleasure, vmath includes the vmath::quaternion class that implements most of the functionality described here(若是你使用四元數的話,就不會有萬向鎖的問題,咱們的vmath是個好青年,咱們這裏講的這些玩意它都實現了)code

本日的翻譯就到這裏,明天見,拜拜~~component

第一時間獲取最新橋段,請關注東漢書院以及圖形之心公衆號orm

東漢書院,等你來玩哦three

相關文章
相關標籤/搜索