週一到週五,天天一篇,北京時間早上7點準時更新~數組
First, we do not pretend here that we will cover everything that is important for you to know(首先,咱們不會在這裏去涵蓋全部對你來講很重要的東西). In fact, we will not even try to cover everything you should know(實際上,咱們甚至都不會涵蓋你應該知道的東西). In this chapter, we are just going to cover what you really need to know(在本章節,咱們只是介紹一下那些你必須知道的東西). If you’re already a math whiz, you should skip immediately to the section ahead on the standard 3D transformations(若是你數學已經學的很好了,那麼你應該跳過本段). Not only do you already know what we are about to cover, but most math fans will be somewhat offended that we did not give sufficient space to their favorite feature of homogeneous coordinate spaces(不只僅是由於你已經知道了咱們要介紹的東西,而且對於不少數學狂魔來講,他們會以爲本段根本連個屁都沒說). Imagine one of those reality TV shows where you must escape a virtual swamp filled with crocodiles(想象一下綜藝頻道里那些從虛擬的充滿了鱷魚的沼澤裏逃亡的遊戲(這或許是做者本身看過的什麼綜藝節目,法克,誰能想象出來啊)). How much 3D math do you really need to know to survive(爲了生存,你須要多少3D數學呢?)? That’s what the next two sections are going to be about—3D math survival skills(接下來的2個小段就是關於如何生存下來的3D數學). The crocodiles do not care if you really know what a homogeneous coordinate space is(若是你真的懂得齊次座標的話,鱷魚根本不在話下,分分鐘能夠吊打,當着它女友的面吃它的肉肉)app
Vectors, or Which Way Is Which?(向量或者啥是啥?)ide
The main input to OpenGL is the vertex, which has a number of attributes that normally include a position(OpenGL的輸入數據是vertex,它一般狀況下都會包含位置這個屬性). Basically, this is a position in xyz coordinate space, and a given position in space is defined by exactly one and only one unique xyz triplet(基本上這個位置在給定的某個空間裏是惟一存在的). An xyz triplet, however, can be represented by a vector (in fact, for the mathematically pure of heart, a position is actually a vector too—there, we threw you a bone)(一個xyz的位置可使用一個向量去表示). A vector is perhaps the single most important foundational concept to understand when it comes to manipulating 3D geometry(當俺們開始聊3D幾何的時候,或許向量就是一個最重要的玩意了). Those three values (x, y, and z) combined represent two important values: a direction and a magnitude(向量的那三個值能表達兩個很是重要的東西:方向和長度)函數
Figure 4.1 shows a point in space (picked arbitrarily) and an arrow drawn from the origin of the coordinate system to that point in space(圖4.1展現了一個空間中的點以及一個從座標系原點發出來,指向該點的一個箭頭). The point can be thought of as a vertex when you are stitching together triangles, but the arrow can be thought of as a vector(這個點能夠被當成是你去拼湊三角形的那些數據,然而這個箭頭能夠被當作是一個向量). A vector is first, and most simply, a direction from the origin toward a point in space(向量首先最簡單的表達的是從原點指向一個點的方向). We use vectors all the time in OpenGL to represent directional quantities(咱們老是在OpenGL裏面使用向量去描述方向數據). For example, the x axis is the vector (1, 0, 0)(好比x軸的向量是(1,0,0)). This says to go positive one unit in the x direction, and zero in the y and z directions(意思是說,指向x軸正方向一個單位的距離以及y,z方向0個單位距離的地方). A vector is also how we point to where we are going—for example, which way is the camera pointing, or in which direction do we want to move to get away from that crocodile?(一個向量一樣也被用來指向咱們將要去哪裏,好比攝像機指向哪裏,還好比咱們向哪一個方向走來避開鱷魚先生)
The vector is so fundamental to the operation of OpenGL that vectors of various sizes are first-class types in GLSL and are given names such as vec3 and vec4 (representing three- and four-element vectors, respectively)(向量是OpenGL中不少操做的基礎,根據向量中數據個數的不一樣又分爲N維向量,好比vec四、vec3這樣子). The second quantity a vector can represent is the magnitude(向量能表達的第二個東西就是長度). The magnitude of a vector is the length of the vector(向量的magnitude就是指向量的長度,因此以前咱們就直譯成長度了,在國外混過的都知道這個基本概念). For our x-axis vector (1, 0, 0), the length of the vector is 1(對於咱們x軸的向量來講,它的長度是1). A vector with a length of 1 we call a unit vector(長度只有1的向量叫作單位向量). If a vector is not a unit vector and we want to scale it to make it one, we call that normalization(若是一個向量的長度不是1,而咱們想把它的長度變成1的話,這個過程叫作單位化). Normalizing a vector scales it such that its length becomes 1 and the vector is then said to be normalized(單位化一個向量會讓它的長度變成1,變化完後,這個向量就能夠被當作是已經被單位化鳥). Unit vectors are important when we want to represent only a direction and not a magnitude(當咱們僅想表達一個方向,而不關心長度的時候,單位向量是很重要的). Also, if vector lengths appear in the equations we’ll be using, they get a whole lot simpler when those lengths are 1!(而且,當向量的長度是1的時候,咱們在作向量運算的時候,整體來講會讓狀況變得更簡單) A magnitude can be important as well; for example, it can tell us how far we need to move in a given direction—how far away we need to get from that crocodile(固然,向量的長度也很重要,好比,它能夠告訴咱們,沿着給定的方向,咱們須要移動多少距離,才能避開咱們的鱷魚lady gaga們)this
Vectors (and matrices) are such important concepts in 3D graphics that they are firstclass citizens in GLSL—the language in which you write your shaders(向量和矩陣是如此的重要,以致於當你在寫shader的時候,他們是被GLSL原生的支持的). However, this is not so in languages like C++(然而,C++並不支持這貨). To allow you to use them in your C++ programs, the vmath library, which is provided with this book’s source code, contains classes that can represent vectors and matrices that are named similarly to their GLSL counterparts(爲了讓你在C++程序裏也能使用,俺寫的vmath庫將會提供這部分能力,它的命名和使用方式就是抄襲了GLSL裏的). For instance, vmath::vec3 can represent a three-component floating-point vector (x, y, z), vmath::vec4 can represent a four-component floating-point vector (x, y, z, w), and so on(例如,vmath::vec3就能夠表達3個浮點數組成的向量,vmath::vec4能表達四個浮點數組成的向量等等). The w coordinate is added to make the vector homogeneous but is typically set to 1.0(w份量使得向量變成了奇次向量,基本上這傢伙的值就是1.0). The x, y, and z values might later be divided by w, which, when it is 1.0, essentially leaves the xyz values alone(x、y、z的值可能在後面會被w除,當它是1的時候,會保持xyz的值不變). The classes in vmath are actually templated classes with type definitions to represent common types such as single- and doubleprecision floating-point values, and signed- and unsigned-integer variables. (vmath是模板類,隨便用就能夠了,若是不清楚模板原理的能夠觀看東漢書院C++ Tricks課程,只要9塊8,遇上書院正在舉行老漢下鄉,支持社會主義新農村建設,咱們鄭重承諾,買一送一! 買課程送壯妹一名,9塊8買不到吃虧,9塊8買不到上當,9塊8你什麼都買不到,哈哈哈哈。 不過說句正經的,尤爲是須要寫引擎內核的同窗,讓你站在深層次去看C++代碼到底在幹嗎,看完後對於如何使用別人的C++模板庫是很是有幫助的。) vmath::vec3 and vmath::vec4 are defined simply as follows(vmath::vec3和vmath::vec4的定義以下)spa
typedef Tvec3 vec3; typedef Tvec4 vec4;
Declaring a three-component vector is as simple as(申明一個三維向量以下的例子以下)翻譯
vmath::vec3 vVector;
If you include using namespace vmath; in your source code, you can even write(若是你直接把vmath的命名空間所有導入到你的項目中,你甚至能夠像下面這樣使用vmath)code
vec3 vVector;
However, in these examples, we’ll always qualify our use of the vmath library by explicitly using the vmath:: namespace(然而,在這些例子中,咱們依然會顯示的去使用vmath這個命名空間). All of the vmath classes define a number of constructors and copy operators, which means you can declare and initialize vectors as follows:(全部的vmath的類都定義了不少構造函數和拷貝的運算符,也就是說,你能夠像下面如此這般來使用這個庫)component
vec3 vmath::vVertex1(0.0f, 0.0f, 1.0f); vec4 vmath::vVertex2 = vec4(1.0f, 0.0f, 1.0f, 1.0f); vec4 vmath::vVertex3(vVertex1, 1.0f);
Now, an array of three-component vertices, such as for a triangle, can be declared as(一個三維的點能夠被以下相似的代碼定義:)orm
vec3 vmath::vVerts[] = { vmath::vec3(-0.5f, 0.0f, 0.0f), vmath::vec3(0.5f, 0.0f, 0.0f), vmath::vec3(0.0f, 0.5f, 0.0f) } ;
This should look similar to the code that we introduced in the 「Drawing Our First Triangle」 section in Chapter 2(這個應該跟咱們在第2章裏畫三角形時的那些代碼長的很像,反正我是已經忘了第二章那個代碼長啥樣了). The vmath library also includes lots of math-related functions and overrides most operators on its class to allow vectors and matrices to be added, subtracted, multiplied, transposed, and so on(vmath庫也包含了不少跟數學相關的函數而且重載了不少運算符,這樣一來,那些個向量和矩陣就能夠進行一些基本的運算操做了)
We need to be careful here not to gloss over that fourth w component too much(咱們須要注意的是,別被w向量所吸引鳥). Most of the time when you specify geometry with vertex positions, a three-component vertex is all you want to store and send to OpenGL(大部分狀況下,你須要表達一個幾何形體的某個點的時候,三維向量就夠了). For many directional vectors, such as a surface normal (a vector pointing perpendicular to a surface that is used for lighting calculations), a three-component vector suffices(對於不少方向向量,好比法線,三維向量就很夠用了). However, we will soon delve into the world of matrices, and to transform a 3D vertex, you must multiply it by a 4 × 4 transformation matrix(固然,咱們將很快進入矩陣的世界,而且,咱們須要使用4x4的矩陣來對3D的點來進行操做). The rules are you must multiply a four-component vector by a 4 × 4 matrix(這裏須要注意的是,你必需要用一個四維向量去與一個4x4的矩陣進行乘法操做); if you try and use a three-component vector with a 4 × 4 matrix, the crocodiles will eat you!(若是你用一個三維向量與一個4x4的矩陣進行配對,那些鱷魚就會來吃你的大腿上的肉,就是咱們在公衆號裏,那我的妖大腿內側畫了紋身的那一塊肉) More on what all this means soon. Essentially, if you are going to do your own matrix operations on vectors, then you will probably want four-component vectors in many cases(更多的內容咱們將會很快進行介紹。基本上,若是你但願使用矩陣對向量進行操做的時候,你將更多的會使用四維向量)
本日的翻譯就到這裏,明天見,拜拜~~
第一時間獲取最新橋段,請關注東漢書院以及圖形之心公衆號
東漢書院,等你來玩哦