Chapter 4. Math for 3D Graphics(3D圖形中的數學)

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

What You’ll Learn in This Chapter(本章你將會學到啥)編程

What a vector is, and why you should care about them(什麼是向量以及爲何你須要注意他們)
What a matrix is, and why you should care more about them(什麼是矩陣以及爲何你須要更注意它們)
How we use matrices and vectors to move geometry around(咱們如何使用矩陣和向量來移動幾何物體)
What the OpenGL conventions and coordinate spaces are(OpenGL中的轉換以及座標空間是啥)
So far, you have learned to draw points, lines, and triangles and have written simple shaders that pass your hard-coded vertex data through unmodified(如今,你已經學過了如何繪製點、線以及三角形,而且咱們寫了一些簡單的shader,它們不對數據進行修改,僅僅是起到了在圖形管線中傳遞數據的做用). We haven’t really been rendering in 3D—which is odd for a book on 3D graphics!(咱們尚未真正的渲染3D的東西,這對於一本3D圖形的書來講不是很奇怪嗎) Well, to turn a collection of shapes into a coherent scene, you must arrange them in relation to one another and to the viewer(然而,要有條理的去在3D場景中組織物體,你必須管理他們之間的相對關係以及和攝像機的關係). In this chapter, you start moving shapes and objects around in your coordinate system(在本小節,你將開始在你的座標系統中移動物體). The ability to place and orient your objects in a scene is a crucial tool for any 3D graphics programmer(對於3D程序員來講,在場景中去放置和旋轉物體的能力是很是關鍵的). As you will see, it is actually convenient to describe your objects’ dimensions around the origin and then transform the objects into the desired positions(你將會看到,去定義物體的維度以及控制物體的位置仍是比較容易的)promise

Is This the Dreaded Math Chapter?(這是一個狗帶的數學章節嗎)app

In most books on 3D graphics programming, yes, this would be the dreaded math chapter(在大多數3D圖形書籍裏,確實,這就是一章狗帶的數學內容). However, you can relax; we take a more moderate approach to these principles than some texts(不過,你能夠放輕鬆,咱們將使用一些更溫馨的手段來說解這些原理而不是緊靠文字描述)ide

One of the fundamental mathematical operations that will be performed by your shaders is the coordinate transform, which boils down to multiplying matrices with vectors and with each other(最基本的數學操做中的一個就是你會在shader中用到的座標系轉換,它涉及到矩陣與矩陣以及矩陣與向量的乘法). The keys to object and coordinate transformations are two matrix conventions used by OpenGL programmers(對於OpenGL程序員來講,座標系轉換的關鍵在於兩個矩陣). To familiarize you with these matrices, this chapter strikes a compromise between two extremes in computer graphics philosophy(爲了讓你熟悉 這些矩陣,本章節將採用計算機圖形學中兩種極端 思路的這種方案來進行講解). On the one hand, we could warn you, 「Please review a textbook on linear algebra before reading this chapter.」(一方面,咱們會警告你說,請在閱讀本章以前,先看一看線性代數的 書) On the other hand, we could perpetuate the deceptive reassurance that you can 「learn to do 3D graphics without all those complex mathematical formulas.」(另外一方面,咱們也會不斷的告訴你,即使沒有那些複雜的數學理論,你也能夠學會3D圖形學) But we don’t agree with either camp(可是,咱們實際上兩邊都不一樣意)工具

In reality, you can get along just fine without understanding the finer mathematics of 3D graphics, just as you can drive your car every day without having to know anything at all about automotive mechanics and the internal combustion engine(實際上,你能夠在沒必要精通3D數學的狀況下,很好的處理3D編程問題,就像你沒必要懂什麼汽車生產以及發動機的原理就能開車同樣). But you had better know enough about your car to realize that you need an oil change every so often, that you have to fill the tank with gas regularly, and that you must change the tires when they get bald(可是你最好是更多的瞭解你的車子,這樣你就能夠知道何時該加油了 ,何時該打氣了). This knowledge makes you a responsible (and safe!) automobile owner(這些知識能讓你更好的玩轉你的車子). If you want to be a responsible and capable OpenGL programmer, the same standards apply(若是你但願變成一個更強的OpenGL程序員,道理是同樣的). You need to understand at least the basics so you know what can be done and which tools best suit the job(你至少須要瞭解最基本的數學,這樣你才能知道你能夠作什麼以及什麼工具是最適合你的項目的). If you are a beginner you will find that, with some practice, matrix math and vectors will gradually make more sense, and you will develop a more intuitive (and powerful) ability to make full use of the concepts we introduce in this chapter(若是你是一個新手,你將會發現,隨着不斷的練習,你將會獲得更多的矩陣以及向量方面的體會,而且你將能更好的去理解咱們本章節提到的這些概念)ui

So even if you don’t already have the ability to multiply two matrices in your head, you need to know what matrices are and how they serve as the means to OpenGL’s 3D magic(因此,即使你不知道如何計算矩陣的乘法,你須要知道什麼是矩陣,以及他們在OpenGL中的意義). But before you go dusting off that old linear algebra textbook (doesn’t everyone have one?)(可是在你開始再次翻開你的線性代數課本以前,不要懼怕:由於咱們的sb7庫裏面應包含了一個叫vmath的組件,它能很好的幫你解決數學計算方面的問題), have no fear: The sb7 library has a component called vmath that contains a number of useful classes and functions that can be used to represent and manipulate vectors and matrices. They can be used directly with OpenGL and are very similar in syntax and appearance to GLSL—the language you’ll be writing your shaders in(他們能夠像你即將在shader中使用矩陣的方式同樣使用). So, you don’t have to do all your matrix and vector manipulation yourself, but it’s still a good idea to know what they are and how to apply them(因此你並不須要本身親手實現全部這些數學計算,可是知道怎麼實現這些數學計算以及如何應用他們任然是值得你努力的方向). See—you can eat your cake and have it, too!this

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

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

東漢書院,等你來玩哦