週一到週五,天天一篇,北京時間早上7點準時更新~算法
Vectors behave as you would expect for operations such as addition, subtraction, unary negation, and so on(向量們常常須要進行加減乘除這樣的操做). These operators perform a per-component calculation and result in a vector of the same size as their inputs(這些操做獲得的結果依然是一個一樣維度的向量). The vmath vector classes override the addition, subtraction, and unary negation operators, along with several others, to provide such functionality. This allows you to use code such as(我寫的這個vmath是個好青年啊,它裏面封裝了這些操做,你能夠像下面這些代碼同樣進行向量的數學運算,別忘了關注個人推特兒哦)app
vmath::vec3 a(1.0f, 2.0f, 3.0f); vmath::vec3 b(4.0f, 5.0f, 6.0f); vmath::vec3 c; c = a + b; c = a - b; c += b; c = -c;
However, there are many more operations on vectors that are explained from a mathematical perspective in the following subsections(然而,數學意義上,接下來的這部份內容將會講述更多的向量操做). They also have implementations in the vmath library, which will be outlined here(一樣我寫的vmath庫已經把下面寫的這些所有都實現了)ide
Dot Product(點積)函數
Vectors can be added, subtracted, and scaled by simply adding, subtracting, or scaling their individual xyz components(向量能夠分別對xyz份量進行加減乘數或者縮放). An interesting and useful operation that can be applied only to two vectors, however, is called the dot product, which is also sometimes known as the inner product(這其中一個很是有用的只能發生在向量之間的操做叫點積,人們也管它叫內積). The dot product between two (three-component) vectors returns a scalar (just one value) that is the cosine of the angle between the two vectors scaled by the product of their lengths(倆向量的點積獲得的是一個標量,這個標量又等於這倆向量的長度相乘後再乘以倆向量的夾角的餘弦值). If the two vectors are of unit length, the value returned falls between −1.0 and 1.0 and is equal to the cosine of the angle between them(若是這倆向量是單位向量,那麼點積就等於這倆向量夾角的餘弦值了). Of course, to get the actual angle between the vectors, you’d need to take the inverse cosine (or arccosine) of this value(固然,爲了得到那個角度的大小,你須要經過餘弦值求arc餘弦值). The dot product is used extensively during lighting calculations and is taken between a surface normal vector and a vector pointing toward a light source in diffuse lighting calculations(點積在光照計算的時候常常會用到,在漫反射中參與運算的向量是法線和一個指向光源的向量). We will delve deeper into this type of shader code in the 「Lighting Models」 section in Chapter 13(咱們將會在Ligting Models這個章節裏來深刻了解一下這個shader的玩法). Figure 4.2 shows two vectors, V1 and V2, and how the angle between them is represented by θ(圖4.2展現了兩個向量v1和v2,以及他們之間的夾角θ)this
Mathematically, the dot product of two vectors V1 and V2 is calculated as(數學上,點積的計算公式以下)scala
V1 × V2 = V1.x × V2.x + V1.y × V2.y + V1.z × V2.z翻譯
The vmath library has some useful functions that use the dot product operation(vmath庫有不少使用了點積的函數). For starters, you can actually get the dot product itself between two vectors with the function vmath::dot, or with the dot member function of the vector classes(對於新司機來講,你可使用vmath::dot或者是向量的成員函數來計算點積)rest
vmath::vec3 a(...); vmath::vec3 b(...); float c = a.dot(b); float d = dot(a, b);
As we mentioned, the dot product between a pair of unit vectors is a value (between−1.0 and +1.0) that represents the cosine of the angle between them. (就如同咱們提到的,兩個單位向量的點積是這倆向量夾角的餘弦值) A slightly higher level function, vmath::angle, actually returns this angle in radians(另外一個更高端的接口vmath::angle計算的則是兩個向量之間夾角的大小,單位是弧度)code
float angle(const vmath::vec3& u, const vmath::vec3& v);
Cross Product(叉積)component
Another useful mathematical operation between two vectors is the cross product, which is also sometimes known as the vector product(另外一個比較有用的操做是叉積). The cross product between two vectors is a third vector that is perpendicular to the plane in which the first two vectors lie(倆向量叉積會產生一個垂直於這倆向量所在平面的向量). The cross product of two vectors V1 and V2 is defined as(兩個向量叉積的定義以下:)
V1 × V2 = ||V1|| ||V2|| sin(θ)n
where n is the unit vector that is perpendicular to both V1 and V2(其中n是垂直於v1和v2的單位向量). This means that if you normalize the result of a cross product, you get the normal to the plane(意思就是說若是你單位化這個叉積的結果向量,你就會獲得垂直於一個平面的法線). If V1 and V2 are both unit length, and are known to be perpendicular to each other, then you don’t even need to normalize the result, as it will also be unit length(若是v1和v2都是單位向量且互相垂直,那麼你不須要進行單位化了,由於你獲得的結果就是一個單位向量). Figure 4.3 shows two vectors, V1 and V2, and their cross product V3(圖4.3展現了v1和v2以及他們的叉積v3)
The cross product of two three-dimensional vectors V1 and V2 can be calculated as(三維向量v一、v2的叉積可使用以下公式)
Again, the vmath library has functions that take the cross product of two vectors and return the resulting vector: one member function of the three-component vector classes and one global function.(一樣滴,vmath庫有這樣的一個計算向量叉積的全局函數和向量的成員函數,使用方式以下)
vec3 a(...); vec3 b(...); vec3 c = a.cross(b); vec3 d = cross(a, b);
Unlike in the dot product, the order of the vectors in the cross product is important(與點積相比,誰叉了誰這個順序是不能隨意變更的). In Figure 4.3, V3 is the result of V2 cross V1(圖4.3顯示的都是v2叉了v1的結果v3). If you were to reverse the order of V1 and V2, the resulting vector V3 would point in the opposite direction(若是你用v1去叉v2,那麼v3的方向則會反過來). Applications of the cross product are numerous, from finding surface normals of triangles to constructing transformation matrices(使用叉積的狀況仍是不少的,從計算法線到構建座標系)
Length of a Vector(向量的長度)
As we have already discussed, vectors have a direction and a magnitude(咱們已經提到過,向量有方向和長度). The magnitude of a vector is also known as its length. The magnitude of a three-dimensional vector can be found by using the following equation(計算向量長度的公式以下):
This can be generalized as the square root of the sum of the squares of the components of the vector(這個就是對向量各個部分求平方,而後加起來以後,開平方). In only two dimensions, this is simply Pythagoras’s theorem: The square of the hypotenuse is equal to the sum of the squares of the other two sides(當向量是二維向量的時候,就是說,四邊形的斜邊的平方等於其餘兩條邊的平方的和). This extends to any number of dimensions, and the vmath library includes functions to calculate this for you.(這個能夠擴展到任意維度,咱們vmath庫就已經包含了這部份內容了)
template static inline T length(const vecN& v) { ... }
Reflection and Refraction(反射和折射)
Common operations in computer graphics are calculating reflection and refraction vectors(另外還有一部分的經常使用的操做就是反射和折射向量了). Given an incoming vector Rin and a normal to a surface N, we wish to know the direction in which Rin will be reflected (Rreflect), and given a particular index of refraction η, the direction in which Rin will be refracted(給出一個入射光線和物體表面的法線,咱們就能夠求出反射光線,在進一步,給出表面折射率的時候,俺們還能求出折射光線). We show this in Figure 4.4,with the refracted vectors for various values of η shown as Rrefract,η1 through(圖4.4展現了兩個不一樣折射率的時候,折射光線的狀況)
Although Figure 4.4 shows the system in only two dimensions, we are interested in computing this in three dimensions (this is a 3D graphics book, after all). The math for calculating Rreflect is(圖4.4展現的是一個二維的,咱們實際上對三維的更感興趣,三維的反射計算方式以下:)
The math for calculating Rrefract for a given value of η is(三維向量的折射計算公式以下:)
To get the desired result, both R and N must be unit-length vectors (i.e., they should be normalized before use)(爲鳥獲得指望的結果,R和N必須是單位向量). The two vmath functions, reflect() and refract(), implement these equations(vmath庫中的reflect和refract函數實現了剛纔的這些公式)
本日的翻譯就到這裏,明天見,拜拜~~
第一時間獲取最新橋段,請關注東漢書院以及圖形之心公衆號
東漢書院,等你來玩哦