溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Concatenating Transformations(變換的累加)

發(fā)布時(shí)間:2020-06-13 18:06:09 來源:網(wǎng)絡(luò) 閱讀:360 作者:萌谷王 欄目:游戲開發(fā)

周一到周五,每天一篇,北京時(shí)間早上7點(diǎn)準(zhǔn)時(shí)更新~

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(坐標(biāo)的轉(zhuǎn)換可以用矩陣來表示,把一個(gè)向量從一個(gè)坐標(biāo)系轉(zhuǎn)換到另外一個(gè)坐標(biāo)系涉及到一個(gè)矩陣乘以向量的操作). 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(如果你使用一堆矩陣對向量進(jìn)行轉(zhuǎn)換的話,就牽涉到一堆的轉(zhuǎn)換,你不必邀存儲(chǔ)向量與這一堆矩陣相乘時(shí)每個(gè)階段的向量狀態(tài)). 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(你可以先把所有的矩陣相乘,得到最終矩陣,然后再拿這個(gè)最后的結(jié)果去與向量相乘). 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中,你通常會(huì)使用矩陣乘以向量的操作,并且矩陣的相乘順序與正確的數(shù)學(xué)邏輯意義上的順序是反著來的,比如下面的)

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軸旋轉(zhuǎn)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:(請注意矩陣書寫時(shí)候的順序,與我們邏輯上的順序是反的,我們也可以把代碼寫成下面這樣)

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(這里,我們先得到了合成后的矩陣,然后我們拿這個(gè)合成后的矩陣去乘以向量). 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.(如果你的點(diǎn)比較多的時(shí)候,這種方式會(huì)提高效率,每個(gè)向量只需要乘以最后的合成矩陣,而不用依次去乘以兩個(gè)矩陣) 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!(這一大坨英文的意思就是,這里你需要注意矩陣的相乘順序,如果你順序?qū)戝e(cuò)了,那就得不到你想要的結(jié)果,比如你總想著先旋轉(zhuǎn)再移動(dòng), 于是乎代碼很容易寫成rotation_matrix*translate_matrix)

Quaternions(四元數(shù))

A quaternion is a four-dimensional quantity that is similar in some ways to a complex number(四元數(shù)跟復(fù)數(shù)有點(diǎn)類似,它有一個(gè)實(shí)部和三個(gè)虛部,這個(gè)跟腎虛無關(guān),有人就說腎虛也會(huì)引發(fā)失眠和頭疼,所以小伙子們應(yīng)該保持良好的生活習(xí)慣). 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(如同復(fù)數(shù)有一個(gè)虛部i一樣,四元數(shù)有三個(gè)虛部i、j、k,數(shù)學(xué)上,四元數(shù)q可以寫成下面的樣子)
Concatenating Transformations(變換的累加)
The imaginary parts of the quaternion have properties similar to the imaginary part of a complex number. In particular:(四元數(shù)的虛部跟復(fù)數(shù)的虛部有著相似的性質(zhì),特別是:)

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的兩兩乘積等于除開這倆個(gè)參與乘法的第三者,如下:)
Concatenating Transformations(變換的累加)
Given this, we can see that it is possible to multiply two quaternions together as follows:(有了這些理論之后,四元數(shù)的乘法公式如下:)
Concatenating Transformations(變換的累加)
As with complex numbers, multiplication of quaternions is non-commutative(如同復(fù)數(shù)一樣,四元數(shù)的乘法也是不可交換的). 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(四元數(shù)的加減法就直接用對應(yīng)元素進(jìn)行加減操作就可以了). Other functions such as unary negation and magnitude also behave as expected for a four component vector(四元數(shù)求反和求長度就跟四維向量一樣操作就可以). 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(雖然四元數(shù)是個(gè)四維的數(shù)據(jù),但是通常我們使用一個(gè)標(biāo)量和三維虛部的向量表示它,這樣的表述通??梢赃@么來寫:)
Concatenating Transformations(變換的累加)
Okay, great—but this isn’t the dreaded math chapter, right?(OK,屌爆了,可以看到這并不是一個(gè)很狗帶的數(shù)學(xué)章節(jié)) This is about computer graphics, OpenGL, and all that fun stuff(這是關(guān)于圖形學(xué)、OpenGL和所有那些有趣的玩意,有趣個(gè)卵啊,頭都快禿了,搞不好寫書的就是個(gè)禿友亮). Well, here’s where quaternions get really useful. Recall that our rotation functions take an angle and an axis to rotate around(Well,我們來瞅瞅四元數(shù)為何有用吧,讓我們回想起旋轉(zhuǎn)的那些操作). 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(我們可以使用實(shí)部去記錄旋轉(zhuǎn),虛部去記錄旋轉(zhuǎn)軸,這樣一來一個(gè)四元數(shù)就可以表達(dá)繞任意軸旋轉(zhuǎn)這個(gè)概念了). 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(四元數(shù)的乘法也是可以累加的,你可以把多次旋轉(zhuǎn)讓多個(gè)四元數(shù)相乘之后存到一個(gè)結(jié)果四元數(shù)里). 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(使用四元數(shù)就很好的解決了在使用旋轉(zhuǎn)矩陣去表達(dá)旋轉(zhuǎn)時(shí),會(huì)產(chǎn)生萬向鎖的問題). 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(如果你使用四元數(shù)的話,就不會(huì)有萬向鎖的問題,我們的vmath是個(gè)好青年,我們這里講的這些玩意它都實(shí)現(xiàn)了)

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

第一時(shí)間獲取最新橋段,請關(guān)注東漢書院以及圖形之心公眾號(hào)

東漢書院,等你來玩哦

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI