週一到週五,天天一篇,北京時間早上7點準時更新~,中英文對照,一邊學編程一邊彈吉他,作一個奇葩碼農!程序員
請不要懷疑翻譯是否有問題,咱們的翻譯工程師是藍翔畢業的呢!編程
What You’ll Learn in This Chapter(你將會學到啥)api
This book is about OpenGL(這本書是關於OpenGL的). OpenGL is an interface that your application can use to access and control the graphics subsystem of the device on which it runs(OpenGL是你的應用程序能夠經過它來訪問和控制圖形子系統硬件設備的編程接口). This could be anything from a high-end graphics workstation(這些硬件設備能夠是任何東西,從高端的圖形工做站), to a commodity desktop computer(到商用的桌面電腦), to a video game console(到遊戲主機), to a mobile phone(甚至是手機). Standardizing the interface to a subsystem increases portability and allows software developers to concentrate on creating quality products(標準化這些硬件系統的接口以後,能夠增長代碼的可移植性而且可讓軟件開發者更集中精力在產品質量), producing interesting content(開發更有趣的內容), and ensuring the overall performance of their applications(以及保證他們應用程序的綜合性能的事情上面), rather than worrying about the specifics of the platforms they want them to run on(而不必去擔憂他們的應用程序沒法發佈到某些特殊的平臺上去). These standard interfaces are called application programming interfaces (APIs)(這些標準接口被稱爲應用程序編程接口), of which OpenGL is one(OpenGL就是衆多編程接口中的一個). This chapter introduces OpenGL(本章節介紹OpenGL), describes how it relates to the underlying graphics subsystem(闡述它是如何跟底層的圖形子系統進行關聯的), and provides some history on the origin and(而且提供一些關於OpenGL起源以及OpenGL迭代進化方面的歷史) evolution of OpenGL架構
OpenGL and the Graphics Pipeline(OpenGL和圖形管線)app
Generating a product at high efficiency and volume generally requires two things(要在生產的時候保持高性能以及很產能,一般來講須要把握兩個關鍵點): scalability and parallelism(可擴展性和並行性). In factories, this is achieved by using production lines(在工廠裏面,咱們經過流水線來達到這一目的). While one worker installs the engine in a car(當某一個工人在給汽車安裝發動機的時候), another can be installing the doors(另外一個工人在給汽車裝窗戶), and yet another can be installing the wheels(而別的工人在給汽車裝輪胎). By overlapping the phases of production of the product(經過把產品的生產過程分割成各個小段,而後讓這些小段並行起來), with each phase being executed by a skilled technician who concentrates his or her energy on that single task(每一個小段上安排一個牛叉的技工,他能夠把他的所有精力集中在他本身的那一小段工做任務上), each phase becomes more efficient and overall productivity goes up(這樣一來,全部的小段任務都會變得更加的高效,而且全局的產能會獲得提升,這尼瑪就是典型的資本主義剝削的寫照). Also, by making many cars at the same time(若是同時安排多個車輛製做的流水線), a factory can have multiple workers installing multiple engines or wheels or doors and many cars can be on the production line at the same time(這樣一來多個工人就能夠同時安裝多個發動機或者輪子或者門窗,更多的車子能夠被在同一時間生產出來), each at a different stage of completion(每一個步驟在不一樣的小段中完成). The same is true in computer graphics(這個原理一樣適用於計算機圖形學). The commands from your program are taken by OpenGL and sent to the underlying graphics hardware(你程序發出的渲染指令會經過OpenGL發送給底層的圖形硬件), which works on them in an efficient manner to produce the desired result as quickly and efficiently as possible(這些圖形硬件會經過很是高效的手段儘量快的計算出你預期的結果). There could be many commands lined up to execute on the hardware (a status referred to as in flight)(可能會有不少個指令同時在硬件上執行), and some may even be partially completed(其中可能會有一部分只執行了某個大操做的一半). This allows their execution to be overlapped such that a later stage of one command might run concurrently with an earlier stage of another command(這種只執行了一半的指令可能會與後面某些指令進行結合,完成最終的功能). Futhermore, computer graphics generally consists of many repititions of very similar tasks (such as figuring out what color a pixel should be)(好比說,在計算機圖形學中一般會有不少重複的小操做,好比查詢當前狀態機中顏色是啥顏色的), and these tasks are usually indpendent of one another—that is(這樣的操做每每是互相之間獨立開來的), the result of coloring one pixel doesn’t depend on any other(查詢的結果不會依賴於其餘的那些查詢顏色的指令). Just as a car plant can build multiple carssimultaneously, so OpenGL can break up the work you give it and work on its fundamental elements in parallel(就如同以前的汽車流水線同樣,你應用程序調用OpenGL的API以後,OpenGL會自動把他們切割成小元素,而後讓他們並行執行). Through a combination of pipelining and parallelism, incredible performance of modern graphics processors is realized(經過流水線和並行操做,現代的圖形顯卡表現出了難以置信的效率). The goal of OpenGL is to provide an abstraction layer between your application and the underlying graphics subsystem(OpenGL的目標是提供一個處於應用程序和底層圖形硬件之間的一個抽象層), which is often a hardware accelerator made up of one or more custom, high-performance processors with dedicated memory, display outputs, and so on(一般狀況下,硬件加速器由一個或者更多的高性能處理器以及顯存和顯示輸出等等之類的玩意組成). This abstraction layer allows your application to not need to know who made the graphics processor (or graphics processing unit [GPU])(OpenGL提供的抽象層使得你在寫程序的時候不用去管究竟是誰造了這個顯卡、它是如何工做的或者說它性能到底如何,你統統不用管), how it works, or how well it performs. Certainly it is possible to determine this information, but the point is that applications don’t need to(固然,若是你想的話,你也能夠查詢到這些顯卡信息,這裏想說明的問題是,你能夠不用去知道這些鬼玩意)ide
As a design principle(做爲一個設計的原則), OpenGL must strike a balance between too high and too low an abstraction level(OpenGL必須注意設計時不要搞得太抽象,也不要搞得太具體,這個平衡是很難把握的). On the one hand(另外一方面), it must hide differences between various manufacturers’ products (or between the various products of a single manufacturer) and system-specific traits such as screen resolution(它必須隱藏掉系統特性的差別,好比屏幕分辨率這樣的東西啦、處理器的架構啦、裝在什麼操做系統上等等這種東西), processor architecture, installed operating system, and so on. On the other hand, the level of abstraction must be low enough that programmers can gain access to the underlying hardware and make best use of it(在另外一方面,爲什麼老外寫文章連續寫了兩次在另外一方面,OpenGL又必須讓抽閒的級別足夠的接近底層,以讓編程人員可以更好的使用底層硬件). If OpenGL presented too high of an abstraction level, then it would be easy to create programs that fit the model(若是抽象層次過高,那麼是很是容易寫程序的), but very hard to use advanced features of the graphics hardware that weren’t included(但這樣一來帶來的問題就是,編程人員很難使用系統的一些比較高級的特性). This is the type of model followed by software such as game engines—new features of the graphics hardware generally require relatively large changes in the engine for games built on top of it to gain access to them(須要訪問高級特性的軟件好比說遊戲引擎就是其中一種,一般來講,爲了使用新的硬件特性,遊戲引擎須要擼不少代碼). If the abstraction level is too low, applications need to start worrying about architectural peculiarities of the system they’re running on(若是抽象層次太接近硬件了,那麼迴帶來程序員須要本身去適應各類不一樣硬件的問題). Low levels of abstraction are common in video game consoles(比較接近硬件的代碼一般會時候會讓人想到遊戲主機這種玩意,各類主機遊戲都有自家的獨特的代碼和硬件以及操做系統), for example, but don’t fit well into a graphics library that must support devices ranging from mobile phones to gaming PCs to highpowered professional graphics workstations(主機遊戲的代碼每每沒法很好的適配一些在手機遊戲或者PC遊戲須要使用的圖形庫)性能
As technology advances, more and more research is being conducted in computer graphics(隨着技術的發展,圖形學領域出現了愈來愈多的研究成功), best practices are being developed(優秀的寫代碼的方式已經獲得了證實,我們跟着那些規矩來寫就能夠了), and bottlenecks and requirements are moving—and so OpenGL must also move to keep up(面臨的瓶頸和需求也在隨着時代在變化,因此OpenGL也須要繼續發展)flex
The current state of the art in graphics processing units(在圖形處理單元中的當前狀態的概念,是絕大多數OpenGL的實現的基礎), on which most OpenGL implementations are based, is capable of many teraflops of computing power(這些處理器如今已經強大到每秒能夠作萬億次浮點運算), has gigabytes of memory that can be accessed at hundreds of gigabytes per second(每秒鐘能夠訪問幾百GB的內存數據), and can drive multiple, multi-megapixel displays at high refresh rates(而且能夠同時控制多臺顯示器以很是快的頻率進行畫面更新). GPUs are also extremely flexible(顯卡也很是的易用), and are able to work on tasks that might not be considered graphics at all(已經能夠用它來幹那些原本不會被認爲是顯卡該乾的事情了呢), such as physical simulations, artificial intelligence, and even audio processing(好比物理模擬、人工智能甚至是音頻處理)ui
Current GPUs consist of large numbers of small programmable processors called shader cores that run mini-programs called shaders(如今的這些顯卡由不少可編程的處理器組成,這些處理器被稱爲着色核心,在處理器上運行的程序被稱爲着色器). Each core has a relatively low throughput(每一個核心計算能力相對來講較低), processing a single instruction of the shader in one or more clock cycles and normally lacking advanced features such as out-of-order execution(好比在一個或者多個時間片內處理單一的一個來自shader的指令,而且一般來講是沒有什麼高級特性的), branch prediction,super-scalar issues, and so on. However, each GPU might contain anywhere from a few tens to a few thousands of these cores(可是顯卡一般有幾十個甚至上千個這樣的計算能力不那麼好的核心), and together they can perform an immense amount of work(這些傢伙一塊兒幹活的時候,仍是很牛的呢). The graphics system is broken into a number of stages(圖形系統被分解成了不少個處理部分), each represented either by a shader or by a fixed-function(每個部分要麼被shader處理,要麼被顯卡中固定的硬件處理), possibly configurable processing block. Figure 1.1 shows a simplified schematic of the graphics pipeline(下面的圖就展現了圖形流水線的處理過程)this
In Figure 1.1, the boxes with rounded corners are considered fixed-function stages(上圖中,圓角矩形被認爲是由硬件的固定處理單元來處理的), whereas the boxes with square corners are programmable(非圓角矩形的部分,是可編程部分,就是你能夠塞一個shader進去,想幹嗎幹嗎的), which means that they execute shaders that you supply. In practice(按道理來說,部分甚至所有那些固定硬件處理的部分也是可讓程序員來寫shader的), some or all of the fixed-function stages may really be implemented in shader code, too—it’s just that you don’t supply that code, but rather the GPU manufacturer generally supplies it as part of a driver, firmware, or other system software(然而如今的狀況是,你沒辦法往那裏塞shader,因此你沒法接管那些過程,這些東西目前都是被顯卡的製造商經過它們提供的驅動或者什麼玩意管理的,總之你不要想那塊的問題就是了)
第一時間獲取最新橋段,請關注東漢書院以及圖形之心公衆號
東漢書院、等你來玩哦